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

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * 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() {
		return include SIFENCY_TB_PATH . 'post-type/sifency-cpt-config.php';
	}

	/**
	 * Detect whether Pro version is active.
	 *
	 * This checks a few possible indicators: a constant, a Pro main class, or a stored option.
	 * Adjust detection as needed for your Pro packaging.
	 *
	 * @return bool
	 */
	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 ) {
			// ignore in environments where get_option isn't available yet
		}

		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' ) || function_exists( 'edd_get_download' ) );
				}

				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, [] );
	}

	/**
	 * 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 = [];
		
		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 = [];
		
		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;

		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();
					}
				}
			}
		}
	}
}
