<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('settlement_batches', function (Blueprint $table) {
            $table->id();
            $table->foreignId('draw_id')->constrained('draws')->cascadeOnDelete();
            $table->foreignId('result_batch_id')->constrained('draw_result_batches')->cascadeOnDelete();
            $table->unsignedInteger('settle_version')->default(1);
            $table->string('status', 32);
            $table->unsignedInteger('total_ticket_count')->default(0);
            $table->unsignedInteger('total_win_count')->default(0);
            $table->bigInteger('total_payout_amount')->default(0);
            $table->bigInteger('total_jackpot_payout_amount')->default(0);
            $table->string('review_status', 32)->default('pending');
            $table->foreignId('reviewed_by')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->string('review_remark', 255)->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();

            $table->index(['draw_id', 'settle_version'], 'idx_settlement_batches_draw_version');
        });

        Schema::create('ticket_settlement_details', function (Blueprint $table) {
            $table->id();
            $table->foreignId('settlement_batch_id')->constrained('settlement_batches')->cascadeOnDelete();
            $table->foreignId('ticket_item_id')->constrained('ticket_items')->cascadeOnDelete();
            $table->string('matched_prize_tier', 32)->nullable();
            $table->bigInteger('win_amount')->default(0);
            $table->bigInteger('jackpot_allocation_amount')->default(0);
            $table->json('match_detail_json')->nullable();
            $table->timestamps();

            $table->unique(['settlement_batch_id', 'ticket_item_id'], 'uk_ticket_settlement_batch_ticket');
        });

        Schema::create('jackpot_pools', function (Blueprint $table) {
            $table->id();
            $table->string('currency_code', 16)->unique();
            $table->bigInteger('current_amount')->default(0);
            $table->decimal('contribution_rate', 8, 4)->default(0);
            $table->bigInteger('trigger_threshold')->default(0);
            $table->decimal('payout_rate', 8, 4)->default(0);
            $table->unsignedInteger('force_trigger_draw_gap')->default(0);
            $table->bigInteger('min_bet_amount')->default(0);
            $table->unsignedTinyInteger('status')->default(0)->comment('0=off,1=on');
            $table->foreignId('last_trigger_draw_id')->nullable()->constrained('draws')->nullOnDelete();
            $table->timestamps();
        });

        Schema::create('jackpot_contributions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('jackpot_pool_id')->constrained('jackpot_pools')->cascadeOnDelete();
            $table->foreignId('draw_id')->constrained('draws')->cascadeOnDelete();
            $table->foreignId('player_id')->constrained('players')->cascadeOnDelete();
            $table->foreignId('ticket_item_id')->nullable()->constrained('ticket_items')->nullOnDelete();
            $table->bigInteger('contribution_amount')->default(0);
            $table->string('currency_code', 16);
            $table->timestamps();

            $table->index(['draw_id', 'player_id'], 'idx_jackpot_contrib_draw_player');
        });

        Schema::create('jackpot_payout_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('draw_id')->constrained('draws')->cascadeOnDelete();
            $table->foreignId('jackpot_pool_id')->constrained('jackpot_pools')->cascadeOnDelete();
            $table->string('trigger_type', 32);
            $table->bigInteger('total_payout_amount')->default(0);
            $table->unsignedInteger('winner_count')->default(0);
            $table->json('trigger_snapshot_json')->nullable();
            $table->timestamp('created_at')->useCurrent();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('jackpot_payout_logs');
        Schema::dropIfExists('jackpot_contributions');
        Schema::dropIfExists('jackpot_pools');
        Schema::dropIfExists('ticket_settlement_details');
        Schema::dropIfExists('settlement_batches');
    }
};
