<?php
// Global configuration
// Update these values to match your MySQL setup

define('DB_HOST', '209.133.215.178');
define('DB_USER', 'swgranit_root');
define('DB_PASS', '7v$Sb)a~nfaI8aaL');
define('DB_NAME', 'swgranit_testapp');

// App settings
// If your app runs at http://localhost/new_sw/, set BASE_PATH to '/new_sw'
// If at the Apache docroot e.g., http://localhost/, set it to ''
if (!defined('BASE_PATH')) {
    define('BASE_PATH', '/app');
}

// Currency settings
if (!defined('CURRENCY')) {
    define('CURRENCY', 'LKR');
}

// Start session early for auth
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// User roles
const ROLES = [
    'super_admin',
    'admin',
    'sales_user'
];

// CSRF helper
function csrf_token() {
    if (empty($_SESSION['csrf_token'])) {
        $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
    }
    return $_SESSION['csrf_token'];
}

function validate_csrf($token) {
    return isset($_SESSION['csrf_token']) && hash_equals($_SESSION['csrf_token'], $token ?? '');
}

// Simple helper for building URLs
function url_for($path = '') {
    $path = ltrim($path, '/');
    return BASE_PATH . '/' . $path;
}

// JSON response helper
if (!function_exists('json_response')) {
    function json_response($data = [], $status = 200) {
        http_response_code($status);
        header('Content-Type: application/json');
        echo json_encode($data);
        exit;
    }
}
