<?php

namespace Opencart\Extension\Alsendo\Models;

class Config
{
    private string $region;
    private array $apiConfig;

    public function __construct(string $region = '', array $apiConfig = [])
    {
        $this->region = $region;
        $this->apiConfig = $apiConfig;
    }

    public function getRegion(): string
    {
        return $this->region;
    }

    public function setRegion(string $region): void
    {
        $this->region = $region;
    }

    public function getApiConfig(): array
    {
        if ($this->region === 'romania') {
            $token = $this->apiConfig['ro_client_oauth_access_token'] ?? $this->apiConfig['ro_client_oauth_code'] ?? '';
            $token = preg_replace('/\s+/', '', $token);
            return ['token' => $token];
        }

        $config = $this->getWrapperConfig();
        if (!empty($config['token'])) {
            $config['token'] = preg_replace('/\s+/', '', $config['token']);
        }
        return $config;
    }

    public function setApiConfig(array $apiConfig): void
    {
        $this->apiConfig = $apiConfig;
    }

    public function getAppId(): string
    {
        return $this->apiConfig['app_id'] ?? '';
    }

    public function getAppSecret(): string
    {
        return $this->apiConfig['app_secret'] ?? '';
    }

    public function getToken(): string
    {
        return $this->apiConfig['token'] ?? '';
    }

    public function getApiKey(): string
    {
        return $this->apiConfig['api_key'] ?? '';
    }

    public function getRoClientSecret(): string {
        return $this->apiConfig['ro_client_secret'] ?? '';
    }

    public function getRoClientId(): string {
        return $this->apiConfig['ro_client_id'] ?? '';
    }

    public function getSandbox(): bool {
        if (!isset($this->apiConfig['sandbox'])) {
            return true;
        }

        return $this->apiConfig['sandbox'] === '1';
    }

    public function getRoSandbox(): bool
    {
        if (!isset($this->apiConfig['ro_sandbox'])) {
            return true;
        }
        return $this->apiConfig['ro_sandbox'] === '1';
    }

    public function getCzSandbox(): bool
    {
        if (!isset($this->apiConfig['cz_sandbox'])) {
            return true;
        }
        return $this->apiConfig['cz_sandbox'] === '1';
    }

    public function isComplete(): bool
    {
        if (empty($this->region)) {
            return false;
        }

        if ($this->region === 'romania') {
            $hasOAuth = !empty($this->apiConfig['ro_client_id'])
                && !empty($this->apiConfig['ro_client_secret'])
                && !empty($this->apiConfig['ro_client_oauth_access_token']);
            $hasLegacyToken = !empty($this->apiConfig['token']);
            return $hasOAuth || $hasLegacyToken;
        }

        $requiredFields = $this->getRequiredFields();
        foreach ($requiredFields as $field) {
            if ($field === 'sandbox') {
                continue;
            }

            if (empty($this->apiConfig[$field])) {
                return false;
            }
        }

        return true;
    }

    public function getRequiredFields(): array
    {
        return match($this->region) {
            'poland' => ['app_id', 'app_secret', 'sandbox'],
            'romania' => ['ro_client_id', 'ro_client_secret'],
            'czechia' => ['api_key'],
            default => []
        };
    }

    public function getWrapperApiName(): string
    {
        $regionMap = [
            'poland' => $this->getSandbox() ? 'apaczka_test' : 'apaczka',
            'czechia' => $this->getCzSandbox() ? 'zaslat_test' : 'zaslat',
            'romania' => $this->getRoSandbox() ? 'ecolet_test' : 'ecolet'
        ];

        return $regionMap[$this->region] ?? '';
    }

    public function getWrapperConfig(): array
    {
        return $this->apiConfig;
    }

    public function getPlatformName(): string
    {
        return 'opencart';
    }
} 