<?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('financial_accounts', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->string('name');
            $table->string('account_type');
            $table->decimal('opening_balance', 18, 2)->default(0);
            $table->decimal('current_balance', 18, 2)->default(0);
            $table->string('status')->default('active');
            $table->text('reference_details')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['branch_id', 'status']);
            $table->index(['branch_id', 'account_type']);
        });

        Schema::create('capital_injections', function (Blueprint $table): void {
            $table->id();
            $table->string('capital_number')->unique();
            $table->foreignId('account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->decimal('amount', 18, 2);
            $table->date('injection_date');
            $table->string('reference')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['branch_id', 'injection_date']);
        });

        Schema::create('financial_transfers', function (Blueprint $table): void {
            $table->id();
            $table->string('transfer_number')->unique();
            $table->foreignId('source_account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->foreignId('destination_account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->decimal('amount', 18, 2);
            $table->date('transfer_date');
            $table->string('reference')->nullable();
            $table->text('notes')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->index(['transfer_date', 'source_account_id']);
            $table->index(['transfer_date', 'destination_account_id']);
        });

        Schema::create('financial_account_transactions', function (Blueprint $table): void {
            $table->id();
            $table->string('transaction_number')->unique();
            $table->foreignId('account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->dateTime('transaction_date');
            $table->string('reference')->nullable();
            $table->string('type');
            $table->text('description');
            $table->decimal('money_in', 18, 2)->default(0);
            $table->decimal('money_out', 18, 2)->default(0);
            $table->decimal('running_balance', 18, 2);
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('transfer_id')->nullable()->constrained('financial_transfers')->restrictOnDelete();
            $table->nullableMorphs('related');
            $table->timestamps();
            $table->index(['account_id', 'transaction_date']);
            $table->index(['branch_id', 'transaction_date']);
        });

        Schema::create('accounting_entries', function (Blueprint $table): void {
            $table->id();
            $table->string('journal_number');
            $table->foreignId('branch_id')->constrained()->restrictOnDelete();
            $table->foreignId('financial_account_id')->nullable()->constrained('financial_accounts')->restrictOnDelete();
            $table->dateTime('entry_date');
            $table->string('reference')->nullable();
            $table->string('category');
            $table->string('event_type');
            $table->text('description');
            $table->decimal('debit', 18, 2)->default(0);
            $table->decimal('credit', 18, 2)->default(0);
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->nullableMorphs('related');
            $table->timestamps();
            $table->index(['journal_number', 'category']);
            $table->index(['branch_id', 'entry_date']);
            $table->index(['category', 'entry_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('accounting_entries');
        Schema::dropIfExists('financial_account_transactions');
        Schema::dropIfExists('financial_transfers');
        Schema::dropIfExists('capital_injections');
        Schema::dropIfExists('financial_accounts');
    }
};
