<?php

use App\Http\Controllers\Admin\CountriesPageController as AdminCountriesPageController;
use App\Http\Controllers\Admin\BlogArticleController as AdminBlogArticleController;
use App\Http\Controllers\Admin\BlogCategoryController as AdminBlogCategoryController;
use App\Http\Controllers\Admin\ContactSubmissionController as AdminContactSubmissionController;
use App\Http\Controllers\Admin\CountryController as AdminCountryController;
use App\Http\Controllers\Admin\CountrySectionsContentController as AdminCountrySectionsContentController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\EmployeeController;
use App\Http\Controllers\Admin\EventRegistrationController as AdminEventRegistrationController;
use App\Http\Controllers\Admin\GeneralSettingController;
use App\Http\Controllers\Admin\HomePageController as AdminHomePageController;
use App\Http\Controllers\Admin\LeadController as AdminLeadController;
use App\Http\Controllers\Admin\PageDefinitionController as AdminPageDefinitionController;
use App\Http\Controllers\Admin\OurPartnerController as AdminOurPartnerController;
use App\Http\Controllers\Admin\PartnerInstitutionController as AdminPartnerInstitutionController;
use App\Http\Controllers\Admin\SectionController as AdminSectionController;
use App\Http\Controllers\Admin\EventController as AdminEventController;
use App\Http\Controllers\Admin\ServicePageController as AdminServicePageController;
use App\Http\Controllers\Admin\SuccessStoriesPageController as AdminSuccessStoriesPageController;
use App\Http\Controllers\Admin\StudentReviewController as AdminStudentReviewController;
use App\Http\Controllers\Admin\FaqController as AdminFaqController;
use App\Http\Controllers\Admin\UserInterfaceController as AdminUserInterfaceController;
use App\Http\Controllers\Admin\VisaSuccessStoryController as AdminVisaSuccessStoryController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\CountryController;
use App\Http\Controllers\Employee\BlogArticleController as EmployeeBlogArticleController;
use App\Http\Controllers\Employee\DashboardController as EmployeeDashboard;
use App\Http\Controllers\Employee\LeadController as EmployeeLeadController;
use App\Http\Controllers\Employee\ProfileController as EmployeeProfileController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\EventRegistrationController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LeadController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ServiceController;
use App\Http\Controllers\SuccessStoriesController;
use Illuminate\Support\Facades\Route;

$adminPrefix = trim((string) env('ADMIN_PATH', 'admin'), '/');
$adminPrefix = $adminPrefix !== '' ? $adminPrefix : 'admin';

Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/about', [HomeController::class, 'about'])->name('about');
Route::get('/contact', [HomeController::class, 'contact'])->name('contact');
Route::get('/blogs', [BlogController::class, 'index'])->name('blogs.index');
Route::get('/blogs/{slug}', [BlogController::class, 'show'])->name('blogs.show');
Route::get('/services', [ServiceController::class, 'index'])->name('services');
Route::get('/success-stories', [SuccessStoriesController::class, 'index'])->name('success-stories');
Route::post('/contact', [HomeController::class, 'sendContact'])->name('contact.send')->middleware('throttle:5,1');
Route::get('/sitemap.xml', [HomeController::class, 'sitemap'])->name('sitemap');

Route::get('/countries', [CountryController::class, 'index'])->name('countries.index');
Route::get('/countries/{slug}', [CountryController::class, 'show'])->name('countries.show');
Route::get('/events', [EventController::class, 'index'])->name('events.index');
Route::get('/events/{slug}', [EventController::class, 'show'])->name('events.show');
Route::post('/events/{slug}/register', [EventRegistrationController::class, 'store'])->name('events.register')->middleware('throttle:10,1');

Route::get('/api/countries', [LeadController::class, 'countries'])->name('leads.countries');
Route::get('/api/site-info', [GeneralSettingController::class, 'siteInfo'])->name('api.site-info');
Route::post('/leads/otp/send', [LeadController::class, 'sendOtp'])->name('leads.otp.send')->middleware('throttle:5,1');
Route::post('/leads', [LeadController::class, 'store'])->name('leads.store')->middleware('throttle:10,1');

Route::middleware('auth')->get('/dashboard', function () {
    $user = auth()->user();
    if ($user && $user->isEmployee()) {
        return redirect()->route('employee.dashboard');
    }

    return redirect()->route('admin.dashboard');
})->name('dashboard');

Route::prefix($adminPrefix)
    ->name('admin.')
    ->middleware(['auth', 'admin:owner,admin'])
    ->group(function () {
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
        Route::post('notifications/read-all', [DashboardController::class, 'markAllNotificationsRead'])->name('notifications.read-all');
        Route::post('notifications/{notification}/read', [DashboardController::class, 'markNotificationRead'])->name('notifications.read');

        Route::get('settings/general', [GeneralSettingController::class, 'edit'])->name('settings.general.edit');
        Route::post('settings/general', [GeneralSettingController::class, 'update'])->name('settings.general.update');
        Route::post('settings/email-test', [GeneralSettingController::class, 'sendTestEmail'])->name('settings.email.test');
        Route::post('settings/ai-generate', [GeneralSettingController::class, 'aiGenerate'])->name('settings.ai.generate');

        Route::resource('countries', AdminCountryController::class);
        Route::patch('countries/{country}/toggle', [AdminCountryController::class, 'toggleStatus'])->name('countries.toggle');
        Route::get('countries-page-content', [AdminCountriesPageController::class, 'edit'])->name('countries.page.edit');
        Route::post('countries-page-content', [AdminCountriesPageController::class, 'update'])->name('countries.page.update');
        Route::get('country-sections-content', [AdminCountrySectionsContentController::class, 'edit'])->name('country-sections.edit');
        Route::post('country-sections-content', [AdminCountrySectionsContentController::class, 'update'])->name('country-sections.update');

        Route::resource('events', AdminEventController::class)->except(['show']);
        Route::resource('blog-categories', AdminBlogCategoryController::class)->except(['show']);
        Route::resource('blog-articles', AdminBlogArticleController::class)->except(['show']);
        Route::get('blog-articles/{blogArticle}/preview', [AdminBlogArticleController::class, 'preview'])->name('blog-articles.preview');
        Route::patch('blog-articles/{blogArticle}/approve', [AdminBlogArticleController::class, 'approve'])->name('blog-articles.approve');
        Route::patch('blog-articles/{blogArticle}/reject', [AdminBlogArticleController::class, 'reject'])->name('blog-articles.reject');
        Route::patch('blog-articles/{blogArticle}/unpublish', [AdminBlogArticleController::class, 'unpublish'])->name('blog-articles.unpublish');
        Route::get('event-registrations', [AdminEventRegistrationController::class, 'index'])->name('event-registrations.index');
        Route::resource('student-reviews', AdminStudentReviewController::class)
            ->parameters(['student-reviews' => 'studentReview'])
            ->except(['show']);
        Route::resource('partner-institutions', AdminPartnerInstitutionController::class)
            ->parameters(['partner-institutions' => 'partnerInstitution'])
            ->except(['show']);
        Route::resource('our-partners', AdminOurPartnerController::class)
            ->parameters(['our-partners' => 'ourPartner'])
            ->except(['show']);
        Route::resource('faqs', AdminFaqController::class)->except(['show']);
        Route::resource('visa-stories', AdminVisaSuccessStoryController::class)
            ->parameters(['visa-stories' => 'visaStory'])
            ->except(['show']);

        Route::get('services-content', [AdminServicePageController::class, 'edit'])->name('services.edit');
        Route::post('services-content', [AdminServicePageController::class, 'update'])->name('services.update');

        Route::get('ui-sections', [AdminUserInterfaceController::class, 'sections'])->name('ui.sections');
        Route::resource('sections', AdminSectionController::class)->except(['show']);
        Route::get('pages', [AdminPageDefinitionController::class, 'index'])->name('pages.index');
        Route::get('pages/{page}', [AdminPageDefinitionController::class, 'edit'])->name('pages.edit');
        Route::put('pages/{page}', [AdminPageDefinitionController::class, 'update'])->name('pages.update');

        Route::get('home-content', [AdminHomePageController::class, 'edit'])->name('home.edit');
        Route::post('home-content', [AdminHomePageController::class, 'update'])->name('home.update');

        Route::get('success-stories-content', [AdminSuccessStoriesPageController::class, 'edit'])->name('success-stories.edit');
        Route::post('success-stories-content', [AdminSuccessStoriesPageController::class, 'update'])->name('success-stories.update');

        Route::get('leads', [AdminLeadController::class, 'index'])->name('leads.index');
        Route::get('leads/{lead}', [AdminLeadController::class, 'show'])->name('leads.show');
        Route::patch('leads/{lead}', [AdminLeadController::class, 'update'])->name('leads.update');
        Route::delete('leads/{lead}', [AdminLeadController::class, 'destroy'])->name('leads.destroy');
        Route::post('leads/{lead}/notes', [AdminLeadController::class, 'addNote'])->name('leads.notes.store');
        Route::post('leads/{lead}/assign', [AdminLeadController::class, 'assign'])->name('leads.assign');
        Route::get('contact-submissions', [AdminContactSubmissionController::class, 'index'])->name('contact-submissions.index');
        Route::get('contact-submissions/{contactSubmission}', [AdminContactSubmissionController::class, 'show'])->name('contact-submissions.show');
        Route::patch('contact-submissions/{contactSubmission}', [AdminContactSubmissionController::class, 'update'])->name('contact-submissions.update');
        Route::post('contact-submissions/{contactSubmission}/convert', [AdminContactSubmissionController::class, 'convertToLead'])->name('contact-submissions.convert');
        Route::delete('contact-submissions/{contactSubmission}', [AdminContactSubmissionController::class, 'destroy'])->name('contact-submissions.destroy');

        Route::resource('employees', EmployeeController::class)->except(['show']);
    });

Route::prefix('employee')
    ->name('employee.')
    ->middleware(['auth', 'admin'])
    ->group(function () {
        Route::get('/', [EmployeeDashboard::class, 'index'])->name('dashboard');
        Route::get('profile', [EmployeeProfileController::class, 'edit'])->name('profile.edit');
        Route::resource('blog-articles', EmployeeBlogArticleController::class)
            ->parameters(['blog-articles' => 'blogArticle'])
            ->except(['show']);
        Route::patch('profile', [EmployeeProfileController::class, 'update'])->name('profile.update');
        Route::patch('profile/password', [EmployeeProfileController::class, 'updatePassword'])->name('profile.password.update');
        Route::get('leads', [EmployeeLeadController::class, 'index'])->name('leads.index');
        Route::get('leads/{lead}', [EmployeeLeadController::class, 'show'])->name('leads.show');
        Route::post('leads/{lead}/notes', [EmployeeLeadController::class, 'addNote'])->name('leads.notes.store');
        Route::patch('leads/{lead}/status', [EmployeeLeadController::class, 'updateStatus'])->name('leads.status');
    });

Route::middleware('auth')->group(function () {
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/read-all', [NotificationController::class, 'markAllRead'])->name('notifications.read-all');
    Route::post('/notifications/{notification}/read', [NotificationController::class, 'markRead'])->name('notifications.read');
    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');
});

require __DIR__.'/auth.php';
