<?php

declare(strict_types=1);

namespace App\Domain\Cron\Models;

use App\Domain\Account\Models\Account;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class CronJob extends Model
{
    use SoftDeletes;

    protected $table = 'cron_jobs';

    protected $fillable = [
        'account_id', 'label', 'expression', 'command',
        'email_output', 'is_active', 'last_run_at', 'last_exit_code', 'last_output',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'last_run_at' => 'datetime',
            'last_exit_code' => 'integer',
        ];
    }

    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    /**
     * Cron expression için human-readable çevirisi (basit).
     */
    public function humanReadable(): string
    {
        $presets = [
            '* * * * *' => 'Her dakika',
            '*/5 * * * *' => 'Her 5 dakikada bir',
            '*/15 * * * *' => 'Her 15 dakikada bir',
            '*/30 * * * *' => 'Her 30 dakikada bir',
            '0 * * * *' => 'Her saat başı',
            '0 */6 * * *' => 'Her 6 saatte bir',
            '0 0 * * *' => 'Her gün gece yarısı',
            '0 3 * * *' => 'Her gün saat 03:00',
            '0 0 * * 0' => 'Her Pazar gece yarısı',
            '0 0 1 * *' => 'Her ayın 1\'inde',
        ];

        return $presets[$this->expression] ?? $this->expression;
    }
}
