<?php

namespace app\common\model;

use app\common\service\GameLiveService;
use support\think\Model;
use Throwable;

class PlayRecord extends Model
{
    protected $name = 'game_play_record';

    protected $autoWriteTimestamp = true;

    protected $type = [
        'create_time' => 'integer',
        'update_time' => 'integer',
        'pick_numbers' => 'json',
        'total_amount' => 'string',
        'win_amount' => 'string',
        'jackpot_extra_amount' => 'string',
        'status' => 'integer',
        'streak_at_bet' => 'integer',
        'is_auto' => 'integer',
    ];

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function channel(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(Channel::class, 'channel_id', 'id');
    }

    protected static function onAfterInsert($model): void
    {
        try {
            $periodId = isset($model['period_id']) ? intval($model['period_id']) : null;
            GameLiveService::publishSnapshot($periodId);
        } catch (Throwable) {
        }
    }

    public function gameRecord(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(GameRecord::class, 'period_id', 'id');
    }
}

