<?php

namespace App\Models;

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

class HeaderFooterSetting extends Model
{
    use HasFactory;

    protected $fillable = [
        'section',
        'label',
        'is_enabled',
        // Header
        'show_logo',
        'show_search',
        'show_navigation',
        'show_auth_buttons',
        'logo_url',
        'custom_css',
        // Footer
        'brand_description',
        'show_social_links',
        'show_platform_links',
        'show_tools_links',
        'show_corporate_links',
        'custom_html',
        'copyright_text',
        'sort_order',
    ];

    protected $casts = [
        'is_enabled' => 'boolean',
        'show_logo' => 'boolean',
        'show_search' => 'boolean',
        'show_navigation' => 'boolean',
        'show_auth_buttons' => 'boolean',
        'show_social_links' => 'boolean',
        'show_platform_links' => 'boolean',
        'show_tools_links' => 'boolean',
        'show_corporate_links' => 'boolean',
    ];

    protected static int $cacheTtl = 3600; // 1 saat

    /**
     * Get all settings cached by section
     */
    public static function getAllCached(): array
    {
        return Cache::remember('header_footer_settings', self::$cacheTtl, function () {
            return self::all()->keyBy('section')->toArray();
        });
    }

    /**
     * Get settings for a specific section
     */
    public static function forSection(string $section): ?array
    {
        $all = self::getAllCached();
        return $all[$section] ?? null;
    }

    /**
     * Check if a section is enabled
     */
    public static function isEnabled(string $section): bool
    {
        $settings = self::forSection($section);
        return $settings['is_enabled'] ?? true;
    }

    /**
     * Get header settings
     */
    public static function getHeader(): ?array
    {
        return self::forSection('header');
    }

    /**
     * Get footer settings
     */
    public static function getFooter(): ?array
    {
        return self::forSection('footer');
    }

    /**
     * Clear settings cache
     */
    public static function clearCache(): void
    {
        Cache::forget('header_footer_settings');
    }

    /**
     * Clear cache after saving
     */
    protected static function boot()
    {
        parent::boot();

        static::saved(function () {
            self::clearCache();
        });

        static::deleted(function () {
            self::clearCache();
        });
    }

    /**
     * Get available sections
     */
    public static function getSections(): array
    {
        return [
            'header' => 'Header (Üst Menü)',
            'footer' => 'Footer (Alt Menü)',
        ];
    }
}
