<?php
// Application configuration
define('APP_NAME', 'UpTours - Gestion Hajj & Omra');
define('APP_VERSION', '1.0.0');
define('APP_URL', '/uptours');
define('APP_ROOT', dirname(__DIR__));
define('UPLOAD_DIR', APP_ROOT . '/uploads/');
define('DOCUMENTS_DIR', APP_ROOT . '/uploads/documents/');
define('PHOTOS_DIR', APP_ROOT . '/uploads/photos/');

// Session configuration
ini_set('session.cookie_httponly', 1);
ini_set('session.use_strict_mode', 1);
session_start();

// Error reporting
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

// Timezone
date_default_timezone_set('Africa/Casablanca');

// Prevent caching for dynamic pages
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// Number to words in French (Dirhams)
function numberToWords($number) {
    $hyphen = '-';
    $conjunction = ' et ';
    $separator = ' ';
    $negative = 'moins ';
    $decimal = ' virgule ';
    $dictionary = array(
        0 => 'zéro', 1 => 'un', 2 => 'deux', 3 => 'trois', 4 => 'quatre', 5 => 'cinq',
        6 => 'six', 7 => 'sept', 8 => 'huit', 9 => 'neuf', 10 => 'dix', 11 => 'onze',
        12 => 'douze', 13 => 'treize', 14 => 'quatorze', 15 => 'quinze', 16 => 'seize',
        17 => 'dix-sept', 18 => 'dix-huit', 19 => 'dix-neuf', 20 => 'vingt', 30 => 'trente',
        40 => 'quarante', 50 => 'cinquante', 60 => 'soixante', 70 => 'soixante-dix',
        80 => 'quatre-vingts', 90 => 'quatre-vingt-dix', 100 => 'cent', 1000 => 'mille',
        1000000 => 'million', 1000000000 => 'milliard', 1000000000000 => 'billion'
    );

    if (!is_numeric($number)) return false;
    $number = floatval($number);
    if ($number < 0) return $negative . numberToWords(abs($number));
    
    $string = $fraction = null;
    if (strpos((string)$number, '.') !== false) {
        list($number, $fraction) = explode('.', (string)$number);
    }
    $number = (int)$number;

    switch (true) {
        case $number < 21:
            $string = $dictionary[$number];
            break;
        case $number < 100:
            $tens = ((int)($number / 10)) * 10;
            $units = $number % 10;
            $string = $dictionary[$tens];
            if ($units) {
                if ($tens == 70 || $tens == 90) {
                    $string = $dictionary[$tens - 10] . $hyphen . $dictionary[$units + 10];
                } elseif ($tens == 80) {
                    $string = 'quatre-vingt-' . $dictionary[$units];
                } else {
                    $string .= ($units == 1 ? ' et ' : $hyphen) . $dictionary[$units];
                }
            } elseif ($tens == 80) {
                $string = 'quatre-vingts';
            }
            break;
        case $number < 1000:
            $hundreds = floor($number / 100);
            $remainder = $number % 100;
            if ($hundreds == 1) {
                $string = 'cent';
            } else {
                $string = $dictionary[$hundreds] . ' cent' . ($remainder == 0 ? 's' : '');
            }
            if ($remainder) $string .= ' ' . numberToWords($remainder);
            break;
        default:
            $baseUnit = pow(1000, floor(log($number, 1000)));
            $numBaseUnits = (int)($number / $baseUnit);
            $remainder = $number % $baseUnit;
            if ($baseUnit == 1000 && $numBaseUnits == 1) {
                $string = 'mille';
            } else {
                $string = numberToWords($numBaseUnits) . ' ' . $dictionary[$baseUnit];
                // millions and milliards take 's' when plural
                if ($baseUnit >= 1000000 && $numBaseUnits > 1) $string .= 's';
            }
            if ($remainder) $string .= ' ' . numberToWords($remainder);
            break;
    }
    
    return $string;
}

// Create upload directories
$dirs = [UPLOAD_DIR, DOCUMENTS_DIR, PHOTOS_DIR];
foreach ($dirs as $dir) {
    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
    }
}

// Autoload helpers
require_once __DIR__ . '/database.php';
require_once APP_ROOT . '/includes/functions.php';
require_once APP_ROOT . '/includes/auth.php';

// Start output buffering to prevent header errors
ob_start();
