<?php

namespace Tests\Unit\Services\ExtraCharges;

use App\Services\ExtraCharges\ExtraChargesMetaService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ExtraChargesMetaServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_school_years_uses_fallback(): void
    {
        $service = new ExtraChargesMetaService();
        $years = $service->getSchoolYears('2025-2026');

        $this->assertSame(['2025-2026', '2024-2025', '2023-2024', '2022-2023'], $years);
    }

    public function test_get_school_years_merges_sources(): void
    {
        DB::table('additional_charges')->insert([
            [
                'parent_id' => 1,
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'charge_type' => 'add',
                'title' => 'Test',
                'amount' => 1,
            ],
            [
                'parent_id' => 1,
                'school_year' => '2024-2025',
                'semester' => 'Fall',
                'charge_type' => 'add',
                'title' => 'Test',
                'amount' => 1,
            ],
        ]);

        DB::table('invoices')->insert([
            [
                'parent_id' => 1,
                'invoice_number' => 'INV-1',
                'total_amount' => 10,
                'balance' => 10,
                'paid_amount' => 0,
                'issue_date' => '2025-09-01',
                'status' => 'Unpaid',
                'school_year' => '2023-2024',
            ],
        ]);

        $service = new ExtraChargesMetaService();
        $years = $service->getSchoolYears();

        $this->assertSame(['2025-2026', '2024-2025', '2023-2024'], $years);
    }
}
