<?php
/**
 * Global Helper Functions
 */

if (!function_exists('dd')) {
    function dd($data) {
        echo '<pre>';
        var_dump($data);
        echo '</pre>';
        die();
    }
}

if (!function_exists('dump')) {
    function dump($data) {
        echo '<pre>';
        var_dump($data);
        echo '</pre>';
    }
}

if (!function_exists('asset')) {
    function asset($path) {
        return APP_URL . '/assets/' . ltrim($path, '/');
    }
}

if (!function_exists('url')) {
    function url($path = '') {
        return APP_URL . '/' . ltrim($path, '/');
    }
}

if (!function_exists('csrf_field')) {
    function csrf_field() {
        $token = Security::generateCSRFToken();
        return '<input type="hidden" name="csrf_token" value="' . $token . '">';
    }
}

if (!function_exists('old')) {
    function old($key, $default = '') {
        return $_SESSION['old'][$key] ?? $default;
    }
}

if (!function_exists('format_date')) {
    function format_date($date, $format = 'd/m/Y') {
        if (!$date) return '';
        return date($format, strtotime($date));
    }
}

if (!function_exists('format_datetime')) {
    function format_datetime($datetime, $format = 'd/m/Y H:i') {
        if (!$datetime) return '';
        return date($format, strtotime($datetime));
    }
}

if (!function_exists('is_active')) {
    function is_active($path, $class = 'active') {
        $current = $_SERVER['REQUEST_URI'];
        if (strpos($current, $path) !== false) {
            return $class;
        }
        return '';
    }
}

if (!function_exists('truncate')) {
    function truncate($text, $limit = 100) {
        if (strlen($text) <= $limit) return $text;
        return substr($text, 0, $limit) . '...';
    }
}

if (!function_exists('random_string')) {
    function random_string($length = 10) {
        return bin2hex(random_bytes($length / 2));
    }
}

if (!function_exists('get_status_badge')) {
    function get_status_badge($status) {
        $classes = [
            'Draft' => 'secondary',
            'Submitted' => 'warning',
            'Under Review' => 'info',
            'Approved' => 'success',
            'Rejected' => 'danger'
        ];
        $class = $classes[$status] ?? 'secondary';
        return '<span class="badge bg-' . $class . '">' . $status . '</span>';
    }
}