<?php

use App\Http\Controllers\ApplicationController;
use App\Http\Controllers\ApplicationPipelineController;
use App\Http\Controllers\ApplicantDashboardController;
use App\Http\Controllers\Admin\ApplicantExportController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\GoogleAuthController;
use App\Http\Controllers\Auth\PasswordResetOtpController;
use App\Http\Controllers\BrevoWebhookController;
use App\Http\Controllers\ContentPageController;
use App\Http\Controllers\InstallerController;
use App\Http\Controllers\JobAlertPreferenceController;
use App\Http\Controllers\JobAiAssistantController;
use App\Http\Controllers\JobController;
use App\Http\Controllers\JobNoteController;
use App\Http\Controllers\LandingController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\QuestionnaireController;
use App\Http\Controllers\RazorpayWebhookController;
use App\Http\Controllers\ResumeController;
use App\Http\Controllers\SavedJobController;
use Illuminate\Support\Facades\Route;

Route::get('/install', [InstallerController::class, 'index'])->name('install.index');
Route::post('/install', [InstallerController::class, 'store'])->name('install.store');
Route::get('/install/complete', [InstallerController::class, 'complete'])->name('install.complete');

Route::get('/', LandingController::class)->name('landing');

Route::get('/pages/{slug}', [ContentPageController::class, 'show'])
    ->where('slug', '[A-Za-z0-9\\-]+')
    ->name('content-pages.show');

Route::get('/job-alerts/{preference}/unsubscribe', [JobAlertPreferenceController::class, 'unsubscribe'])
    ->middleware('signed')
    ->name('job-alerts.unsubscribe');

Route::middleware('guest')->group(function (): void {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])
        ->name('login');

    Route::post('/login', [AuthenticatedSessionController::class, 'store'])
        ->name('login.store');

    Route::get('/forgot-password', [PasswordResetOtpController::class, 'requestForm'])
        ->name('password.request');

    Route::post('/forgot-password', [PasswordResetOtpController::class, 'send'])
        ->name('password.email');

    Route::get('/reset-password', [PasswordResetOtpController::class, 'resetForm'])
        ->name('password.otp.verify');

    Route::post('/reset-password', [PasswordResetOtpController::class, 'reset'])
        ->name('password.otp.reset');

    Route::get('/auth/google', [GoogleAuthController::class, 'redirect'])
        ->name('auth.google.redirect');

    Route::get('/auth/google/callback', [GoogleAuthController::class, 'callback'])
        ->name('auth.google.callback');
});

Route::post('/logout', [GoogleAuthController::class, 'logout'])
    ->middleware('auth')
    ->name('logout');

Route::post('/webhooks/razorpay', RazorpayWebhookController::class)
    ->name('webhooks.razorpay');

Route::post('/webhooks/brevo/email-events', BrevoWebhookController::class)
    ->name('webhooks.brevo.email-events');

Route::middleware('auth:admin')
    ->prefix('admin/exports')
    ->name('admin.exports.')
    ->group(function (): void {
        Route::get('/applicants.csv', [ApplicantExportController::class, 'applicants'])
            ->name('applicants');

        Route::get('/applications.csv', [ApplicantExportController::class, 'applications'])
            ->name('applications');
    });

Route::middleware('auth')->group(function (): void {
    Route::get('/questionnaire', [QuestionnaireController::class, 'show'])
        ->name('questionnaire.show');

    Route::post('/questionnaire', [QuestionnaireController::class, 'store'])
        ->name('questionnaire.store');

    Route::get('/checkout', [PaymentController::class, 'checkout'])
        ->middleware('questionnaire.completed')
        ->name('payment.checkout');

    Route::post('/checkout/order', [PaymentController::class, 'createOrder'])
        ->middleware('questionnaire.completed')
        ->name('payment.order');

    Route::post('/checkout/verify', [PaymentController::class, 'verify'])
        ->middleware('questionnaire.completed')
        ->name('payment.verify');

    Route::get('/resume', [ResumeController::class, 'show'])
        ->middleware(['questionnaire.completed', 'access.unlocked'])
        ->name('resume.show');

    Route::post('/resume', [ResumeController::class, 'store'])
        ->middleware(['questionnaire.completed', 'access.unlocked'])
        ->name('resume.store');

    Route::get('/resume/status', [ResumeController::class, 'status'])
        ->middleware(['questionnaire.completed', 'access.unlocked'])
        ->name('resume.status');

    Route::get('/resume/report', [ResumeController::class, 'report'])
        ->middleware(['questionnaire.completed', 'access.unlocked'])
        ->name('resume.report');

    Route::get('/resume/history', [ResumeController::class, 'history'])
        ->middleware(['questionnaire.completed', 'access.unlocked'])
        ->name('resume.history');

    Route::get('/profile', [ProfileController::class, 'edit'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed'])
        ->name('profile.edit');

    Route::post('/profile', [ProfileController::class, 'update'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed'])
        ->name('profile.update');

    Route::get('/dashboard', ApplicantDashboardController::class)
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('applicant.dashboard');

    Route::get('/jobs', [JobController::class, 'index'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.index');

    Route::get('/jobs/{job}', [JobController::class, 'show'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.show');

    Route::post('/jobs/{job}/save', [SavedJobController::class, 'store'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.save');

    Route::delete('/jobs/{job}/save', [SavedJobController::class, 'destroy'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.unsave');

    Route::post('/jobs/{job}/apply', [ApplicationController::class, 'store'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.apply');

    Route::post('/jobs/{job}/ai', [JobAiAssistantController::class, 'store'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.ai.store');

    Route::patch('/jobs/{job}/note', [JobNoteController::class, 'update'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('jobs.notes.update');

    Route::get('/job-alerts/preferences', [JobAlertPreferenceController::class, 'edit'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('job-alerts.preferences');

    Route::patch('/job-alerts/preferences', [JobAlertPreferenceController::class, 'update'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('job-alerts.preferences.update');

    Route::get('/applications/pipeline', ApplicationPipelineController::class)
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('applications.pipeline');

    Route::patch('/applications/{application}', [ApplicationController::class, 'update'])
        ->middleware(['questionnaire.completed', 'access.unlocked', 'resume.analyzed', 'profile.completed'])
        ->name('applications.update');
});
