<?php
declare(strict_types=1);

define('CRM_ROOT', dirname(__DIR__));

if (session_status() !== PHP_SESSION_ACTIVE) {
    $sessionPath = (string) (getenv('CRM_SESSION_PATH') ?: CRM_ROOT . '/storage/sessions');
    if (!is_dir($sessionPath)) {
        @mkdir($sessionPath, 0700, true);
    }
    if (is_dir($sessionPath) && is_writable($sessionPath)) {
        session_save_path($sessionPath);
    }
    session_start();
}

$configuredBaseUrl = getenv('CRM_BASE_URL');
define(
    'CRM_BASE_URL',
    rtrim($configuredBaseUrl === false ? '/modules/crm' : (string) $configuredBaseUrl, '/')
);

/**
 * The production ABOX shell can set these session keys before including a CRM page.
 * Local development remains usable until the destination auth include is supplied.
 */
function crm_current_user(): array
{
    return [
        'name' => (string) ($_SESSION['full_name'] ?? $_SESSION['name'] ?? 'ABOX User'),
        'role' => (string) ($_SESSION['role_name'] ?? $_SESSION['role'] ?? 'Administrator'),
    ];
}

function crm_url(string $path = ''): string
{
    return CRM_BASE_URL . ($path === '' ? '' : '/' . ltrim($path, '/'));
}

function crm_e(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

function crm_csrf_token(): string
{
    if (empty($_SESSION['crm_csrf'])) {
        $_SESSION['crm_csrf'] = bin2hex(random_bytes(24));
    }

    return (string) $_SESSION['crm_csrf'];
}

function crm_master_catalogue(): array
{
    return [
        ['icon' => 'fa-signal', 'title' => 'Add Status', 'description' => 'Maintain lead pipeline statuses and their display order.', 'path' => 'masters/add-status.php'],
        ['icon' => 'fa-bullhorn', 'title' => 'Add Lead Source', 'description' => 'Manage enquiry origins, ordering and publication state.', 'path' => 'masters/add-lead-source.php'],
        ['icon' => 'fa-compass', 'title' => 'Add Nature', 'description' => 'Maintain the nature or type used to classify leads.', 'path' => 'masters/add-nature.php'],
        ['icon' => 'fa-layer-group', 'title' => 'Add Segment', 'description' => 'Maintain customer market segments used by CRM.', 'path' => 'masters/add-segment.php'],
        ['icon' => 'fa-tags', 'title' => 'Add Lead Category', 'description' => 'Manage lead categories used for routing and reports.', 'path' => 'masters/add-lead-category.php'],
    ];
}

function crm_region_catalogue(): array
{
    return [
        ['icon' => 'fa-map', 'title' => 'Add State', 'description' => 'Maintain states and their optional numeric codes.', 'path' => 'masters/add-state.php'],
        ['icon' => 'fa-map-location-dot', 'title' => 'Add District', 'description' => 'Maintain districts under their correct state.', 'path' => 'masters/add-district.php'],
        ['icon' => 'fa-city', 'title' => 'Add Taluka', 'description' => 'Maintain talukas under the selected state and district.', 'path' => 'masters/add-taluka.php'],
        ['icon' => 'fa-location-dot', 'title' => 'Add Locality', 'description' => 'Maintain localities under the complete region hierarchy.', 'path' => 'masters/add-locality.php'],
    ];
}
