<?php

namespace App\Models;

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

class SiteSetting extends Model
{
    protected $fillable = ['key', 'value', 'type', 'group', 'label', 'description'];

    /**
     * Durée du cache en secondes (1 heure)
     */
    protected const CACHE_TTL = 3600;

    /**
     * Obtenir une valeur de paramètre
     */
    public static function get(string $key, $default = null)
    {
        return Cache::remember("site_setting_{$key}", self::CACHE_TTL, function () use ($key, $default) {
            $setting = self::where('key', $key)->first();
            return $setting ? $setting->value : $default;
        });
    }

    /**
     * Définir une valeur de paramètre
     */
    public static function set(string $key, $value, string $type = 'text', string $group = 'general'): void
    {
        self::updateOrCreate(
            ['key' => $key],
            ['value' => $value, 'type' => $type, 'group' => $group]
        );
        Cache::forget("site_setting_{$key}");
        Cache::forget('site_settings_all');
    }

    /**
     * Obtenir tous les paramètres d'un groupe
     */
    public static function getGroup(string $group): array
    {
        return Cache::remember("site_settings_group_{$group}", self::CACHE_TTL, function () use ($group) {
            return self::where('group', $group)->pluck('value', 'key')->toArray();
        });
    }

    /**
     * Obtenir tous les paramètres
     */
    public static function getAllSettings(): array
    {
        return Cache::remember('site_settings_all', self::CACHE_TTL, function () {
            return self::all()->pluck('value', 'key')->toArray();
        });
    }

    /**
     * Obtenir l'URL du logo
     */
    public static function getLogoUrl(): string
    {
        $logo = self::get('site_logo', 'images/logo.png');
        
        if (empty($logo)) {
            return asset('images/logo.png');
        }
        
        // Si c'est un chemin storage
        if (str_starts_with($logo, 'site/') || str_starts_with($logo, 'logos/')) {
            return asset('storage/' . $logo);
        }
        
        // Sinon c'est dans public
        return asset($logo);
    }

    /**
     * Obtenir la hauteur du logo
     */
    public static function getLogoHeight(): int
    {
        return (int) self::get('logo_height', 40);
    }

    /**
     * Vérifier si on doit afficher le texte à côté du logo
     */
    public static function showLogoText(): bool
    {
        return (bool) self::get('show_logo_text', false);
    }

    /**
     * Obtenir le nom du site
     */
    public static function getSiteName(): string
    {
        return self::get('site_name', config('app.name', 'ATARA LifeStyle'));
    }

    /**
     * Vider le cache des paramètres
     */
    public static function clearCache(): void
    {
        $settings = self::all();
        foreach ($settings as $setting) {
            Cache::forget("site_setting_{$setting->key}");
        }
        Cache::forget('site_settings_all');
        
        $groups = self::select('group')->distinct()->pluck('group');
        foreach ($groups as $group) {
            Cache::forget("site_settings_group_{$group}");
        }
    }
}
