<?php
// app/Helpers/format.php - Formatting Functions

// Format currency
function format_money($amount, $currency = null) {
    $currency = $currency ?: CURRENCY_SYMBOL;
    if($amount == "0.00" || $amount == 0){
        return $currency . "0.00";
    }
    else {
        return $currency . number_format($amount, 2);
    }
}

// Format number
function format_number($number, $decimals = 0) {
    return number_format($number, $decimals);
}

// Format date
function format_date($date, $format = 'M d, Y') {
    if (empty($date) || $date === '0000-00-00' || $date === '0000-00-00 00:00:00') {
        return 'N/A';
    }
    return date($format, strtotime($date));
}

// Format datetime
function format_datetime($datetime, $format = 'M d, Y h:i A') {
    if (empty($datetime) || $datetime === '0000-00-00 00:00:00') {
        return 'N/A';
    }
    return date($format, strtotime($datetime));
}

// Time ago format
function time_ago($datetime) {
    if (empty($datetime) || $datetime === '0000-00-00 00:00:00') {
        return 'N/A';
    }
    
    $timestamp = strtotime($datetime);
    $diff = time() - $timestamp;
    
    if ($diff < 1) {
        return 'just now';
    }
    
    $periods = [
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    ];
    
    foreach ($periods as $seconds => $label) {
        if ($diff >= $seconds) {
            $count = floor($diff / $seconds);
            return $count . ' ' . $label . ($count > 1 ? 's' : '') . ' ago';
        }
    }
    
    return 'just now';
}

// Format phone number
function format_phone($phone) {
    // Remove non-numeric characters
    $phone = preg_replace('/[^0-9]/', '', $phone);
    
    // Format Nigerian phone number
    if (strlen($phone) === 11 && substr($phone, 0, 1) === '0') {
        return substr($phone, 0, 4) . ' ' . substr($phone, 4, 3) . ' ' . substr($phone, 7);
    } elseif (strlen($phone) === 13 && substr($phone, 0, 3) === '234') {
        return '+234 ' . substr($phone, 3, 3) . ' ' . substr($phone, 6, 3) . ' ' . substr($phone, 9);
    }
    
    return $phone;
}

// Format file size
function format_filesize($bytes) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= (1 << (10 * $pow));
    return round($bytes, 2) . ' ' . $units[$pow];
}

// Shorten text
function str_limit($text, $limit = 100, $end = '...') {
    if (mb_strlen($text) <= $limit) {
        return $text;
    }
    return mb_substr($text, 0, $limit) . $end;
}

// Format percentage
function format_percentage($value, $decimals = 2) {
    return number_format($value, $decimals) . '%';
}

// Format status badge
function status_badge($status) {
    $badges = [
        'active' => '<span class="badge badge-success">Active</span>',
        'inactive' => '<span class="badge badge-secondary">Inactive</span>',
        'pending' => '<span class="badge badge-warning">Pending</span>',
        'approved' => '<span class="badge badge-success">Approved</span>',
        'rejected' => '<span class="badge badge-danger">Rejected</span>',
        'suspended' => '<span class="badge badge-danger">Suspended</span>',
        'completed' => '<span class="badge badge-success">Completed</span>',
        'cancelled' => '<span class="badge badge-danger">Cancelled</span>',
        'processing' => '<span class="badge badge-info">Processing</span>',
        'paid' => '<span class="badge badge-success">Paid</span>',
        'unpaid' => '<span class="badge badge-warning">Unpaid</span>',
        'refunded' => '<span class="badge badge-info">Refunded</span>',
        'available' => '<span class="badge badge-success">Available</span>',
        'rented' => '<span class="badge badge-warning">Rented</span>',
        'maintenance' => '<span class="badge badge-secondary">Maintenance</span>',
    ];
    
    return $badges[strtolower($status)] ?? '<span class="badge badge-secondary">' . ucfirst($status) . '</span>';
}

// Format order status badge
function order_status_badge($status) {
    $badges = [
        'pending' => '<span class="badge bg-warning text-dark">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[strtolower($status)] ?? '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
}

// Format payment status badge
function payment_status_badge($status) {
    $badges = [
        'pending' => '<span class="badge bg-warning text-dark">Pending</span>',
        'processing' => '<span class="badge bg-info">Processing</span>',
        'completed' => '<span class="badge bg-success">Completed</span>',
        'failed' => '<span class="badge bg-danger">Failed</span>',
        'refunded' => '<span class="badge bg-secondary">Refunded</span>',
    ];
    
    return $badges[strtolower($status)] ?? '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
}

// Format boolean to Yes/No
function format_boolean($value) {
    return $value ? 'Yes' : 'No';
}

// Format rating stars
function format_rating($rating, $max = 5) {
    $rating = max(0, min($rating, $max));
    $full_stars = floor($rating);
    $half_star = ($rating - $full_stars) >= 0.5 ? 1 : 0;
    $empty_stars = $max - $full_stars - $half_star;
    
    $html = '<div class="rating">';
    
    // Full stars
    for ($i = 0; $i < $full_stars; $i++) {
        $html .= '<i class="fas fa-star text-warning"></i>';
    }
    
    // Half star
    if ($half_star) {
        $html .= '<i class="fas fa-star-half-alt text-warning"></i>';
    }
    
    // Empty stars
    for ($i = 0; $i < $empty_stars; $i++) {
        $html .= '<i class="far fa-star text-warning"></i>';
    }
    
    $html .= ' <span class="rating-value">(' . number_format($rating, 1) . ')</span>';
    $html .= '</div>';
    
    return $html;
}

// Pluralize
function pluralize($count, $singular, $plural = null) {
    $plural = $plural ?: $singular . 's';
    return $count . ' ' . ($count == 1 ? $singular : $plural);
}

// Format order ID
function format_order_id($id, $prefix = 'ORD') {
    return $prefix . '-' . str_pad($id, 6, '0', STR_PAD_LEFT);
}


/**
 * Format time ago (e.g., "2 hours ago", "3 days ago")
 */
function format_time_ago($datetime) {
    $time = strtotime($datetime);
    $time_difference = time() - $time;
    
    if ($time_difference < 1) {
        return 'just now';
    }
    
    $condition = [
        12 * 30 * 24 * 60 * 60  =>  'year',
        30 * 24 * 60 * 60        =>  'month',
        24 * 60 * 60             =>  'day',
        60 * 60                  =>  'hour',
        60                       =>  'minute',
        1                        =>  'second'
    ];
    
    foreach ($condition as $secs => $str) {
        $d = $time_difference / $secs;
        
        if ($d >= 1) {
            $t = round($d);
            return $t . ' ' . $str . ($t > 1 ? 's' : '') . ' ago';
        }
    }
}
