<?php

namespace App\Http\Controllers\Admin\Manage\CourseClass;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\CourseClass\CourseClassRequest;
use App\Http\Requests\Admin\Manage\CourseClass\DuplicateCourseClassesRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\CourseClass;
use App\Services\Admin\Manage\CourseClass\CourseClassService;
use App\Services\Admin\Master\AcademicTermService;
use App\Services\Admin\Master\CourseService;
use App\Services\Admin\Users\LecturerService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class CourseClassController extends Controller
{
    public function __construct(
        private readonly CourseClassService $service,
        private readonly CourseService $courseService,
        private readonly LecturerService $lecturerService,
        private readonly AcademicTermService $academicTermService,
    ) {}

    public function index(PaginatedRequest $request): Response
    {
        return Inertia::render('admin/manage/course-classes/index', [
            'courseClasses' => $this->service->paginated(
                ...$request->validatedWithDefaults(),
                academicTermId: $request->validated('academic_term_id'),
                method: $request->validated('method'),
            ),
            'courses' => $this->courseService->getAllForSelect(),
            'lecturers' => $this->lecturerService->getAllForSelect(),
            'academicTerms' => $this->academicTermService->getAllForSelect(),
            'courseClassAssignments' => $this->service->getAllForSelect(),
            'filters' => $request->only(['academic_term_id', 'method']),
        ]);
    }

    public function store(CourseClassRequest $request): RedirectResponse
    {
        $created = $this->service->createMany($request->validated());

        Inertia::flash('toast', [
            'type' => 'success',
            'message' => $created->count() > 1
                ? "{$created->count()} kelas mata kuliah berhasil ditambahkan."
                : 'Kelas mata kuliah berhasil ditambahkan.',
        ]);

        return to_route('admin.manage.course-classes.index');
    }

    public function update(CourseClassRequest $request, CourseClass $courseClass): RedirectResponse
    {
        $this->service->update($courseClass, $request->validated());

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

        return to_route('admin.manage.course-classes.index');
    }

    public function destroy(CourseClass $courseClass): RedirectResponse
    {
        $this->service->delete($courseClass);

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

    public function duplicate(DuplicateCourseClassesRequest $request): RedirectResponse
    {
        $result = $this->service->duplicateFromTerm(
            $request->validated('source_academic_term_id'),
            $request->validated('target_academic_term_id'),
        );

        $message = $result['created'] > 0
            ? "{$result['created']} kelas mata kuliah berhasil diduplikasi."
            : 'Tidak ada kelas mata kuliah baru yang diduplikasi.';

        if ($result['skipped'] > 0) {
            $message .= " {$result['skipped']} dilewati karena sudah ada di periode tujuan.";
        }

        Inertia::flash('toast', ['type' => 'success', 'message' => $message]);

        return to_route('admin.manage.course-classes.index');
    }
}
