<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model
{
    protected $fillable = [
        'congregazione_nome',
        'logo_path',
        'giorni_giacenza_da_assegnare',
        'giorni_giacenza_prioritari',
        'giorni_per_smarrito',
        'home_limit_list',
        'audit_retention_days',
        'setup_completed',
    ];

    protected function casts(): array
    {
        return [
            'setup_completed' => 'boolean',
            'giorni_giacenza_da_assegnare' => 'integer',
            'giorni_giacenza_prioritari' => 'integer',
            'giorni_per_smarrito' => 'integer',
            'home_limit_list' => 'integer',
            'audit_retention_days' => 'integer',
        ];
    }

    /**
     * Get the singleton settings instance (first row).
     */
    public static function instance(): static
    {
        return static::firstOrCreate([], [
            'giorni_giacenza_da_assegnare' => 120,
            'giorni_giacenza_prioritari' => 180,
            'giorni_per_smarrito' => 120,
            'home_limit_list' => 10,
            'audit_retention_days' => 730,
        ]);
    }

    public static function isSetupComplete(): bool
    {
        $setting = static::first();
        return $setting && $setting->setup_completed;
    }

    public static function getValue(string $key, mixed $default = null): mixed
    {
        return static::instance()->{$key} ?? $default;
    }
}
