<?php

use App\Http\Controllers\Admin\AcademicClasses\AssignmentController;
use App\Http\Controllers\Admin\AcademicClasses\AttendanceController;
use App\Http\Controllers\Admin\AcademicClasses\MaterialController;
use App\Http\Controllers\Admin\AcademicClasses\ScheduleController;
use App\Http\Controllers\Admin\AcademicClasses\SubmissionController;
use App\Http\Controllers\Admin\Announcements\AnnouncementController;
use App\Http\Controllers\Admin\FeedbackController;
use App\Http\Controllers\Admin\Finances\TuitionInvoiceController;
use App\Http\Controllers\Admin\Finances\TuitionPaymentController;
use App\Http\Controllers\Admin\Manage\ClassEnrollmentController;
use App\Http\Controllers\Admin\Manage\CourseClassController;
use App\Http\Controllers\Admin\Manage\CourseRegistrationController;
use App\Http\Controllers\Admin\Master\AcademicTermController;
use App\Http\Controllers\Admin\Master\CourseController;
use App\Http\Controllers\Admin\Master\DepartmentController;
use App\Http\Controllers\Admin\Services\AcademicAdvisingLogController;
use App\Http\Controllers\Admin\Services\LetterRequestController;
use App\Http\Controllers\Admin\Settings\ProfileController;
use App\Http\Controllers\Admin\Settings\SecurityController;
use App\Http\Controllers\Admin\Users\AdministratorController;
use App\Http\Controllers\Admin\Users\LecturerController;
use App\Http\Controllers\Admin\Users\StudentController;
use Illuminate\Support\Facades\Route;

Route::middleware(['auth', 'verified'])->group(function () {
    Route::prefix('admin/master')->name('admin.master.')->group(function () {
        Route::resource('academic-terms', AcademicTermController::class)->except(['create', 'edit', 'show']);
        Route::patch('academic-terms/{academic_term}/status', [AcademicTermController::class, 'updateStatus'])->name('academic-terms.update_status');
        Route::resource('departments', DepartmentController::class)->except(['create', 'edit', 'show']);
        Route::resource('courses', CourseController::class)->except(['create', 'edit', 'show']);
    });

    Route::prefix('admin/academic-classes')->name('admin.academic-classes.')->group(function () {
        Route::resource('materials', MaterialController::class)->except(['create', 'edit', 'show']);

        Route::resource('assignments', AssignmentController::class)->except(['create', 'edit', 'show']);

        Route::prefix('assignments/{assignment}/submissions')->name('assignments.submissions.')->group(function () {
            Route::get('/', [SubmissionController::class, 'index'])->name('index');
            Route::post('/', [SubmissionController::class, 'store'])->name('store');
            Route::put('{submission}', [SubmissionController::class, 'update'])->name('update');
            Route::delete('{submission}', [SubmissionController::class, 'destroy'])->name('destroy');
        });

        Route::resource('schedules', ScheduleController::class)->except(['create', 'edit', 'show']);

        Route::prefix('attendances')->name('attendances.')->group(function () {
            Route::get('/', [AttendanceController::class, 'index'])->name('index');
            Route::get('{course_class}/{meeting_number}', [AttendanceController::class, 'session'])
                ->whereNumber('meeting_number')
                ->name('session');
            Route::post('{course_class}/{meeting_number}', [AttendanceController::class, 'store'])
                ->whereNumber('meeting_number')
                ->name('store');
            Route::delete('{course_class}/{meeting_number}', [AttendanceController::class, 'destroy'])
                ->whereNumber('meeting_number')
                ->name('destroy');
        });
    });

    Route::prefix('admin/manage')->name('admin.manage.')->group(function () {
        Route::resource('course-registrations', CourseRegistrationController::class)->only(['index', 'store']);

        Route::prefix('course-registrations/{submission}')->name('course-registrations.')->group(function () {
            Route::get('/', [CourseRegistrationController::class, 'show'])->name('show');
            Route::patch('approve', [CourseRegistrationController::class, 'approve'])->name('approve');
            Route::patch('reject', [CourseRegistrationController::class, 'reject'])->name('reject');
        });

        Route::resource('course-classes', CourseClassController::class)->except(['create', 'edit', 'show']);

        Route::prefix('course-classes/{course_class}/enrollments')->name('course-classes.enrollments.')->group(function () {
            Route::get('/', [ClassEnrollmentController::class, 'index'])->name('index');
            Route::post('/', [ClassEnrollmentController::class, 'store'])->name('store');
            Route::delete('{enrollment}', [ClassEnrollmentController::class, 'destroy'])->name('destroy');
        });
    });

    Route::middleware('role:mahasiswa')->prefix('course-registrations')->name('student.course-registrations.')->group(function () {
        Route::get('/', [CourseRegistrationController::class, 'mine'])->name('index');
        Route::get('create', [CourseRegistrationController::class, 'create'])->name('create');
        Route::post('/', [CourseRegistrationController::class, 'store'])->name('store');
        Route::get('{submission}', [CourseRegistrationController::class, 'show'])->name('show');
    });

    Route::prefix('admin/announcements')->name('admin.announcements.')->group(function () {
        Route::get('/', [AnnouncementController::class, 'index'])->name('index');
        Route::post('/', [AnnouncementController::class, 'store'])->name('store');
        Route::put('{announcement}', [AnnouncementController::class, 'update'])->name('update');
        Route::delete('{announcement}', [AnnouncementController::class, 'destroy'])->name('destroy');
    });

    Route::prefix('admin/finances')->name('admin.finances.')->group(function () {
        Route::resource('tuition-invoices', TuitionInvoiceController::class)->except(['create', 'edit', 'show']);

        Route::prefix('tuition-invoices/{tuition_invoice}/payments')->name('tuition-invoices.payments.')->group(function () {
            Route::get('/', [TuitionPaymentController::class, 'index'])->name('index');
            Route::post('/', [TuitionPaymentController::class, 'store'])->name('store');
            Route::put('{payment}', [TuitionPaymentController::class, 'update'])->name('update');
            Route::delete('{payment}', [TuitionPaymentController::class, 'destroy'])->name('destroy');
        });
    });

    Route::prefix('admin/services')->name('admin.services.')->group(function () {
        Route::resource('letter-requests', LetterRequestController::class)->except(['create', 'edit', 'show']);

        Route::resource('academic-advising-logs', AcademicAdvisingLogController::class)->except(['create', 'edit', 'show']);
    });

    Route::prefix('admin/settings')->name('admin.settings.')->group(function () {
        Route::redirect('/', '/admin/settings/profile');

        Route::get('profile', [ProfileController::class, 'edit'])->name('profile.edit');
        Route::patch('profile', [ProfileController::class, 'update'])->name('profile.update');
        Route::delete('profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

        Route::get('security', [SecurityController::class, 'edit'])
            ->name('security.edit');

        Route::put('password', [SecurityController::class, 'update'])
            ->middleware('throttle:6,1')
            ->name('password.update');

        Route::inertia('appearance', 'admin/settings/appearance')->name('appearance.edit');
    });

    Route::resource('admin/feedback', FeedbackController::class)
        ->except(['create', 'edit', 'show'])
        ->names('admin.feedback');

    Route::prefix('admin/users')->name('admin.users.')->group(function () {
        Route::resource('lecturers', LecturerController::class)->except(['show'])->parameters(['lecturers' => 'user']);
        Route::patch('lecturers/{user}/reset-password', [LecturerController::class, 'resetPassword'])->name('lecturers.reset_password');
        Route::patch('lecturers/{user}/user-status', [LecturerController::class, 'updateUserStatus'])->name('lecturers.update_user_status');
        Route::get('lecturers/export', [LecturerController::class, 'export'])->name('lecturers.export');

        Route::resource('students', StudentController::class)->except(['show'])->parameters(['students' => 'user']);
        Route::patch('students/{user}/reset-password', [StudentController::class, 'resetPassword'])->name('students.reset_password');
        Route::patch('students/{user}/status', [StudentController::class, 'updateStatus'])->name('students.update_status');
        Route::patch('students/{user}/user-status', [StudentController::class, 'updateUserStatus'])->name('students.update_user_status');
        Route::get('students/export', [StudentController::class, 'export'])->name('students.export');

        Route::resource('administrators', AdministratorController::class)->except(['show'])->parameters(['administrators' => 'user']);
        Route::patch('administrators/{user}/reset-password', [AdministratorController::class, 'resetPassword'])->name('administrators.reset_password');
        Route::patch('administrators/{user}/user-status', [AdministratorController::class, 'updateUserStatus'])->name('administrators.update_user_status');
    });
});
