<?php
/**
 * SMTP relay defaults for API providers that also support SMTP.
 *
 * @package EmailSmtp
 */

namespace EmailSmtp\Includes;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class SmtpRelayProfiles {

	/**
	 * Providers that expose an SMTP relay in addition to HTTP API.
	 *
	 * @return list<string>
	 */
	public static function provider_ids(): array {
		return [
			'sendgrid',
			'mailgun',
			'brevo',
			'postmark',
			'mailersend',
			'resend',
			'amazon-ses',
			'sparkpost',
			'elastic-email',
			'smtp2go',
			'mailjet',
			'mandrill',
			'sendlayer',
			'smtpcom',
			'maileroo',
			'turbo-smtp',
			'mail-baby',
			'emailit',
		];
	}

	public static function supports( string $provider_id ): bool {
		return in_array( sanitize_key( $provider_id ), self::provider_ids(), true );
	}

	/**
	 * @return array{host:string, port:string, encryption:string, username:string, hint:string}
	 */
	public static function defaults( string $provider_id, array $settings = [] ): array {
		$provider_id = sanitize_key( $provider_id );
		$region      = '';

		switch ( $provider_id ) {
			case 'sendgrid':
				return [
					'host'       => 'smtp.sendgrid.net',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => 'apikey',
					'hint'       => __( 'Username is “apikey”. Password is your SendGrid API key.', 'wp-powersuite' ),
				];

			case 'mailgun':
				$domain = trim( (string) ( $settings['mailgun_domain'] ?? '' ) );
				$region = sanitize_key( (string) ( $settings['mailgun_region'] ?? 'us' ) );
				return [
					'host'       => $region === 'eu' ? 'smtp.eu.mailgun.org' : 'smtp.mailgun.org',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => $domain !== '' ? 'postmaster@' . $domain : '',
					'hint'       => __( 'Use your Mailgun SMTP credentials (often postmaster@your-domain). Match US/EU region to your domain.', 'wp-powersuite' ),
				];

			case 'brevo':
				return [
					'host'       => 'smtp-relay.brevo.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP login and key from your Brevo SMTP & API settings.', 'wp-powersuite' ),
				];

			case 'postmark':
				return [
					'host'       => 'smtp.postmarkapp.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use your Postmark Server API token as both username and password.', 'wp-powersuite' ),
				];

			case 'mailersend':
				return [
					'host'       => 'smtp.mailersend.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP username and password from MailerSend Domains → SMTP.', 'wp-powersuite' ),
				];

			case 'resend':
				return [
					'host'       => 'smtp.resend.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => 'resend',
					'hint'       => __( 'Username is “resend”. Password is your Resend API key.', 'wp-powersuite' ),
				];

			case 'amazon-ses':
				$region = sanitize_key( (string) ( $settings['aws_region'] ?? 'us-east-1' ) );
				if ( $region === '' ) {
					$region = 'us-east-1';
				}
				return [
					'host'       => 'email-smtp.' . $region . '.amazonaws.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Create SMTP credentials in Amazon SES (different from IAM access keys).', 'wp-powersuite' ),
				];

			case 'sparkpost':
				$region = sanitize_key( (string) ( $settings['sparkpost_region'] ?? 'us' ) );
				$host   = $region === 'eu' ? 'smtp.eu.sparkpostmail.com' : 'smtp.sparkpostmail.com';
				return [
					'host'       => $host,
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => 'SMTP_Injection',
					'hint'       => __( 'Username is SMTP_Injection. Password is your SparkPost API key.', 'wp-powersuite' ),
				];

			case 'elastic-email':
				return [
					'host'       => 'smtp.elasticemail.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use your Elastic Email account email and SMTP password / API key.', 'wp-powersuite' ),
				];

			case 'smtp2go':
				return [
					'host'       => 'mail.smtp2go.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP username and password from your SMTP2GO dashboard.', 'wp-powersuite' ),
				];

			case 'mailjet':
				return [
					'host'       => 'in-v3.mailjet.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use your Mailjet API key as username and Secret key as password.', 'wp-powersuite' ),
				];

			case 'mandrill':
				return [
					'host'       => 'smtp.mandrillapp.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Username is any non-empty value (often your account email). Password is your Mandrill API key.', 'wp-powersuite' ),
				];

			case 'sendlayer':
				return [
					'host'       => 'smtp.sendlayer.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP credentials from your SendLayer account.', 'wp-powersuite' ),
				];

			case 'smtpcom':
				return [
					'host'       => 'send.smtp.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use your SMTP.com channel credentials.', 'wp-powersuite' ),
				];

			case 'maileroo':
				return [
					'host'       => 'smtp.maileroo.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP username and sending key from Maileroo.', 'wp-powersuite' ),
				];

			case 'turbo-smtp':
				$region = sanitize_key( (string) ( $settings['turbo_smtp_region'] ?? 'us' ) );
				$host   = $region === 'eu' ? 'pro.eu.turbo-smtp.com' : 'pro.turbo-smtp.com';
				return [
					'host'       => $host,
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use your TurboSMTP username and password.', 'wp-powersuite' ),
				];

			case 'mail-baby':
				return [
					'host'       => 'relay.mailbaby.net',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => __( 'Use the SMTP username/password from GET /mail/{id} (host relay.mailbaby.net).', 'wp-powersuite' ),
				];

			case 'emailit':
				return [
					'host'       => 'smtp.emailit.com',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => 'emailit',
					'hint'       => __( 'Use your Emailit SMTP username and API key / password.', 'wp-powersuite' ),
				];

			default:
				return [
					'host'       => '',
					'port'       => '587',
					'encryption' => 'tls',
					'username'   => '',
					'hint'       => '',
				];
		}
	}

	/**
	 * Fill empty SMTP host/port/encryption/username from the provider profile.
	 *
	 * @param array<string, mixed> $settings
	 * @return array<string, mixed>
	 */
	public static function merge_smtp_settings( array $settings, string $provider_id ): array {
		if ( ! self::supports( $provider_id ) ) {
			return $settings;
		}

		$defaults = self::defaults( $provider_id, $settings );

		if ( trim( (string) ( $settings['smtp_host'] ?? '' ) ) === '' && $defaults['host'] !== '' ) {
			$settings['smtp_host'] = $defaults['host'];
		}
		if ( trim( (string) ( $settings['smtp_port'] ?? '' ) ) === '' ) {
			$settings['smtp_port'] = $defaults['port'];
		}
		if ( ! isset( $settings['smtp_encryption'] ) || (string) $settings['smtp_encryption'] === '' ) {
			$settings['smtp_encryption'] = $defaults['encryption'];
		}
		if ( trim( (string) ( $settings['smtp_username'] ?? '' ) ) === '' && $defaults['username'] !== '' ) {
			$settings['smtp_username'] = $defaults['username'];
		}
		if ( ! isset( $settings['smtp_auth'] ) || (string) $settings['smtp_auth'] === '' ) {
			$settings['smtp_auth'] = '1';
		}

		return $settings;
	}

	/**
	 * Whether this connection should send via PHPMailer SMTP.
	 *
	 * @param array<string, mixed> $settings
	 */
	public static function connection_uses_smtp( array $settings, ?ProviderInterface $provider = null ): bool {
		$provider_id = sanitize_key( (string) ( $settings['provider'] ?? '' ) );
		if ( $provider && $provider->uses_phpmailer_smtp() ) {
			return true;
		}
		if ( $provider_id === 'smtp' ) {
			return true;
		}
		if ( ! self::supports( $provider_id ) ) {
			return false;
		}
		return sanitize_key( (string) ( $settings['transport'] ?? 'api' ) ) === 'smtp';
	}

	/**
	 * Whether this connection should send via HTTP API.
	 *
	 * @param array<string, mixed> $settings
	 */
	public static function connection_uses_api( array $settings, ?ProviderInterface $provider = null ): bool {
		if ( ! $provider || ! $provider->uses_api_transport() ) {
			return false;
		}
		return ! self::connection_uses_smtp( $settings, $provider );
	}
}
