<?php

namespace App\Services\ClassSections;

use App\Models\ClassSection;
use App\Models\Configuration;
use App\Models\Student;
use App\Models\User;

class ClassAttendanceService
{
    public function getAttendancePayload(int $classSectionId, int $teacherId, ?string $schoolYear = null, ?string $semester = null): array
    {
        $schoolYear = $schoolYear ?: (string) (Configuration::getConfig('school_year') ?? '');
        $semester = $semester ?: (string) (Configuration::getConfig('semester') ?? '');

        $teacher = User::query()->find($teacherId);
        $teacherName = trim((string) ($teacher?->firstname ?? '') . ' ' . (string) ($teacher?->lastname ?? ''));

        $className = ClassSection::getClassSectionNameBySectionId($classSectionId) ?? (string) $classSectionId;
        $students = Student::getStudentInfoByClassSectionId($classSectionId, $semester, $schoolYear, $teacherId);

        return [
            'teacher_name' => $teacherName,
            'class_name' => $className,
            'class_section_id' => $classSectionId,
            'semester' => $semester,
            'school_year' => $schoolYear,
            'students' => $students,
        ];
    }
}
