<?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('businesses', function (Blueprint $table): void {
            $table->boolean('require_signed_contract_before_disbursement')->default(true);
        });

        Schema::create('loans', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('customer_id')->constrained()->restrictOnDelete();
            $table->foreignId('loan_product_id')->constrained()->restrictOnDelete();
            $table->foreignId('guarantor_id')->nullable()->constrained()->restrictOnDelete();
            $table->unsignedBigInteger('sequence_number');
            $table->unsignedSmallInteger('sequence_year');
            $table->string('loan_number')->unique();
            $table->decimal('principal', 18, 2);
            $table->decimal('interest', 18, 2);
            $table->decimal('processing_fee', 18, 2)->default(0);
            $table->decimal('insurance_fee', 18, 2)->default(0);
            $table->decimal('other_charges', 18, 2)->default(0);
            $table->decimal('fees', 18, 2)->default(0);
            $table->decimal('total_repayment', 18, 2);
            $table->text('purpose');
            $table->date('schedule_start_date');
            $table->json('custom_repayment_dates')->nullable();
            $table->string('status')->default('draft');
            $table->timestamp('submitted_at')->nullable();
            $table->timestamp('contract_generated_at')->nullable();
            $table->foreignId('contract_generated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('contract_signed_at')->nullable();
            $table->foreignId('contract_signed_uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['branch_id', 'sequence_year', 'sequence_number']);
            $table->index(['branch_id', 'status']);
            $table->index(['customer_id', 'status']);
        });

        Schema::create('loan_collateral', function (Blueprint $table): void {
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->foreignId('collateral_id')->constrained()->restrictOnDelete();
            $table->timestamps();
            $table->primary(['loan_id', 'collateral_id']);
        });

        Schema::create('loan_installments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->unsignedInteger('installment_number');
            $table->decimal('principal', 18, 2);
            $table->decimal('interest', 18, 2);
            $table->decimal('fees', 18, 2);
            $table->decimal('amount_due', 18, 2);
            $table->date('due_date');
            $table->decimal('amount_paid', 18, 2)->default(0);
            $table->decimal('remaining', 18, 2);
            $table->string('status')->default('upcoming');
            $table->timestamps();
            $table->unique(['loan_id', 'installment_number']);
            $table->index(['loan_id', 'due_date']);
        });

        Schema::create('loan_documents', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->string('kind')->default('supporting');
            $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_id', 'kind']);
        });

        Schema::create('loan_status_histories', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('loan_id')->constrained()->restrictOnDelete();
            $table->string('from_status')->nullable();
            $table->string('to_status');
            $table->text('notes')->nullable();
            $table->foreignId('changed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('changed_at');
            $table->timestamps();
            $table->index(['loan_id', 'changed_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('loan_status_histories');
        Schema::dropIfExists('loan_documents');
        Schema::dropIfExists('loan_installments');
        Schema::dropIfExists('loan_collateral');
        Schema::dropIfExists('loans');
        Schema::table('businesses', function (Blueprint $table): void {
            $table->dropColumn('require_signed_contract_before_disbursement');
        });
    }
};
