<?php

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $now = Carbon::now();
        $currencyCodes = DB::table('currencies')
            ->where('is_enabled', true)
            ->where('is_bettable', true)
            ->pluck('code')
            ->filter(static fn ($code): bool => is_string($code) && trim($code) !== '')
            ->map(static fn (string $code): string => strtoupper($code))
            ->unique()
            ->values();

        if ($currencyCodes->isEmpty()) {
            $currencyCodes = collect([strtoupper((string) config('lottery.default_currency', 'NPR'))]);
        }

        foreach ($currencyCodes as $currencyCode) {
            $exists = DB::table('jackpot_pools')->where('currency_code', $currencyCode)->exists();
            if ($exists) {
                continue;
            }

            DB::table('jackpot_pools')->insert([
                'currency_code' => $currencyCode,
                'current_amount' => 0,
                'contribution_rate' => '0.0200',
                'trigger_threshold' => 100_000_000,
                'payout_rate' => '0.5000',
                'force_trigger_draw_gap' => 100,
                'min_bet_amount' => 100,
                'status' => 0,
                'last_trigger_draw_id' => null,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }
    }

    public function down(): void
    {
        // 保留奖池配置与水位，避免回滚误删运营数据。
    }
};
