<?php

namespace App\Http\Controllers\Admin\AcademicClasses;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\AcademicClasses\ScheduleRequest;
use App\Models\Schedule;
use App\Services\Admin\AcademicClasses\ScheduleService;
use App\Services\Admin\Manage\CourseClassService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class ScheduleController extends Controller
{
    public function __construct(
        private readonly ScheduleService $service,
        private readonly CourseClassService $courseClassService,
    ) {}

    public function index(): Response
    {
        return Inertia::render('admin/academic-classes/schedules/index', [
            'schedules' => $this->service->all(),
            'courseClasses' => $this->courseClassService->getAllForSelect(),
        ]);
    }

    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();
    }
}
