<?php
/**
 * LandSales Pro - Application Configuration
 *
 * This file contains all application-wide configuration settings.
 */

// Prevent direct access
if (!defined('APP_RUNNING')) {
    define('APP_RUNNING', true);
}

// ============================================
// ENVIRONMENT DETECTION
// ============================================
$is_production = (
    isset($_SERVER['HTTP_HOST']) &&
    (strpos($_SERVER['HTTP_HOST'], 'landsales.co.tz') !== false ||
     strpos($_SERVER['HTTP_HOST'], 'landsalespro') !== false)
);

// Detect localhost
$is_localhost = (
    isset($_SERVER['HTTP_HOST']) &&
    (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false ||
     strpos($_SERVER['HTTP_HOST'], '127.0.0.1') !== false)
);

// Also check if running from command line with production flag
if (php_sapi_name() === 'cli' && getenv('APP_ENV') === 'production') {
    $is_production = true;
}

// ============================================
// DATABASE CONFIGURATION
// ============================================
if ($is_production) {
    // Production settings
    define('DB_HOST', 'localhost');
    define('DB_USER', 'pitechco_mylands');
    define('DB_PASS', 'RomilusK>%+66');
    define('DB_NAME', 'pitechco_landsales');

    // Production error handling
    error_reporting(0);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);
    ini_set('error_log', __DIR__ . '/logs/php_errors.log');
} else {
    // Local development settings (XAMPP)
    define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASS', '');
    define('DB_NAME', 'landsales');

    // Development error handling
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
}

// ============================================
// APPLICATION SETTINGS
// ============================================
define('APP_NAME', 'LandSales Pro');
define('APP_TAGLINE', 'Professional Land Sales Management');
define('APP_VERSION', '1.0.0');

// URLs
if ($is_production) {
    define('SITE_URL', 'https://landsales.co.tz');
    define('ASSETS_URL', 'https://landsales.co.tz/assets');
    define('BASE_PATH', '');
} else {
    // Detect if accessed via HTTPS
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
    define('SITE_URL', $protocol . '://localhost/landsales');
    define('ASSETS_URL', $protocol . '://localhost/landsales/assets');
    define('BASE_PATH', '/landsales');
}

// Environment flag
define('IS_PRODUCTION', $is_production);
define('IS_LOCALHOST', $is_localhost);

// ============================================
// PATHS
// ============================================
define('ROOT_PATH', __DIR__);
define('INCLUDES_PATH', ROOT_PATH . '/includes');
define('UPLOADS_PATH', ROOT_PATH . '/uploads');
define('ASSETS_PATH', ROOT_PATH . '/assets');

// ============================================
// TIMEZONE & LOCALE
// ============================================
define('APP_TIMEZONE', 'Africa/Dar_es_Salaam');
define('APP_LOCALE', 'en_TZ');
define('APP_CURRENCY', 'TZS');
define('APP_CURRENCY_SYMBOL', 'TZS');
define('APP_DATE_FORMAT', 'd M Y');
define('APP_DATETIME_FORMAT', 'd M Y H:i');

// Set timezone
date_default_timezone_set(APP_TIMEZONE);

// ============================================
// SESSION CONFIGURATION
// ============================================
define('SESSION_NAME', 'landsales_session');
define('SESSION_LIFETIME', 86400); // 24 hours
define('SESSION_SECURE', $is_production); // Use secure cookies in production
define('SESSION_HTTPONLY', true);
define('SESSION_SAMESITE', 'Lax');

// ============================================
// SECURITY SETTINGS
// ============================================
define('JWT_SECRET', 'your-super-secret-key-change-this-in-production-' . md5(__FILE__));
define('JWT_EXPIRY', 86400); // 24 hours
define('CSRF_TOKEN_NAME', 'csrf_token');
define('CSRF_TOKEN_LIFETIME', 3600); // 1 hour
define('PASSWORD_MIN_LENGTH', 6);
define('MAX_LOGIN_ATTEMPTS', 5);
define('LOGIN_LOCKOUT_MINUTES', 15);

// ============================================
// SUBSCRIPTION SETTINGS
// ============================================
define('TRIAL_DAYS', 14);
define('GRACE_PERIOD_DAYS', 7);

// Subscription prices (TZS)
define('PRICE_MONTHLY', 50000);
define('PRICE_QUARTERLY', 140000);
define('PRICE_SEMIANNUAL', 270000);
define('PRICE_YEARLY', 500000);

// ============================================
// BUSINESS SETTINGS
// ============================================
define('DEFAULT_INSTALLMENT_MONTHS', 12);
define('DEFAULT_DEPOSIT_PERCENT', 30);
define('LATE_PAYMENT_FEE_PERCENT', 5);

// ============================================
// FILE UPLOAD SETTINGS
// ============================================
define('MAX_UPLOAD_SIZE', 10 * 1024 * 1024); // 10MB
define('ALLOWED_IMAGE_TYPES', ['jpg', 'jpeg', 'png', 'gif', 'webp']);
define('ALLOWED_DOCUMENT_TYPES', ['pdf', 'doc', 'docx', 'xls', 'xlsx']);

// ============================================
// PAGINATION
// ============================================
define('ITEMS_PER_PAGE', 25);
define('MAX_ITEMS_PER_PAGE', 100);

// ============================================
// CONTACT INFORMATION
// ============================================
define('SUPPORT_EMAIL', 'info@landsecure.co.tz');
define('SUPPORT_PHONE', '+255 781 115 402');
define('WHATSAPP_NUMBER', '+255781115402');

// ============================================
// HELPER FUNCTION TO GET CONFIG VALUES
// ============================================
function config($key, $default = null) {
    $configs = [
        'app.name' => APP_NAME,
        'app.tagline' => APP_TAGLINE,
        'app.version' => APP_VERSION,
        'app.url' => SITE_URL,
        'app.timezone' => APP_TIMEZONE,
        'app.currency' => APP_CURRENCY,
        'app.currency_symbol' => APP_CURRENCY_SYMBOL,
        'app.date_format' => APP_DATE_FORMAT,
        'db.host' => DB_HOST,
        'db.name' => DB_NAME,
        'session.name' => SESSION_NAME,
        'session.lifetime' => SESSION_LIFETIME,
        'trial.days' => TRIAL_DAYS,
        'grace.days' => GRACE_PERIOD_DAYS,
        'upload.max_size' => MAX_UPLOAD_SIZE,
        'pagination.per_page' => ITEMS_PER_PAGE,
    ];

    return $configs[$key] ?? $default;
}
