<?php
/**
 * Database Configuration - PRODUCTION VERSION
 * Digital Marketplace
 * 
 * This file contains database connection settings and core constants
 */

// Prevent direct access
if (!defined('MARKETPLACE_VERSION')) {
    define('MARKETPLACE_VERSION', '2.0.0');
}

// Start output buffering
ob_start();

// Error reporting (set to 0 in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Database credentials - CHANGE THESE FOR YOUR SERVER
define('DB_HOST', 'localhost');
define('DB_NAME', 'marketplace_db');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_CHARSET', 'utf8mb4');

// Create database connection
try {
    $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
    $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES " . DB_CHARSET . " COLLATE utf8mb4_unicode_ci"
    ];
    
    $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
} catch (PDOException $e) {
    // Log error and show user-friendly message
    error_log("Database connection failed: " . $e->getMessage());
    die("<div style='text-align:center; padding:50px; font-family:sans-serif;'>
        <h1>Service Temporarily Unavailable</h1>
        <p>We're experiencing technical difficulties. Please try again later.</p>
    </div>");
}

// Site URL configuration
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';

// Detect if running in subdirectory
$script_name = $_SERVER['SCRIPT_NAME'] ?? '';
$base_path = dirname($script_name);
$base_path = ($base_path === '/' || $base_path === '\\') ? '' : $base_path;

// Define URLs
define('SITE_URL', $protocol . '://' . $host);
define('BASE_URL', SITE_URL . $base_path);
define('ADMIN_URL', BASE_URL . '/admin');
define('ACCOUNT_URL', BASE_URL . '/account');

// Upload paths
define('UPLOAD_PATH', dirname(__DIR__) . '/uploads');
define('UPLOAD_URL', BASE_URL . '/uploads');
define('ASSETS_URL', BASE_URL . '/assets');

// Security settings
define('CSRF_TOKEN_NAME', 'csrf_token');
define('SESSION_LIFETIME', 7200); // 2 hours
define('MAX_LOGIN_ATTEMPTS', 5);
define('LOGIN_LOCKOUT_TIME', 900); // 15 minutes

// Start session securely
if (session_status() === PHP_SESSION_NONE) {
    ini_set('session.cookie_httponly', 1);
    ini_set('session.cookie_secure', $protocol === 'https');
    ini_set('session.cookie_samesite', 'Strict');
    ini_set('session.gc_maxlifetime', SESSION_LIFETIME);
    session_start();
}

// Include helper functions
$functions_file = dirname(__DIR__) . '/includes/functions.php';
if (file_exists($functions_file)) {
    require_once $functions_file;
}

// Load settings from database
$settings = [];
if (isset($pdo)) {
    try {
        $settings = get_all_settings();
    } catch (Exception $e) {
        // Settings will use defaults
    }
}

// Set timezone
date_default_timezone_set($settings['timezone'] ?? 'UTC');

// Define currency
define('CURRENCY', $settings['currency'] ?? 'USD');
define('CURRENCY_SYMBOL', $settings['currency_symbol'] ?? '$');
define('CURRENCY_POSITION', $settings['currency_position'] ?? 'before');
