<?php
/**
 * Bulk Post Page Class
 *
 * Manages bulk post generation page in WordPress admin
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Bulk Post Page class
 */
class SmartyPress_AI_Bulk_Post_Page {

	/**
	 * Page slug
	 *
	 * @var string
	 */
	private $page_slug = 'smartypress-ai-bulk-generator';

	/**
	 * Add menu page
	 */
	public function add_menu_page() {
		add_submenu_page(
			'smartypress-ai-settings',
			__( 'Bulk Post Generator', 'smartypress-ai-engine' ),
			__( 'Bulk Generator', 'smartypress-ai-engine' ),
			'publish_posts',
			$this->page_slug,
			array( $this, 'render_page' )
		);
	}

	/**
	 * Enqueue scripts
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_scripts( $hook ) {
		if ( strpos( $hook, $this->page_slug ) === false ) {
			return;
		}

		$settings = get_option( 'smartypress_ai_settings', array() );
		$post_types = get_post_types(
			array(
				'public' => true,
			),
			'objects'
		);

		// Enqueue compiled React app
		$asset_file = SPAI_CG_PLUGIN_DIR . '/assets/dist/content-generator.asset.php';
		
		if ( file_exists( $asset_file ) ) {
			$asset = require $asset_file;
		} else {
			$asset = array(
				'dependencies' => array( 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n' ),
				'version'      => SPAI_CG_PLUGIN_VERSION,
			);
		}

		wp_enqueue_script(
			'smartypress-ai-bulk-generator',
			SPAI_CG_PLUGIN_URL . '/assets/dist/content-generator.js',
			$asset['dependencies'],
			$asset['version'],
			true
		);

		wp_enqueue_style(
			'smartypress-ai-bulk-generator',
			SPAI_CG_PLUGIN_URL . '/assets/dist/content-generator.css',
			array( 'wp-components' ),
			SPAI_CG_PLUGIN_VERSION
		);

		// Get available providers (show all supported providers, not just configured ones)
		$available_providers = SmartyPress_AI_Providers::get_all();
		$all_supported_providers = array( 'openai', 'deepseek' );
		
		// Use configured providers if available, otherwise show all supported
		$providers_to_show = ! empty( $available_providers ) 
			? array_keys( $available_providers ) 
			: $all_supported_providers;

		wp_localize_script(
			'smartypress-ai-bulk-generator',
			'smartypressAIBulk',
			array(
				'restUrl'       => rest_url( 'smartypress/v1/' ),
				'nonce'         => wp_create_nonce( 'wp_rest' ),
				'pluginUrl'     => SPAI_CG_PLUGIN_URL,
				'adminUrl'      => admin_url(),
				'settings'      => $settings,
				'postTypes'     => array_map(
					function( $post_type ) {
						return array(
							'name'  => $post_type->name,
							'label' => $post_type->label,
						);
					},
					$post_types
				),
				'providers'     => $providers_to_show,
				'providerModels' => $this->get_provider_models(),
				'locale'        => get_locale(),
				'strings'       => array(
					'generating' => __( 'Generating posts...', 'smartypress-ai-engine' ),
					'completed'  => __( 'Generation completed!', 'smartypress-ai-engine' ),
					'error'      => __( 'Error occurred during generation', 'smartypress-ai-engine' ),
				),
			)
		);
	}

	/**
	 * Get provider models
	 *
	 * @return array Provider models.
	 */
	private function get_provider_models() {
		$provider_models = array();
		$providers = SmartyPress_AI_Providers::get_all();

		foreach ( $providers as $provider_name => $provider ) {
			if ( method_exists( $provider, 'get_available_models' ) ) {
				$provider_models[ $provider_name ] = $provider->get_available_models();
			}
		}

		return $provider_models;
	}

	/**
	 * Render page
	 */
	public function render_page() {
		if ( ! current_user_can( 'publish_posts' ) ) {
			return;
		}
		?>
		<div class="wrap">
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
			<div id="smartypress-ai-bulk-generator-root"></div>
		</div>
		<?php
	}
}

