<?php

namespace GlobalPayments\Api\ServiceConfigs\Gateways;

use GlobalPayments\Api\ConfiguredServices;
use GlobalPayments\Api\Entities\Enums\{Environment, GatewayProvider, ServiceEndpoints};
use GlobalPayments\Api\Entities\Exceptions\ConfigurationException;
use GlobalPayments\Api\Gateways\GeniusConnector;

class GeniusConfig extends GatewayConfig
{
    public ?string $clearkId = null;
    public ?string $merchantName = null;
    public ?string $merchantSiteId = null;
    public ?string $merchantKey = null;
    public ?string $registerNumber = null;
    public ?string $dba = null;
    public ?string $terminalId = null;

    public function __construct()
    {
        $this->gatewayProvider = GatewayProvider::GENIUS;
    }

    public function configureContainer(ConfiguredServices $services)
    {
        if (empty($this->serviceUrl)) {
            $this->serviceUrl = $this->environment == Environment::TEST ? ServiceEndpoints::MERCHANTWARE_TEST : ServiceEndpoints::MERCHANTWARE_PRODUCTION;
        }

        $gateway = new GeniusConnector();
        $gateway->merchantName = $this->merchantName;
        $gateway->merchantSiteId = $this->merchantSiteId;
        $gateway->merchantKey = $this->merchantKey;
        $gateway->registerNumber = $this->registerNumber;
        $gateway->terminalId = $this->terminalId;
        $gateway->timeout = $this->timeout;
        $gateway->serviceUrl = $this->serviceUrl;
        $gateway->webProxy = $this->webProxy;

        $services->gatewayConnector = $gateway;
    }

    public function validate()
    {
        parent::validate();

        if (empty($this->merchantSiteId)) {
            throw new ConfigurationException('MerchantSiteId is required for this configuration.');
        }

        if (empty($this->merchantName)) {
            throw new ConfigurationException('Merchantname is required for this configuration.');
        }

        if (empty($this->merchantKey)) {
            throw new ConfigurationException('MerchantKey is required for this configuration.');
        }
    }
}
