<?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('loan_early_settlements', function (Blueprint $table): void {
            $table->id();
            $table->string('settlement_number')->unique();
            $table->foreignId('loan_id')->unique()->constrained()->restrictOnDelete();
            $table->foreignId('customer_id')->constrained()->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('financial_account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->decimal('principal_amount', 18, 2);
            $table->decimal('interest_amount', 18, 2);
            $table->decimal('fees_amount', 18, 2);
            $table->decimal('penalties_amount', 18, 2);
            $table->decimal('settlement_adjustment', 18, 2)->default(0);
            $table->decimal('final_amount', 18, 2);
            $table->text('adjustment_reason')->nullable();
            $table->date('settlement_date');
            $table->string('reference')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('settled_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('financial_account_transaction_id')->nullable()->constrained('financial_account_transactions')->nullOnDelete();
            $table->timestamps();
            $table->index(['branch_id', 'settlement_date']);
        });

        Schema::create('loan_restructurings', function (Blueprint $table): void {
            $table->id();
            $table->string('restructuring_number')->unique();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->unsignedInteger('previous_schedule_version');
            $table->unsignedInteger('new_schedule_version');
            $table->decimal('principal_outstanding', 18, 2);
            $table->decimal('interest_outstanding', 18, 2);
            $table->decimal('fees_outstanding', 18, 2);
            $table->decimal('penalties_outstanding', 18, 2);
            $table->decimal('total_outstanding', 18, 2);
            $table->json('previous_schedule');
            $table->json('new_schedule');
            $table->text('reason');
            $table->string('status')->default('pending');
            $table->foreignId('requested_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('requested_at');
            $table->foreignId('approved_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('approved_at')->nullable();
            $table->foreignId('rejected_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('rejected_at')->nullable();
            $table->text('decision_comments')->nullable();
            $table->timestamps();
            $table->index(['loan_id', 'status']);
            $table->index(['branch_id', 'status']);
        });

        Schema::create('loan_restructuring_documents', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('loan_restructuring_id')->constrained('loan_restructurings')->restrictOnDelete();
            $table->string('document_type')->nullable();
            $table->string('original_name');
            $table->string('file_path');
            $table->string('mime_type')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['loan_restructuring_id', 'created_at'], 'loan_restruct_docs_created_idx');
        });

        Schema::table('loan_installments', function (Blueprint $table): void {
            $table->dropUnique(['loan_id', 'installment_number']);
            $table->unsignedInteger('schedule_version')->default(1)->after('loan_id');
            $table->boolean('is_active')->default(true)->after('schedule_version');
            $table->foreignId('loan_restructuring_id')->nullable()->after('is_active')->constrained('loan_restructurings')->restrictOnDelete();
            $table->unique(['loan_id', 'schedule_version', 'installment_number'], 'loan_installments_version_number_unique');
            $table->index(['loan_id', 'is_active', 'schedule_version'], 'loan_installments_active_version_index');
        });

        Schema::table('loans', function (Blueprint $table): void {
            $table->unsignedInteger('current_schedule_version')->default(1)->after('schedule_activated_at');
        });
    }

    public function down(): void
    {
        Schema::table('loans', function (Blueprint $table): void {
            $table->dropColumn('current_schedule_version');
        });

        Schema::table('loan_installments', function (Blueprint $table): void {
            $table->dropForeign(['loan_restructuring_id']);
            $table->dropUnique('loan_installments_version_number_unique');
            $table->dropIndex('loan_installments_active_version_index');
            $table->dropColumn(['schedule_version', 'is_active', 'loan_restructuring_id']);
            $table->unique(['loan_id', 'installment_number']);
        });

        Schema::dropIfExists('loan_restructuring_documents');
        Schema::dropIfExists('loan_restructurings');
        Schema::dropIfExists('loan_early_settlements');
    }
};
