<?php
/**
 * SalonPro X - Enterprise Salon ERP + CRM + POS + Appointment Management System
 * Front Controller - Entry point for all requests
 * 
 * @package SalonProX
 * @version 2.0.0
 */

// Define base path constant
define('BASE_PATH', __DIR__);
define('DS', DIRECTORY_SEPARATOR);

// Load autoloader
require_once BASE_PATH . DS . 'autoload.php';

// Load configuration
require_once BASE_PATH . DS . 'config' . DS . 'app.php';
require_once BASE_PATH . DS . 'config' . DS . 'database.php';
require_once BASE_PATH . DS . 'config' . DS . 'paths.php';
require_once BASE_PATH . DS . 'config' . DS . 'permissions.php';

// Load helpers
require_once BASE_PATH . DS . 'helpers' . DS . 'functions.php';

// Start session
\core\Session::start();

// Initialize the application
try {
    $app = new \core\App();
    $app->run();
} catch (\Throwable $e) {
    // Log the error
    $errorMessage = date('Y-m-d H:i:s') . ' - ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
    file_put_contents(BASE_PATH . DS . 'logs' . DS . 'error.log', $errorMessage, FILE_APPEND);
    
    // Display error page
    $errorCode = is_int($e->getCode()) ? $e->getCode() : 500;
    http_response_code($errorCode);
    
    if (defined('APP_DEBUG') && APP_DEBUG) {
        echo '<h1>Error ' . $errorCode . '</h1>';
        echo '<p>' . $e->getMessage() . '</p>';
        echo '<pre>' . $e->getTraceAsString() . '</pre>';
    } else {
        include BASE_PATH . DS . 'templates' . DS . 'layouts' . DS . 'error.php';
    }
}
