<?php

declare(strict_types=1);

namespace App\Models;

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

/**
 * 宿主系统更新备份记录（表 host_system_update_backups）。
 *
 * @property int $execution_id
 * @property string $backup_no
 * @property string|null $backup_type
 * @property string|null $status
 * @property string|null $backup_path
 * @property int|null $backup_size
 * @property string|null $checksum
 * @property string|null $checksum_algorithm
 * @property array<string, mixed>|null $included_paths
 * @property array<string, mixed>|null $excluded_paths
 * @property Carbon|null $started_at
 * @property Carbon|null $finished_at
 * @property Carbon|null $failed_at
 * @property string|null $error_code
 * @property string|null $error_message
 */
class HostSystemUpdateBackup extends Model
{
    public const TYPE_FILE_SNAPSHOT = 'file_snapshot';

    public const TYPE_ENV_SNAPSHOT = 'env_snapshot';

    public const TYPE_DATABASE_SNAPSHOT = 'database_snapshot';

    public const TYPE_VENDOR_SNAPSHOT = 'vendor_snapshot';

    public const STATUS_PENDING = 'pending';

    public const STATUS_RUNNING = 'running';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_FAILED = 'failed';

    protected $table = 'host_system_update_backups';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'execution_id',
        'backup_no',
        'backup_type',
        'status',
        'backup_path',
        'backup_size',
        'checksum',
        'checksum_algorithm',
        'included_paths',
        'excluded_paths',
        'started_at',
        'finished_at',
        'failed_at',
        'error_code',
        'error_message',
    ];

    protected function casts(): array
    {
        return [
            'execution_id' => 'integer',
            'backup_size' => 'integer',
            'included_paths' => 'array',
            'excluded_paths' => 'array',
            'started_at' => 'datetime',
            'finished_at' => 'datetime',
            'failed_at' => 'datetime',
        ];
    }

    public function execution(): BelongsTo
    {
        return $this->belongsTo(HostSystemUpdateExecution::class, 'execution_id');
    }
}
