<?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('agent_profiles', function (Blueprint $table): void {
            $table->foreignId('agent_node_id')->primary()->constrained('agent_nodes')->cascadeOnDelete();
            $table->decimal('total_share_rate', 5, 2)->default(0)->comment('总占成 0-100');
            $table->unsignedBigInteger('credit_limit')->default(0);
            $table->unsignedBigInteger('allocated_credit')->default(0);
            $table->unsignedBigInteger('used_credit')->default(0);
            $table->decimal('rebate_limit', 8, 4)->default(0);
            $table->decimal('default_player_rebate', 8, 4)->default(0);
            $table->string('settlement_cycle', 16)->default('weekly');
            $table->boolean('can_grant_extra_rebate')->default(false);
            $table->timestamps();
        });

        Schema::create('player_credit_accounts', function (Blueprint $table): void {
            $table->foreignId('player_id')->primary()->constrained('players')->cascadeOnDelete();
            $table->unsignedBigInteger('credit_limit')->default(0);
            $table->unsignedBigInteger('used_credit')->default(0);
            $table->unsignedBigInteger('frozen_credit')->default(0);
            $table->timestamps();
        });

        Schema::create('player_rebate_profiles', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('player_id')->constrained('players')->cascadeOnDelete();
            $table->string('game_type', 32)->default('*');
            $table->boolean('inherit_from_agent')->default(true);
            $table->decimal('rebate_rate', 8, 4)->default(0)->comment('回水比例 0-100');
            $table->decimal('extra_rebate_rate', 8, 4)->default(0)->comment('额外回水比例 0-100');
            $table->timestamps();
            $table->unique(['player_id', 'game_type']);
        });

        Schema::create('settlement_periods', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('admin_site_id')->constrained('admin_sites')->cascadeOnDelete();
            $table->timestamp('period_start');
            $table->timestamp('period_end');
            $table->string('status', 16)->default('open');
            $table->timestamps();
            $table->index(['admin_site_id', 'status']);
        });

        Schema::create('settlement_bills', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('settlement_period_id')->constrained('settlement_periods')->cascadeOnDelete();
            $table->string('bill_type', 16);
            $table->string('owner_type', 16);
            $table->unsignedBigInteger('owner_id');
            $table->string('counterparty_type', 16);
            $table->unsignedBigInteger('counterparty_id');
            $table->bigInteger('gross_win_loss')->default(0);
            $table->bigInteger('rebate_amount')->default(0);
            $table->bigInteger('adjustment_amount')->default(0);
            $table->bigInteger('net_amount')->default(0);
            $table->bigInteger('paid_amount')->default(0);
            $table->bigInteger('unpaid_amount')->default(0);
            $table->string('status', 16)->default('pending');
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamps();
            $table->index(['settlement_period_id', 'bill_type']);
        });

        Schema::create('rebate_records', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('player_id')->constrained('players')->cascadeOnDelete();
            $table->foreignId('settlement_period_id')->nullable()->constrained('settlement_periods')->nullOnDelete();
            $table->string('game_type', 32)->default('*');
            $table->unsignedBigInteger('valid_bet_amount')->default(0);
            $table->decimal('rebate_rate', 8, 4)->default(0);
            $table->unsignedBigInteger('rebate_amount')->default(0);
            $table->string('rebate_type', 16)->default('basic');
            $table->foreignId('owner_agent_id')->nullable()->constrained('agent_nodes')->nullOnDelete();
            $table->string('status', 16)->default('pending');
            $table->timestamps();
        });

        Schema::create('rebate_allocations', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('rebate_record_id')->constrained('rebate_records')->cascadeOnDelete();
            $table->foreignId('settlement_bill_id')->nullable()->constrained('settlement_bills')->nullOnDelete();
            $table->string('participant_type', 16);
            $table->unsignedBigInteger('participant_id')->default(0);
            $table->decimal('actual_share_rate', 5, 2)->default(0);
            $table->bigInteger('allocated_amount')->default(0);
            $table->string('allocation_rule', 32)->default('share');
            $table->timestamps();
        });

        Schema::create('payment_records', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('settlement_bill_id')->constrained('settlement_bills')->cascadeOnDelete();
            $table->string('payer_type', 16);
            $table->unsignedBigInteger('payer_id');
            $table->string('payee_type', 16);
            $table->unsignedBigInteger('payee_id');
            $table->bigInteger('amount');
            $table->string('method', 32)->nullable();
            $table->string('status', 16)->default('pending');
            $table->foreignId('created_by')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->foreignId('confirmed_by')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->timestamp('confirmed_at')->nullable();
            $table->timestamps();
        });

        Schema::create('credit_ledger', function (Blueprint $table): void {
            $table->id();
            $table->string('owner_type', 16);
            $table->unsignedBigInteger('owner_id');
            $table->bigInteger('amount');
            $table->string('reason', 64);
            $table->string('ref_type', 32)->nullable();
            $table->unsignedBigInteger('ref_id')->nullable();
            $table->timestamps();
            $table->index(['owner_type', 'owner_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('credit_ledger');
        Schema::dropIfExists('payment_records');
        Schema::dropIfExists('rebate_allocations');
        Schema::dropIfExists('rebate_records');
        Schema::dropIfExists('settlement_bills');
        Schema::dropIfExists('settlement_periods');
        Schema::dropIfExists('player_rebate_profiles');
        Schema::dropIfExists('player_credit_accounts');
        Schema::dropIfExists('agent_profiles');
    }
};
