<?php

namespace App\Services\Ticket;

use App\Models\OddsItem;
use App\Lottery\ErrorCode;
use App\Models\OddsVersion;
use App\Models\RiskCapItem;
use App\Models\PlayConfigItem;
use App\Models\RiskCapVersion;
use App\Models\PlayConfigVersion;
use Illuminate\Support\Collection;
use App\Lottery\ConfigVersionStatus;
use App\Exceptions\TicketOperationException;

final class PlayCatalogResolver
{
    /**
     * 当前生效的三套配置版本号（无锁，供预览展示；与 {@see lockActiveConfigVersionsForPlacement} 配对使用）。
     *
     * @return array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}
     */
    public function currentActiveVersionStamp(): array
    {
        $playV = PlayConfigVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->firstOrFail();

        $oddsV = OddsVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->firstOrFail();

        $riskV = RiskCapVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->firstOrFail();

        return [
            'play_config_version_no' => (int) $playV->version_no,
            'odds_version_no' => (int) $oddsV->version_no,
            'risk_cap_version_no' => (int) $riskV->version_no,
        ];
    }

    /**
     * 下注事务内：按固定顺序锁住当前生效的三套配置版本，与后台切版互斥；可选与预览戳比对。
     *
     * @param  array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}|null  $expectedFromPreview
     * @return array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}
     */
    public function lockActiveConfigVersionsForPlacement(?array $expectedFromPreview = null): array
    {
        $playV = PlayConfigVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->lockForUpdate()
            ->firstOrFail();

        $oddsV = OddsVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->lockForUpdate()
            ->firstOrFail();

        $riskV = RiskCapVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->orderBy('id')
            ->lockForUpdate()
            ->firstOrFail();

        if ($expectedFromPreview !== null) {
            if ((int) $playV->version_no !== (int) $expectedFromPreview['play_config_version_no']
                || (int) $oddsV->version_no !== (int) $expectedFromPreview['odds_version_no']
                || (int) $riskV->version_no !== (int) $expectedFromPreview['risk_cap_version_no']) {
                throw new TicketOperationException('config_version_stale', ErrorCode::BetConfigStale->value);
            }
        }

        return [
            'play_config_version_no' => (int) $playV->version_no,
            'odds_version_no' => (int) $oddsV->version_no,
            'risk_cap_version_no' => (int) $riskV->version_no,
        ];
    }

    /**
     * @return array{play_config: PlayConfigItem, odds_items: Collection<int, OddsItem>}
     */
    public function resolve(string $playCode, string $currencyCode): array
    {
        $playVersion = PlayConfigVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->firstOrFail();

        $playConfig = PlayConfigItem::query()
            ->where('version_id', $playVersion->id)
            ->where('play_code', $playCode)
            ->first();

        if ($playConfig === null || ! $playConfig->is_enabled) {
            throw new TicketOperationException('play_config_disabled', ErrorCode::PlayModeClosed->value);
        }

        $oddsVersion = OddsVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->firstOrFail();

        $oddsItems = OddsItem::query()
            ->where('version_id', $oddsVersion->id)
            ->where('play_code', $playCode)
            ->where('currency_code', strtoupper($currencyCode))
            ->get();

        if ($oddsItems->isEmpty()) {
            throw new TicketOperationException('odds_missing', ErrorCode::BetPlayUnsupported->value);
        }

        return [
            'play_config' => $playConfig,
            'odds_items' => $oddsItems,
        ];
    }

    public function resolveCapAmount(int $drawId, string $number4d): int
    {
        $riskVersion = RiskCapVersion::query()
            ->where('status', ConfigVersionStatus::Active->value)
            ->firstOrFail();

        $specific = RiskCapItem::query()
            ->where('version_id', $riskVersion->id)
            ->where('draw_id', $drawId)
            ->where('normalized_number', $number4d)
            ->orderByDesc('id')
            ->first();

        if ($specific !== null) {
            return (int) $specific->cap_amount;
        }

        $generic = RiskCapItem::query()
            ->where('version_id', $riskVersion->id)
            ->whereNull('draw_id')
            ->where('normalized_number', $number4d)
            ->orderByDesc('id')
            ->first();

        if ($generic !== null) {
            return (int) $generic->cap_amount;
        }

        $default = RiskCapItem::query()
            ->where('version_id', $riskVersion->id)
            ->whereNull('draw_id')
            ->where('cap_type', 'default')
            ->orderByDesc('id')
            ->first();

        if ($default !== null) {
            return (int) $default->cap_amount;
        }

        return 50_000_000_000;
    }
}
