<?php

namespace Tests\App\Controllers\View;

use App\Controllers\View\ParentController;
use CodeIgniter\Test\CIUnitTestCase;
use ReflectionMethod;

final class ParentControllerAgeTest extends CIUnitTestCase
{
    public function testEnrollmentAgeUsesSchoolYearStartYearCutoff(): void
    {
        $controller = new class extends ParentController {
            public function __construct()
            {
            }
        };

        $method = new ReflectionMethod(ParentController::class, 'calculateAgeAsOfSchoolYearStartYear');
        $method->setAccessible(true);

        $this->assertSame(6, $method->invoke($controller, '2019-09-15', '2025-2026'));
        $this->assertSame(5, $method->invoke($controller, '2020-01-01', '2025-2026'));
    }

    public function testEnrollmentAgeRejectsInvalidInputs(): void
    {
        $controller = new class extends ParentController {
            public function __construct()
            {
            }
        };

        $method = new ReflectionMethod(ParentController::class, 'calculateAgeAsOfSchoolYearStartYear');
        $method->setAccessible(true);

        $this->assertNull($method->invoke($controller, '', '2025-2026'));
        $this->assertNull($method->invoke($controller, 'not-a-date', '2025-2026'));
        $this->assertNull($method->invoke($controller, '2026-01-01', '2025-2026'));
        $this->assertNull($method->invoke($controller, '2019-09-15', ''));
    }
}
