<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class BrandKit extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id',
        'name',
        'description',
        'agency_name',
        'agency_logo_url',
        'client_logo_url',
        'primary_color',
        'accent_color',
        'secondary_color',
        'text_color',
        'muted_color',
        'cover_style',
        'cover_background_url',
        'footer_note',
        'contact_email',
        'contact_phone',
        'website_url',
        'settings',
        'is_default',
        'is_active',
        'sort_order',
    ];

    protected $casts = [
        'settings' => 'array',
        'is_default' => 'boolean',
        'is_active' => 'boolean',
        'sort_order' => 'integer',
    ];

    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    public function reportTemplates(): HasMany
    {
        return $this->hasMany(ReportTemplate::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeGeneral($query)
    {
        return $query->whereNull('client_id');
    }

    /** @return array<string, mixed> */
    public function toReportSettings(): array
    {
        return array_filter([
            'agency_name' => $this->agency_name,
            'agency_logo_url' => $this->agency_logo_url,
            'client_logo_url' => $this->client_logo_url,
            'primary_color' => $this->primary_color,
            'accent_color' => $this->accent_color,
            'secondary_color' => $this->secondary_color,
            'text_color' => $this->text_color,
            'muted_color' => $this->muted_color,
            'cover_style' => $this->cover_style,
            'cover_background_url' => $this->cover_background_url,
            'footer_note' => $this->footer_note,
            'contact_email' => $this->contact_email,
            'contact_phone' => $this->contact_phone,
            'website_url' => $this->website_url,
        ], fn ($value) => $value !== null && $value !== '');
    }

    public function previewGradient(): string
    {
        $primary = $this->primary_color ?: '#111827';
        $accent = $this->accent_color ?: '#ef5f86';

        return "linear-gradient(135deg, {$primary}, {$accent})";
    }
}
