<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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');
    }
}
