<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Setting extends Model
{
    protected $table = 'site_settings';

    protected $fillable = [
        'theme_color',
        'theme_custom_hex',
        'site_title',
        'site_description',
        'index_heading',
        'index_description',
        'index_box_heading',
        'index_box_content',
        'contact_heading',
        'contact_box_content',
        'meta_title',
        'meta_description',
        'meta_keywords',
    ];

    public static function getSettings(): self
    {
        try {
            return Cache::remember('site_settings', 3600, function () {
                return self::first() ?? self::createDefault();
            });
        } catch (\Throwable) {
            return new self([
                'theme_color' => 'indigo',
                'site_title' => 'Local Citation Links',
                'site_description' => 'Elevate your local business visibility',
                'index_heading' => 'Understanding Local Citations',
                'index_description' => 'Discover how local citations can transform your business\'s online presence and drive more customers to your door.',
            ]);
        }
    }

    protected static function createDefault(): self
    {
        return self::create([
            'theme_color' => 'indigo',
            'site_title' => 'Local Citation Links',
            'site_description' => 'Elevate your local business visibility',
        ]);
    }

    public static function clearCache(): void
    {
        Cache::forget('site_settings');
    }

    public function getThemeGradientCssAttribute(): string
    {
        if ($this->theme_color === 'custom' && $this->theme_custom_hex) {
            $hex = ltrim($this->theme_custom_hex, '#');
            $darker = $this->darkenHex($hex, 15);
            return "linear-gradient(135deg, #{$hex} 0%, #{$darker} 100%)";
        }

        return match ($this->theme_color) {
            'blue' => 'linear-gradient(135deg, #3b82f6 0%, #0891b2 100%)',
            'purple' => 'linear-gradient(135deg, #a855f7 0%, #ec4899 100%)',
            'teal' => 'linear-gradient(135deg, #14b8a6 0%, #10b981 100%)',
            'emerald' => 'linear-gradient(135deg, #10b981 0%, #14b8a6 100%)',
            'violet' => 'linear-gradient(135deg, #8b5cf6 0%, #a855f7 100%)',
            default => 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
        };
    }

    public function getThemeBgGradientAttribute(): string
    {
        if ($this->theme_color === 'custom' && $this->theme_custom_hex) {
            $hex = ltrim($this->theme_custom_hex, '#');
            return "linear-gradient(135deg,#{$hex}15 0%,#{$hex}25 100%)";
        }

        return match ($this->theme_color) {
            'blue' => 'linear-gradient(135deg,#3b82f615 0%,#0891b215 100%)',
            'purple' => 'linear-gradient(135deg,#a855f715 0%,#ec489915 100%)',
            'teal' => 'linear-gradient(135deg,#14b8a615 0%,#10b98115 100%)',
            'emerald' => 'linear-gradient(135deg,#10b98115 0%,#14b8a615 100%)',
            'violet' => 'linear-gradient(135deg,#8b5cf615 0%,#a855f715 100%)',
            default => 'linear-gradient(135deg,#667eea15 0%,#764ba215 100%)',
        };
    }

    protected function darkenHex(string $hex, int $percent): string
    {
        $hex = strlen($hex) === 3
            ? $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2]
            : $hex;
        $r = max(0, hexdec(substr($hex, 0, 2)) - (255 * $percent / 100));
        $g = max(0, hexdec(substr($hex, 2, 2)) - (255 * $percent / 100));
        $b = max(0, hexdec(substr($hex, 4, 2)) - (255 * $percent / 100));
        return sprintf('%02x%02x%02x', (int) $r, (int) $g, (int) $b);
    }
}
