<?php

namespace App\Services\Assignment;

use Illuminate\Support\Facades\DB;

class AssignmentMetaService
{
    public function getSchoolYears(string $fallbackYear = ''): array
    {
        $years = DB::table('teacher_class')
            ->selectRaw('DISTINCT school_year')
            ->whereNotNull('school_year')
            ->orderByDesc('school_year')
            ->pluck('school_year')
            ->map(fn ($year) => (string) $year)
            ->filter()
            ->values()
            ->all();

        if (empty($years) && $fallbackYear !== '') {
            $years[] = $fallbackYear;
        }

        return $years;
    }

    public function firstNonEmpty(array $values): mixed
    {
        foreach ($values as $value) {
            if (filled($value)) {
                return $value;
            }
        }

        return null;
    }
}
