<?php
namespace EasyChat\Library\Email;

final class OutboundMailTransportResolver {
    private $registry;
    public function __construct($registry = null) { $this->registry = $registry; }

    public function resolve(array $config): OutboundMailTransportInterface {
        $selected = strtolower((string)($config['outbound_transport'] ?? 'auto'));
        if ($selected === 'smtp_bz_api') return new SmtpBzApiTransport($this->registry);
        if ($selected === 'local_relay') return new LocalRelayMailTransport();
        if ($selected === 'cms_mail') {
            $bridge = $this->registry ? $this->registry->get('cms_outbound_mail_bridge') : null;
            if ($bridge instanceof CmsOutboundMailBridgeInterface) return new CmsMailTransport($bridge);
            throw new \RuntimeException('cms_mail_bridge_unavailable');
        }
        return new DirectSmtpMailTransport();
    }

     
    public function probe(array $config): array {
        $selected = strtolower((string)($config['outbound_transport'] ?? 'auto'));
        if ($selected !== '' && $selected !== 'auto') {
            $result=$this->resolve($config)->probe($config);
            $result['transport']=(string)($result['transport']??$selected);
            $result['selection_mode']='manual';$result['requested_transport']=$selected;$result['rejected_transports']=array();$result['verified_alternatives']=array();
            return $result;
        }

        $rejected=array();

        if (trim((string)($config['smtp_bz_api_key'] ?? '')) !== '') {
            $api = new SmtpBzApiTransport($this->registry);
            $result = $api->probe($config);
            if (!empty($result['success'])) return $this->autoResult($result,'smtp_bz_api',$rejected);
            $rejected['smtp_bz_api']=(string)($result['code']??'smtp_bz_unavailable');
        }

        $smtp = new DirectSmtpMailTransport();
        $result = $smtp->probe($config);
        if (!empty($result['success'])) return $this->autoResult($result,'direct_smtp',$rejected);
        $rejected['direct_smtp']=(string)($result['code']??'smtp_unavailable');

        $bridge = $this->registry ? $this->registry->get('cms_outbound_mail_bridge') : null;
        if ($bridge instanceof CmsOutboundMailBridgeInterface) {
            $cms = new CmsMailTransport($bridge);
            $cmsResult = $cms->probe($config);
            if (!empty($cmsResult['success'])) return $this->autoResult($cmsResult,'cms_mail',$rejected);
            $rejected['cms_mail']=(string)($cmsResult['code']??'cms_mail_unavailable');
        }

        $relay = new LocalRelayMailTransport();
        $relayResult = $relay->probe($config);
        if (!empty($relayResult['success'])) return $this->autoResult($relayResult,'local_relay',$rejected);
        $rejected['local_relay']=(string)($relayResult['code']??'local_relay_unavailable');
        if (($result['code'] ?? '') === 'smtp_tls_identity_mismatch') $result['code'] = 'external_smtp_intercepted';
        $result['selection_mode']='auto';$result['requested_transport']='auto';$result['rejected_transports']=$rejected;$result['verified_alternatives']=array();
        return $result;
    }

    private function autoResult(array $result,string $transport,array $rejected): array {
        $result['transport']=(string)($result['transport']??$transport);$result['selection_mode']='auto';$result['requested_transport']='auto';$result['rejected_transports']=$rejected;$result['verified_alternatives']=array();return $result;
    }
}
