<?php
/**
 * CPT Module Manager - Handles enable/disable state for custom post type modules
 *
 * @package Sifency_Addons
 */

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

/**
 * Class Sifency_Addons_CPT_Module_Manager
 *
 * Manages the enable/disable state of CPT modules via WordPress options.
 * Stores module states in 'sifency_cpt_modules_status' option.
 */
class Sifency_Addons_CPT_Module_Manager {

	const OPTION_KEY = 'sifency_cpt_modules_status';

	/**
	 * Get all module configurations.
	 *
	 * @return array
	 */
	public static function get_all_modules() {
		$modules = include SIFENCY_TB_PATH . 'post-type/sifency-cpt-config.php';
		return apply_filters( 'sifency_addons_cpt_modules', $modules );
	}

	public static function is_pro_active() {
		if ( defined( 'SIFENCY_ADDONS_PRO' ) && SIFENCY_ADDONS_PRO ) {
			return true;
		}

		if ( class_exists( 'Sifency_Addons_Pro' ) ) {
			return true;
		}

		try {
			if ( function_exists( 'get_option' ) && get_option( 'sifency_addons_pro_active', false ) ) {
				return true;
			}
		} catch ( Throwable $e ) {
		}

		return false;
	}

	/**
	 * Check if a specific module is enabled.
	 *
	 * @param string $module_key The module key (e.g., 'team', 'service', 'blog').
	 * @return bool True if enabled, false otherwise.
	 */
	public static function is_module_enabled( $module_key ) {
		$modules_status = self::get_modules_status();

		if ( ! isset( $modules_status[ $module_key ] ) ) {
			if ( self::is_pro_active() ) {
				if ( 'shop' === $module_key ) {
					return class_exists( 'WooCommerce' );
				}
				if ( 'edd' === $module_key ) {
					return class_exists( 'Easy_Digital_Downloads' );
				}
				return true;
			}

			$all_modules = self::get_all_modules();
			if ( isset( $all_modules[ $module_key ]['enabled'] ) ) {
				return $all_modules[ $module_key ]['enabled'];
			}
			return false;
		}

		return (bool) $modules_status[ $module_key ];
	}

	/**
	 * Get all modules with their current enabled/disabled status.
	 *
	 * @return array {
	 *     @type string $module_key => bool $status
	 * }
	 */
	public static function get_modules_status() {
		return get_option( self::OPTION_KEY, array() );
	}

	/**
	 * Enable a specific module.
	 *
	 * @param string $module_key The module key.
	 * @return bool True on success.
	 */
	public static function enable_module( $module_key ) {
		$modules_status                = self::get_modules_status();
		$modules_status[ $module_key ] = true;
		return update_option( self::OPTION_KEY, $modules_status );
	}

	/**
	 * Disable a specific module.
	 *
	 * @param string $module_key The module key.
	 * @return bool True on success.
	 */
	public static function disable_module( $module_key ) {
		$modules_status                = self::get_modules_status();
		$modules_status[ $module_key ] = false;
		return update_option( self::OPTION_KEY, $modules_status );
	}

	/**
	 * Toggle a module's status.
	 *
	 * @param string $module_key The module key.
	 * @return bool The new status.
	 */
	public static function toggle_module( $module_key ) {
		$current_status = self::is_module_enabled( $module_key );
		$new_status     = ! $current_status;

		if ( $new_status ) {
			self::enable_module( $module_key );
		} else {
			self::disable_module( $module_key );
		}

		return $new_status;
	}

	/**
	 * Reset all modules to their default status from config.
	 *
	 * @return bool True on success.
	 */
	public static function reset_to_defaults() {
		$all_modules = self::get_all_modules();
		$defaults    = array();

		foreach ( $all_modules as $module_key => $config ) {
			$defaults[ $module_key ] = isset( $config['enabled'] ) ? $config['enabled'] : false;
		}

		return update_option( self::OPTION_KEY, $defaults );
	}

	/**
	 * Get enabled modules only.
	 *
	 * @return array Array of module keys that are enabled.
	 */
	public static function get_enabled_modules() {
		$all_modules = self::get_all_modules();
		$enabled     = array();

		foreach ( array_keys( $all_modules ) as $module_key ) {
			if ( self::is_module_enabled( $module_key ) ) {
				$enabled[] = $module_key;
			}
		}

		return $enabled;
	}

	/**
	 * Get configuration for a specific module.
	 *
	 * @param string $module_key The module key.
	 * @return array|null The module configuration or null if not found.
	 */
	public static function get_module_config( $module_key ) {
		$all_modules = self::get_all_modules();
		return $all_modules[ $module_key ] ?? null;
	}

	/**
	 * Load files and instantiate classes for enabled modules.
	 * This should be called during the plugin initialization.
	 *
	 * @return void
	 */
	public static function load_enabled_modules() {
		$all_modules = self::get_all_modules();
		$base_path   = SIFENCY_TB_PATH;

		$base_files = array(
			'post-type/base/class-cpt-factory-base.php',
			'post-type/base/class-single-post-render-base.php',
			'post-type/base/class-archive-post-render-base.php',
			'post-type/includes/class-cpt-ui-page.php',
			'post-type/includes/cpt-ui-ajax.php',
		);

		foreach ( $base_files as $base_file ) {
			if ( file_exists( $base_path . $base_file ) ) {
				require_once $base_path . $base_file;
			}
		}
		// ---------------------------------------------------------------------------------

		foreach ( $all_modules as $module_key => $config ) {
			if ( ! self::is_module_enabled( $module_key ) ) {
				continue;
			}

			// Load required files
			if ( isset( $config['files'] ) && is_array( $config['files'] ) ) {
				foreach ( $config['files'] as $file ) {
					$file_path = $base_path . $file;
					if ( file_exists( $file_path ) ) {
						require_once $file_path;
					}
				}
			}

			// Instantiate classes
			if ( isset( $config['classes'] ) && is_array( $config['classes'] ) ) {
				foreach ( $config['classes'] as $class_name ) {
					if ( class_exists( $class_name ) ) {
						new $class_name();
					}
				}
			}
		}
	}
}
