<?php

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

return new class extends Migration
{
    /**
     * 执行迁移：创建用户成就进度快照表。
     */
    public function up(): void
    {
        Schema::create('user_achievement_progress', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete()->comment('用户ID');
            $table->string('achievement_key', 80)->comment('成就唯一标识');
            $table->unsignedBigInteger('progress_value')->default(0)->comment('当前进度值');
            $table->unsignedBigInteger('threshold_value')->default(0)->comment('达成门槛快照');
            $table->timestamp('last_scanned_at')->nullable()->index()->comment('最近扫描时间');
            $table->timestamps();

            $table->unique(['user_id', 'achievement_key']);
        });
    }

    /**
     * 回滚迁移：删除用户成就进度快照表。
     */
    public function down(): void
    {
        Schema::dropIfExists('user_achievement_progress');
    }
};
