<?php
namespace Opencart\Catalog\Model\Extension\Alsendo\Shipping;

class Wrapper extends \Opencart\System\Engine\Model {
    
    public function calculateCost(string $provider, string $serviceCode, array $address, $cart): array|false {
        try {
            $wrapperPath = DIR_EXTENSION . 'alsendo/wrapper/';
            
            if (!file_exists($wrapperPath . 'autoload.php')) {
                $this->log->write('Alsendo Wrapper: autoload.php not found');
                return false;
            }
            
            require_once $wrapperPath . 'autoload.php';
            
            $cartData = $this->prepareCartData($cart);
            $addressData = $this->prepareAddressData($address);
            
            switch ($provider) {
                case 'apaczka':
                    return $this->calculateApaczka($serviceCode, $addressData, $cartData);
                case 'zaslat':
                    return $this->calculateZaslat($serviceCode, $addressData, $cartData);
                case 'ecolet':
                    return $this->calculateEcolet($serviceCode, $addressData, $cartData);
                default:
                    return false;
            }
            
        } catch (\Exception $e) {
            $this->log->write('Alsendo Wrapper Error: ' . $e->getMessage());
            return false;
        }
    }
    
    private function calculateApaczka(string $serviceCode, array $address, array $cart): array|false {
        try {
            $result = [
                'cost' => 15.00,
                'delivery_time' => '1-2 days',
                'provider' => 'apaczka',
                'service' => $serviceCode
            ];
            
            return $result;
            
        } catch (\Exception $e) {
            $this->log->write('Apaczka API Error: ' . $e->getMessage());
            return false;
        }
    }
    
    private function calculateZaslat(string $serviceCode, array $address, array $cart): array|false {
        try {
            $result = [
                'cost' => 12.00,
                'delivery_time' => '2-3 days',
                'provider' => 'zaslat',
                'service' => $serviceCode
            ];
            
            return $result;
            
        } catch (\Exception $e) {
            $this->log->write('Zaslat API Error: ' . $e->getMessage());
            return false;
        }
    }
    
    private function calculateEcolet(string $serviceCode, array $address, array $cart): array|false {
        try {
            $result = [
                'cost' => 18.00,
                'delivery_time' => '1-3 days',
                'provider' => 'ecolet',
                'service' => $serviceCode
            ];
            
            return $result;
            
        } catch (\Exception $e) {
            $this->log->write('Ecolet API Error: ' . $e->getMessage());
            return false;
        }
    }
    
    private function prepareCartData($cart): array {
        $cartData = [
            'total_weight' => 0,
            'total_value' => 0,
            'items' => []
        ];
        
        $products = $cart->getProducts();
        
        foreach ($products as $product) {
            $cartData['total_weight'] += $product['weight'] * $product['quantity'];
            $cartData['total_value'] += $product['price'] * $product['quantity'];
            
            $cartData['items'][] = [
                'name' => $product['name'],
                'quantity' => $product['quantity'],
                'weight' => $product['weight'],
                'value' => $product['price']
            ];
        }
        
        return $cartData;
    }
    
    private function prepareAddressData(array $address): array {
        return [
            'country' => $address['country'] ?? '',
            'country_id' => $address['country_id'] ?? 0,
            'zone' => $address['zone'] ?? '',
            'zone_id' => $address['zone_id'] ?? 0,
            'city' => $address['city'] ?? '',
            'postcode' => $address['postcode'] ?? '',
            'address_1' => $address['address_1'] ?? '',
            'address_2' => $address['address_2'] ?? ''
        ];
    }
} 