<?php
/**
 * Module Base Class
 *
 * Abstract base class for all modules
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Abstract module base class
 */
abstract class SmartyPress_AI_Module_Base {

	/**
	 * Module ID
	 *
	 * @var string
	 */
	protected $module_id;

	/**
	 * Module name
	 *
	 * @var string
	 */
	protected $module_name;

	/**
	 * Module version
	 *
	 * @var string
	 */
	protected $module_version;

	/**
	 * Plugin settings
	 *
	 * @var array
	 */
	protected $settings;

	/**
	 * AI Providers instances
	 *
	 * @var array
	 */
	protected $providers;

	/**
	 * Constructor
	 *
	 * @param array $providers AI providers instances.
	 * @param array $settings Plugin settings.
	 */
	public function __construct( $providers = array(), $settings = array() ) {
		$this->providers = $providers;
		$this->settings  = $settings;
		$this->init();
	}

	/**
	 * Initialize module
	 * Can be overridden in child class
	 */
	public function init() {
		$this->register_settings();
		$this->register_hooks();
		$this->register_rest_routes();

		if ( is_admin() ) {
			$this->register_admin_pages();
		}
	}

	/**
	 * Register module settings
	 * Override in child class
	 */
	abstract public function register_settings();

	/**
	 * Get module settings
	 * Override in child class
	 *
	 * @return array Module settings.
	 */
	abstract public function get_module_settings();

	/**
	 * Register WordPress hooks
	 * Override in child class if needed
	 */
	protected function register_hooks() {
		// Override in child class
	}

	/**
	 * Register REST API routes
	 * Override in child class if needed
	 */
	protected function register_rest_routes() {
		// Override in child class
	}

	/**
	 * Register admin pages
	 * Override in child class if needed
	 */
	protected function register_admin_pages() {
		// Override in child class
	}

	/**
	 * Get module ID
	 *
	 * @return string Module ID.
	 */
	public function get_module_id() {
		return $this->module_id;
	}

	/**
	 * Get module name
	 *
	 * @return string Module name.
	 */
	public function get_module_name() {
		return $this->module_name;
	}

	/**
	 * Get module version
	 *
	 * @return string Module version.
	 */
	public function get_module_version() {
		return $this->module_version;
	}

	/**
	 * Check if module is enabled
	 *
	 * @return bool True if enabled, false otherwise.
	 */
	public function is_enabled() {
		// If settings are empty, enable module by default
		if ( empty( $this->settings ) ) {
			return true;
		}

		$modules = $this->settings['modules'] ?? array();
		
		// If modules array is empty or module is not set, enable by default
		if ( empty( $modules ) || ! isset( $modules[ $this->module_id ] ) ) {
			return true;
		}

		return $modules[ $this->module_id ];
	}

	/**
	 * Get provider instance
	 *
	 * @param string $provider_name Provider name.
	 * @return SmartyPress_AI_AI_Provider|null
	 */
	protected function get_provider( $provider_name = '' ) {
		if ( empty( $provider_name ) ) {
			$provider_name = $this->settings['default_provider'] ?? '';
		}

		// Get provider from static class to ensure we have the latest instance
		// This is important when settings are updated and providers are re-initialized
		$provider = null;
		
		if ( ! empty( $provider_name ) ) {
			$provider = SmartyPress_AI_Providers::get_provider( $provider_name );
		}
		
		// If provider doesn't exist or wasn't found, get first available provider
		if ( ! $provider ) {
			$available = SmartyPress_AI_Providers::get_available_providers();
			if ( ! empty( $available ) ) {
				$first_available = array_keys( $available )[0];
				$provider = $available[ $first_available ];
			}
		}
		
		// Fallback to instance providers if static class doesn't have it
		if ( ! $provider && ! empty( $provider_name ) && isset( $this->providers[ $provider_name ] ) ) {
			$provider = $this->providers[ $provider_name ];
		}
		
		return $provider;
	}
}

