<?php

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

/**
 * #128 — Alinea las columnas monetarias de `credits` e `installments` al
 * estándar DECIMAL(12,2) del proyecto (CLAUDE.md). Estaban en (10,2) (tope
 * 99.999.999,99) mientras que payments/payment_installment/expenses/incomes y
 * el resto del ledger ya usan (12,2). `PaymentMaterializer` suma
 * `payment_installment.applied_amount` (12,2) en `installments.amount_paid`
 * (10,2), por lo que un valor ≥100M truncaría (MySQL no estricto) o lanzaría.
 *
 * Ampliar la precisión no pierde datos. `interest_rate` decimal(5,2) NO se toca
 * (es un porcentaje, no dinero). Se replican todos los atributos por regla de
 * Laravel 13: un `change()` debe redeclarar comment/default o se pierden.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('credits', function (Blueprint $table) {
            $table->decimal('amount', 12, 2)->comment('Monto principal del préstamo.')->change();
        });

        Schema::table('installments', function (Blueprint $table) {
            $table->decimal('principal_amount', 12, 2)->comment('Parte del capital de la cuota.')->change();
            $table->decimal('interest_amount', 12, 2)->comment('Parte del interés de la cuota.')->change();
            $table->decimal('total_amount', 12, 2)->comment('Suma de principal + interés.')->change();
            $table->decimal('balance_due', 12, 2)->comment('Saldo restante del principal después de esta cuota.')->change();
            $table->decimal('amount_paid', 12, 2)->default(0.00)->comment('Monto total abonado a esta cuota.')->change();
        });
    }

    public function down(): void
    {
        Schema::table('credits', function (Blueprint $table) {
            $table->decimal('amount', 10, 2)->comment('Monto principal del préstamo.')->change();
        });

        Schema::table('installments', function (Blueprint $table) {
            $table->decimal('principal_amount', 10, 2)->comment('Parte del capital de la cuota.')->change();
            $table->decimal('interest_amount', 10, 2)->comment('Parte del interés de la cuota.')->change();
            $table->decimal('total_amount', 10, 2)->comment('Suma de principal + interés.')->change();
            $table->decimal('balance_due', 10, 2)->comment('Saldo restante del principal después de esta cuota.')->change();
            $table->decimal('amount_paid', 10, 2)->default(0.00)->comment('Monto total abonado a esta cuota.')->change();
        });
    }
};
