<?php

namespace App\Models;

use App\Lottery\DrawStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/** 彩票期号 {@see draws} */
final class Draw extends Model
{
    protected $fillable = [
        'draw_no',
        'business_date',
        'sequence_no',
        'status',
        'start_time',
        'close_time',
        'draw_time',
        'cooling_end_time',
        'result_source',
        'current_result_version',
        'settle_version',
        'is_reopened',
    ];

    protected function casts(): array
    {
        return [
            'business_date' => 'date',
            'start_time' => 'datetime',
            'close_time' => 'datetime',
            'draw_time' => 'datetime',
            'cooling_end_time' => 'datetime',
            'current_result_version' => 'integer',
            'settle_version' => 'integer',
            'is_reopened' => 'boolean',
        ];
    }

    public function statusEnum(): ?DrawStatus
    {
        return DrawStatus::tryFrom((string) $this->status);
    }

    public function resultBatches(): HasMany
    {
        return $this->hasMany(DrawResultBatch::class);
    }

    public function resultItems(): HasMany
    {
        return $this->hasMany(DrawResultItem::class);
    }
}
