<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;

class ScheduleController extends Controller
{
    private const DAYS = [
        0 => 'شنبه',
        1 => 'یکشنبه',
        2 => 'دوشنبه',
        3 => 'سه‌شنبه',
        4 => 'چهارشنبه',
        5 => 'پنجشنبه',
        6 => 'جمعه',
    ];

    public function index(Request $request): Response
    {
        $schoolId = $this->currentSchoolId($request);
        $userId = $request->user()?->id;
        $studentId = $userId && $schoolId
            ? DB::table('students')->where('school_id', $schoolId)->where('user_id', $userId)->value('id')
            : null;

        $classroomIds = $studentId
            ? DB::table('class_students')
                ->where('school_id', $schoolId)
                ->where('student_id', $studentId)
                ->where('status', 'active')
                ->pluck('classroom_id')
            : collect();

        $schedules = ($schoolId && $classroomIds->isNotEmpty())
            ? DB::table('schedules')
                ->leftJoin('classrooms', 'classrooms.id', '=', 'schedules.classroom_id')
                ->leftJoin('subjects', 'subjects.id', '=', 'schedules.subject_id')
                ->leftJoin('teachers', 'teachers.id', '=', 'schedules.teacher_id')
                ->leftJoin('users as teacher_users', 'teacher_users.id', '=', 'teachers.user_id')
                ->whereIn('schedules.classroom_id', $classroomIds)
                ->where('schedules.school_id', $schoolId)
                ->select([
                    'schedules.day_of_week',
                    'schedules.starts_at',
                    'schedules.ends_at',
                    'classrooms.name as classroom_name',
                    'subjects.name as subject_name',
                    'teacher_users.first_name as teacher_first',
                    'teacher_users.last_name as teacher_last',
                ])
                ->orderBy('schedules.day_of_week')
                ->orderBy('schedules.starts_at')
                ->get()
                ->map(fn ($s): array => [
                    'day' => (int) $s->day_of_week,
                    'dayLabel' => self::DAYS[(int) $s->day_of_week] ?? '—',
                    'startsAt' => substr((string) $s->starts_at, 0, 5),
                    'endsAt' => substr((string) $s->ends_at, 0, 5),
                    'classroom' => $s->classroom_name ?: '—',
                    'subject' => $s->subject_name ?: '—',
                    'teacher' => trim(($s->teacher_first ?? '').' '.($s->teacher_last ?? '')) ?: '—',
                ])
                ->values()
            : collect()->values();

        $todayDayOfWeek = ((int) now()->dayOfWeek + 1) % 7;

        return Inertia::render('Student/Schedule', [
            'schedules' => $schedules,
            'todayDayOfWeek' => $todayDayOfWeek,
            'days' => self::DAYS,
        ]);
    }
}
