<?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::table('ticket_items', function (Blueprint $table): void {
            $table->foreignId('agent_node_id')->nullable()->after('player_id')->constrained('agent_nodes')->nullOnDelete();
            $table->json('share_snapshot')->nullable()->after('rule_snapshot_json');
            $table->decimal('agent_rebate_rate_snapshot', 8, 4)->nullable()->after('share_snapshot');
            $table->timestamp('agent_settled_at')->nullable()->after('settled_at');
            $table->foreignId('agent_settlement_reversal_of_id')->nullable()->after('agent_settled_at')
                ->constrained('ticket_items')->nullOnDelete();
        });

        Schema::create('share_ledger', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('ticket_item_id')->constrained('ticket_items')->cascadeOnDelete();
            $table->foreignId('player_id')->constrained('players')->cascadeOnDelete();
            $table->foreignId('agent_node_id')->nullable()->constrained('agent_nodes')->nullOnDelete();
            $table->json('agent_path')->nullable();
            $table->json('share_snapshot')->nullable();
            $table->bigInteger('game_win_loss')->default(0);
            $table->bigInteger('basic_rebate')->default(0);
            $table->bigInteger('shared_net_win_loss')->default(0);
            $table->json('allocations_json')->nullable();
            $table->foreignId('settlement_period_id')->nullable()->constrained('settlement_periods')->nullOnDelete();
            $table->unsignedBigInteger('reversal_of_id')->nullable();
            $table->timestamp('settled_at');
            $table->timestamps();
            $table->index(['settled_at', 'player_id']);
            $table->index(['settlement_period_id']);
        });

        Schema::table('share_ledger', function (Blueprint $table): void {
            $table->foreign('reversal_of_id')->references('id')->on('share_ledger')->nullOnDelete();
        });

        Schema::table('rebate_records', function (Blueprint $table): void {
            $table->foreignId('ticket_item_id')->nullable()->after('player_id')->constrained('ticket_items')->nullOnDelete();
            $table->foreignId('reversal_of_id')->nullable()->after('ticket_item_id')->constrained('rebate_records')->nullOnDelete();
        });

        Schema::table('settlement_bills', function (Blueprint $table): void {
            $table->timestamp('locked_at')->nullable()->after('confirmed_at');
            $table->foreignId('reversed_bill_id')->nullable()->after('locked_at')->constrained('settlement_bills')->nullOnDelete();
            $table->json('meta_json')->nullable()->after('reversed_bill_id');
        });

        Schema::create('settlement_adjustments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('settlement_period_id')->nullable()->constrained('settlement_periods')->nullOnDelete();
            $table->foreignId('original_bill_id')->nullable()->constrained('settlement_bills')->nullOnDelete();
            $table->string('adjustment_type', 32);
            $table->bigInteger('amount');
            $table->string('reason', 255)->nullable();
            $table->foreignId('created_by')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->timestamps();
        });
    }

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

        Schema::table('settlement_bills', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('reversed_bill_id');
            $table->dropColumn(['locked_at', 'meta_json']);
        });

        Schema::table('rebate_records', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('reversal_of_id');
            $table->dropConstrainedForeignId('ticket_item_id');
        });

        Schema::dropIfExists('share_ledger');

        Schema::table('ticket_items', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('agent_settlement_reversal_of_id');
            $table->dropConstrainedForeignId('agent_node_id');
            $table->dropColumn(['share_snapshot', 'agent_rebate_rate_snapshot', 'agent_settled_at']);
        });
    }
};
