<?php
/**
 * Admin page for generated code snippets.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Snippets_Page {
	const MENU_SLUG = 'niwa-ai-code-snippets';

	private $snippets;
	private $page_hook_suffix = '';

	public function __construct( Niwa_AI_Snippet_Repository $snippets ) {
		$this->snippets = $snippets;
	}

	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',
			__( 'Code Snippets', 'niwa-ai' ),
			__( 'Code Snippets', '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' ),
				'i18n'     => array(
					'validated'     => __( 'Snippet validated.', 'niwa-ai' ),
					'published'     => __( 'Snippet published.', 'niwa-ai' ),
					'actionFailed'   => __( 'Snippet action failed.', 'niwa-ai' ),
				),
			)
		);
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Insufficient permissions.', 'niwa-ai' ) );
		}
		$snippets = $this->snippets->list_snippets( '', 100, false );
		echo '<div class="wrap niwa-wrap niwa-snippets-page">';
		echo '<div class="niwa-page-header"><h1 class="niwa-page-header__title">' . esc_html__( 'Code Snippets', 'niwa-ai' ) . '</h1><p class="niwa-page-header__subtitle">' . esc_html__( 'Generated template artifacts Niwa can validate, publish, and reuse in Elementor or as shortcodes.', 'niwa-ai' ) . '</p></div>';
		echo '<div class="niwa-snippet-table-wrap"><table class="widefat fixed striped niwa-snippet-table"><thead><tr><th>' . esc_html__( 'Name', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Type', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Files', 'niwa-ai' ) . '</th><th>' . esc_html__( 'State', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Shortcode', 'niwa-ai' ) . '</th><th>' . esc_html__( 'Actions', 'niwa-ai' ) . '</th></tr></thead><tbody>';
		if ( empty( $snippets ) ) {
			echo '<tr><td colspan="6">' . esc_html__( 'No snippets yet. Use Template Library or ask Niwa in chat to create one.', 'niwa-ai' ) . '</td></tr>';
		}
		foreach ( $snippets as $snippet ) {
			echo '<tr data-snippet-id="' . esc_attr( (string) $snippet['id'] ) . '">';
			echo '<td><strong>' . esc_html( (string) $snippet['title'] ) . '</strong><br><small>v' . esc_html( (string) $snippet['version'] ) . '</small></td>';
			echo '<td>' . esc_html( (string) $snippet['type_label'] ) . '</td>';
			echo '<td>' . esc_html( (string) $snippet['file_count'] ) . '</td>';
			echo '<td><span class="niwa-snippet-state niwa-snippet-state--' . esc_attr( (string) $snippet['status'] ) . '">' . esc_html( (string) $snippet['status'] ) . '</span></td>';
			echo '<td><code>' . esc_html( (string) $snippet['shortcode'] ) . '</code></td>';
			echo '<td><button type="button" class="button" data-niwa-snippet-validate="' . esc_attr( (string) $snippet['id'] ) . '">' . esc_html__( 'Validate', 'niwa-ai' ) . '</button> <button type="button" class="button button-primary" data-niwa-snippet-publish="' . esc_attr( (string) $snippet['id'] ) . '">' . esc_html__( 'Publish', 'niwa-ai' ) . '</button></td>';
			echo '</tr>';
			if ( ! empty( $snippet['last_error'] ) ) {
				echo '<tr class="niwa-snippet-error"><td colspan="6">' . esc_html( (string) $snippet['last_error'] ) . '</td></tr>';
			}
		}
		echo '</tbody></table></div><div class="niwa-template-notice" data-niwa-template-notice hidden></div></div>';
	}
}
