<?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_achievements', 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->timestamp('achieved_at')->nullable()->index()->comment('达成时间');
            $table->timestamp('notified_at')->nullable()->comment('通知时间');
            $table->json('metadata')->nullable()->comment('成就解锁附加信息');
            $table->timestamps();

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

    /**
     * 回滚迁移：删除用户成就解锁记录表。
     */
    public function down(): void
    {
        Schema::dropIfExists('user_achievements');
    }
};
