<?php

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

/**
 * 对齐高频查询路径（PostgreSQL B-tree）。
 *
 * - ticket_orders：按 draw_id 结算/后台汇总
 * - wallet_txns / ticket_items：player_id + ORDER BY id DESC 分页
 * - draws：business_date 筛选 + draw_time 排序（往期/报表）
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('ticket_orders', function (Blueprint $table): void {
            $table->index('draw_id', 'idx_ticket_orders_draw_id');
        });

        Schema::table('wallet_txns', function (Blueprint $table): void {
            $table->index(['player_id', 'id'], 'idx_wallet_txns_player_id');
        });

        Schema::table('ticket_items', function (Blueprint $table): void {
            $table->index(['player_id', 'id'], 'idx_ticket_items_player_id');
        });

        Schema::table('draws', function (Blueprint $table): void {
            $table->index(['business_date', 'draw_time'], 'idx_draws_business_date_draw_time');
        });
    }

    public function down(): void
    {
        Schema::table('draws', function (Blueprint $table): void {
            $table->dropIndex('idx_draws_business_date_draw_time');
        });

        Schema::table('ticket_items', function (Blueprint $table): void {
            $table->dropIndex('idx_ticket_items_player_id');
        });

        Schema::table('wallet_txns', function (Blueprint $table): void {
            $table->dropIndex('idx_wallet_txns_player_id');
        });

        Schema::table('ticket_orders', function (Blueprint $table): void {
            $table->dropIndex('idx_ticket_orders_draw_id');
        });
    }
};
