<?php
/**
 * CargoHub Free Carriers Class
 * 
 * Handles carrier information and tracking URLs for FREE version
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class CargoHub_Free_Carriers {
    
    /**
     * Get all supported carriers
     * 
     * @return array
     */
    public static function get_carriers() {
        // Informative warning if trying to add more than the limit
        // No built-in carriers; all are custom; no limits

        // Return empty array initially
        return array();
    }

    /**
     * Get carrier information by code (cached)
     * 
     * @param string $carrier_code
     * @return array|null
     */
    public static function get_carrier($carrier_code) {
        // Try cache first
        $cache_key = "carrier_{$carrier_code}";
        $cached_result = wp_cache_get($cache_key, 'cargohub_free_carriers');
        
        if ($cached_result !== false) {
            return $cached_result;
        }
        
        // Get from database (restricted)
        $custom_carriers = get_option('cargohub_free_custom_carriers', array());
        
        $result = null;
        if (isset($custom_carriers[$carrier_code])) {
            $result = $custom_carriers[$carrier_code];
        }
        
        // Cache the result (even if null) - use configurable timeout
        $cache_timeout = defined('CARGOHUB_FREE_CACHE_TIMEOUT') ? CARGOHUB_FREE_CACHE_TIMEOUT : 600; // default fallback
        wp_cache_set($cache_key, $result, 'cargohub_free_carriers', $cache_timeout);
        
        return $result;
    }

    /**
     * Get carrier name by code
     * 
     * @param string $carrier_code
     * @return string
     */
    public static function get_carrier_name($carrier_code) {
        $carrier = self::get_carrier($carrier_code);
        return ($carrier && isset($carrier['name'])) ? $carrier['name'] : ucfirst($carrier_code);
    }
    
    /**
     * Get tracking URL for a carrier and tracking number
     * 
     * @param string $carrier_code
     * @param string $tracking_number
     * @return string|null
     */
    public static function get_tracking_url($carrier_code, $tracking_number) {
        $carrier = self::get_carrier($carrier_code);
        
        if (!$carrier || empty($tracking_number) || !isset($carrier['tracking_url'])) {
            return null;
        }
        
        // Return only the base URL without tracking number
        return $carrier['tracking_url'];
    }
    
    /**
     * Get carrier logo URL
     * 
     * @param string $carrier_code
     * @return string
     */
    public static function get_carrier_logo($carrier_code) {
        $custom_carriers = get_option('cargohub_free_custom_carriers', array());
        
        $upload_dir = wp_upload_dir();
        $base_url = $upload_dir['baseurl'] . '/cargohub-free-logos/';
        
        // Check custom carriers (all carriers are now custom)
        if (isset($custom_carriers[$carrier_code])) {
            $carrier = $custom_carriers[$carrier_code];
            
            if (!empty($carrier['logo'])) {
                // If it's a URL (uploaded file), use it
                if (filter_var($carrier['logo'], FILTER_VALIDATE_URL)) {
                    return $carrier['logo'];
                }
                // If it's a filename, use from cargohub-free-logos folder
                return $base_url . $carrier['logo'];
            }
        }
        
        // No carrier found or no logo, use default logo
        return $base_url . 'default-logo.png';
    }
    
    /**
     * Get carrier color
     * 
     * @param string $carrier_code
     * @return string
     */
    public static function get_carrier_color($carrier_code) {
        $carrier = self::get_carrier($carrier_code);
        return ($carrier && isset($carrier['color'])) ? $carrier['color'] : '#333333';
    }
    
    /**
     * Validate carrier code
     * 
     * @param string $carrier_code
     * @return bool
     */
    public static function is_valid_carrier($carrier_code) {
        $custom_carriers = get_option('cargohub_free_custom_carriers', array());
        return isset($custom_carriers[$carrier_code]);
    }
    
    /**
     * Get carriers as options for select field (with limitation check)
     * 
     * @return array
     */
    public static function get_carrier_options() {
        $custom_carriers = get_option('cargohub_free_custom_carriers', array());
        
        $options = array();
        foreach ($custom_carriers as $code => $carrier) {
            $options[$code] = $carrier['name'];
        }
        
        // No upgrade option injected into select
        
        return $options;
    }
    
    /**
     * Format tracking number for display
     * 
     * @param string $tracking_number
     * @param string $carrier_code
     * @return string
     */
    public static function format_tracking_number($tracking_number, $carrier_code = '') {
        // Remove any spaces and convert to uppercase for consistency
        $formatted = strtoupper(str_replace(' ', '', $tracking_number));
        
        // Carrier-specific formatting rules
        switch ($carrier_code) {
            case 'yurtici':
                // Yurtiçi format: 123456789012 (12 digits)
                if (preg_match('/^(\d{4})(\d{4})(\d{4})$/', $formatted, $matches)) {
                    return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
                }
                break;
                
            case 'aras':
                // Aras format: AR123456789TR
                if (preg_match('/^(AR)(\d+)(TR)$/', $formatted, $matches)) {
                    return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
                }
                break;
                
            case 'dhl':
                // DHL format: varies, usually 10-11 digits
                if (preg_match('/^(\d{4})(\d{3})(\d{3,4})$/', $formatted, $matches)) {
                    return $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
                }
                break;
        }
        
        // Return original if no specific formatting applies
        return $tracking_number;
    }
}
