<?php

use App\Modules\HR\Recruitment\Controllers\ApplicationsPageController;
use App\Modules\HR\Recruitment\Controllers\CandidateCvsPageController;
use App\Modules\HR\Recruitment\Controllers\CandidatesPageController;
use App\Modules\HR\Recruitment\Controllers\CareerPortalAdminController;
use App\Modules\HR\Recruitment\Controllers\InterviewScorecardsPageController;
use App\Modules\HR\Recruitment\Controllers\InterviewsPageController;
use App\Modules\HR\Recruitment\Controllers\JobRequestsPageController;
use App\Modules\HR\Recruitment\Controllers\RecruitmentDashboardPageController;
use App\Modules\HR\Recruitment\Controllers\RecruitmentReportsPageController;
use App\Modules\HR\Recruitment\Controllers\HiringWizardPageController;
use App\Modules\HR\Recruitment\Controllers\OnboardingWorkspaceController;
use App\Modules\HR\Recruitment\Controllers\OfferLetterWorkflowController;
use App\Modules\HR\Recruitment\Controllers\OfferLettersPageController;
use App\Modules\HR\Recruitment\Controllers\VacanciesPageController;
use Illuminate\Support\Facades\Route;

Route::prefix('hr/recruitment')
    ->name('hr.recruitment.')
    ->middleware(['module:hr', 'can:recruitment.view'])
    ->group(function () {
        Route::get('/dashboard', [RecruitmentDashboardPageController::class, 'index'])->name('dashboard');

        Route::prefix('reports')->name('reports.')->middleware('can:recruitment.reports.view')->group(function () {
            Route::get('/', [RecruitmentReportsPageController::class, 'index'])->name('index');
            Route::post('/filters', [RecruitmentReportsPageController::class, 'storeFilter'])->name('filters.store');
            Route::get('/filters/{saved_filter}/apply', [RecruitmentReportsPageController::class, 'applyFilter'])->name('filters.apply');
            Route::delete('/filters/{saved_filter}', [RecruitmentReportsPageController::class, 'destroyFilter'])->name('filters.destroy');
            Route::get('/{report}/export/excel', [RecruitmentReportsPageController::class, 'exportExcel'])->name('export.excel');
            Route::get('/{report}/export/pdf', [RecruitmentReportsPageController::class, 'exportPdf'])->name('export.pdf');
            Route::get('/{report}', [RecruitmentReportsPageController::class, 'show'])->name('show');
        });

        Route::prefix('hiring')->name('hiring.')->group(function () {
            Route::get('/', [HiringWizardPageController::class, 'index'])->name('index');
            Route::get('/{offer_letter}/wizard', [HiringWizardPageController::class, 'create'])
                ->middleware('can:recruitment.manage')->name('wizard');
            Route::post('/{offer_letter}', [HiringWizardPageController::class, 'store'])
                ->middleware('can:recruitment.manage')->name('store');
        });

        Route::prefix('onboarding')->name('onboarding.')->group(function () {
            Route::get('/', [OnboardingWorkspaceController::class, 'index'])->name('index');
            Route::post('/', [OnboardingWorkspaceController::class, 'store'])
                ->middleware('can:recruitment.manage')->name('store');
            Route::get('/{onboarding}', [OnboardingWorkspaceController::class, 'show'])->name('show');
            Route::put('/{onboarding}/tasks/{task}', [OnboardingWorkspaceController::class, 'updateTask'])
                ->middleware('can:recruitment.manage')->name('tasks.update');
            Route::post('/{onboarding}/tasks/{task}/attachments', [OnboardingWorkspaceController::class, 'storeAttachment'])
                ->middleware('can:recruitment.manage')->name('tasks.attachments.store');
            Route::get('/{onboarding}/attachments/{attachment}/download', [OnboardingWorkspaceController::class, 'downloadAttachment'])
                ->name('attachments.download');
            Route::post('/{onboarding}/documents', [OnboardingWorkspaceController::class, 'storeDocument'])
                ->middleware('can:recruitment.manage')->name('documents.store');
        });

        Route::get('/career-portal', [CareerPortalAdminController::class, 'show'])->name('career-portal');
        Route::put('/career-portal/settings', [CareerPortalAdminController::class, 'updateSettings'])
            ->middleware('can:recruitment.manage')->name('career-portal.settings');

        Route::prefix('candidates')->name('candidates.')->group(function () {
            Route::get('/', [CandidatesPageController::class, 'index'])->name('index');
            Route::get('/create', [CandidatesPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [CandidatesPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::get('/{candidate}/edit', [CandidatesPageController::class, 'edit'])->name('edit');
            Route::put('/{candidate}', [CandidatesPageController::class, 'update'])->name('update');
            Route::delete('/{candidate}', [CandidatesPageController::class, 'destroy'])->name('destroy');
            Route::post('/{candidate}/cvs', [CandidateCvsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('cvs.store');
        });

        Route::prefix('cvs')->name('cvs.')->group(function () {
            Route::get('/', [CandidateCvsPageController::class, 'index'])->name('index');
            Route::post('/', [CandidateCvsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::get('/{cv}/download', [CandidateCvsPageController::class, 'download'])->name('download');
            Route::delete('/{cv}', [CandidateCvsPageController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('job-requests')->name('job-requests.')->group(function () {
            Route::get('/', [JobRequestsPageController::class, 'index'])->name('index');
            Route::get('/create', [JobRequestsPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [JobRequestsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::get('/{job_request}/edit', [JobRequestsPageController::class, 'edit'])->name('edit');
            Route::put('/{job_request}', [JobRequestsPageController::class, 'update'])->name('update');
            Route::delete('/{job_request}', [JobRequestsPageController::class, 'destroy'])->name('destroy');
            Route::post('/{job_request}/submit', [JobRequestsPageController::class, 'submit'])->name('submit');
            Route::post('/{job_request}/approve', [JobRequestsPageController::class, 'approve'])->name('approve');
            Route::post('/{job_request}/reject', [JobRequestsPageController::class, 'reject'])->name('reject');
        });

        Route::prefix('vacancies')->name('vacancies.')->group(function () {
            Route::get('/', [VacanciesPageController::class, 'index'])->name('index');
            Route::get('/create', [VacanciesPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [VacanciesPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::get('/{vacancy}', [VacanciesPageController::class, 'show'])->name('show');
            Route::get('/{vacancy}/edit', [VacanciesPageController::class, 'edit'])->name('edit');
            Route::put('/{vacancy}', [VacanciesPageController::class, 'update'])->name('update');
            Route::delete('/{vacancy}', [VacanciesPageController::class, 'destroy'])->name('destroy');
            Route::post('/{vacancy}/publish', [VacanciesPageController::class, 'publish'])->name('publish');
            Route::post('/{vacancy}/close', [VacanciesPageController::class, 'close'])->name('close');
            Route::post('/{vacancy}/portal-publish', [CareerPortalAdminController::class, 'publish'])
                ->name('portal-publish');
            Route::post('/{vacancy}/portal-unpublish', [CareerPortalAdminController::class, 'unpublish'])
                ->name('portal-unpublish');
            Route::post('/{vacancy}/feature', [CareerPortalAdminController::class, 'feature'])->name('feature');
            Route::post('/{vacancy}/unfeature', [CareerPortalAdminController::class, 'unfeature'])->name('unfeature');
            Route::put('/{vacancy}/portal-meta', [CareerPortalAdminController::class, 'updateMeta'])
                ->name('portal-meta');
        });

        Route::prefix('applications')->name('applications.')->group(function () {
            Route::get('/', [ApplicationsPageController::class, 'index'])->name('index');
            Route::get('/create', [ApplicationsPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [ApplicationsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::get('/{application}/edit', [ApplicationsPageController::class, 'edit'])->name('edit');
            Route::put('/{application}', [ApplicationsPageController::class, 'update'])->name('update');
            Route::post('/{application}/status', [ApplicationsPageController::class, 'updateStatus'])->name('status');
            Route::delete('/{application}', [ApplicationsPageController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('interviews')->name('interviews.')->group(function () {
            Route::get('/', [InterviewsPageController::class, 'index'])->name('index');
            Route::get('/create', [InterviewsPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [InterviewsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::post('/bulk-destroy', [InterviewsPageController::class, 'bulkDestroy'])
                ->middleware('can:recruitment.manage')->name('bulk-destroy');
            Route::get('/{interview}/edit', [InterviewsPageController::class, 'edit'])->name('edit');
            Route::put('/{interview}', [InterviewsPageController::class, 'update'])->name('update');
            Route::post('/{interview}/complete', [InterviewsPageController::class, 'complete'])->name('complete');
            Route::post('/{interview}/cancel', [InterviewsPageController::class, 'cancel'])->name('cancel');
            Route::post('/{interview}/no-show', [InterviewsPageController::class, 'noShow'])->name('no-show');
            Route::delete('/{interview}', [InterviewsPageController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('interview-scorecards')->name('interview-scorecards.')->group(function () {
            Route::get('/', [InterviewScorecardsPageController::class, 'index'])->name('index');
            Route::get('/create', [InterviewScorecardsPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [InterviewScorecardsPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::post('/bulk-destroy', [InterviewScorecardsPageController::class, 'bulkDestroy'])
                ->middleware('can:recruitment.manage')->name('bulk-destroy');
            Route::get('/{scorecard}/edit', [InterviewScorecardsPageController::class, 'edit'])->name('edit');
            Route::put('/{scorecard}', [InterviewScorecardsPageController::class, 'update'])->name('update');
            Route::delete('/{scorecard}', [InterviewScorecardsPageController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('offer-letters')->name('offer-letters.')->group(function () {
            Route::get('/', [OfferLettersPageController::class, 'index'])->name('index');
            Route::get('/create', [OfferLettersPageController::class, 'create'])
                ->middleware('can:recruitment.create')->name('create');
            Route::post('/', [OfferLettersPageController::class, 'store'])
                ->middleware('can:recruitment.create')->name('store');
            Route::post('/bulk-destroy', [OfferLettersPageController::class, 'bulkDestroy'])
                ->middleware('can:recruitment.manage')->name('bulk-destroy');
            Route::get('/{offer_letter}', [OfferLetterWorkflowController::class, 'show'])->name('show');
            Route::get('/{offer_letter}/edit', [OfferLettersPageController::class, 'edit'])->name('edit');
            Route::put('/{offer_letter}', [OfferLettersPageController::class, 'update'])->name('update');
            Route::delete('/{offer_letter}', [OfferLettersPageController::class, 'destroy'])->name('destroy');
            Route::post('/{offer_letter}/duplicate', [OfferLettersPageController::class, 'duplicate'])
                ->middleware('can:recruitment.create')->name('duplicate');
            Route::post('/{offer_letter}/submit', [OfferLetterWorkflowController::class, 'submit'])->name('submit');
            Route::post('/{offer_letter}/approve', [OfferLetterWorkflowController::class, 'approve'])->name('approve');
            Route::post('/{offer_letter}/send', [OfferLetterWorkflowController::class, 'send'])->name('send');
            Route::post('/{offer_letter}/resend', [OfferLetterWorkflowController::class, 'resend'])->name('resend');
            Route::post('/{offer_letter}/accept', [OfferLetterWorkflowController::class, 'accept'])->name('accept');
            Route::post('/{offer_letter}/reject', [OfferLetterWorkflowController::class, 'reject'])->name('reject');
            Route::post('/{offer_letter}/expire', [OfferLetterWorkflowController::class, 'expire'])->name('expire');
            Route::post('/{offer_letter}/withdraw', [OfferLetterWorkflowController::class, 'withdraw'])->name('withdraw');
            Route::get('/{offer_letter}/print', [OfferLetterWorkflowController::class, 'print'])->name('print');
            Route::get('/{offer_letter}/pdf', [OfferLetterWorkflowController::class, 'downloadPdf'])->name('pdf');
            Route::post('/{offer_letter}/attachments', [OfferLetterWorkflowController::class, 'storeAttachment'])
                ->name('attachments.store');
            Route::get('/attachments/{attachment}/download', [OfferLetterWorkflowController::class, 'downloadAttachment'])
                ->name('attachments.download');
            Route::delete('/attachments/{attachment}', [OfferLetterWorkflowController::class, 'destroyAttachment'])
                ->name('attachments.destroy');
        });
    });
