<?php

namespace App\Services\Config;

use App\Models\AdminUser;
use App\Models\RiskCapItem;
use Illuminate\Http\Request;
use App\Services\AuditLogger;
use App\Models\RiskCapVersion;
use Illuminate\Support\Facades\DB;
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
use App\Http\Middleware\RecordAdminApiAudit;
use App\Lottery\ConfigVersionStatus;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

/** 后台：风控封顶版本（{@see risk_cap_versions} / {@see risk_cap_items}） */
final class RiskCapStreamService
{
    public function __construct(
        private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
    ) {}

    /** @return LengthAwarePaginator<int, RiskCapVersion> */
    public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator
    {
        $q = RiskCapVersion::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): RiskCapVersion
    {
        $nextNo = (int) (RiskCapVersion::query()->max('version_no') ?? 0) + 1;

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

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

            if ($source !== null) {
                foreach ($source->items()->orderBy('normalized_number')->get() as $row) {
                    RiskCapItem::query()->create([
                        'version_id' => $draft->id,
                        'draw_id' => $row->draw_id,
                        'normalized_number' => $row->normalized_number,
                        'cap_amount' => $row->cap_amount,
                        'cap_type' => $row->cap_type,
                    ]);
                }
            } else {
                RiskCapItem::query()->create([
                    'version_id' => $draft->id,
                    'draw_id' => null,
                    'normalized_number' => '0000',
                    'cap_amount' => 50_000_000_000,
                    'cap_type' => 'default',
                ]);
            }

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

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

            foreach ($items as $row) {
                RiskCapItem::query()->create([
                    'version_id' => $draft->id,
                    'draw_id' => isset($row['draw_id']) ? (int) $row['draw_id'] : null,
                    'normalized_number' => (string) $row['normalized_number'],
                    'cap_amount' => (int) $row['cap_amount'],
                    'cap_type' => (string) $row['cap_type'],
                ]);
            }

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

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

        DB::transaction(function () use ($draft, $admin): void {
            /** @var RiskCapVersion|null $current */
            $current = RiskCapVersion::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->notifyPlayCatalogUpdated(
            'risk_cap',
            (int) $draft->id,
            'v'.(string) $draft->version_no,
            ['version_no' => (int) $draft->version_no],
        );

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

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

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

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

    /** @return array<string, mixed> */
    private function snapshotVersion(RiskCapVersion $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(RiskCapVersion $draft): void
    {
        $items = $draft->items()->orderBy('draw_id')->orderBy('normalized_number')->get();
        $errors = [];
        $seenKeys = [];

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

        foreach ($items as $index => $row) {
            $normalizedNumber = (string) $row->normalized_number;
            $capAmount = (int) $row->cap_amount;
            $drawId = $row->draw_id === null ? '__null__' : (string) $row->draw_id;
            $capType = (string) $row->cap_type;
            $key = $capType === 'default'
                ? 'default|'.$drawId
                : $drawId.'|'.$normalizedNumber;

            if (! preg_match('/^[0-9]{4}$/', $normalizedNumber)) {
                $errors["items.$index.normalized_number"][] = '号码必须是 4 位数字';
            }

            if ($capAmount <= 0) {
                $errors["items.$index.cap_amount"][] = '封顶金额必须大于 0';
            }

            if ($capType === 'default' && $row->draw_id !== null) {
                $errors["items.$index.cap_type"][] = '默认封顶不能绑定具体期号';
            }

            if (isset($seenKeys[$key])) {
                $errors["items.$index"][] = '同一期号与号码存在重复封顶配置';
            }
            $seenKeys[$key] = true;
        }

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