<?php

namespace App\Models;

use App\Lottery\SettlementBatchStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/** 单期单次结算批次 {@see settlement_batches} */
final class SettlementBatch extends Model
{
    protected $fillable = [
        'draw_id',
        'result_batch_id',
        'settle_version',
        'status',
        'total_ticket_count',
        'total_win_count',
        'total_payout_amount',
        'total_jackpot_payout_amount',
        'review_status',
        'reviewed_by',
        'reviewed_at',
        'review_remark',
        'paid_at',
        'started_at',
        'finished_at',
    ];

    protected function casts(): array
    {
        return [
            'draw_id' => 'integer',
            'result_batch_id' => 'integer',
            'settle_version' => 'integer',
            'total_ticket_count' => 'integer',
            'total_win_count' => 'integer',
            'total_payout_amount' => 'integer',
            'total_jackpot_payout_amount' => 'integer',
            'reviewed_by' => 'integer',
            'reviewed_at' => 'datetime',
            'paid_at' => 'datetime',
            'started_at' => 'datetime',
            'finished_at' => 'datetime',
        ];
    }

    public function draw(): BelongsTo
    {
        return $this->belongsTo(Draw::class);
    }

    public function resultBatch(): BelongsTo
    {
        return $this->belongsTo(DrawResultBatch::class, 'result_batch_id');
    }

    public function details(): HasMany
    {
        return $this->hasMany(TicketSettlementDetail::class, 'settlement_batch_id');
    }

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