<?php
/**
 * Admin menu, pages and settings registration.
 *
 * @package AccessLens
 */

declare(strict_types=1);

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

/**
 * AccessLens_Admin
 */
class AccessLens_Admin {

	/** Settings group name. */
	private const SETTINGS_GROUP = 'accesslens_audit_settings';

	/** Settings section ID. */
	private const SECTION_ID = 'accesslens_audit_section_engine';

	/**
	 * Register all hooks.
	 */
	public function register(): void {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_init', array( $this, 'register_settings' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	/**
	 * Add top-level admin menu.
	 */
	public function register_menu(): void {
		add_menu_page(
			__( 'AccessLens - Web Accessibility Audit', 'accesslens' ),
			__( 'AccessLens', 'accesslens' ),
			'manage_options',
			'accesslens',
			array( $this, 'render_dashboard' ),
			'dashicons-universal-access-alt',
			80
		);

		add_submenu_page(
			'accesslens',
			__( 'Dashboard', 'accesslens' ),
			__( 'Dashboard', 'accesslens' ),
			'manage_options',
			'accesslens',
			array( $this, 'render_dashboard' )
		);

		add_submenu_page(
			'accesslens',
			__( 'Ajustes', 'accesslens' ),
			__( 'Ajustes', 'accesslens' ),
			'manage_options',
			'accesslens-settings',
			array( $this, 'render_settings' )
		);
	}

	/**
	 * Register settings, sections and fields via Settings API.
	 */
	public function register_settings(): void {
		register_setting(
			self::SETTINGS_GROUP,
			'accesslens_engine_url',
			array(
				'type'              => 'string',
				'sanitize_callback' => 'esc_url_raw',
				'default'           => 'http://localhost:3000',
			)
		);

		add_settings_section(
			self::SECTION_ID,
			__( 'Configuracion del Motor', 'accesslens' ),
			array( $this, 'render_section_description' ),
			'accesslens-settings'
		);

		add_settings_field(
			'accesslens_engine_url',
			__( 'URL del Motor de Auditoria', 'accesslens' ),
			array( $this, 'render_field_engine_url' ),
			'accesslens-settings',
			self::SECTION_ID
		);
	}

	/**
	 * Render settings section description.
	 */
	public function render_section_description(): void {
		echo '<p>' . esc_html__( 'Configura la URL base del motor externo de auditoria de accesibilidad.', 'accesslens' ) . '</p>';
	}

	/**
	 * Render the engine URL field.
	 */
	public function render_field_engine_url(): void {
		$value = get_option( 'accesslens_engine_url', 'http://localhost:3000' );
		?>
		<input
			type="url"
			id="accesslens_engine_url"
			name="accesslens_engine_url"
			value="<?php echo esc_attr( $value ); ?>"
			class="regular-text"
			placeholder="http://localhost:3000"
		/>
		<p class="description">
			<?php esc_html_e( 'Ejemplo: http://localhost:3000 o https://tu-motor.ejemplo.com', 'accesslens' ); ?>
		</p>
		<?php
	}

	/**
	 * Enqueue admin JS + CSS only on plugin pages.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_assets( string $hook ): void {
		$allowed_hooks = array( 'toplevel_page_accesslens', 'accesslens_page_accesslens-settings' );
		if ( ! in_array( $hook, $allowed_hooks, true ) ) {
			return;
		}

		wp_enqueue_style(
			'accesslens-admin',
			ACCESSLENS_PLUGIN_URL . 'admin/css/admin.css',
			array(),
			ACCESSLENS_VERSION
		);

		wp_enqueue_script(
			'accesslens-admin',
			ACCESSLENS_PLUGIN_URL . 'admin/js/admin.js',
			array( 'jquery' ),
			ACCESSLENS_VERSION,
			true
		);

		wp_localize_script(
			'accesslens-admin',
			'accessLensData',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'accesslens_audit_nonce' ),
				'i18n'    => array(
					'scanning'      => __( 'Escaneando...', 'accesslens' ),
					'done'          => __( 'Completado', 'accesslens' ),
					'error'         => __( 'Error', 'accesslens' ),
					'noResults'     => __( 'Sin resultados todavia.', 'accesslens' ),
					'downloadReport'=> __( 'Descargar informe PDF', 'accesslens' ),
					'pollError'     => __( 'Error al consultar el estado del escaneo.', 'accesslens' ),
					'startError'    => __( 'Error al iniciar el escaneo.', 'accesslens' ),
					'queued'        => __( 'En cola', 'accesslens' ),
					'discovering'   => __( 'Descubriendo URLs', 'accesslens' ),
					'running'       => __( 'Analizando', 'accesslens' ),
					'progress'      => __( 'Progreso', 'accesslens' ),
				),
			)
		);
	}

	/**
	 * Render the dashboard page.
	 */
	public function render_dashboard(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'No tienes permisos para acceder a esta pagina.', 'accesslens' ) );
		}
		require_once ACCESSLENS_PLUGIN_DIR . 'admin/views/dashboard.php';
	}

	/**
	 * Render the settings page.
	 */
	public function render_settings(): void {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'No tienes permisos para acceder a esta pagina.', 'accesslens' ) );
		}
		require_once ACCESSLENS_PLUGIN_DIR . 'admin/views/settings.php';
	}
}
