<?php

namespace Tests\Unit\Services\Finance;

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

class FinancialUnpaidParentsServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_unpaid_parents_returns_installment_data(): void
    {
        DB::table('configuration')->insert([
            ['id' => 1, 'config_key' => 'installment_date', 'config_value' => date('Y-m-t')],
        ]);

        DB::table('users')->insert([
            'id' => 2,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'parent@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

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

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 2,
            '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('discount_usages')->insert([
            'id' => 1,
            'voucher_id' => 1,
            'invoice_id' => 1,
            'parent_id' => 2,
            'discount_amount' => 10,
            'school_year' => '2025-2026',
        ]);

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

        DB::table('additional_charges')->insert([
            'id' => 1,
            'parent_id' => 2,
            'invoice_id' => null,
            'semester' => 'Fall',
            'title' => 'Extra Charge',
            'amount' => 20,
            'school_year' => '2025-2026',
            'status' => 'pending',
        ]);

        $service = new FinancialUnpaidParentsService(new FinancialSchoolYearService());
        $result = $service->getUnpaidParents('2025-2026');

        $this->assertSame('2025-2026', $result['schoolYear']);
        $this->assertCount(1, $result['results']);

        $row = $result['results'][0];
        $this->assertSame(45.0, $row['total_balance']);
        $this->assertSame('installment', $row['type']);
        $this->assertSame(1, $row['has_installment']);
    }
}
