<?php
/**
 * Admin gallery for Niwa template cards.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Template_Library_Page {
	const MENU_SLUG = 'niwa-ai-template-library';

	private $templates;
	private $elementor;
	private $page_hook_suffix = '';

	public function __construct( Niwa_AI_Template_Repository $templates, Niwa_AI_Elementor_Adapter $elementor ) {
		$this->templates = $templates;
		$this->elementor = $elementor;
	}

	public function register_hooks() {
		add_action( 'admin_menu', array( $this, 'register_menu' ), 12 );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function register_menu() {
		$this->page_hook_suffix = (string) add_submenu_page(
			'niwa-ai',
			__( 'Template Library', 'niwa-ai' ),
			__( 'Template Library', 'niwa-ai' ),
			'manage_options',
			self::MENU_SLUG,
			array( $this, 'render_page' )
		);
	}

	public function enqueue_assets( $hook ) {
		if ( $hook !== $this->page_hook_suffix ) {
			return;
		}
		wp_enqueue_style( 'niwa-template-library', NIWA_AI_URL . 'assets/admin/template-library/template-library.css', array(), NIWA_AI_VERSION );
		wp_enqueue_script( 'niwa-template-library', NIWA_AI_URL . 'assets/admin/template-library/template-library.js', array( 'wp-api-fetch' ), NIWA_AI_VERSION, true );
		wp_localize_script(
			'niwa-template-library',
			'NiwaTemplateLibrary',
			array(
				'restRoot'        => esc_url_raw( rest_url( 'niwa-ai/v1/template-library' ) ),
				'nonce'           => wp_create_nonce( 'wp_rest' ),
				'elementorActive' => $this->elementor->is_elementor_active(),
				'i18n'            => array(
					'useIdea'       => __( 'Use this idea', 'niwa-ai' ),
					'describe'      => __( 'Describe what you want', 'niwa-ai' ),
					'briefReady'    => __( 'Template brief ready. Open Niwa chat, add the target page details, then send.', 'niwa-ai' ),
					'chatReady'     => __( 'Niwa chat is ready with this template request. Add the target page/section details and send it.', 'niwa-ai' ),
					'chatMissing'   => __( 'Niwa chat is not available on this screen yet. Copy the prompt and send it from Niwa chat.', 'niwa-ai' ),
					'createFailed'  => __( 'Could not create snippet.', 'niwa-ai' ),
				),
			)
		);
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Insufficient permissions.', 'niwa-ai' ) );
		}
		$types     = Niwa_AI_Template_Library_Types::template_types();
		$labels    = Niwa_AI_Template_Library_Types::labels();
		$templates = $this->templates->list_templates( '', 80 );

		echo '<div class="wrap niwa-wrap niwa-template-library" data-elementor-active="' . esc_attr( $this->elementor->is_elementor_active() ? '1' : '0' ) . '">';
		echo '<div class="niwa-template-hero"><div class="niwa-template-logo">niwa</div><h1>' . esc_html__( 'What would you like to build?', 'niwa-ai' ) . '</h1>';
		echo '<div class="niwa-template-tabs" role="tablist">';
		foreach ( $types as $index => $type ) {
			echo '<button type="button" class="niwa-template-tab' . ( 0 === $index ? ' is-active' : '' ) . '" data-type="' . esc_attr( $type ) . '">' . esc_html( isset( $labels[ $type ] ) ? $labels[ $type ] : $type ) . '</button>';
		}
		echo '</div></div>';
		echo '<div class="niwa-template-library__bar"><span>' . esc_html__( 'Curated prompts to start building quickly.', 'niwa-ai' ) . '</span><a href="' . esc_url( admin_url( 'admin.php?page=niwa-ai-code-snippets' ) ) . '">' . esc_html__( 'Code Snippets', 'niwa-ai' ) . '</a></div>';
		echo '<section class="niwa-template-brief" data-niwa-template-brief hidden>';
		echo '<div><strong>' . esc_html__( 'Selected idea', 'niwa-ai' ) . '</strong><p>' . esc_html__( 'This does not create a shortcode. It prepares a Niwa chat request so Niwa can build the real Elementor section on the page you choose.', 'niwa-ai' ) . '</p></div>';
		echo '<textarea rows="5" data-niwa-template-brief-text aria-label="' . esc_attr__( 'Niwa chat template request', 'niwa-ai' ) . '"></textarea>';
		echo '<div class="niwa-template-brief__actions"><button type="button" class="button button-primary" data-niwa-template-open-chat>' . esc_html__( 'Open in Niwa chat', 'niwa-ai' ) . '</button><button type="button" class="button" data-niwa-template-copy>' . esc_html__( 'Copy prompt', 'niwa-ai' ) . '</button></div>';
		echo '</section>';
		echo '<div class="niwa-template-grid">';
		foreach ( $templates as $template ) {
			$this->render_card( $template );
		}
		echo '</div>';
		echo '<div class="niwa-template-or"><span></span><em>' . esc_html__( 'or', 'niwa-ai' ) . '</em><span></span></div>';
		echo '<form class="niwa-template-create" data-niwa-template-create><input type="text" name="title" placeholder="' . esc_attr__( 'Create a new reusable idea card', 'niwa-ai' ) . '" required><select name="type">';
		foreach ( $types as $type ) {
			echo '<option value="' . esc_attr( $type ) . '">' . esc_html( isset( $labels[ $type ] ) ? $labels[ $type ] : $type ) . '</option>';
		}
		echo '</select><button type="submit" class="button button-primary">' . esc_html__( 'Add template', 'niwa-ai' ) . '</button></form>';
		echo '<div class="niwa-template-notice" data-niwa-template-notice hidden></div>';
		echo '</div>';
	}

	private function render_card( $template ) {
		echo '<article class="niwa-template-card" data-type="' . esc_attr( (string) $template['type'] ) . '" data-template-id="' . esc_attr( (string) $template['id'] ) . '" data-template-title="' . esc_attr( (string) $template['title'] ) . '">';
		echo '<div class="niwa-template-card__media">';
		if ( ! empty( $template['preview_url'] ) ) {
			echo '<img src="' . esc_url( $template['preview_url'] ) . '" alt="">';
		}
		echo '</div>';
		echo '<h2>' . esc_html( (string) $template['title'] ) . '</h2>';
		echo '<p>' . esc_html( (string) $template['subtitle'] ) . '</p>';
		echo '<button type="button" class="button niwa-template-use" data-template-id="' . esc_attr( (string) $template['id'] ) . '">' . esc_html__( 'Use in Niwa chat', 'niwa-ai' ) . '</button>';
		echo '</article>';
	}
}
