<?php
/**
 * File Path: includes/api/class-mbs-esp-registry.php
 *
 * ESP Registry — My Book Showroom
 *
 * Resolves the active Email Service Provider platform slug to an instantiated
 * MBS_ESP_Adapter. Returns null when no platform is configured, the key is
 * empty, or the license is not PRO/PUB.
 *
 * Sprint F-026: Email Channel Integration.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Api
 * @since      1.1.0
 */

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

class MBS_ESP_Registry { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Platform slug -> concrete adapter class map.
	 *
	 * @var array<string, string>
	 */
	private const PLATFORM_MAP = array(
		'mailchimp'       => 'MBS_ESP_Mailchimp',
		'mailerlite'      => 'MBS_ESP_MailerLite',
		'convertkit'      => 'MBS_ESP_ConvertKit',
		'brevo'           => 'MBS_ESP_Brevo',
		'sendy'           => 'MBS_ESP_Sendy',
		'activecampaign'  => 'MBS_ESP_ActiveCampaign',
		'getresponse'     => 'MBS_ESP_GetResponse',
		'campaignmonitor' => 'MBS_ESP_CampaignMonitor',
		'moosend'         => 'MBS_ESP_Moosend',
		'flodesk'         => 'MBS_ESP_Flodesk',
		'authoremail'     => 'MBS_ESP_AuthorEmail',
	);

	/**
	 * Return an instantiated adapter for the active platform, or null if
	 * no platform is configured or the plugin is not PRO.
	 *
	 * @return MBS_ESP_Adapter|null
	 */
	public static function get_adapter(): ?MBS_ESP_Adapter {
		if ( ! mbs_is_pro() ) {
			return null;
		}

		$platform = self::get_platform();
		if ( '' === $platform ) {
			return null;
		}

		$api_key = self::get_api_key();
		if ( '' === $api_key ) {
			return null;
		}

		if ( ! isset( self::PLATFORM_MAP[ $platform ] ) ) {
			return null;
		}

		$class = self::PLATFORM_MAP[ $platform ];
		if ( ! class_exists( $class ) ) {
			return null;
		}

		$account_url = self::get_account_url();

		if ( in_array( $platform, array( 'sendy', 'activecampaign' ), true ) ) {
			return new $class( $api_key, $account_url );
		}

		return new $class( $api_key );
	}

	/**
	 * Return the active platform slug from mbs_setting_email_platform.
	 * Returns '' if not set.
	 *
	 * @return string
	 */
	public static function get_platform(): string {
		$platform = (string) get_option( 'mbs_setting_email_platform', '' );
		$allowed  = array_keys( self::PLATFORM_MAP );
		if ( ! in_array( $platform, $allowed, true ) ) {
			return '';
		}
		return $platform;
	}

	/**
	 * Return the decrypted API key for the active platform.
	 * Returns '' if not configured.
	 *
	 * @return string
	 */
	public static function get_api_key(): string {
		$platform = self::get_platform();
		if ( '' === $platform ) {
			return '';
		}

		$option_map = array(
			'mailchimp'       => 'mbs_setting_email_mailchimp_api_key',
			'mailerlite'      => 'mbs_setting_email_mailerlite_api_key',
			'convertkit'      => 'mbs_setting_email_convertkit_api_key',
			'brevo'           => 'mbs_setting_email_brevo_api_key',
			'sendy'           => 'mbs_setting_email_sendy_api_key',
			'activecampaign'  => 'mbs_setting_email_activecampaign_api_key',
			'getresponse'     => 'mbs_setting_email_getresponse_api_key',
			'campaignmonitor' => 'mbs_setting_email_campaignmonitor_api_key',
			'moosend'         => 'mbs_setting_email_moosend_api_key',
			'flodesk'         => 'mbs_setting_email_flodesk_api_key',
			'authoremail'     => 'mbs_setting_email_authoremail_api_key',
		);

		$option_key = $option_map[ $platform ] ?? '';
		if ( '' === $option_key ) {
			return '';
		}

		return mbs_decrypt_option( $option_key );
	}

	/**
	 * Return the account URL for Sendy / ActiveCampaign.
	 * Returns '' for other platforms.
	 *
	 * @return string
	 */
	public static function get_account_url(): string {
		$platform = self::get_platform();

		$url_map = array(
			'sendy'          => 'mbs_setting_email_sendy_url',
			'activecampaign' => 'mbs_setting_email_activecampaign_url',
		);

		if ( ! isset( $url_map[ $platform ] ) ) {
			return '';
		}

		return mbs_decrypt_option( $url_map[ $platform ] );
	}

	/**
	 * Instantiate an adapter directly from supplied credentials (for test-connection).
	 * Does NOT check mbs_is_pro() -- caller is responsible for PRO gate.
	 *
	 * @param string $platform    Platform slug.
	 * @param string $api_key     Raw (unencrypted) API key.
	 * @param string $account_url Raw account URL (Sendy/ActiveCampaign only).
	 * @return MBS_ESP_Adapter|null
	 */
	/**
	 * Map a platform slug to its display label.
	 *
	 * @param string $platform Platform slug.
	 * @return string
	 */
	public static function get_platform_label( string $platform ): string {
		$labels = array(
			'mailchimp'       => 'Mailchimp',
			'mailerlite'      => 'MailerLite',
			'convertkit'      => 'ConvertKit (Kit)',
			'brevo'           => 'Brevo',
			'sendy'           => 'Sendy',
			'activecampaign'  => 'ActiveCampaign',
			'getresponse'     => 'GetResponse',
			'campaignmonitor' => 'Campaign Monitor',
			'moosend'         => 'Moosend',
			'flodesk'         => 'Flodesk',
			'authoremail'     => 'Author.Email',
		);
		return $labels[ $platform ] ?? $platform;
	}

	public static function make_adapter( string $platform, string $api_key, string $account_url = '' ): ?MBS_ESP_Adapter {
		if ( ! isset( self::PLATFORM_MAP[ $platform ] ) ) {
			return null;
		}
		$class = self::PLATFORM_MAP[ $platform ];
		if ( ! class_exists( $class ) ) {
			return null;
		}
		if ( in_array( $platform, array( 'sendy', 'activecampaign' ), true ) ) {
			return new $class( $api_key, $account_url );
		}
		return new $class( $api_key );
	}
}
