<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Phase 1: Create company financial settings table.
 *
 * Stores financial configuration and opening balances for each company.
 * This enables proper cash box reconciliation and capital tracking.
 *
 * KEY CONCEPTS:
 * - Opening Balance: Initial cash when the company starts using the system
 * - Initial Capital: Working capital available for lending
 * - Auto-lock: Automatically lock old financial records after X days
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('company_financial_settings', function (Blueprint $table) {
            $table->id();

            // One settings record per company
            $table->foreignId('company_id')
                ->unique()
                ->constrained()
                ->cascadeOnDelete();

            // Opening balances (foundational for all calculations)
            $table->decimal('opening_balance', 15, 2)
                ->default(0.00)
                ->comment('Initial cash balance when starting to use the system');

            $table->date('opening_balance_date')
                ->nullable()
                ->comment('Date of the opening balance record');

            $table->decimal('initial_capital', 15, 2)
                ->default(0.00)
                ->comment('Initial working capital for lending operations');

            $table->boolean('opening_balance_locked')
                ->default(false)
                ->comment('Once set and locked, opening balance cannot be changed');

            // Approval settings
            $table->boolean('profit_withdrawal_requires_approval')
                ->default(true)
                ->comment('Require approval for profit withdrawals');

            $table->boolean('capital_transaction_requires_approval')
                ->default(true)
                ->comment('Require approval for capital injections/returns');

            $table->decimal('approval_threshold_amount', 15, 2)
                ->default(5000000.00)
                ->comment('Amount above which expenses require approval');

            // Auto-lock settings
            $table->integer('capital_transaction_lock_hours')
                ->default(24)
                ->comment('Hours after which capital transactions are auto-locked');

            $table->integer('auto_lock_after_days')
                ->default(30)
                ->comment('Days after which all financial records are auto-locked');

            // Fiscal settings
            $table->tinyInteger('fiscal_year_start_month')
                ->default(1)
                ->comment('Month when fiscal year starts (1=January)');

            $table->tinyInteger('fiscal_year_start_day')
                ->default(1)
                ->comment('Day when fiscal year starts');

            // Accounting preferences
            $table->string('currency_code', 3)
                ->default('COP')
                ->comment('ISO 4217 currency code');

            $table->tinyInteger('decimal_places')
                ->default(2)
                ->comment('Decimal places for monetary amounts');

            $table->boolean('track_principal_interest_separately')
                ->default(true)
                ->comment('Split loan payments into principal and interest incomes');

            // Audit
            $table->foreignId('created_by_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->foreignId('updated_by_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->timestamps();
        });
    }

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