<?php
class Helper {
    // Format currency
    public static function formatCurrency($amount) {
        return 'Rp ' . number_format($amount, 0, ',', '.');
    }
    
    // Format date
    public static function formatDate($date, $format = DATE_FORMAT) {
        return date($format, strtotime($date));
    }
    
    // Format datetime
    public static function formatDateTime($datetime, $format = DATETIME_FORMAT) {
        return date($format, strtotime($datetime));
    }
    
    // Truncate string
    public static function truncate($str, $length = 50, $suffix = '...') {
        if (strlen($str) > $length) {
            return substr($str, 0, $length) . $suffix;
        }
        return $str;
    }
    
    // Generate random string
    public static function randomString($length = 10) {
        return substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/62))), 0, $length);
    }
    
    // Get status badge
    public static function getStatusBadge($status) {
        $badges = [
            'active' => '<span class="badge bg-success">Aktif</span>',
            'trial' => '<span class="badge bg-primary">Trial</span>',
            'inactive' => '<span class="badge bg-danger">Tidak Aktif</span>',
            'expired' => '<span class="badge bg-warning">Kadaluarsa</span>',
            'paid' => '<span class="badge bg-success">Lunas</span>',
            'pending' => '<span class="badge bg-warning">Menunggu Pembayaran</span>',
            'reviewing' => '<span class="badge bg-info">Menunggu Verifikasi</span>',
            'failed' => '<span class="badge bg-danger">Ditolak</span>'
        ];
        
        return $badges[$status] ?? '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
    }
    
    // Redirect
    public static function redirect($url) {
        header('Location: ' . $url);
        exit;
    }
    
    // Back
    public static function back() {
        header('Location: ' . $_SERVER['HTTP_REFERER']);
        exit;
    }
    
    // Get IP address
    public static function getIpAddress() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            return $_SERVER['REMOTE_ADDR'];
        }
    }
    
    // Sanitize
    public static function sanitize($data) {
        if (is_array($data)) {
            return array_map(['Helper', 'sanitize'], $data);
        }
        return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
    }
    
    // Validate email
    public static function validateEmail($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }
    
    // Validate phone
    public static function validatePhone($phone) {
        return preg_match('/^[0-9\-\+\s]+$/', $phone);
    }
}
