<?php

namespace App\Services\Config;

use App\Models\Currency;
use App\Models\OddsItem;
use App\Models\PlayType;
use App\Models\AdminUser;
use App\Models\OddsVersion;
use Illuminate\Http\Request;
use App\Services\AuditLogger;
use Illuminate\Support\Facades\DB;
use App\Support\OddsStandardScopes;
use App\Lottery\ConfigVersionStatus;
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
use App\Http\Middleware\RecordAdminApiAudit;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

/** 后台：赔率版本（{@see odds_versions} / {@see odds_items}） */
final class OddsStreamService
{
    public function __construct(
        private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
    ) {}

    /** @return LengthAwarePaginator<int, OddsVersion> */
    public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator
    {
        $q = OddsVersion::query()->orderByDesc('id');
        if ($status !== null && $status !== '') {
            $q->where('status', $status);
        }

        return $q->paginate($perPage, ['*'], 'page', max(1, $page));
    }

    public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): OddsVersion
    {
        $nextNo = (int) (OddsVersion::query()->max('version_no') ?? 0) + 1;

        return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): OddsVersion {
            $draft = OddsVersion::query()->create([
                'version_no' => $nextNo,
                'status' => ConfigVersionStatus::Draft->value,
                'effective_at' => null,
                'updated_by' => $admin->id,
                'reason' => $reason,
            ]);

            $source = null;
            if ($cloneFromVersionId !== null) {
                $source = OddsVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
            } else {
                $source = OddsVersion::query()
                    ->where('status', ConfigVersionStatus::Active->value)
                    ->first();
            }

            if ($source !== null) {
                foreach ($source->items()->orderBy('currency_code')->orderBy('play_code')->get() as $row) {
                    OddsItem::query()->create([
                        'version_id' => $draft->id,
                        'play_code' => $row->play_code,
                        'prize_scope' => $row->prize_scope,
                        'dimension' => $row->dimension,
                        'odds_value' => $row->odds_value,
                        'rebate_rate' => $row->rebate_rate,
                        'commission_rate' => $row->commission_rate,
                        'currency_code' => $row->currency_code,
                        'extra_config_json' => $row->extra_config_json,
                    ]);
                }
            } else {
                $currency = Currency::query()->where('is_bettable', true)->where('is_enabled', true)->orderBy('code')->firstOrFail();
                foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) {
                    foreach (OddsStandardScopes::PRESET_ODDS_BY_SCOPE as $scope => $oddsValue) {
                        OddsItem::query()->create([
                            'version_id' => $draft->id,
                            'play_code' => $pt->play_code,
                            'prize_scope' => $scope,
                            'dimension' => $pt->dimension,
                            'odds_value' => $oddsValue,
                            'rebate_rate' => 0,
                            'commission_rate' => 0,
                            'currency_code' => $currency->code,
                            'extra_config_json' => null,
                        ]);
                    }
                }
            }

            $draft->refresh();
            OddsStandardScopes::syncMissingForVersion($draft);

            return $draft->fresh(['items']);
        });
    }

    /**
     * @param  array<int, array<string, mixed>>  $items
     */
    public function replaceItems(OddsVersion $draft, array $items, AdminUser $admin): void
    {
        DB::transaction(function () use ($draft, $items, $admin): void {
            OddsItem::query()->where('version_id', $draft->id)->delete();

            foreach ($items as $row) {
                OddsItem::query()->create([
                    'version_id' => $draft->id,
                    'play_code' => (string) $row['play_code'],
                    'prize_scope' => (string) $row['prize_scope'],
                    'dimension' => isset($row['dimension']) ? (int) $row['dimension'] : null,
                    'odds_value' => (int) $row['odds_value'],
                    'rebate_rate' => (float) ($row['rebate_rate'] ?? 0),
                    'commission_rate' => (float) ($row['commission_rate'] ?? 0),
                    'currency_code' => strtoupper((string) $row['currency_code']),
                    'extra_config_json' => $row['extra_config_json'] ?? null,
                ]);
            }

            $draft->forceFill(['updated_by' => $admin->id])->save();
        });
    }

    public function publish(OddsVersion $draft, AdminUser $admin, ?Request $request = null): void
    {
        $this->validatePublishableDraft($draft);
        $before = $this->snapshotVersion($draft);

        DB::transaction(function () use ($draft, $admin): void {
            /** @var OddsVersion|null $current */
            $current = OddsVersion::query()
                ->where('status', ConfigVersionStatus::Active->value)
                ->lockForUpdate()
                ->first();

            if ($current !== null) {
                $current->forceFill(['status' => ConfigVersionStatus::Archived->value])->save();
            }

            $draft->forceFill([
                'status' => ConfigVersionStatus::Active->value,
                'effective_at' => now(),
                'updated_by' => $admin->id,
            ])->save();
        });

        $after = $this->snapshotVersion($draft->fresh(['items']));
        $this->hallRealtime->notifyOddsUpdate(
            (int) $draft->id,
            'v'.(string) $draft->version_no,
            ['version_no' => (int) $draft->version_no],
        );
        $this->hallRealtime->notifyPlayCatalogUpdated(
            'odds',
            (int) $draft->id,
            'v'.(string) $draft->version_no,
            ['version_no' => (int) $draft->version_no],
        );

        AuditLogger::recordForAdmin(
            $admin,
            $request,
            moduleCode: 'odds',
            actionCode: 'publish',
            targetType: 'odds_version',
            targetId: (string) $draft->id,
            beforeJson: $before,
            afterJson: $after,
        );
        $request?->attributes->set(RecordAdminApiAudit::ATTRIBUTE_AUDIT_RECORDED, true);
    }

    public function deleteVersion(OddsVersion $version, AdminUser $admin, ?Request $request = null): void
    {
        $before = $this->snapshotVersion($version);

        DB::transaction(function () use ($version): void {
            $version->delete();
        });

        AuditLogger::recordForAdmin(
            $admin,
            $request,
            moduleCode: 'odds',
            actionCode: 'delete',
            targetType: 'odds_version',
            targetId: (string) $version->id,
            beforeJson: $before,
            afterJson: null,
        );
        $request?->attributes->set(RecordAdminApiAudit::ATTRIBUTE_AUDIT_RECORDED, true);
    }

    /** @return array<string, mixed> */
    private function snapshotVersion(OddsVersion $v): array
    {
        return [
            'id' => $v->id,
            'version_no' => $v->version_no,
            'status' => $v->status,
            'effective_at' => $v->effective_at?->toIso8601String(),
            'items_count' => $v->items()->count(),
        ];
    }

    private function validatePublishableDraft(OddsVersion $draft): void
    {
        $items = $draft->items()->orderBy('currency_code')->orderBy('play_code')->orderBy('prize_scope')->get();
        $allowedPlayCodes = array_fill_keys(
            PlayType::query()->pluck('play_code')->all(),
            true,
        );
        $allowedCurrencyCodes = array_fill_keys(
            Currency::query()
                ->where('is_bettable', true)
                ->where('is_enabled', true)
                ->pluck('code')
                ->map(fn (string $code) => strtoupper($code))
                ->all(),
            true,
        );
        $allowedScopes = array_fill_keys(OddsStandardScopes::SCOPE_KEYS, true);

        $errors = [];
        $seenKeys = [];

        if ($items->isEmpty()) {
            $errors['items'][] = '草稿至少需要一条赔率配置';
        }

        foreach ($items as $index => $row) {
            $playCode = (string) $row->play_code;
            $scope = (string) $row->prize_scope;
            $dimension = $row->dimension;
            $currencyCode = strtoupper((string) $row->currency_code);
            $oddsValue = (int) $row->odds_value;
            $rebateRate = (float) $row->rebate_rate;
            $commissionRate = (float) $row->commission_rate;
            $key = $playCode.'|'.$scope.'|'.$currencyCode;

            if (! isset($allowedPlayCodes[$playCode])) {
                $errors["items.$index.play_code"][] = '玩法不存在';
            }

            if (! isset($allowedScopes[$scope])) {
                $errors["items.$index.prize_scope"][] = '奖项档位不合法';
            }

            if (! isset($allowedCurrencyCodes[$currencyCode])) {
                $errors["items.$index.currency_code"][] = '币种不可下注';
            }

            if ($dimension !== null && ! in_array($dimension, [2, 3, 4], true)) {
                $errors["items.$index.dimension"][] = '维度必须是 2、3 或 4';
            }

            if (isset($seenKeys[$key])) {
                $errors["items.$index"][] = '同一玩法、档位、币种存在重复赔率项';
            }
            $seenKeys[$key] = true;

            if ($oddsValue <= 0) {
                $errors["items.$index.odds_value"][] = '赔率值必须大于 0';
            }

            if ($rebateRate < 0 || $rebateRate > 1) {
                $errors["items.$index.rebate_rate"][] = '返水比例必须在 0 到 1 之间';
            }

            if ($commissionRate < 0 || $commissionRate > 1) {
                $errors["items.$index.commission_rate"][] = '佣金比例必须在 0 到 1 之间';
            }
        }

        if ($errors !== []) {
            throw ValidationException::withMessages($errors);
        }
    }
}
