<?php
/**
 * Çözüm Merkezi - Front Controller Entry Point
 */

// 1. Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 2. Class Autoloader
spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $base_dir = __DIR__ . '/../src/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    if (file_exists($file)) {
        require $file;
    }
});

// 3. Initialize core Router
$router = new \App\Core\Router();
$baseUrl = $router->getBaseUrl();

// 4. Define client-side routes
$router->add(['GET'], '/', 'ClientController@home');
$router->add(['GET'], '/search', 'ClientController@search');
$router->add(['GET'], '/api/search', 'ClientController@apiSearch');
$router->add(['GET'], '/contact', 'ClientController@contact');
$router->add(['GET'], '/compatibilities', 'ClientController@compatibilities');
$router->add(['GET'], '/brands', 'ClientController@brands');

// 5. Define admin auth routes
$router->add(['GET'], '/admin/logout', 'AuthController@logout');

// 6. Define admin layout routes
$router->add(['GET'], '/admin', 'AdminController@dashboard');
$router->add(['GET'], '/admin/dashboard', 'AdminController@dashboard');

// Articles CRUD
$router->add(['GET'], '/admin/articles', 'AdminController@articles');
$router->add(['GET'], '/admin/articles/create', 'AdminController@articleCreate');
$router->add(['POST'], '/admin/articles/store', 'AdminController@articleStore');
$router->add(['GET'], '/admin/articles/edit/{id}', 'AdminController@articleEdit');
$router->add(['POST'], '/admin/articles/update/{id}', 'AdminController@articleUpdate');
$router->add(['GET'], '/admin/articles/delete/{id}', 'AdminController@articleDelete');

// Categories CRUD
$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');

// Brands CRUD
$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/{id}', 'AdminController@brandDelete');

// Compatibility CRUD
$router->add(['GET'], '/admin/compatibility', 'AdminController@compatibility');
$router->add(['GET'], '/admin/compatibility/create', 'AdminController@compatibilityCreate');
$router->add(['POST'], '/admin/compatibility/store', 'AdminController@compatibilityStore');
$router->add(['GET'], '/admin/compatibility/edit/{slug}', 'AdminController@compatibilityEdit');
$router->add(['POST'], '/admin/compatibility/update/{slug}', 'AdminController@compatibilityUpdate');
$router->add(['GET'], '/admin/compatibility/delete/{slug}', 'AdminController@compatibilityDelete');

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

// 7. Dispatch route
$router->dispatch();
