<?php

namespace Tests\Unit\Services\Refunds;

use App\Services\Refunds\RefundSummaryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class RefundSummaryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_summary_calculates_unapplied_balance(): void
    {
        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 100.00,
            'balance' => 0.00,
            'paid_amount' => 100.00,
            'status' => 'Paid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'issue_date' => '2025-01-05',
        ]);

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'paid_amount' => 150.00,
            'total_amount' => 150.00,
            'balance' => 0.00,
            'payment_date' => '2025-01-10',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'status' => 'Paid',
        ]);

        DB::table('refunds')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'refund_amount' => 20,
            'refund_paid_amount' => 20,
            'status' => 'Paid',
            'request' => 'overpayment',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new RefundSummaryService();
        $summary = $service->getParentFinancialSummary(10, '2025-2026', 'Fall');

        $this->assertSame(100.0, $summary['total_invoiced']);
        $this->assertSame(150.0, $summary['total_paid_in']);
        $this->assertSame(20.0, $summary['total_refunded']);
        $this->assertSame(30.0, $summary['unapplied_balance']);
    }
}
