<?php

namespace Tests\Unit\Services\Invoices;

use App\Services\Invoices\InvoiceConfigService;
use App\Services\Invoices\InvoicePaymentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class InvoicePaymentServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_parent_invoice_summary_includes_refunds_and_last_payment(): void
    {
        DB::table('invoices')->insert([
            [
                'id' => 1,
                'parent_id' => 10,
                'invoice_number' => 'INV-001',
                'total_amount' => 100,
                'balance' => 40,
                'paid_amount' => 60,
                'has_discount' => 0,
                'issue_date' => '2025-01-10',
                'status' => 'unpaid',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'total_amount' => 100,
            'paid_amount' => 60,
            'balance' => 40,
            'number_of_installments' => 1,
            'payment_method' => 'Cash',
            'payment_date' => '2025-01-15',
            'school_year' => '2025-2026',
            'status' => 'Paid',
        ]);

        DB::table('refunds')->insert([
            'id' => 1,
            'parent_id' => 10,
            'school_year' => '2025-2026',
            'invoice_id' => 1,
            'refund_amount' => 5,
            'status' => 'Paid',
            'refund_paid_amount' => 5,
        ]);

        $service = new InvoicePaymentService(new InvoiceConfigService());
        $data = $service->getParentInvoiceSummary(10, '2025-2026');

        $this->assertSame('2025-2026', $data['selectedYear']);
        $this->assertSame(5.0, $data['invoices'][0]['refund_amount']);
        $this->assertSame(60.0, $data['invoices'][0]['last_paid_amount']);
    }
}
