<?php
// Expose all errors to easily debug host-side HTTP 500 crashes
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();
$appConfig = require_once __DIR__ . '/../config/app.php';
date_default_timezone_set($appConfig['timezone']);

require_once __DIR__ . '/../helpers/response.php';
require_once __DIR__ . '/../helpers/auth.php';
require_once __DIR__ . '/../helpers/validation.php';
require_once __DIR__ . '/../helpers/security.php';
require_once __DIR__ . '/../helpers/lang.php';

// Dynamic Base Path Detection for zero-setup hosting environments
$basePath = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
if ($basePath === '/') {
    $basePath = '';
}
define('BASE_PATH', $basePath);

// Basic Router
$requestUri = $_SERVER['REQUEST_URI'];
$path = str_replace(BASE_PATH, '', $requestUri);
$path = strtok($path, '?');
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';

// Normalize path by stripping /index.php if it is prepended by webserver rewrites
if (strpos($path, '/index.php') === 0) {
    $path = substr($path, 10);
    if ($path === '' || $path === false) {
        $path = '/';
    }
}

// Handle Language Switch
if (isset($_GET['lang']) && in_array($_GET['lang'], ['en', 'ar'])) {
    $_SESSION['admin_lang'] = $_GET['lang'];
    $redirect = $_SERVER['HTTP_REFERER'] ?? (BASE_PATH . '/admin');
    header('Location: ' . $redirect);
    exit;
}

if ($path === '' || $path === '/') {
    echo "HelperLink API & Admin is running";
    exit;
}

if ($path === '/admin') {
    $page = $_GET['page'] ?? 'dashboard';
    
    // Auth & Public Routes
    if ($page === 'login') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/AuthController.php';
        (new AuthController())->login();
        exit;
    } elseif ($page === 'logout') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/AuthController.php';
        (new AuthController())->logout();
        exit;
    } elseif ($page === 'dev_docs') {
        require __DIR__ . '/../views/admin/dev_docs/index.php';
        exit;
    }
    
    // Protected Routes
    require_once __DIR__ . '/../app/Middleware/AdminAuthMiddleware.php';
    AdminAuthMiddleware::authenticate();
    
    if ($page === 'dashboard') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/DashboardController.php';
        (new DashboardController())->index();
        exit;
    } elseif ($page === 'customers') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/CustomerController.php';
        $controller = new CustomerController();
        if (($_GET['action'] ?? '') === 'bulk') $controller->bulkUpdate();
        else $controller->index();
        exit;
    } elseif ($page === 'vendors') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/VendorController.php';
        $controller = new VendorController();
        if (($_GET['action'] ?? '') === 'bulk') $controller->bulkUpdate();
        elseif (($_GET['action'] ?? '') === 'update_status') $controller->updateStatus();
        elseif (($_GET['action'] ?? '') === 'toggle_featured') $controller->toggleFeatured();
        elseif (($_GET['action'] ?? '') === 'toggle_verified') $controller->toggleVerified();
        else $controller->index();
        exit;
    } elseif ($page === 'maids') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/MaidController.php';
        $controller = new MaidController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'approve') $controller->approve();
        elseif ($action === 'reject') $controller->reject();
        elseif ($action === 'show') $controller->show();
        elseif ($action === 'toggle_recommended') $controller->toggleRecommended();
        elseif ($action === 'toggle_verified') $controller->toggleVerified();
        else $controller->index();
        exit;
    } elseif ($page === 'contracts') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ContractController.php';
        (new ContractController())->index();
        exit;
    } elseif ($page === 'quotations') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/QuotationController.php';
        (new QuotationController())->index();
        exit;
    } elseif ($page === 'interviews') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/InterviewController.php';
        $controller = new InterviewController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'update_status') $controller->updateStatus();
        elseif ($action === 'delete') $controller->delete();
        else $controller->index();
        exit;
    } elseif ($page === 'payments') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/PaymentController.php';
        (new PaymentController())->index();
        exit;
    } elseif ($page === 'lookups') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/LookupController.php';
        $controller = new LookupController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->store();
        elseif ($action === 'update') $controller->update();
        elseif ($action === 'toggle_status') $controller->toggleStatus();
        elseif ($action === 'delete') $controller->delete();
        else $controller->index();
        exit;
    } elseif ($page === 'vendor_documents') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/VendorDocumentController.php';
        $controller = new VendorDocumentController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'approve') $controller->approve();
        elseif ($action === 'reject') $controller->reject();
        elseif ($action === 'update_expiry') $controller->updateExpiry();
        else $controller->index();
        exit;
    } elseif ($page === 'maid_documents') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/MaidDocumentController.php';
        $controller = new MaidDocumentController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'approve') $controller->approve();
        elseif ($action === 'reject') $controller->reject();
        else $controller->index();
        exit;
    } elseif ($page === 'subscription_plans') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/SubscriptionController.php';
        $controller = new SubscriptionController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->storePlan();
        elseif ($action === 'update') $controller->updatePlan();
        elseif ($action === 'delete') $controller->deletePlan();
        else $controller->plans();
        exit;
    } elseif ($page === 'vendor_subscriptions') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/SubscriptionController.php';
        (new SubscriptionController())->vendors();
        exit;
    } elseif ($page === 'ad_packages') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/AdController.php';
        $controller = new AdController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->storePackage();
        elseif ($action === 'update') $controller->updatePackage();
        elseif ($action === 'delete') $controller->deletePackage();
        else $controller->packages();
        exit;
    } elseif ($page === 'vendor_ads') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/AdController.php';
        (new AdController())->vendors();
        exit;
    } elseif ($page === 'quotation_requests') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/QuotationRequestController.php';
        (new QuotationRequestController())->index();
        exit;
    } elseif ($page === 'reviews') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ReviewController.php';
        (new ReviewController())->index();
        exit;
    } elseif ($page === 'reports') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ReportController.php';
        $controller = new ReportController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'export_payments') $controller->exportPayments();
        elseif ($action === 'export_registrations') $controller->exportRegistrations();
        else $controller->index();
        exit;
    } elseif ($page === 'sliders') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/CmsController.php';
        $controller = new CmsController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->storeSlider();
        elseif ($action === 'update') $controller->updateSlider();
        elseif ($action === 'delete') $controller->deleteSlider();
        else $controller->sliders();
        exit;
    } elseif ($page === 'snippets') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/CmsController.php';
        $controller = new CmsController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->storeSnippet();
        elseif ($action === 'update') $controller->updateSnippet();
        elseif ($action === 'delete') $controller->deleteSnippet();
        else $controller->snippets();
        exit;
    } elseif ($page === 'featured') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/FeaturedController.php';
        $controller = new FeaturedController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'add_vendor') $controller->addVendor();
        elseif ($action === 'remove_vendor') $controller->removeVendor();
        elseif ($action === 'add_maid') $controller->addMaid();
        elseif ($action === 'remove_maid') $controller->removeMaid();
        else $controller->index();
        exit;
    } elseif ($page === 'profile') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ProfileController.php';
        $controller = new ProfileController();
        if (($_GET['action'] ?? '') === 'update') $controller->update();
        else $controller->index();
        exit;
    } elseif ($page === 'change_password') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ProfileController.php';
        $controller = new ProfileController();
        if (($_GET['action'] ?? '') === 'update') $controller->updatePassword();
        else $controller->changePassword();
        exit;
    } elseif ($page === 'faqs') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/CmsController.php';
        $controller = new CmsController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'store') $controller->storeFaq();
        elseif ($action === 'update') $controller->updateFaq();
        elseif ($action === 'delete') $controller->deleteFaq();
        elseif ($action === 'store_category') $controller->storeFaqCategory();
        elseif ($action === 'update_category') $controller->updateFaqCategory();
        elseif ($action === 'delete_category') $controller->deleteFaqCategory();
        else $controller->faqs();
        exit;
    } elseif ($page === 'notifications') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/NotificationController.php';
        (new NotificationController())->index();
        exit;
    } elseif ($page === 'support') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/SupportController.php';
        (new SupportController())->index();
        exit;
    } elseif ($page === 'logs') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/SystemLogController.php';
        (new SystemLogController())->index();
        exit;
    } elseif ($page === 'settings') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/SettingController.php';
        $controller = new SettingController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'update_settings') $controller->updateSettings();
        elseif ($action === 'update_profile') $controller->updateProfile();
        elseif ($action === 'update_password') $controller->updatePassword();
        else $controller->index();
        exit;
    } elseif ($page === 'reports') {
        require_once __DIR__ . '/../app/Controllers/Web/Admin/ReportController.php';
        $controller = new ReportController();
        $action = $_GET['action'] ?? 'index';
        if ($action === 'export_payments') $controller->exportPayments();
        elseif ($action === 'export_registrations') $controller->exportRegistrations();
        else $controller->index();
    }
}

// Simple API routing
if (strpos($path, '/api/customer/') === 0) {
    header('Content-Type: application/json');
    $method = $_SERVER['REQUEST_METHOD'];
    
    if ($path === '/api/customer/register' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->register();
    } elseif ($path === '/api/customer/login' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->login();
    } elseif ($path === '/api/customer/profile' && $method === 'GET') {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        AuthMiddleware::authenticate('customer');
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->profile();
    } elseif (strpos($path, '/api/customer/interviews') === 0) {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        AuthMiddleware::authenticate('customer');
        require_once __DIR__ . '/../app/Controllers/Api/Customer/InterviewController.php';
        $controller = new InterviewController();
        if ($path === '/api/customer/interviews/create' && $method === 'POST') {
            $controller->create();
        } elseif ($path === '/api/customer/interviews' && $method === 'GET') {
            $controller->index();
        } elseif (preg_match('#^/api/customer/interviews/(\d+)$#', $path, $matches) && $method === 'GET') {
            $controller->show($matches[1]);
        } elseif (preg_match('#^/api/customer/interviews/(\d+)/reschedule$#', $path, $matches) && $method === 'POST') {
            $controller->reschedule($matches[1]);
        } elseif (preg_match('#^/api/customer/interviews/(\d+)/cancel$#', $path, $matches) && $method === 'POST') {
            $controller->cancel($matches[1]);
        }
    } elseif (preg_match('#^/api/customer/maids/(\d+)/availability$#', $path, $matches) && $method === 'GET') {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        AuthMiddleware::authenticate('customer');
        require_once __DIR__ . '/../app/Controllers/Api/Customer/InterviewController.php';
        $controller = new InterviewController();
        $controller->getMaidAvailability($matches[1]);
    } elseif ($path === '/api/customer/forgot-password' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->forgotPassword();
    } elseif ($path === '/api/customer/reset-password' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->resetPassword();
    } elseif ($path === '/api/customer/change-password' && $method === 'POST') {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->changePassword();
    } elseif ($path === '/api/customer/profile/update' && $method === 'POST') {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        require_once __DIR__ . '/../app/Controllers/Api/Customer/AuthController.php';
        (new AuthController())->updateProfile();
    } elseif ($path === '/api/customer/notification-settings' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/NotificationSettingController.php';
        (new NotificationSettingController())->get();
    } elseif ($path === '/api/customer/notification-settings/update' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/NotificationSettingController.php';
        (new NotificationSettingController())->update();
    } elseif ($path === '/api/customer/home' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/HomeController.php';
        (new HomeController())->index();
    } elseif ($path === '/api/customer/search' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/MarketplaceController.php';
        (new MarketplaceController())->search();
    } elseif ($path === '/api/customer/vendors' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/MarketplaceController.php';
        (new MarketplaceController())->listVendors();
    } elseif (preg_match('#^/api/customer/vendors/(\d+)$#', $path, $matches) && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/MarketplaceController.php';
        (new MarketplaceController())->getVendor($matches[1]);
    } elseif (preg_match('#^/api/customer/vendors/(\d+)/interview-settings$#', $path, $matches) && $method === 'GET') {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        AuthMiddleware::authenticate('customer');
        require_once __DIR__ . '/../app/Controllers/Api/Customer/InterviewController.php';
        $controller = new InterviewController();
        $controller->getVendorInterviewSettings($matches[1]);
    } elseif (preg_match('#^/api/customer/maids/(\d+)$#', $path, $matches) && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/MarketplaceController.php';
        (new MarketplaceController())->getMaid($matches[1]);
    } elseif ($path === '/api/customer/maids' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/MarketplaceController.php';
        (new MarketplaceController())->listMaids();
    } elseif ($path === '/api/customer/favorites/maids') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/FavoritesController.php';
        $controller = new FavoritesController();
        if ($method === 'GET') {
            $controller->getMaids();
        } elseif ($method === 'POST') {
            $controller->toggleMaid();
        }
    } elseif ($path === '/api/customer/favorites/vendors') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/FavoritesController.php';
        $controller = new FavoritesController();
        if ($method === 'GET') {
            $controller->getVendors();
        } elseif ($method === 'POST') {
            $controller->toggleVendor();
        }
    } elseif (strpos($path, '/api/customer/quotations') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/QuotationController.php';
        $controller = new QuotationController();
        if ($path === '/api/customer/quotations' && $method === 'GET') {
            $controller->index();
        } elseif (preg_match('#^/api/customer/quotations/(\d+)/action$#', $path, $matches) && $method === 'POST') {
            $controller->action($matches[1]);
        } elseif (preg_match('#^/api/customer/quotations/(\d+)/sign$#', $path, $matches) && $method === 'POST') {
            $controller->sign($matches[1]);
        } elseif (preg_match('#^/api/customer/quotations/(\d+)$#', $path, $matches) && $method === 'GET') {
            $controller->show($matches[1]);
        }
    } elseif (strpos($path, '/api/customer/quotation-requests') === 0) {
        require_once __DIR__ . '/../app/Middleware/AuthMiddleware.php';
        AuthMiddleware::authenticate('customer');
        require_once __DIR__ . '/../app/Controllers/Api/Customer/QuotationRequestController.php';
        $controller = new QuotationRequestController();
        if ($path === '/api/customer/quotation-requests/create' && $method === 'POST') {
            $controller->create();
        } elseif ($path === '/api/customer/quotation-requests/specific' && $method === 'GET') {
            $controller->indexSpecific();
        } elseif ($path === '/api/customer/quotation-requests/general' && $method === 'GET') {
            $controller->indexGeneral();
        } elseif ($path === '/api/customer/quotation-requests' && $method === 'GET') {
            $controller->index();
        } elseif (preg_match('#^/api/customer/quotation-requests/(\d+)$#', $path, $matches) && $method === 'GET') {
            $controller->show($matches[1]);
        } elseif (preg_match('#^/api/customer/quotation-requests/(\d+)/cancel$#', $path, $matches) && $method === 'POST') {
            $controller->cancel($matches[1]);
        }
    } elseif (strpos($path, '/api/customer/contracts') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/ContractController.php';
        $controller = new ContractController();
        if ($path === '/api/customer/contracts' && $method === 'GET') {
            $controller->index();
        } elseif ($path === '/api/customer/contracts/create' && $method === 'POST') {
            $controller->create();
        } elseif (preg_match('#^/api/customer/contracts/(\d+)$#', $path, $matches) && $method === 'GET') {
            $controller->show($matches[1]);
        } elseif (preg_match('#^/api/customer/contracts/(\d+)/sign$#', $path, $matches) && $method === 'POST') {
            $controller->sign($matches[1]);
        } elseif (preg_match('#^/api/customer/contracts/(\d+)/cancel$#', $path, $matches) && $method === 'POST') {
            $controller->cancel($matches[1]);
        }
    } elseif (strpos($path, '/api/customer/chat/rooms') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/ChatController.php';
        $controller = new ChatController();
        if ($path === '/api/customer/chat/rooms' && $method === 'GET') {
            $controller->rooms();
        } elseif ($path === '/api/customer/chat/rooms/create' && $method === 'POST') {
            $controller->createRoom();
        }
    } elseif (strpos($path, '/api/customer/notifications') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/NotificationController.php';
        $controller = new NotificationController();
        if ($path === '/api/customer/notifications' && $method === 'GET') {
            $controller->index();
        } elseif ($path === '/api/customer/notifications/read-all' && $method === 'POST') {
            $controller->readAll();
        } elseif (preg_match('#^/api/customer/notifications/(\d+)/read$#', $path, $matches) && $method === 'POST') {
            $controller->read($matches[1]);
        }
    } elseif ($path === '/api/customer/reviews' && $method === 'POST') {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/ReviewController.php';
        (new ReviewController())->create();
    } elseif (strpos($path, '/api/customer/support/tickets') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Customer/SupportController.php';
        $controller = new SupportController();
        if ($path === '/api/customer/support/tickets' && $method === 'GET') {
            $controller->index();
        } elseif ($path === '/api/customer/support/tickets' && $method === 'POST') {
            $controller->create();
        }
    }
} elseif (strpos($path, '/api/vendor/') === 0) {
    header('Content-Type: application/json');
    $method = $_SERVER['REQUEST_METHOD'];
    
    // Auth & Settings & Dashboard
    if (strpos($path, '/api/vendor/auth/') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/AuthController.php';
        $controller = new AuthController();
        if ($path === '/api/vendor/auth/register') $controller->register();
        elseif ($path === '/api/vendor/auth/login') $controller->login();
        elseif ($path === '/api/vendor/auth/profile') $controller->profile();
        elseif ($path === '/api/vendor/auth/profile/update') $controller->updateProfile();
        elseif ($path === '/api/vendor/auth/forgot-password') $controller->forgotPassword();
        elseif ($path === '/api/vendor/auth/reset-password') $controller->resetPassword();
        elseif ($path === '/api/vendor/auth/change-password') $controller->changePassword();
        exit;
    } elseif ($path === '/api/vendor/dashboard/stats' && $method === 'GET') {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/AuthController.php';
        (new AuthController())->dashboardStats();
        exit;
    }
    
    // Maids & Gallery & Documents
    elseif (strpos($path, '/api/vendor/maids') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/MaidController.php';
        $controller = new MaidController();
        if ($path === '/api/vendor/maids') {
            if ($method === 'GET') $controller->index();
            elseif ($method === 'POST') $controller->store();
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/gallery/', $path, $m) && $method === 'POST') {
            $controller->uploadGallery($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/gallery\/(\d+)/', $path, $m) && $method === 'DELETE') {
            $controller->deleteGalleryImage($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/documents/', $path, $m) && $method === 'POST') {
            $controller->uploadDocument($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/experience\/(\d+)/', $path, $m)) {
            if ($method === 'PUT' || $method === 'POST') $controller->updateExperience($m[1], $m[2]);
            elseif ($method === 'DELETE') $controller->deleteExperience($m[1], $m[2]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/experience/', $path, $m) && $method === 'POST') {
            $controller->addExperience($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/status/', $path, $m) && $method === 'POST') {
            $controller->updateStatus($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/photo/', $path, $m) && $method === 'POST') {
            $controller->uploadPhoto($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/languages/', $path, $m) && $method === 'POST') {
            $controller->syncLanguages($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/specialties/', $path, $m) && $method === 'POST') {
            $controller->syncSpecialties($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)\/skills/', $path, $m) && $method === 'POST') {
            $controller->syncSkills($m[1]);
        } elseif (preg_match('/\/api\/vendor\/maids\/(\d+)/', $path, $m)) {
            if ($method === 'GET') $controller->show($m[1]);
            elseif ($method === 'PUT' || $method === 'POST') $controller->update($m[1]);
            elseif ($method === 'DELETE') $controller->delete($m[1]);
        }
        exit;
    } 
    
    // Business, Inquiries, Quotations, Contracts, Payments, and Subscriptions
    elseif (strpos($path, '/api/vendor/business/') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/BusinessController.php';
        $controller = new BusinessController();
        if ($path === '/api/vendor/business/requests' && $method === 'GET') {
            $controller->quotationRequests();
        } elseif (preg_match('/\/api\/vendor\/business\/requests\/(\d+)/', $path, $m) && $method === 'GET') {
            $controller->showQuotationRequest($m[1]);
        } elseif (preg_match('/\/api\/vendor\/business\/requests\/(\d+)\/status/', $path, $m) && $method === 'POST') {
            $controller->updateRequestStatus($m[1]);
        } elseif ($path === '/api/vendor/business/quotations') {
            if ($method === 'GET') $controller->listQuotations();
            elseif ($method === 'POST') $controller->createQuotation();
        } elseif (preg_match('/\/api\/vendor\/business\/quotations\/(\d+)/', $path, $m) && $method === 'GET') {
            $controller->showQuotation($m[1]);
        } elseif (preg_match('/\/api\/vendor\/business\/quotations\/(\d+)\/send/', $path, $m) && $method === 'POST') {
            $controller->sendQuotation($m[1]);
        } elseif (preg_match('/\/api\/vendor\/business\/quotations\/(\d+)\/cancel/', $path, $m) && $method === 'POST') {
            $controller->cancelQuotation($m[1]);
        } elseif ($path === '/api/vendor/business/contracts' && $method === 'GET') {
            $controller->contracts();
        } elseif (preg_match('/\/api\/vendor\/business\/contracts\/(\d+)\/status/', $path, $m) && $method === 'POST') {
            $controller->updateContractStatus($m[1]);
        } elseif ($path === '/api/vendor/business/payments' && $method === 'GET') {
            $controller->payments();
        } elseif (preg_match('/\/api\/vendor\/business\/payments\/(\d+)\/invoice/', $path, $m) && $method === 'GET') {
            $controller->downloadInvoice($m[1]);
        } elseif ($path === '/api/vendor/business/subscription') {
            if ($method === 'GET') $controller->subscription();
            elseif ($method === 'POST' && strpos($requestUri, '/cancel') !== false) $controller->cancelSubscription();
        } elseif ($path === '/api/vendor/business/subscription/toggle-auto-renew' && $method === 'POST') {
            $controller->toggleAutoRenew();
        } elseif ($path === '/api/vendor/business/quota' && $method === 'GET') {
            $controller->quota();
        } elseif ($path === '/api/vendor/business/payment-methods') {
            if ($method === 'GET') $controller->getPaymentMethods();
            elseif ($method === 'POST') $controller->addPaymentMethod();
        } elseif (preg_match('/\/api\/vendor\/business\/payment-methods\/(\d+)/', $path, $m) && $method === 'DELETE') {
            $controller->deletePaymentMethod($m[1]);
        } elseif ($path === '/api/vendor/business/billing-addresses') {
            if ($method === 'GET') $controller->getBillingAddresses();
            elseif ($method === 'POST') $controller->addBillingAddress();
        } elseif (preg_match('/\/api\/vendor\/business\/billing-addresses\/(\d+)/', $path, $m)) {
            if ($method === 'PUT' || $method === 'POST') $controller->updateBillingAddress($m[1]);
            elseif ($method === 'DELETE') $controller->deleteBillingAddress($m[1]);
        }
        exit;
    }
    
    // Interviews Scheduler & Management
    elseif (strpos($path, '/api/vendor/interviews') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/InterviewController.php';
        $controller = new InterviewController();
        if ($path === '/api/vendor/interviews' && $method === 'GET') {
            $controller->index();
        } elseif ($path === '/api/vendor/interviews/availability') {
            if ($method === 'GET') $controller->getAvailability();
            elseif ($method === 'POST') $controller->saveAvailability();
        } elseif ($path === '/api/vendor/interviews/settings') {
            if ($method === 'GET') $controller->getSettings();
            elseif ($method === 'POST') $controller->saveSettings();
        } elseif (preg_match('/\\/api\\/vendor\\/interviews\\/(\\d+)\\/availability/', $path, $m) && $method === 'GET') {
            $controller->getInterviewAvailability($m[1]);
        } elseif (preg_match('/\\/api\\/vendor\\/interviews\\/(\\d+)\\/status/', $path, $m) && $method === 'POST') {
            $controller->updateStatus($m[1]);
        } elseif (preg_match('/\\/api\\/vendor\\/interviews\\/(\\d+)\\/reschedule/', $path, $m) && $method === 'POST') {
            $controller->reschedule($m[1]);
        } elseif (preg_match('/\\/api\\/vendor\\/interviews\\/(\\d+)/', $path, $m) && $method === 'GET') {
            $controller->show($m[1]);
        }
        exit;
    }
    
    // Chat references
    elseif (strpos($path, '/api/vendor/chat/') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/ChatController.php';
        $controller = new ChatController();
        if ($path === '/api/vendor/chat/rooms') {
            if ($method === 'GET') $controller->rooms();
            elseif ($method === 'POST') $controller->createRoom();
        }
        exit;
    }
    
    // Notifications ledger
    elseif (strpos($path, '/api/vendor/notifications') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/NotificationController.php';
        $controller = new NotificationController();
        if ($path === '/api/vendor/notifications' && $method === 'GET') {
            $controller->index();
        } elseif ($path === '/api/vendor/notifications/read-all' && $method === 'POST') {
            $controller->readAll();
        } elseif ($path === '/api/vendor/notifications/settings') {
            if ($method === 'GET') $controller->getSettings();
            elseif ($method === 'POST') $controller->updateSettings();
        } elseif (preg_match('/\/api\/vendor\/notifications\/(\d+)\/read/', $path, $m) && $method === 'POST') {
            $controller->read($m[1]);
        }
        exit;
    }
    
    // Support Tickets
    elseif (strpos($path, '/api/vendor/support/') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/SupportController.php';
        $controller = new SupportController();
        if ($path === '/api/vendor/support/tickets') {
            if ($method === 'GET') $controller->listTickets();
            elseif ($method === 'POST') $controller->createTicket();
        }
        exit;
    }
    
    // Basic trade documents (already exists)
    elseif (strpos($path, '/api/vendor/documents') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/DocumentController.php';
        $controller = new DocumentController();
        if ($method === 'GET') $controller->index();
        elseif ($method === 'POST') $controller->upload();
        exit;
    }
    
    // Marketing & Promoted Ads (already exists)
    elseif (strpos($path, '/api/vendor/marketing/') === 0) {
        require_once __DIR__ . '/../app/Controllers/Api/Vendor/MarketingController.php';
        $controller = new MarketingController();
        if ($path === '/api/vendor/marketing/packages') $controller->packages();
        elseif ($path === '/api/vendor/marketing/ads') $controller->ads();
        elseif ($path === '/api/vendor/marketing/purchase') $controller->purchaseAd();
        exit;
    }
} elseif (strpos($path, '/api/faqs') === 0 && $method === 'GET') {
    header('Content-Type: application/json');
    require_once __DIR__ . '/../app/Controllers/Api/FaqController.php';
    $controller = new FaqController();
    if ($path === '/api/faqs') {
        $controller->index();
    } elseif ($path === '/api/faqs/categories') {
        $controller->categories();
    } elseif (preg_match('/\/api\/faqs\/categories\/(\d+)/', $path, $m)) {
        $controller->categoryFaqs($m[1]);
    } else {
        sendError('Route Not Found', ['path' => $path], 404);
    }
    exit;
} elseif (strpos($path, '/api/lookups/') === 0) {
    header('Content-Type: application/json');
    $method = $_SERVER['REQUEST_METHOD'];
    
    require_once __DIR__ . '/../app/Controllers/Api/LookupController.php';
    $controller = new LookupController();
    
    if ($path === '/api/lookups/countries' && $method === 'GET') {
        $controller->countries();
    } elseif ($path === '/api/lookups/cities' && $method === 'GET') {
        $controller->cities();
    } elseif ($path === '/api/lookups/nationalities' && $method === 'GET') {
        $controller->nationalities();
    } elseif ($path === '/api/lookups/languages' && $method === 'GET') {
        $controller->languages();
    } elseif ($path === '/api/lookups/specialties' && $method === 'GET') {
        $controller->specialties();
    } elseif ($path === '/api/lookups/skills' && $method === 'GET') {
        $controller->skills();
    } elseif ($path === '/api/lookups/accommodation-types' && $method === 'GET') {
        $controller->accommodationTypes();
    } elseif ($path === '/api/lookups/service-models' && $method === 'GET') {
        $controller->serviceModels();
    } elseif ($path === '/api/lookups/document-types' && $method === 'GET') {
        $controller->documentTypes();
    }
}

// Default 404
http_response_code(404);
jsonResponse(false, 'Route Not Found', ['path' => $path], null, 404);
