<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AppSetting extends Model
{
    protected $table = 'app_settings';

    protected $fillable = [
        'logo',
        'logo_small',
        'logo_path',
        'logo_small_path',
        'nome_applicazione',
        'nome_organizzazione',
        'footer_text',
        'app_version',
        'dashboard_welcome',
        'show_version',
    ];

    public static function getSetting(string $key, $default = null)
    {
        $setting = self::first();
        return $setting?->{$key} ?? $default;
    }

    public static function getLogo(): ?string
    {
        return self::getSetting('logo');
    }

    public static function getLogoSmall(): ?string
    {
        return self::getSetting('logo_small');
    }

    public static function getAppName(): string
    {
        return self::getSetting('nome_applicazione', 'Glastree');
    }

    public static function getOrgName(): string
    {
        return self::getSetting('nome_organizzazione', 'Parrocchia');
    }

    public static function getFooterText(): string
    {
        $footer = self::getSetting('footer_text');
        if ($footer) {
            return $footer;
        }
        $appName = self::getAppName();
        $year = date('Y');
        return "© {$year} {$appName}";
    }

    public static function getAppVersion(): ?string
    {
        return self::getSetting('app_version');
    }

    public static function getDashboardWelcome(): ?string
    {
        return self::getSetting('dashboard_welcome');
    }

    public static function shouldShowVersion(): bool
    {
        return (bool) self::getSetting('show_version', false);
    }

    public function getLogoUrlInstance(): ?string
    {
        if ($this->logo && file_exists(public_path('storage/' . $this->logo))) {
            return asset('storage/' . $this->logo);
        }
        return null;
    }

    public function getLogoSmallUrlInstance(): ?string
    {
        if ($this->logo_small && file_exists(public_path('storage/' . $this->logo_small))) {
            return asset('storage/' . $this->logo_small);
        }
        return null;
    }

    public function getLogoPath(): ?string
    {
        if ($this->logo_path && file_exists(public_path('storage/' . $this->logo_path))) {
            return asset('storage/' . $this->logo_path);
        }
        if ($this->logo && file_exists(public_path('storage/' . $this->logo))) {
            return asset('storage/' . $this->logo);
        }
        return null;
    }

    public function getLogoSmallPath(): ?string
    {
        if ($this->logo_small_path && file_exists(public_path('storage/' . $this->logo_small_path))) {
            return asset('storage/' . $this->logo_small_path);
        }
        if ($this->logo_small && file_exists(public_path('storage/' . $this->logo_small))) {
            return asset('storage/' . $this->logo_small);
        }
        return null;
    }

    public static function getLogoUrl(): ?string
    {
        $setting = self::first();
        return $setting?->getLogoPath();
    }

    public static function getLogoSmallUrl(): ?string
    {
        $setting = self::first();
        return $setting?->getLogoSmallPath();
    }
}