<?php

namespace Tests\Unit\Services\Invoices;

use App\Services\Invoices\InvoiceGradeService;
use App\Services\Invoices\InvoiceTuitionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class InvoiceTuitionServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_calculate_tuition_fee_counts_regular_and_youth(): void
    {
        DB::table('classSection')->insert([
            ['class_section_id' => 1, 'class_section_name' => '1', 'class_id' => 1, 'semester' => 'Fall', 'school_year' => '2025-2026'],
            ['class_section_id' => 2, 'class_section_name' => '2', 'class_id' => 2, 'semester' => 'Fall', 'school_year' => '2025-2026'],
            ['class_section_id' => 3, 'class_section_name' => '10', 'class_id' => 10, 'semester' => 'Fall', 'school_year' => '2025-2026'],
        ]);

        $grades = new InvoiceGradeService(9);
        $service = new InvoiceTuitionService($grades, 9, 350.0, 200.0, 180.0, 'UTC');

        $registeredKids = [
            ['class_section_id' => 1],
            ['class_section_id' => 2],
            ['class_section_id' => 3],
        ];

        $total = $service->calculateTuitionFee($registeredKids, [], date('Y-m-d', strtotime('+10 days')));

        $this->assertSame(730.0, $total);
    }

    public function test_refund_deadline_includes_withdrawn_when_passed(): void
    {
        DB::table('classSection')->insert([
            ['class_section_id' => 1, 'class_section_name' => '1', 'class_id' => 1, 'semester' => 'Fall', 'school_year' => '2025-2026'],
            ['class_section_id' => 2, 'class_section_name' => '2', 'class_id' => 2, 'semester' => 'Fall', 'school_year' => '2025-2026'],
        ]);

        $grades = new InvoiceGradeService(9);
        $service = new InvoiceTuitionService($grades, 9, 350.0, 200.0, 180.0, 'UTC');

        $registeredKids = [
            ['class_section_id' => 1],
        ];
        $withdrawnKids = [
            ['class_section_id' => 2],
        ];

        $totalAllowed = $service->calculateTuitionFee($registeredKids, $withdrawnKids, date('Y-m-d', strtotime('+10 days')));
        $totalBlocked = $service->calculateTuitionFee($registeredKids, $withdrawnKids, date('Y-m-d', strtotime('-10 days')));

        $this->assertSame(350.0, $totalAllowed);
        $this->assertSame(550.0, $totalBlocked);
    }
}
