<?php

namespace App\Models;

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

class HomePageSetting extends Model
{
    protected $table = 'home_page_settings';

    protected $fillable = [
        'section',
        'title_en',
        'title_km',
        'description_en',
        'description_km',
        'image',
        'highlights',
        'display_limit',
        'status',
        'order',
    ];

    protected $casts = [
        'highlights' => 'array',
    ];

    /**
     * Get a section's settings
     */
    public static function getSection($section, $locale = 'en')
    {
        // Normalize locale: accept both 'kh' and 'km' for Khmer
        if ($locale === 'kh') {
            $locale = 'km';
        }

        $cacheKey = "home_page_settings_{$section}_{$locale}";

        return Cache::remember($cacheKey, 3600, function () use ($section, $locale) {
            $setting = self::where('section', $section)
                ->where('status', 'active')
                ->first();

            if (!$setting) {
                return null;
            }

            $imageUrl = $setting->image ? public_asset_url($setting->image) : null;

            return [
                'section' => $setting->section,
                'title' => $locale === 'km' ? $setting->title_km : $setting->title_en,
                'description' => $locale === 'km' ? $setting->description_km : $setting->description_en,
                'image' => $imageUrl,
                'highlights' => $setting->highlights,
                'display_limit' => $setting->display_limit,
            ];
        });
    }

    /**
     * Get all active sections
     */
    public static function getAllSections($locale = 'en')
    {
        // Normalize locale: accept both 'kh' and 'km' for Khmer
        if ($locale === 'kh') {
            $locale = 'km';
        }

        $sections = ['about', 'products', 'contact', 'promise'];
        $result = [];

        foreach ($sections as $section) {
            $data = self::getSection($section, $locale);
            if ($data) {
                $result[$section] = $data;
            }
        }

        return $result;
    }

    /**
     * Clear cache for a section
     */
    public static function clearSectionCache($section)
    {
        Cache::forget("home_page_settings_{$section}_en");
        Cache::forget("home_page_settings_{$section}_km");
        Cache::forget("home_page_settings_{$section}_kh");
    }

    /**
     * Clear cache - alias for clearSectionCache
     */
    public static function clearCache($section = null)
    {
        if ($section) {
            self::clearSectionCache($section);
        } else {
            // Clear all sections
            foreach (['about', 'products', 'contact', 'promise'] as $section) {
                self::clearSectionCache($section);
            }
        }
    }

    /**
     * Boot model
     */
    protected static function booted()
    {
        static::saved(function ($model) {
            self::clearSectionCache($model->section);
        });

        static::deleted(function ($model) {
            self::clearSectionCache($model->section);
        });
    }
}
