<?php
/**
 * Content Generator Module Class
 *
 * Main module class for content generation features
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Content Generator Module class
 */
class SmartyPress_AI_Content_Generator_Module extends SmartyPress_AI_Module_Base {

	/**
	 * Single Post Generator instance
	 *
	 * @var SmartyPress_AI_Single_Post_Generator
	 */
	private $single_post_generator;

	/**
	 * Bulk Post Generator instance
	 *
	 * @var SmartyPress_AI_Bulk_Post_Generator
	 */
	private $bulk_post_generator;

	/**
	 * Gutenberg Integration instance
	 *
	 * @var SmartyPress_AI_Gutenberg_Integration
	 */
	private $gutenberg_integration;

	/**
	 * Classic Editor Integration instance
	 *
	 * @var SmartyPress_AI_Classic_Editor_Integration
	 */
	private $classic_editor_integration;

	/**
	 * Constructor
	 *
	 * @param array $providers AI providers instances.
	 * @param array $settings Plugin settings.
	 */
	public function __construct( $providers = array(), $settings = array() ) {
		$this->module_id      = 'content_generator';
		$this->module_name    = 'Content Generator';
		$this->module_version = '1.0.0';

		parent::__construct( $providers, $settings );
	}

	/**
	 * Initialize module components
	 */
	public function init() {
		if ( ! $this->is_enabled() ) {
			return;
		}

		// Initialize subcomponents
		$this->single_post_generator = new SmartyPress_AI_Single_Post_Generator( $this->providers, $this->settings );
		$this->bulk_post_generator   = new SmartyPress_AI_Bulk_Post_Generator( $this->providers, $this->settings );
		$this->gutenberg_integration  = new SmartyPress_AI_Gutenberg_Integration( $this->providers );
		$this->classic_editor_integration = new SmartyPress_AI_Classic_Editor_Integration( $this->providers );

		// Register Gutenberg integration
		add_action( 'enqueue_block_editor_assets', array( $this->gutenberg_integration, 'enqueue_editor_assets' ) );
		
		// Register Classic Editor integration
		$this->classic_editor_integration->init();

		// Register settings and hooks first
		$this->register_settings();
		$this->register_hooks();

		// Register REST API routes after components are initialized
		// Register on init hook to ensure module is loaded, then hook into rest_api_init
		add_action( 'init', function() {
			// Register on rest_api_init with priority to ensure components are ready
			add_action( 'rest_api_init', array( $this, 'register_module_rest_routes' ), 20 );

			$this->register_post_meta_fields();
		}, 20 );

		// Register admin pages if in admin
		if ( is_admin() ) {
			$this->register_admin_pages();
		}
	}

	/**
	 * Register module settings
	 */
	public function register_settings() {
		// Module settings are registered via WordPress hooks
		do_action( 'smartypress_ai_module_content_generator_settings', $this );
	}

	/**
	 * Get module settings
	 *
	 * @return array Module settings.
	 */
	public function get_module_settings() {
		return array(
			'enabled' => $this->is_enabled(),
		);
	}

	/**
	 * Register custom post meta fields exposed via REST API.
	 */
	private function register_post_meta_fields() {
		$post_types = get_post_types( array( 'public' => true ) );

		foreach ( $post_types as $post_type ) {
			register_post_meta( $post_type, '_smartypress_ai_generation_language', array(
				'show_in_rest'  => true,
				'single'        => true,
				'type'          => 'string',
				'auth_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
			) );
		}
	}

	/**
	 * Register REST API routes
	 */
	protected function register_rest_routes() {
		// Register routes on rest_api_init hook to ensure components are initialized
		add_action( 'rest_api_init', array( $this, 'register_module_rest_routes' ) );
	}

	/**
	 * Register module REST API routes
	 */
	public function register_module_rest_routes() {
		// Ensure components are initialized
		if ( ! $this->single_post_generator || ! $this->bulk_post_generator ) {
			return;
		}

		$single_routes = new SmartyPress_AI_Content_Generator_Single_Generation_Routes();
		$single_routes->register( $this->single_post_generator, $this );

		$bulk_legacy_controller = new SmartyPress_AI_Content_Generator_Bulk_Legacy_Job_Controller( $this->bulk_post_generator );
		$bulk_legacy_routes     = new SmartyPress_AI_Content_Generator_Bulk_Legacy_Routes();
		$bulk_legacy_routes->register( $bulk_legacy_controller, $this );

		$bulk_plan_controller = new SmartyPress_AI_Content_Generator_Bulk_Plan_Controller();
		$bulk_plan_routes     = new SmartyPress_AI_Content_Generator_Bulk_Plan_Routes();
		$bulk_plan_routes->register( $bulk_plan_controller, $this );
	}

	/**
	 * Register admin pages
	 *
	 * Note: Admin pages are now registered through the main admin app
	 * This method is kept for backward compatibility but does nothing
	 */
	protected function register_admin_pages() {
		// Admin pages are now handled by the main admin app with React Router
		// No need to register separate menu pages here
	}

	/**
	 * Check REST API permissions
	 *
	 * @return bool
	 */
	public function check_permissions() {
		return current_user_can( 'edit_posts' );
	}

	/**
	 * Check bulk generation permissions
	 *
	 * @return bool
	 */
	public function check_bulk_permissions() {
		return current_user_can( 'publish_posts' );
	}
}

