<?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('kylin_game_saves', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->comment('用户 ID');
            $table->foreignId('game_id')->constrained('kylin_games')->cascadeOnDelete()->comment('游戏 ID');
            $table->json('data')->comment('存档数据，JSON 格式，每款游戏自定义 schema');
            $table->unsignedInteger('version')->default(1)->comment('服务端版本号');
            $table->unsignedInteger('client_version')->default(1)->comment('客户端版本号');
            $table->timestamp('synced_at')->nullable()->comment('最后同步时间');
            $table->string('checksum', 64)->nullable()->comment('数据校验和（SHA256）');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['user_id', 'game_id']);
            $table->index('synced_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kylin_game_saves');
    }
};
