<?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_play_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_id')->constrained('kylin_games')->cascadeOnDelete();
            $table->timestamp('session_start')->comment('会话开始时间');
            $table->timestamp('session_end')->nullable()->comment('会话结束时间');
            $table->unsignedInteger('duration')->default(0)->comment('会话时长(秒)');
            $table->string('ip_address', 45)->nullable();
            $table->json('device_info')->nullable()->comment('设备信息');
            $table->timestamps();
            $table->softDeletes();

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

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