<?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_spending_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_id')->constrained('kylin_games')->cascadeOnDelete();
            $table->unsignedInteger('amount_cents')->comment('消费金额(分)');
            $table->string('order_no', 64)->unique()->comment('订单号');
            $table->string('status', 20)->default('pending')->comment('状态: pending/success/failed/refunded');
            $table->string('age_group_at_time', 20)->nullable()->comment('消费时年龄分组');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'status']);
            $table->index(['user_id', 'created_at']);
        });
    }

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