<?php

namespace Tests\Unit\Services\Assignment;

use App\Services\Assignment\AssignmentMetaService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AssignmentMetaServiceTest extends TestCase
{
    use RefreshDatabase;

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

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

    public function test_get_school_years_returns_distinct_sorted(): void
    {
        DB::table('teacher_class')->insert([
            ['class_section_id' => 101, 'teacher_id' => 1, 'position' => 'main', 'school_year' => '2024-2025', 'semester' => 'Fall'],
            ['class_section_id' => 102, 'teacher_id' => 2, 'position' => 'main', 'school_year' => '2025-2026', 'semester' => 'Fall'],
            ['class_section_id' => 103, 'teacher_id' => 3, 'position' => 'main', 'school_year' => '2025-2026', 'semester' => 'Fall'],
        ]);

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

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

    public function test_first_non_empty_returns_first_value(): void
    {
        $service = new AssignmentMetaService();
        $value = $service->firstNonEmpty([null, '', 'Fall', 'Spring']);

        $this->assertSame('Fall', $value);
    }
}
