<?php

namespace Tests\Unit\Services\Finance;

use App\Services\Finance\FinancialPaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FinancialPaymentServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_payment_aggregates_exclude_void_statuses(): void
    {
        DB::table('payments')->insert([
            [
                'id' => 1,
                'parent_id' => 10,
                'invoice_id' => 1,
                'total_amount' => 100,
                'paid_amount' => 30,
                'balance' => 70,
                'number_of_installments' => 1,
                'payment_method' => 'Cash',
                'payment_date' => '2025-01-05',
                'school_year' => '2025-2026',
                'status' => 'Paid',
            ],
            [
                'id' => 2,
                'parent_id' => 10,
                'invoice_id' => 1,
                'total_amount' => 100,
                'paid_amount' => 20,
                'balance' => 50,
                'number_of_installments' => 1,
                'payment_method' => 'Credit Card',
                'payment_date' => '2025-01-06',
                'school_year' => '2025-2026',
                'status' => 'Paid',
            ],
            [
                'id' => 3,
                'parent_id' => 10,
                'invoice_id' => 1,
                'total_amount' => 100,
                'paid_amount' => 50,
                'balance' => 0,
                'number_of_installments' => 1,
                'payment_method' => 'Cash',
                'payment_date' => '2025-01-07',
                'school_year' => '2025-2026',
                'status' => 'void',
            ],
            [
                'id' => 4,
                'parent_id' => 11,
                'invoice_id' => 2,
                'total_amount' => 40,
                'paid_amount' => 10,
                'balance' => 30,
                'number_of_installments' => 1,
                'payment_method' => 'Check',
                'payment_date' => '2025-01-08',
                'school_year' => '2025-2026',
                'status' => 'Paid',
            ],
        ]);

        $service = new FinancialPaymentService();

        $payments = $service->paymentsByInvoice('2025-2026', '2025-01-01', '2025-01-31');
        $breakdown = $service->paymentBreakdown('2025-2026', '2025-01-01', '2025-01-31');
        $totals = $service->paymentTotals('2025-2026', '2025-01-01', '2025-01-31');

        $map = [];
        foreach ($payments as $row) {
            $map[(int) $row['invoice_id']] = (float) $row['paid_amount'];
        }

        $this->assertSame(50.0, $map[1]);
        $this->assertSame(10.0, $map[2]);

        $this->assertSame(30.0, (float) ($breakdown[1]['cash'] ?? 0));
        $this->assertSame(20.0, (float) ($breakdown[1]['credit'] ?? 0));
        $this->assertSame(10.0, (float) ($breakdown[2]['check'] ?? 0));

        $this->assertSame(60.0, $totals['total_all']);
        $this->assertSame(30.0, $totals['total_cash']);
        $this->assertSame(20.0, $totals['total_credit']);
        $this->assertSame(10.0, $totals['total_check']);
    }
}
