<?php

declare(strict_types=1);

namespace App\Domain\Backup\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property string $name
 * @property bool $is_active
 * @property string $cron
 * @property string $scope
 * @property array|null $include_accounts
 * @property array|null $exclude_paths
 * @property int $destination_id
 * @property string $compression
 * @property bool $encrypt
 * @property int $keep_daily
 * @property int $keep_weekly
 * @property int $keep_monthly
 * @property int $keep_yearly
 * @property Carbon|null $last_run_at
 * @property string|null $last_run_status
 */
class BackupSchedule extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'name', 'is_active', 'cron', 'scope',
        'backup_mode', 'full_frequency_days', 'max_chain_length',
        'include_accounts', 'exclude_paths',
        'destination_id',
        'compression', 'encrypt', 'encryption_key_id',
        'keep_daily', 'keep_weekly', 'keep_monthly', 'keep_yearly',
        'last_run_at', 'last_run_status', 'last_run_error',
        'last_run_duration_seconds', 'last_run_size_bytes',
        'next_run_at',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'encrypt' => 'boolean',
            'include_accounts' => 'array',
            'exclude_paths' => 'array',
            'keep_daily' => 'integer',
            'keep_weekly' => 'integer',
            'keep_monthly' => 'integer',
            'keep_yearly' => 'integer',
            'full_frequency_days' => 'integer',
            'max_chain_length' => 'integer',
            'last_run_at' => 'datetime',
            'next_run_at' => 'datetime',
        ];
    }

    public function destination(): BelongsTo
    {
        return $this->belongsTo(BackupDestination::class, 'destination_id');
    }

    /**
     * 5-field cron'u human-readable'a çevir.
     */
    public function humanCron(): string
    {
        $parts = explode(' ', $this->cron);
        if (count($parts) !== 5) {
            return $this->cron;
        }

        [$min, $hour, $dom, $month, $dow] = $parts;

        if ($min === '0' && $hour !== '*' && $dom === '*' && $month === '*' && $dow === '*') {
            return "Her gün {$hour}:00";
        }
        if ($dom === '*' && $month === '*' && $dow !== '*') {
            // Cron day-of-week: 0=Pazar, 1=Pzt, 2=Sal, ..., 6=Cmt (Linux standard)
            // 7 de Pazar olarak kabul edilir (BSD uyumluluk)
            $days = ['Paz', 'Pzt', 'Sal', 'Çar', 'Per', 'Cum', 'Cmt'];
            $idx = ((int) $dow) % 7;

            return "Her hafta {$days[$idx]} {$hour}:".str_pad($min, 2, '0', STR_PAD_LEFT);
        }
        if ($dom !== '*' && $month === '*' && $dow === '*') {
            return "Her ayın {$dom}'ı, saat {$hour}:".str_pad($min, 2, '0', STR_PAD_LEFT);
        }

        return $this->cron;
    }
}
