<?php

namespace Tests\App\Database\Migrations;

require_once APPPATH . 'Database/Migrations/2026-05-30-000001_FinancialSystemLedgerCleanup.php';

use App\Database\Migrations\FinancialSystemLedgerCleanup;
use CodeIgniter\Test\CIUnitTestCase;

class TestableFinancialSystemLedgerCleanup extends FinancialSystemLedgerCleanup
{
    public array $calls = [];

    public function __construct()
    {
    }

    protected function addInstallmentSequenceColumn(): void
    {
        $this->calls[] = 'addInstallmentSequenceColumn';
    }

    protected function ensureIndexes(): void
    {
        $this->calls[] = 'ensureIndexes';
    }

    protected function backfillInstallmentSequence(): void
    {
        $this->calls[] = 'backfillInstallmentSequence';
    }

    protected function ensurePaymentDateHasTime(): void
    {
        $this->calls[] = 'ensurePaymentDateHasTime';
    }

    protected function repairPaymentInvoiceTerms(): void
    {
        $this->calls[] = 'repairPaymentInvoiceTerms';
    }

    protected function normalizePaymentStatuses(): void
    {
        $this->calls[] = 'normalizePaymentStatuses';
    }

    protected function ensureConfigurationDefaults(): void
    {
        $this->calls[] = 'ensureConfigurationDefaults';
    }

    protected function archivePaypalTables(): void
    {
        $this->calls[] = 'archivePaypalTables';
    }

    protected function refreshFinancialNavItems(): void
    {
        $this->calls[] = 'refreshFinancialNavItems';
    }

    protected function restorePaypalTables(): void
    {
        $this->calls[] = 'restorePaypalTables';
    }

    protected function dropIndexIfExists(string $table, string $indexName): void
    {
        $this->calls[] = "dropIndexIfExists:$table:$indexName";
    }
}

class FinancialSystemLedgerCleanupTest extends CIUnitTestCase
{
    public function testUpRunsCleanupStepsInExpectedOrder(): void
    {
        $migration = new TestableFinancialSystemLedgerCleanup();

        $migration->up();

        $this->assertSame([
            'addInstallmentSequenceColumn',
            'backfillInstallmentSequence',
            'ensurePaymentDateHasTime',
            'ensureIndexes',
            'repairPaymentInvoiceTerms',
            'normalizePaymentStatuses',
            'ensureConfigurationDefaults',
            'archivePaypalTables',
            'refreshFinancialNavItems',
        ], $migration->calls);
    }

    public function testDownRestoresPaypalAndDropsFinancialIndexes(): void
    {
        $migration = new TestableFinancialSystemLedgerCleanup();
        $fakeDb = new class () {
            public function tableExists(string $name): bool
            {
                return false;
            }

            public function fieldExists(string $field, string $table): bool
            {
                return false;
            }
        };
        $fakeForge = new class () {
            public array $dropped = [];

            public function dropColumn(string $table, string $column): void
            {
                $this->dropped[] = [$table, $column];
            }
        };

        self::setPrivateProperty($migration, 'db', $fakeDb);
        self::setPrivateProperty($migration, 'forge', $fakeForge);

        $migration->down();

        $this->assertSame('restorePaypalTables', $migration->calls[0]);
        $this->assertContains('dropIndexIfExists:payments:uniq_payments_transaction_id', $migration->calls);
        $this->assertContains('dropIndexIfExists:invoice_event:idx_invoice_event_invoice_id', $migration->calls);
    }
}
