<?php

/**
 * 文件功能：百家乐买单活动用户记录表迁移
 *
 * 为每次活动中的每个用户维护一条聚合记录，
 * 便于前台查看参与情况、补偿金额以及领取状态。
 */

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

return new class extends Migration
{
    /**
     * 创建 baccarat_loss_cover_records 表。
     */
    public function up(): void
    {
        Schema::create('baccarat_loss_cover_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('event_id')
                ->constrained('baccarat_loss_cover_events')
                ->cascadeOnDelete()
                ->comment('所属活动 ID');
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete()
                ->comment('参与用户 ID');
            $table->unsignedBigInteger('total_bet_amount')->default(0)->comment('活动内累计下注金额');
            $table->unsignedBigInteger('total_win_payout')->default(0)->comment('活动内累计赢钱赔付');
            $table->unsignedBigInteger('total_loss_amount')->default(0)->comment('活动内累计输掉金币');
            $table->unsignedBigInteger('compensation_amount')->default(0)->comment('可领取补偿总额');
            $table->enum('claim_status', ['not_eligible', 'pending', 'claimed', 'expired'])
                ->default('not_eligible')
                ->comment('领取状态');
            $table->unsignedBigInteger('claimed_amount')->default(0)->comment('已领取补偿金额');
            $table->timestamp('claimed_at')->nullable()->comment('领取时间');
            $table->timestamps();

            $table->unique(['event_id', 'user_id'], 'uq_blcr_event_user');
            $table->index(['user_id', 'claim_status'], 'blcr_user_claim_status_idx');
        });
    }

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