<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class BackupSetting extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    protected $casts = [
        'is_automated_enabled' => 'boolean',
        'last_run_at' => 'datetime',
    ];

    public static function current(): self
    {
        return static::query()->firstOrCreate([]);
    }

    public function isDue(): bool
    {
        if (!$this->is_automated_enabled) {
            return false;
        }

        if (!$this->last_run_at) {
            return true;
        }

        return match ($this->frequency) {
            'weekly' => $this->last_run_at->lt(now()->subWeek()),
            'monthly' => $this->last_run_at->lt(now()->subMonth()),
            default => $this->last_run_at->lt(now()->subDay()),
        };
    }
}
