<?php

namespace App\Http\Controllers\Admin\AcademicClasses;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\AcademicClasses\ScheduleRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\Schedule;
use App\Services\Admin\AcademicClasses\ScheduleService;
use App\Services\Admin\Master\AcademicTermService;
use App\Services\Admin\Master\CourseService;
use App\Services\Admin\Master\DepartmentService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class ScheduleController extends Controller
{
    public function __construct(
        private readonly ScheduleService $service,
        private readonly AcademicTermService $academicTermService,
        private readonly DepartmentService $departmentService,
        private readonly CourseService $courseService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        $user = $request->user();
        $isPersonalView = $user->hasRole('mahasiswa') || $user->hasRole('dosen');

        $academicTermId = $request->has('academic_term_id')
            ? $request->validated('academic_term_id')
            : $this->academicTermService->getActive()?->id;

        return Inertia::render('admin/academic-classes/schedules/index', [
            'schedules' => $this->service->all(
                $user,
                academicTermId: $academicTermId,
                departmentId: $request->validated('department_id'),
                semesterNumber: $request->validated('semester_number'),
            ),
            'courseClasses' => $this->service->courseClassOptions(),
            'academicTerms' => $this->academicTermService->getAllForSelect(),
            'departments' => $this->departmentService->getAllForSelect(),
            'semesterNumbers' => $this->courseService->getSemesterNumbers(),
            'isPersonalView' => $isPersonalView,
            'filters' => [
                'academic_term_id' => $academicTermId ? (string) $academicTermId : null,
                'department_id' => $request->validated('department_id'),
                'semester_number' => $request->validated('semester_number'),
            ],
        ]);
    }

    public function store(ScheduleRequest $request): RedirectResponse
    {
        $this->service->create($request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Jadwal berhasil ditambahkan.']);

        return to_route('admin.academic-classes.schedules.index');
    }

    public function update(ScheduleRequest $request, Schedule $schedule): RedirectResponse
    {
        $this->service->update($schedule, $request->validated());

        Inertia::flash('toast', ['type' => 'success', 'message' => 'Jadwal berhasil diperbarui.']);

        return to_route('admin.academic-classes.schedules.index');
    }

    public function destroy(Schedule $schedule): RedirectResponse
    {
        $this->service->delete($schedule);

        return Inertia::flash('toast', ['type' => 'success', 'message' => 'Jadwal berhasil dihapus.'])->back();
    }
}
