<?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('cash_reconciliations', function (Blueprint $table): void {
            $table->id();
            $table->string('reconciliation_number')->unique();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('financial_account_id')->constrained('financial_accounts')->restrictOnDelete();
            $table->date('reconciliation_date');
            $table->unsignedInteger('version')->default(1);
            $table->string('status', 20)->default('active');
            $table->decimal('opening_cash', 18, 2)->default(0);
            $table->decimal('cash_in', 18, 2)->default(0);
            $table->decimal('cash_out', 18, 2)->default(0);
            $table->decimal('expected_cash', 18, 2)->default(0);
            $table->decimal('physical_cash', 18, 2)->default(0);
            $table->decimal('variance', 18, 2)->default(0);
            $table->text('variance_explanation')->nullable();
            $table->foreignId('reconciled_by')->constrained('users')->restrictOnDelete();
            $table->timestamp('reconciled_at');
            $table->foreignId('superseded_by_id')->nullable()->constrained('cash_reconciliations')->nullOnDelete();
            $table->timestamp('superseded_at')->nullable();
            $table->timestamps();
            $table->unique(['financial_account_id', 'reconciliation_date', 'version'], 'cash_rec_account_date_version_unique');
            $table->index(['branch_id', 'reconciliation_date', 'status'], 'cash_rec_branch_date_status_index');
        });

        Schema::create('daily_closings', function (Blueprint $table): void {
            $table->id();
            $table->string('closing_number')->unique();
            $table->foreignId('business_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->date('closing_date');
            $table->string('status', 20)->default('prepared');
            $table->decimal('opening_funds', 18, 2)->default(0);
            $table->decimal('collections', 18, 2)->default(0);
            $table->decimal('cash_collections', 18, 2)->default(0);
            $table->decimal('mobile_money_collections', 18, 2)->default(0);
            $table->decimal('bank_collections', 18, 2)->default(0);
            $table->decimal('other_collections', 18, 2)->default(0);
            $table->decimal('disbursements', 18, 2)->default(0);
            $table->decimal('expenses', 18, 2)->default(0);
            $table->decimal('transfers', 18, 2)->default(0);
            $table->decimal('capital_inflows', 18, 2)->default(0);
            $table->decimal('other_inflows', 18, 2)->default(0);
            $table->decimal('other_outflows', 18, 2)->default(0);
            $table->decimal('closing_funds', 18, 2)->default(0);
            $table->decimal('expected_cash', 18, 2)->default(0);
            $table->decimal('actual_cash', 18, 2)->default(0);
            $table->decimal('variance', 18, 2)->default(0);
            $table->string('snapshot_fingerprint', 64);
            $table->json('snapshot')->nullable();
            $table->foreignId('prepared_by')->constrained('users')->restrictOnDelete();
            $table->timestamp('prepared_at');
            $table->foreignId('manager_confirmed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('manager_confirmed_at')->nullable();
            $table->text('manager_comments')->nullable();
            $table->timestamps();
            $table->unique(['branch_id', 'closing_date']);
            $table->index(['branch_id', 'status', 'closing_date']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('daily_closings');
        Schema::dropIfExists('cash_reconciliations');
    }
};
