<?php

use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\BlogController as AdminBlogController;
use App\Http\Controllers\Admin\CallbackRequestController;
use App\Http\Controllers\Admin\CandidateController as AdminCandidateController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\HomePageController;
use App\Http\Controllers\Admin\InquiryController;
use App\Http\Controllers\Admin\MaidLinkController as AdminMaidLinkController;
use App\Http\Controllers\Admin\PageController as AdminPageController;
use App\Http\Controllers\Admin\PageContentController as AdminPageContentController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\FrontendController;
use Illuminate\Support\Facades\Route;

Route::get('/lang/{locale}', function (string $locale) {
    abort_unless(in_array($locale, ['en', 'ar'], true), 404);

    $redirect = request()->query('redirect');

    $previous = url()->previous();
    $path = parse_url($previous, PHP_URL_PATH) ?? '/';
    $query = parse_url($previous, PHP_URL_QUERY);

    $scriptName = str_replace('\\', '/', request()->server('SCRIPT_NAME', ''));
    $basePath = trim(str_replace('/index.php', '', $scriptName), '/');

    $cleanPath = '/' . ltrim($path, '/');

    if ($basePath !== '' && str_starts_with($cleanPath, '/' . $basePath)) {
        $cleanPath = substr($cleanPath, strlen('/' . $basePath));
        $cleanPath = $cleanPath === '' ? '/' : $cleanPath;
    }

    $cleanPath = preg_replace('#^/ar(?=/|$)#', '', $cleanPath);
    $cleanPath = $cleanPath === '' ? '/' : $cleanPath;

    $targetPath = $locale === 'ar'
        ? '/ar' . ($cleanPath === '/' ? '' : $cleanPath)
        : $cleanPath;

    if ($basePath !== '') {
        $targetPath = '/' . $basePath . $targetPath;
    }

    if ($query) {
        $targetPath .= '?' . $query;
    }

    return redirect($targetPath);
})->name('lang.switch');

Route::controller(FrontendController::class)->group(function () {
    Route::get('/', 'index')->name('home');
    // Handle setups where webserver forwards "/project" to Laravel path directly.
    Route::get('/project', 'index')->name('home.project');
    // Route::get('/packages', 'packages')->name('packages');
    Route::get('/candidates-profiles', 'candidatesProfiles')->name('candidates.profiles');
    Route::get('/candidate-profiles', 'profiles')->name('profiles');
    Route::get('/profiles/{slug}', 'profileBySlug')->name('profiles.show');
    Route::get('/services', 'services')->name('services');
    Route::get('/full-time-live-in-maid-nanny-dubai', 'fullTimeLiveIn')->name('full.time.live.in');
    Route::get('/about-us', 'aboutUs')->name('about.us');
    Route::get('/maid-visa', 'maidVisa')->name('maid.visa');
    Route::get('/contact-us', 'contactUs')->name('contact.us');
    Route::post('/contact-inquiry', 'contactInquiry')->name('contact.inquiry');
    Route::post('/newsletter-subscribe', 'newsletterSubscribe')
        ->middleware('throttle:20,1')
        ->name('newsletter.subscribe');
    Route::post('/callback-request', 'callbackRequest')->name('callback.request');
    Route::get('/callback-success', 'callbackSuccess')->name('callback.success');
    Route::get('/enquiry-thank-you', 'callbackSuccess')->name('enquiry.thank.you');
    Route::get('/terms-and-conditions', 'termsAndConditions')->name('terms.and.conditions');
    Route::get('/privacy-policy', 'privacyPolicy')->name('privacy.policy');
});

Route::prefix('blogs')->name('blogs.')->controller(BlogController::class)->group(function () {
    Route::get('/', 'index')->name('index');
    Route::get('/{blog:slug}', 'show')->name('show');
});

Route::prefix('ar')->name('ar.')->group(function () {
    Route::controller(FrontendController::class)->group(function () {
        Route::get('/', 'index')->name('home');
        // Route::get('/packages', 'packages')->name('packages');
        Route::get('/candidates-profiles', 'candidatesProfiles')->name('candidates.profiles');
        Route::get('/candidate-profiles', 'profiles')->name('profiles');
        Route::get('/profiles/{slug}', 'profileBySlug')->name('profiles.show');
        Route::get('/services', 'services')->name('services');
        Route::get('/full-time-live-in-maid-nanny-dubai', 'fullTimeLiveIn')->name('full.time.live.in');
        Route::get('/about-us', 'aboutUs')->name('about.us');
        Route::get('/maid-visa', 'maidVisa')->name('maid.visa');
        Route::get('/contact-us', 'contactUs')->name('contact.us');
        Route::post('/contact-inquiry', 'contactInquiry')->name('contact.inquiry');
        Route::post('/newsletter-subscribe', 'newsletterSubscribe')
            ->middleware('throttle:20,1')
            ->name('newsletter.subscribe');
        Route::post('/callback-request', 'callbackRequest')->name('callback.request');
        Route::get('/enquiry-thank-you', 'callbackSuccess')->name('enquiry.thank.you');
        Route::get('/terms-and-conditions', 'termsAndConditions')->name('terms.and.conditions');
        Route::get('/privacy-policy', 'privacyPolicy')->name('privacy.policy');
    });

    Route::prefix('blogs')->name('blogs.')->controller(BlogController::class)->group(function () {
        Route::get('/', 'index')->name('index');
        Route::get('/{blog:slug}', 'show')->name('show');
    });
});

Route::prefix('admin')->name('admin.')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('/login', [AuthController::class, 'create'])->name('login');
        Route::post('/login', [AuthController::class, 'store'])->name('login.store');
    });

    Route::middleware('auth')->group(function () {
        Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
        Route::post('/logout', [AuthController::class, 'destroy'])->name('logout');

        Route::prefix('settings')->name('settings.')->group(function () {
            Route::get('/', [SettingController::class, 'edit'])->name('edit');
            Route::put('/', [SettingController::class, 'update'])->name('update');
        });

        Route::prefix('home')->name('home.')->group(function () {
            Route::get('/', [HomePageController::class, 'edit'])->name('edit');
            Route::put('/', [HomePageController::class, 'update'])->name('update');
        });

        Route::prefix('pages')->name('pages.')->controller(AdminPageController::class)->group(function () {
            Route::get('/index', 'editIndex')->name('index.edit');
            Route::put('/index', 'updateIndex')->name('index.update');

            Route::get('/packages', 'editPackages')->name('packages.edit');
            Route::put('/packages', 'updatePackages')->name('packages.update');

            Route::get('/candidates-profiles', 'editCandidatesProfiles')->name('candidates-profiles.edit');
            Route::put('/candidates-profiles', 'updateCandidatesProfiles')->name('candidates-profiles.update');

            Route::get('/profiles', 'editProfiles')->name('profiles.edit');
            Route::put('/profiles', 'updateProfiles')->name('profiles.update');

            Route::get('/profile-details', 'editProfileDetails')->name('profile-details.edit');
            Route::put('/profile-details', 'updateProfileDetails')->name('profile-details.update');

            Route::get('/full-time-live-in', 'editFullTimeLiveIn')->name('full-time-live-in.edit');
            Route::put('/full-time-live-in', 'updateFullTimeLiveIn')->name('full-time-live-in.update');

            Route::get('/services', 'editServices')->name('services.edit');
            Route::put('/services', 'updateServices')->name('services.update');

            Route::get('/about-us', 'editAboutUs')->name('about-us.edit');
            Route::put('/about-us', 'updateAboutUs')->name('about-us.update');

            Route::get('/maid-visa', 'editMaidVisa')->name('maid-visa.edit');
            Route::put('/maid-visa', 'updateMaidVisa')->name('maid-visa.update');

            Route::get('/contact-us', 'editContactUs')->name('contact-us.edit');
            Route::put('/contact-us', 'updateContactUs')->name('contact-us.update');

            Route::get('/terms-and-conditions', 'editTermsAndConditions')->name('terms-and-conditions.edit');
            Route::put('/terms-and-conditions', 'updateTermsAndConditions')->name('terms-and-conditions.update');

            Route::get('/privacy-policy', 'editPrivacyPolicy')->name('privacy-policy.edit');
            Route::put('/privacy-policy', 'updatePrivacyPolicy')->name('privacy-policy.update');
        });

        Route::prefix('pages')->name('pages.')->group(function () {
            Route::get('/page-contents', [AdminPageContentController::class, 'index'])->name('page-contents.index');
            Route::get('/page-contents/{pageKey}/edit', [AdminPageContentController::class, 'edit'])->name('page-contents.edit');
            Route::put('/page-contents/{pageKey}', [AdminPageContentController::class, 'update'])->name('page-contents.update');
        });

        Route::resource('blogs', AdminBlogController::class)->except('show');
        Route::resource('candidates', AdminCandidateController::class)->except('show');
        Route::resource('maid-links', AdminMaidLinkController::class);

        Route::get('/inquiries', [InquiryController::class, 'index'])->name('inquiries.index');
        Route::get('/callback-requests', [CallbackRequestController::class, 'index'])->name('callback-requests.index');
    });
});

Route::get('/login', fn () => redirect()->route('admin.login'))->name('login');

Route::fallback(function () {
    abort(404);
});