<?php
/**
 * CUSTOMER HELPER FUNCTIONS
 * File: app/Helpers/customer.php
 * Add these functions to your general.php or create a new customer.php helper file
 */

/**
 * Format money with Naira symbol
 */
if (!function_exists('format_money')) {
    function format_money($amount) {
        return '₦' . number_format($amount, 2);
    }
}

/**
 * Format number
 */
if (!function_exists('format_number')) {
    function format_number($number) {
        return number_format($number);
    }
}

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

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

/**
 * Time ago format
 */
if (!function_exists('time_ago')) {
    function time_ago($datetime) {
        $timestamp = strtotime($datetime);
        $diff = time() - $timestamp;
        
        if ($diff < 60) {
            return $diff . ' seconds ago';
        } elseif ($diff < 3600) {
            return floor($diff / 60) . ' minutes ago';
        } elseif ($diff < 86400) {
            return floor($diff / 3600) . ' hours ago';
        } elseif ($diff < 604800) {
            return floor($diff / 86400) . ' days ago';
        } else {
            return date('M d, Y', $timestamp);
        }
    }
}

/**
 * Truncate string
 */
if (!function_exists('str_limit')) {
    function str_limit($string, $limit = 100, $end = '...') {
        if (strlen($string) <= $limit) {
            return $string;
        }
        return substr($string, 0, $limit) . $end;
    }
}

/**
 * Format status badge
 */
if (!function_exists('format_status_badge')) {
    function format_status_badge($status) {
        $badges = [
            'pending' => '<span class="badge bg-warning">Pending</span>',
            'confirmed' => '<span class="badge bg-info">Confirmed</span>',
            'ongoing' => '<span class="badge bg-primary">Ongoing</span>',
            'completed' => '<span class="badge bg-success">Completed</span>',
            'cancelled' => '<span class="badge bg-danger">Cancelled</span>',
            'returned' => '<span class="badge bg-secondary">Returned</span>',
        ];
        
        return $badges[$status] ?? '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
    }
}

/**
 * Format payment status badge
 */
if (!function_exists('format_payment_badge')) {
    function format_payment_badge($status) {
        $badges = [
            'pending' => '<span class="badge bg-warning">Pending</span>',
            'paid' => '<span class="badge bg-success">Paid</span>',
            'refunded' => '<span class="badge bg-info">Refunded</span>',
        ];
        
        return $badges[$status] ?? '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
    }
}

/**
 * Format rating stars
 */
if (!function_exists('format_rating')) {
    function format_rating($rating) {
        $html = '';
        $fullStars = floor($rating);
        $halfStar = ($rating - $fullStars) >= 0.5;
        $emptyStars = 5 - $fullStars - ($halfStar ? 1 : 0);
        
        for ($i = 0; $i < $fullStars; $i++) {
            $html .= '<i class="fas fa-star text-warning"></i>';
        }
        
        if ($halfStar) {
            $html .= '<i class="fas fa-star-half-alt text-warning"></i>';
        }
        
        for ($i = 0; $i < $emptyStars; $i++) {
            $html .= '<i class="far fa-star text-warning"></i>';
        }
        
        return $html;
    }
}

/**
 * Get asset URL
 */
if (!function_exists('asset')) {
    function asset($path) {
        return url('/assets/' . ltrim($path, '/'));
    }
}

/**
 * Get upload URL
 */
if (!function_exists('upload')) {
    function upload($path) {
        return url('/uploads/' . ltrim($path, '/'));
    }
}

/**
 * Generate URL
 */
if (!function_exists('url')) {
    function url($path = '') {
        $base_url = defined('BASE_URL') ? BASE_URL : 
                    (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . 
                    '://' . $_SERVER['HTTP_HOST'];
        return rtrim($base_url, '/') . '/' . ltrim($path, '/');
    }
}

/**
 * Order status badge function
 */
if (!function_exists('order_status_badge')) {
    function order_status_badge($status) {
        return format_status_badge($status);
    }
}

/**
 * Flash message functions
 */
if (!function_exists('flash')) {
    function flash($key, $message, $type = 'success') {
        $_SESSION['flash'][$key] = [
            'message' => $message,
            'type' => $type
        ];
    }
}

if (!function_exists('has_flash')) {
    function has_flash($key = null) {
        if ($key) {
            return isset($_SESSION['flash'][$key]);
        }
        return !empty($_SESSION['flash']);
    }
}

if (!function_exists('get_flash')) {
    function get_flash($key) {
        if (isset($_SESSION['flash'][$key])) {
            $flash = $_SESSION['flash'][$key];
            unset($_SESSION['flash'][$key]);
            return $flash;
        }
        return null;
    }
}

if (!function_exists('display_flash')) {
    function display_flash() {
        if (!empty($_SESSION['flash'])) {
            foreach ($_SESSION['flash'] as $key => $flash) {
                $type = $flash['type'];
                $message = $flash['message'];
                
                $alertClass = [
                    'success' => 'alert-success',
                    'error' => 'alert-danger',
                    'warning' => 'alert-warning',
                    'info' => 'alert-info',
                    'danger' => 'alert-danger'
                ];
                
                $class = $alertClass[$type] ?? 'alert-info';
                
                echo "<div class='alert {$class} alert-dismissible fade show' role='alert'>";
                echo htmlspecialchars($message);
                echo "<button type='button' class='btn-close' data-bs-dismiss='alert'></button>";
                echo "</div>";
            }
            unset($_SESSION['flash']);
        }
    }
}

/**
 * Redirect function
 */
if (!function_exists('redirect')) {
    function redirect($path) {
        header('Location: ' . url($path));
        exit;
    }
}

/**
 * Check if logged in
 */
if (!function_exists('isLoggedIn')) {
    function isLoggedIn() {
        return Auth::check();
    }
}

/**
 * Calculate days between dates
 */
if (!function_exists('days_between')) {
    function days_between($date1, $date2) {
        $datetime1 = new DateTime($date1);
        $datetime2 = new DateTime($date2);
        $interval = $datetime1->diff($datetime2);
        return $interval->days;
    }
}

/**
 * Calculate rental cost
 */
if (!function_exists('calculate_rental_cost')) {
    function calculate_rental_cost($price_per_day, $start_date, $end_date) {
        $days = days_between($start_date, $end_date);
        $days = $days < 1 ? 1 : $days; // Minimum 1 day
        return $price_per_day * $days;
    }
}