<?php

namespace App\Models;

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

class ConfiguracionPlataforma extends Model
{
    protected $table = 'configuracion_plataforma';

    protected $fillable = [
        'nombre_plataforma', 'nombre_corto', 'descripcion',
        'logo_path', 'logo_login_path', 'favicon_path', 'login_bg_path',
        'color_primario', 'color_secundario', 'color_acento', 'footer_texto',
        'smtp_host', 'smtp_port', 'smtp_user', 'smtp_password', 'smtp_encryption',
        'smtp_from_address', 'smtp_from_name',
        'whatsapp_numero', 'whatsapp_mensaje',
    ];

    protected $casts = [
        'smtp_port' => 'integer',
    ];

    /**
     * Singleton: obtener o crear la única fila de configuración.
     */
    public static function actual(): self
    {
        return Cache::remember('config_plataforma', 3600, function () {
            return self::firstOrCreate([], [
                'nombre_plataforma' => 'YBER',
                'nombre_corto' => 'YBER',
                'footer_texto' => 'Powered by Singularidad IA',
                'color_primario' => '#102a43',
                'color_secundario' => '#10b981',
                'color_acento' => '#6366f1',
            ]);
        });
    }

    public static function boot()
    {
        parent::boot();
        static::saved(function () {
            Cache::forget('config_plataforma');
        });
    }

    public function setSmtpPasswordAttribute($value)
    {
        if ($value && $value !== '********') {
            $this->attributes['smtp_password'] = Crypt::encryptString($value);
        }
    }

    public function getSmtpPasswordDecryptedAttribute(): ?string
    {
        if (!$this->smtp_password) return null;
        try {
            return Crypt::decryptString($this->smtp_password);
        } catch (\Exception $e) {
            return null;
        }
    }

    public function getLogoUrlAttribute(): ?string
    {
        if ($this->logo_path && file_exists(public_path($this->logo_path))) {
            return asset($this->logo_path);
        }
        foreach (['images/branding/logo.png', 'images/branding/logo.svg', 'images/login/logo.png'] as $p) {
            if (file_exists(public_path($p))) return asset($p);
        }
        return null;
    }

    public function getFaviconUrlAttribute(): ?string
    {
        if ($this->favicon_path && file_exists(public_path($this->favicon_path))) {
            return asset($this->favicon_path);
        }
        return null;
    }

    public function getLoginBgUrlAttribute(): ?string
    {
        if ($this->login_bg_path && file_exists(public_path($this->login_bg_path))) {
            return asset($this->login_bg_path);
        }
        foreach (['images/login/bg-left.jpg', 'images/login/bg-left.png', 'images/login/bg-desktop.jpg'] as $p) {
            if (file_exists(public_path($p))) return asset($p);
        }
        return null;
    }
}
