<?php

/**
 * 文件功能：双色球购票记录表迁移
 *
 * 记录每用户每注的选号、中奖等级、派奖金额。
 *
 * @author  ChatRoom Laravel
 *
 * @version 1.0.0
 */

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

return new class extends Migration
{
    /**
     * 创建 lottery_tickets 表。
     */
    public function up(): void
    {
        Schema::create('lottery_tickets', function (Blueprint $table) {
            $table->id();

            $table->foreignId('issue_id')->constrained('lottery_issues')->cascadeOnDelete()->comment('期次 ID');
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete()->comment('购票用户 ID');

            // 用户选号（红球已排序存储，方便比对）
            $table->unsignedTinyInteger('red1')->comment('用户选红球1（较小）');
            $table->unsignedTinyInteger('red2')->comment('用户选红球2');
            $table->unsignedTinyInteger('red3')->comment('用户选红球3（较大）');
            $table->unsignedTinyInteger('blue')->comment('用户选蓝球');

            // 购票信息
            $table->unsignedInteger('amount')->comment('购票金额（每注固定 ticket_price）');
            $table->boolean('is_quick_pick')->default(false)->comment('是否机选');

            // 开奖结果（开奖后写入）
            $table->unsignedTinyInteger('prize_level')->default(0)->comment('中奖等级：0=未中 1-5=各等级');
            $table->unsignedBigInteger('payout')->default(0)->comment('实际派奖金额');

            $table->timestamps();

            $table->index(['issue_id', 'user_id']);
            $table->index(['user_id', 'created_at']);
            $table->index('prize_level');
        });
    }

    /**
     * 回滚迁移。
     */
    public function down(): void
    {
        Schema::dropIfExists('lottery_tickets');
    }
};
