<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class AppVersion extends Model
{
    protected $table = 'versions';

    protected $keyType = 'string';
    public $incrementing = false;

    protected $fillable = [
        'version', 'name', 'changelog',
        'status', 'platform', 'show_popup',
        'released_at',
    ];

    protected $casts = [
        'show_popup'  => 'boolean',
        'released_at' => 'datetime',
    ];

    protected static function boot(): void
    {
        parent::boot();
        static::creating(function ($model) {
            if (!$model->id) {
                $model->id = (string) Str::uuid();
            }
        });
    }

    public static function current(string $platform = 'web'): ?self
    {
        return static::where('status', 'live')
            ->whereIn('platform', [$platform, 'all'])
            ->latest('released_at')
            ->first();
    }
}
