<?php

namespace App\Apps\Versionmgr\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UpgradePackage extends BaseModel
{
    protected $table = 'app_versionmgr_upgrade_package';

    public const CREATED_AT = 'create_time';

    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'from_version',
        'to_version',
        'build',
        'changelog',
        'package_path',
        'package_size',
        'checksum',
        'source',
        'status',
        'operator_id',
    ];

    protected $casts = [
        'status' => 'integer',
        'package_size' => 'integer',
    ];

    public function operator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\AdminUser::class, 'operator_id');
    }

    public function scopeAvailable($query)
    {
        return $query->where('status', 1);
    }

    public static function findByVersions(string $fromVersion, string $toVersion): ?self
    {
        return static::where('from_version', $fromVersion)
            ->where('to_version', $toVersion)
            ->where('status', 1)
            ->first();
    }
}
