<?php

/**
 * 文件功能：创建每日签到记录表。
 *
 * 每个用户每天只能签到一次，并保存当次连续天数与实际发放奖励快照。
 */

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

/**
 * 类功能：维护 daily_sign_ins 表结构与回滚逻辑。
 */
return new class extends Migration
{
    /**
     * 创建每日签到记录表。
     */
    public function up(): void
    {
        Schema::create('daily_sign_ins', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete()->comment('用户ID');
            $table->unsignedBigInteger('room_id')->nullable()->index()->comment('签到所在房间ID');
            $table->date('sign_in_date')->comment('签到日期');
            $table->unsignedInteger('streak_days')->comment('连续签到天数');
            $table->foreignId('reward_rule_id')->nullable()->constrained('sign_in_reward_rules')->nullOnDelete()->comment('命中奖励规则ID');
            $table->unsignedInteger('gold_reward')->default(0)->comment('实际发放金币');
            $table->unsignedInteger('exp_reward')->default(0)->comment('实际发放经验');
            $table->unsignedInteger('charm_reward')->default(0)->comment('实际发放魅力');
            $table->string('identity_badge_code', 50)->nullable()->comment('本次刷新身份徽章编码');
            $table->string('identity_badge_name', 50)->nullable()->comment('本次刷新身份徽章名称');
            $table->string('identity_badge_icon', 120)->nullable()->comment('本次刷新身份徽章图标');
            $table->string('identity_badge_color', 20)->nullable()->comment('本次刷新身份徽章颜色');
            $table->timestamps();

            $table->unique(['user_id', 'sign_in_date'], 'uniq_user_daily_sign_in');
            $table->index(['sign_in_date', 'streak_days'], 'idx_daily_sign_ins_date_streak');
        });
    }

    /**
     * 回滚：删除每日签到记录表。
     */
    public function down(): void
    {
        Schema::dropIfExists('daily_sign_ins');
    }
};
