<?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\Developer\LogController;
use App\Http\Controllers\Admin\Developer\RolePermissionController;
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\AnnouncementController;
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'])
            ->middlewareFor(['index'], 'permission:view-academic-terms')
            ->middlewareFor(['store'], 'permission:create-academic-terms')
            ->middlewareFor(['update'], 'permission:update-academic-terms')
            ->middlewareFor(['destroy'], 'permission:delete-academic-terms');
        Route::patch('academic-terms/{academic_term}/status', [AcademicTermController::class, 'updateStatus'])->name('academic-terms.update_status')->middleware('permission:update-academic-terms-status');

        Route::resource('departments', DepartmentController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-departments')
            ->middlewareFor(['store'], 'permission:create-departments')
            ->middlewareFor(['update'], 'permission:update-departments')
            ->middlewareFor(['destroy'], 'permission:delete-departments');

        Route::resource('courses', CourseController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-courses')
            ->middlewareFor(['store'], 'permission:create-courses')
            ->middlewareFor(['update'], 'permission:update-courses')
            ->middlewareFor(['destroy'], 'permission:delete-courses');
    });

    Route::prefix('admin/academic-classes')->name('admin.academic-classes.')->group(function () {
        Route::resource('materials', MaterialController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-materials')
            ->middlewareFor(['store'], 'permission:create-materials')
            ->middlewareFor(['update'], 'permission:update-materials')
            ->middlewareFor(['destroy'], 'permission:delete-materials');

        Route::resource('assignments', AssignmentController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-assignments')
            ->middlewareFor(['store'], 'permission:create-assignments')
            ->middlewareFor(['update'], 'permission:update-assignments')
            ->middlewareFor(['destroy'], 'permission:delete-assignments');

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

        Route::resource('schedules', ScheduleController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-schedules')
            ->middlewareFor(['store'], 'permission:create-schedules')
            ->middlewareFor(['update'], 'permission:update-schedules')
            ->middlewareFor(['destroy'], 'permission:delete-schedules');

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

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

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

        Route::resource('course-classes', CourseClassController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-course-classes')
            ->middlewareFor(['store'], 'permission:create-course-classes')
            ->middlewareFor(['update'], 'permission:update-course-classes')
            ->middlewareFor(['destroy'], 'permission:delete-course-classes');

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

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

    Route::middleware('role:mahasiswa')->group(function () {
        Route::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::middleware('role:dosen')->prefix('academic-advising-logs')->name('lecturer.academic-advising-logs.')->group(function () {
        Route::get('/', [AcademicAdvisingLogController::class, 'mine'])->name('index');
        Route::post('/', [AcademicAdvisingLogController::class, 'store'])->name('store');
    });

    Route::prefix('admin/finances')->name('admin.finances.')->group(function () {
        Route::resource('tuition-invoices', TuitionInvoiceController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-tuition-invoices')
            ->middlewareFor(['store'], 'permission:create-tuition-invoices')
            ->middlewareFor(['update'], 'permission:update-tuition-invoices')
            ->middlewareFor(['destroy'], 'permission:delete-tuition-invoices');

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

    Route::prefix('admin/services')->name('admin.services.')->group(function () {
        Route::resource('letter-requests', LetterRequestController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-letter-requests')
            ->middlewareFor(['store'], 'permission:create-letter-requests')
            ->middlewareFor(['update'], 'permission:update-letter-requests')
            ->middlewareFor(['destroy'], 'permission:delete-letter-requests');
        Route::patch('letter-requests/{letter_request}/status', [LetterRequestController::class, 'updateStatus'])->name('letter-requests.update_status')->middleware('permission:update-letter-requests-status');

        Route::resource('academic-advising-logs', AcademicAdvisingLogController::class)
            ->except(['create', 'edit', 'show'])
            ->middlewareFor(['index'], 'permission:view-academic-advising-logs')
            ->middlewareFor(['store'], 'permission:create-academic-advising-logs')
            ->middlewareFor(['update'], 'permission:update-academic-advising-logs')
            ->middlewareFor(['destroy'], 'permission:delete-academic-advising-logs');
    });

    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(['show'])
        ->names('admin.feedback')
        ->middlewareFor(['index'], 'permission:view-feedback')
        ->middlewareFor(['create', 'store'], 'permission:create-feedback')
        ->middlewareFor(['edit', 'update'], 'permission:update-feedback')
        ->middlewareFor(['destroy'], 'permission:delete-feedback');
    Route::patch('admin/feedback/{feedback}/status', [FeedbackController::class, 'updateStatus'])->name('admin.feedback.update_status')->middleware('permission:update-feedback-status');

    Route::prefix('admin/users')->name('admin.users.')->group(function () {
        Route::resource('lecturers', LecturerController::class)
            ->except(['show'])
            ->parameters(['lecturers' => 'user'])
            ->middlewareFor(['index'], 'permission:view-lecturers')
            ->middlewareFor(['create', 'store'], 'permission:create-lecturers')
            ->middlewareFor(['edit', 'update'], 'permission:update-lecturers')
            ->middlewareFor(['destroy'], 'permission:delete-lecturers');
        Route::patch('lecturers/{user}/reset-password', [LecturerController::class, 'resetPassword'])->name('lecturers.reset_password')->middleware('permission:reset-lecturers-password');
        Route::patch('lecturers/{user}/user-status', [LecturerController::class, 'updateUserStatus'])->name('lecturers.update_user_status')->middleware('permission:update-lecturers-status');
        Route::get('lecturers/export', [LecturerController::class, 'export'])->name('lecturers.export')->middleware('permission:export-lecturers');

        Route::resource('students', StudentController::class)
            ->except(['show'])
            ->parameters(['students' => 'user'])
            ->middlewareFor(['index'], 'permission:view-students')
            ->middlewareFor(['create', 'store'], 'permission:create-students')
            ->middlewareFor(['edit', 'update'], 'permission:update-students')
            ->middlewareFor(['destroy'], 'permission:delete-students');
        Route::patch('students/{user}/reset-password', [StudentController::class, 'resetPassword'])->name('students.reset_password')->middleware('permission:reset-students-password');
        Route::patch('students/{user}/status', [StudentController::class, 'updateStatus'])->name('students.update_status')->middleware('permission:update-students-academic-status');
        Route::patch('students/{user}/user-status', [StudentController::class, 'updateUserStatus'])->name('students.update_user_status')->middleware('permission:update-students-account-status');
        Route::get('students/export', [StudentController::class, 'export'])->name('students.export')->middleware('permission:export-students');

        Route::resource('administrators', AdministratorController::class)
            ->except(['show'])
            ->parameters(['administrators' => 'user'])
            ->middlewareFor(['index'], 'permission:view-administrators')
            ->middlewareFor(['create', 'store'], 'permission:create-administrators')
            ->middlewareFor(['edit', 'update'], 'permission:update-administrators')
            ->middlewareFor(['destroy'], 'permission:delete-administrators');
        Route::patch('administrators/{user}/reset-password', [AdministratorController::class, 'resetPassword'])->name('administrators.reset_password')->middleware('permission:reset-administrators-password');
        Route::patch('administrators/{user}/user-status', [AdministratorController::class, 'updateUserStatus'])->name('administrators.update_user_status')->middleware('permission:update-administrators-status');
    });

    Route::prefix('admin/developer')->name('admin.developer.')->group(function () {
        Route::get('logs', [LogController::class, 'index'])->name('logs.index')->middleware('permission:view-logs');
        Route::get('logs/download', [LogController::class, 'download'])->name('logs.download')->middleware('permission:view-logs');

        Route::get('roles', [RolePermissionController::class, 'index'])->name('roles.index')->middleware('permission:view-roles');
        Route::put('roles/{role}', [RolePermissionController::class, 'update'])->name('roles.update')->middleware('permission:update-roles');
    });
});
