<?php
/**
 * Supported form provider registry.
 *
 * @package SubmissionInbox
 */

namespace SUBIN\Integrations;

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

/**
 * Centralizes provider keys, labels, icons, and availability checks.
 */
class ProviderRegistry {
	const SUBMISSION_INBOX_FORM = 'submission_inbox_form';
	const CONTACT_FORM_7        = 'contact_form_7';
	const WPFORMS               = 'wpforms';
	const FLUENT_FORMS          = 'fluent_forms';
	const FORMINATOR            = 'forminator';
	const NINJA_FORMS           = 'ninja_forms';
	const GRAVITY_FORMS         = 'gravity_forms';

	/**
	 * Provider definitions.
	 *
	 * @return array<string,array<string,string>>
	 */
	public function all() {
		return array(
			self::SUBMISSION_INBOX_FORM => array(
				'label' => __( 'InboxMend Forms', 'submission-inbox' ),
				'icon'  => 'dashicons-feedback',
			),
			self::CONTACT_FORM_7        => array(
				'label' => __( 'Contact Form 7', 'submission-inbox' ),
				'icon'  => 'dashicons-email',
			),
			self::WPFORMS               => array(
				'label' => __( 'WPForms', 'submission-inbox' ),
				'icon'  => 'dashicons-forms',
			),
			self::FLUENT_FORMS          => array(
				'label' => __( 'Fluent Forms', 'submission-inbox' ),
				'icon'  => 'dashicons-forms',
			),
			self::FORMINATOR            => array(
				'label' => __( 'Forminator', 'submission-inbox' ),
				'icon'  => 'dashicons-forms',
			),
			self::NINJA_FORMS           => array(
				'label' => __( 'Ninja Forms', 'submission-inbox' ),
				'icon'  => 'dashicons-forms',
			),
			self::GRAVITY_FORMS         => array(
				'label' => __( 'Gravity Forms', 'submission-inbox' ),
				'icon'  => 'dashicons-forms',
			),
		);
	}

	/**
	 * Active provider keys supported by the current plugin release.
	 *
	 * @return array<int,string>
	 */
	public function active_keys() {
		return array(
			self::SUBMISSION_INBOX_FORM,
			self::CONTACT_FORM_7,
			self::WPFORMS,
			self::FLUENT_FORMS,
			self::FORMINATOR,
			self::NINJA_FORMS,
		);
	}

	/**
	 * Whether a provider key is active.
	 *
	 * @param string $provider Provider key.
	 * @return bool
	 */
	public function is_active( $provider ) {
		return in_array( sanitize_key( (string) $provider ), $this->active_keys(), true );
	}

	/**
	 * Sanitize provider to a known active key.
	 *
	 * @param string $provider Provider key.
	 * @return string
	 */
	public function sanitize( $provider ) {
		$provider = sanitize_key( (string) $provider );

		return $this->is_active( $provider ) ? $provider : '';
	}

	/**
	 * Provider label.
	 *
	 * @param string $provider Provider key.
	 * @return string
	 */
	public function label( $provider ) {
		$providers = $this->all();
		$provider  = sanitize_key( (string) $provider );

		return $providers[ $provider ]['label'] ?? __( 'Unknown source', 'submission-inbox' );
	}

	/**
	 * Provider dashicon.
	 *
	 * @param string $provider Provider key.
	 * @return string
	 */
	public function icon( $provider ) {
		$providers = $this->all();
		$provider  = sanitize_key( (string) $provider );

		return $providers[ $provider ]['icon'] ?? 'dashicons-forms';
	}

	/**
	 * Whether a provider plugin appears to be installed/active.
	 *
	 * @param string $provider Provider key.
	 * @return bool
	 */
	public function is_available( $provider ) {
		switch ( sanitize_key( (string) $provider ) ) {
			case self::SUBMISSION_INBOX_FORM:
				return true;
			case self::CONTACT_FORM_7:
				return defined( 'WPCF7_VERSION' ) || class_exists( 'WPCF7_ContactForm' );
			case self::WPFORMS:
				return function_exists( 'wpforms' ) || class_exists( 'WPForms' );
			case self::FLUENT_FORMS:
				return defined( 'FLUENTFORM' ) || function_exists( 'wpFluentForm' );
			case self::FORMINATOR:
				return defined( 'FORMINATOR_VERSION' ) || class_exists( 'Forminator' );
			case self::NINJA_FORMS:
				return defined( 'NINJA_FORMS_VERSION' ) || class_exists( 'Ninja_Forms' ) || function_exists( 'Ninja_Forms' );
			case self::GRAVITY_FORMS:
				return class_exists( 'GFForms' );
		}

		return false;
	}
}
