<?php

declare(strict_types=1);

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('dberp_settlement_adjustments', function (Blueprint $table): void {
            $table->id();
            $table->string('code', 64)->comment('调整单号');
            $table->string('settlement_side', 16)->comment('结算方向');
            $table->string('counterparty_type', 16)->comment('往来对象类型');
            $table->unsignedBigInteger('counterparty_id')->comment('往来对象主键');
            $table->foreignId('receivable_record_id')->nullable();
            $table->foreign('receivable_record_id', 'fk_dberp_adj_recv_record')->references('id')->on('dberp_receivable_records')->restrictOnDelete();
            $table->foreignId('payable_record_id')->nullable();
            $table->foreign('payable_record_id', 'fk_dberp_adj_pay_record')->references('id')->on('dberp_payable_records')->restrictOnDelete();
            $table->string('adjustment_direction', 16)->comment('调整方向');
            $table->string('adjustment_type', 32)->default('manual')->comment('调整类型');
            $table->decimal('amount', 18, 4)->default(0)->comment('调整金额');
            $table->decimal('effective_amount', 18, 4)->default(0)->comment('有效金额');
            $table->string('status', 32)->default('draft')->comment('状态');
            $table->dateTime('business_date')->comment('业务时间');
            $table->foreignId('handler_employee_id')->nullable();
            $table->foreign('handler_employee_id', 'fk_dberp_adj_handler')->references('id')->on('dberp_employees')->nullOnDelete();
            $table->string('source_system', 32)->nullable()->comment('来源系统');
            $table->string('external_document_no', 128)->nullable()->comment('外部单号');
            $table->string('external_reference_id', 128)->nullable()->comment('外部关联 ID');
            $table->dateTime('effective_at')->nullable()->comment('生效时间');
            $table->dateTime('voided_at')->nullable()->comment('作废时间');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_settle_adj_code');
            $table->index(['settlement_side', 'counterparty_type', 'counterparty_id'], 'idx_dberp_settle_adj_counterparty');
            $table->index(['status', 'business_date'], 'idx_dberp_settle_adj_status_time');
        });

        Schema::create('dberp_settlement_statements', function (Blueprint $table): void {
            $table->id();
            $table->string('code', 64)->comment('对账单号');
            $table->string('settlement_side', 16)->comment('结算方向');
            $table->string('counterparty_type', 16)->comment('往来对象类型');
            $table->unsignedBigInteger('counterparty_id')->comment('往来对象主键');
            $table->string('counterparty_name', 255)->nullable()->comment('往来对象快照名称');
            $table->date('statement_date')->comment('对账日期');
            $table->date('period_start_date')->comment('期间开始');
            $table->date('period_end_date')->comment('期间结束');
            $table->decimal('opening_balance', 18, 4)->default(0)->comment('期初余额');
            $table->decimal('period_increase_amount', 18, 4)->default(0)->comment('期间增加');
            $table->decimal('period_decrease_amount', 18, 4)->default(0)->comment('期间减少');
            $table->decimal('closing_balance', 18, 4)->default(0)->comment('期末余额');
            $table->unsignedInteger('item_count')->default(0)->comment('快照行数');
            $table->string('statement_status', 32)->default('draft')->comment('状态');
            $table->foreignId('handler_employee_id')->nullable();
            $table->foreign('handler_employee_id', 'fk_dberp_statement_handler')->references('id')->on('dberp_employees')->nullOnDelete();
            $table->dateTime('confirmed_at')->nullable()->comment('确认时间');
            $table->dateTime('voided_at')->nullable()->comment('作废时间');
            $table->dateTime('last_exported_at')->nullable()->comment('最近导出时间');
            $table->foreignId('last_exported_by')->nullable();
            $table->foreign('last_exported_by', 'fk_dberp_statement_exported_by')->references('id')->on('users')->nullOnDelete();
            $table->string('source_system', 32)->nullable()->comment('来源系统');
            $table->string('external_document_no', 128)->nullable()->comment('外部单号');
            $table->string('external_reference_id', 128)->nullable()->comment('外部关联 ID');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('code', 'uq_dberp_statement_code');
            $table->index(['settlement_side', 'counterparty_type', 'counterparty_id'], 'idx_dberp_statement_counterparty');
            $table->index(['statement_status', 'statement_date'], 'idx_dberp_statement_status_date');
            $table->index(['period_start_date', 'period_end_date'], 'idx_dberp_statement_period');
        });

        Schema::create('dberp_settlement_statement_items', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('statement_id');
            $table->foreign('statement_id', 'fk_dberp_statement_item_head')->references('id')->on('dberp_settlement_statements')->cascadeOnDelete();
            $table->unsignedInteger('line_no')->default(1)->comment('行号');
            $table->string('source_type', 32)->comment('来源类型');
            $table->unsignedBigInteger('source_id')->nullable()->comment('来源主键');
            $table->string('source_document_no', 64)->nullable()->comment('来源单号');
            $table->string('summary', 255)->nullable()->comment('摘要');
            $table->dateTime('business_time')->comment('业务时间');
            $table->decimal('debit_amount', 18, 4)->default(0)->comment('增加金额');
            $table->decimal('credit_amount', 18, 4)->default(0)->comment('减少金额');
            $table->decimal('net_change_amount', 18, 4)->default(0)->comment('净变动');
            $table->decimal('balance_after', 18, 4)->default(0)->comment('余额');
            $table->string('source_status', 32)->nullable()->comment('来源状态');
            $table->json('snapshot_payload')->nullable()->comment('快照');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique(['statement_id', 'line_no'], 'uq_dberp_statement_item_line');
            $table->index(['statement_id', 'business_time'], 'idx_dberp_statement_item_time');
            $table->index(['source_type', 'source_id'], 'idx_dberp_statement_item_source');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_settlement_statement_items');
        Schema::dropIfExists('dberp_settlement_statements');
        Schema::dropIfExists('dberp_settlement_adjustments');
    }
};