<?php
/**
 * Everest Büro Makinaları - Front Controller Entry Point
 * Sets up custom PSR-4 autoloader, resolves tenant based on HTTP_HOST,
 * initializes services, defines routes, and dispatches requests.
 */

// Load Composer Autoloader
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

// 1. Simple PSR-4 Autoloader
spl_autoload_register(function ($class) {
    // Project-specific namespace prefix
    $prefix = 'App\\';
    
    // Base directory for the namespace prefix
    $base_dir = __DIR__ . '/../src/';
    
    // Does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // No, move to the next registered autoloader
        return;
    }
    
    // Get the relative class name
    $relative_class = substr($class, $len);
    
    // Replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    
    // If the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

// 2. Resolve Multi-tenant Site
use App\Models\Tenant;

$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'everestburo.com';
$tenant = Tenant::findByHost($host);
$tenantId = $tenant ? $tenant['id'] : 'everestburo';

// Define globally accessible constant
define('TENANT_ID', $tenantId);

// 3. Initialize Global Router
$router = new \App\Core\Router();
$GLOBALS['router'] = $router;
$baseUrl = $router->getBaseUrl();

// 4. Initialize Core Services
\App\Services\TranslationService::init();
\App\Services\CurrencyService::init();

// 5. Register System Routes
// Client-side routes
$router->add(['GET'], '/', 'ClientController@home');
$router->add(['GET'], '/search', 'ClientController@search');
$router->add(['GET'], '/contact', 'ClientController@contact');
$router->add(['GET'], '/markalar', 'ClientController@brandsPage');
$router->add(['GET'], '/product/manufacturer', 'ClientController@brandsPage');

// Client-side authentication routes
$router->add(['GET'], '/login', 'AuthController@login');
$router->add(['GET'], '/logout', 'AuthController@logout');
$router->add(['GET'], '/profile', 'AuthController@profile');
$router->add(['POST'], '/profile/update', 'AuthController@profileUpdate');
$router->add(['GET'], '/profile/orders', 'AuthController@orders');
$router->add(['GET'], '/profile/orders/{id}', 'AuthController@orderDetail');
$router->add(['POST'], '/profile/orders/{id}/receipt', 'AuthController@uploadReceipt');
$router->add(['GET'], '/auth/google', 'AuthController@googleRedirect');
$router->add(['GET'], '/auth/google/callback', 'AuthController@googleCallback');

// Cart & Checkout
$router->add(['GET'], '/cart', 'CartController@view');
$router->add(['GET'], '/checkout', 'CartController@checkout');
$router->add(['POST'], '/checkout/submit', 'CartController@submitOrder');

// Cart AJAX APIs
$router->add(['POST'], '/api/cart/add', 'CartController@add');
$router->add(['POST'], '/api/cart/update', 'CartController@update');

// Admin panel authentication
$router->add(['GET'], '/admin/login', 'AdminController@login');
$router->add(['POST'], '/admin/auth', 'AdminController@auth');
$router->add(['GET'], '/admin/logout', 'AdminController@logout');

// Admin panel dashboard & operations
$router->add(['GET'], '/admin', 'AdminController@dashboard');
$router->add(['GET'], '/admin/dashboard', 'AdminController@dashboard');
$router->add(['GET'], '/admin/products', 'AdminController@products');
$router->add(['GET'], '/admin/products/create', 'AdminController@productCreate');
$router->add(['POST'], '/admin/products/store', 'AdminController@productStore');
$router->add(['GET'], '/admin/products/edit/{id}', 'AdminController@productEdit');
$router->add(['POST'], '/admin/products/update/{id}', 'AdminController@productUpdate');
$router->add(['POST'], '/admin/products/update-field/{id}', 'AdminController@productUpdateFieldAjax');
$router->add(['POST'], '/admin/products/toggle-featured/{id}', 'AdminController@productToggleFeatured');
$router->add(['POST'], '/admin/products/bulk-action', 'AdminController@productBulkAction');
$router->add(['GET'], '/admin/products/delete/{id}', 'AdminController@productDelete');

$router->add(['GET'], '/admin/categories', 'AdminController@categories');
$router->add(['GET'], '/admin/categories/create', 'AdminController@categoryCreate');
$router->add(['POST'], '/admin/categories/store', 'AdminController@categoryStore');
$router->add(['GET'], '/admin/categories/edit/{id}', 'AdminController@categoryEdit');
$router->add(['POST'], '/admin/categories/update/{id}', 'AdminController@categoryUpdate');
$router->add(['GET'], '/admin/categories/delete/{id}', 'AdminController@categoryDelete');
$router->add(['POST'], '/admin/categories/reorder', 'AdminController@categoriesReorder');

$router->add(['GET'], '/admin/orders', 'AdminController@orders');
$router->add(['POST'], '/admin/orders/update-status/{id}', 'AdminController@orderUpdateStatus');
$router->add(['GET'], '/admin/orders/delete/{id}', 'AdminController@orderDelete');

$router->add(['GET'], '/admin/settings', 'AdminController@settings');
$router->add(['POST'], '/admin/settings/update', 'AdminController@settingsUpdate');

$router->add(['GET'], '/admin/database', 'AdminController@database');

$router->add(['GET'], '/admin/brands', 'AdminController@brands');
$router->add(['POST'], '/admin/brands/store', 'AdminController@brandStore');
$router->add(['POST'], '/admin/brands/update/{id}', 'AdminController@brandUpdate');
$router->add(['GET'], '/admin/brands/delete/{slug}', 'AdminController@brandDelete');

$router->add(['GET'], '/admin/theme', 'AdminController@theme');
$router->add(['POST'], '/admin/theme/update', 'AdminController@themeUpdate');

$router->add(['GET'], '/admin/pages', 'AdminController@pages');
$router->add(['GET'], '/admin/pages/create', 'AdminController@pageCreate');
$router->add(['POST'], '/admin/pages/store', 'AdminController@pageStore');
$router->add(['GET'], '/admin/pages/edit/{id}', 'AdminController@pageEdit');
$router->add(['POST'], '/admin/pages/update/{id}', 'AdminController@pageUpdate');
$router->add(['GET'], '/admin/pages/delete/{id}', 'AdminController@pageDelete');

$router->add(['GET'], '/admin/users', 'AdminController@users');
$router->add(['POST'], '/admin/users/update-role/{id}', 'AdminController@userUpdateRole');
$router->add(['GET'], '/admin/users/toggle-status/{id}', 'AdminController@userToggleStatus');

$router->add(['GET'], '/admin/orders/edit/{id}', 'AdminController@orderEdit');
$router->add(['POST'], '/admin/orders/update/{id}', 'AdminController@orderUpdate');

// Super Admin panel
$router->add(['GET'], '/superadmin/login', 'SuperAdminController@login');
$router->add(['POST'], '/superadmin/auth', 'SuperAdminController@auth');
$router->add(['GET'], '/superadmin/logout', 'SuperAdminController@logout');

$router->add(['GET'], '/superadmin/dashboard', 'SuperAdminController@dashboard');
$router->add(['GET'], '/superadmin/tenants/create', 'SuperAdminController@tenantCreate');
$router->add(['POST'], '/superadmin/tenants/store', 'SuperAdminController@tenantStore');
$router->add(['GET'], '/superadmin/tenants/delete/{id}', 'SuperAdminController@tenantDelete');

// 6. Dispatch and Output
$router->dispatch();
