<?php

namespace Tests\Unit\Services\Parents;

use App\Services\Parents\ParentInvoiceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ParentInvoiceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_invoices_filters_by_parent(): void
    {
        $parentId = $this->seedParent();
        $otherParentId = $this->seedParent('other@example.com');

        DB::table('invoices')->insert([
            'parent_id' => $parentId,
            'invoice_number' => 'INV-1',
            'total_amount' => 100,
            'paid_amount' => 0,
            'balance' => 100,
            'issue_date' => '2025-09-01',
        ]);

        DB::table('invoices')->insert([
            'parent_id' => $otherParentId,
            'invoice_number' => 'INV-2',
            'total_amount' => 50,
            'paid_amount' => 0,
            'balance' => 50,
            'issue_date' => '2025-09-02',
        ]);

        $service = new ParentInvoiceService();
        $rows = $service->listInvoices($parentId);

        $this->assertCount(1, $rows);
        $this->assertSame('INV-1', $rows[0]['invoice_number']);
    }

    private function seedParent(string $email = 'parent@example.com'): int
    {
        return DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => $email,
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);
    }
}
