<?php
/**
 * Feature gating helpers for free/pro capabilities.
 *
 * @package Vedrixa_Forms
 */

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

/**
 * Central feature gate manager.
 */
class WPEFB_Features {

	/**
	 * Check whether pro capabilities are active.
	 *
	 * @return bool
	 */
	public static function is_pro_active() {
		/**
		 * Filters whether Vedrixa Forms Pro capabilities are active.
		 *
		 * @param bool $is_pro_active Default false.
		 */
		return (bool) apply_filters( 'wpefb_is_pro_active', false );
	}

	/**
	 * Free field types.
	 *
	 * @return array
	 */
	public static function get_free_field_types() {
		$default = array(
			'heading',
			'paragraph',
			'text',
			'textarea',
			'email',
			'number',
			'select',
			'radio',
			'checkbox',
			'DatePicker',
			'first_name',
			'last_name',
			'user_name',
			'user_email',
			'user_pass',
			'confirm_pass',
			'country',
			'timezone',
			'nickname',
			'user_url',
			'description',
			'mobile_number',
			'phone_number',
			'gender',
			'language',
			'facebook',
			'twitter',
			'google',
			'linked_in',
			'youtube',
			'mixcloud',
			'soundcloud',
			'instagram',
			'divider',
			'spacing',
		);

		/**
		 * Filters the free field type list.
		 *
		 * @param array $default Default list.
		 */
		return array_values( array_unique( (array) apply_filters( 'wpefb_free_field_types', $default ) ) );
	}

	/**
	 * Pro-only field types.
	 *
	 * @return array
	 */
	public static function get_pro_field_types() {
		$default = array(
			'term_checkbox',
			'file',
			'repeatable_text',
			'user_avatar',
			'multi_dropdown',
		);

		/**
		 * Filters the pro-only field type list.
		 *
		 * @param array $default Default list.
		 */
		return array_values( array_unique( (array) apply_filters( 'wpefb_pro_field_types', $default ) ) );
	}

	/**
	 * Field types that are currently locked in this install.
	 *
	 * @return array
	 */
	public static function get_locked_field_types() {
		if ( self::is_pro_active() ) {
			return array();
		}

		return self::get_pro_field_types();
	}

	/**
	 * Pro feature modules displayed in configure screens.
	 *
	 * @return array
	 */
	public static function get_pro_modules() {
		$default = array(
			'advanced_workflow'     => __( 'Advanced Conditional Logic', 'vedrixa-forms-registration-builder' ),
			'progressive_forms'     => __( 'Progressive Forms', 'vedrixa-forms-registration-builder' ),
			'validation_ux'         => __( 'Advanced Validation UX', 'vedrixa-forms-registration-builder' ),
			'file_upload_controls'  => __( 'Enhanced File Upload', 'vedrixa-forms-registration-builder' ),
			'email_workflows'       => __( 'Advanced Email Workflows', 'vedrixa-forms-registration-builder' ),
			'user_lifecycle'        => __( 'User Lifecycle Controls', 'vedrixa-forms-registration-builder' ),
			'submission_management' => __( 'Submission Management Pro', 'vedrixa-forms-registration-builder' ),
			'templates_styling'     => __( 'Templates & Styling Pro', 'vedrixa-forms-registration-builder' ),
			'export_filters'        => __( 'Export & Advanced Filters', 'vedrixa-forms-registration-builder' ),
			'priority_support'      => __( 'Priority Support', 'vedrixa-forms-registration-builder' ),
		);

		/**
		 * Filters pro feature module list.
		 *
		 * @param array $default Module list keyed by configure type slug.
		 */
		return (array) apply_filters( 'wpefb_pro_modules', $default );
	}

	/**
	 * Get configure screen types (core + pro when available).
	 *
	 * @return array
	 */
	public static function get_configure_types() {
		$types = array( 'general', 'accounts', 'post_submission', 'auto_responder', 'limits', 'email_templates' );

		if ( self::is_pro_active() ) {
			$types = array_merge( $types, array_keys( self::get_pro_modules() ) );
		}

		/**
		 * Filters configure types for form dashboard.
		 *
		 * @param array $types Configure types.
		 */
		return array_values( array_unique( (array) apply_filters( 'wpefb_configure_types', $types ) ) );
	}

	/**
	 * Check if a field type can be used in current plan.
	 *
	 * @param string $field_type Field type.
	 * @return bool
	 */
	public static function is_field_type_available( $field_type ) {
		$field_type = (string) $field_type;

		if ( '' === $field_type ) {
			return false;
		}

		if ( in_array( $field_type, self::get_free_field_types(), true ) ) {
			return true;
		}

		if ( self::is_pro_active() && in_array( $field_type, self::get_pro_field_types(), true ) ) {
			return true;
		}

		/**
		 * Filters whether an individual field type is available.
		 *
		 * @param bool   $available  Whether field is available.
		 * @param string $field_type Field type.
		 */
		return (bool) apply_filters( 'wpefb_is_field_type_available', false, $field_type );
	}
}
