<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('wallet_txns', function (Blueprint $table) {
            $table->id();
            $table->string('txn_no', 64)->unique();
            $table->foreignId('player_id')->constrained('players')->cascadeOnDelete();
            $table->foreignId('wallet_id')->constrained('player_wallets')->cascadeOnDelete();
            $table->string('biz_type', 32);
            $table->string('biz_no', 64)->nullable();
            $table->unsignedTinyInteger('direction')->comment('1=in,2=out');
            $table->bigInteger('amount');
            $table->bigInteger('balance_before');
            $table->bigInteger('balance_after');
            $table->string('status', 32);
            $table->string('external_ref_no', 64)->nullable();
            $table->string('idempotent_key', 64)->nullable();
            $table->string('remark', 255)->nullable();
            $table->timestamps();

            $table->index(['player_id', 'created_at'], 'idx_wallet_txns_player_time');
            $table->index(['biz_type', 'biz_no'], 'idx_wallet_txns_biz');
            $table->unique(['idempotent_key', 'biz_type'], 'uk_wallet_txns_idempotent_biz');
        });
    }

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