<?php
/**
 * General > Schema tab renderer.
 *
 * @package SEOvaultSEO\Admin
 */

namespace SEOvaultSEO\Admin;

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

/**
 * Server-renders the Schema settings tab without requiring JavaScript.
 */
class SchemaSettingsTab {

	/**
	 * Render the Schema tab.
	 *
	 * @return void
	 */
	public static function render() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		settings_errors( 'seovault_seo_schema' );

		self::render_v2();
	}

	/**
	 * Render the V2 sectioned Schema tab.
	 *
	 * @return void
	 */
	public static function render_v2() {
		$view     = ( new SchemaSettingsTabPresenter() )->present();
		$settings = isset( $view['settings'] ) && is_array( $view['settings'] ) ? $view['settings'] : array();
		$revision = isset( $settings['settings_revision'] ) ? (int) $settings['settings_revision'] : 0;
		?>
		<div
			class="seovaultseo-schema-settings seovaultseo-schema-settings-v2"
			id="seovaultseo-schema-settings-v2"
			data-settings-revision="<?php echo esc_attr( (string) $revision ); ?>"
		>
			<h2>
				<span class="dashicons dashicons-list-view" aria-hidden="true"></span>
				<?php esc_html_e( 'Structured Data (Schema)', 'seovault-ai' ); ?>
			</h2>
			<p>
				<?php esc_html_e( 'SEOvault automatically adds basic schema for your site and pages. Add FAQ, HowTo, Recipe, Event, Course, Job, Review, or Software schema only when that content really exists.', 'seovault-ai' ); ?>
			</p>

			<div id="seovaultseo-schema-v2-notices" class="seovaultseo-schema-v2-notices" aria-live="polite"></div>

			<?php self::render_migration_banner( $view['migration'] ); ?>
			<?php self::render_output_section( $view['output'] ); ?>
			<?php self::render_automatic_section( $view['automatic'] ); ?>
			<?php self::render_additional_section( $view['additional'] ); ?>
			<?php self::render_templates_section( $view['templates'] ); ?>
			<?php self::render_advanced_section( $view['advanced'] ); ?>

			<p class="submit">
				<button type="button" class="button button-primary" id="seovaultseo-save-schema-v2">
					<span class="dashicons dashicons-saved" aria-hidden="true"></span>
					<?php esc_html_e( 'Save Schema Settings', 'seovault-ai' ); ?>
				</button>
				<span id="seovaultseo-schema-v2-saved-msg" class="seovaultseo-saved-msg" style="display:none;" role="status">
					<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
					<?php esc_html_e( 'Saved!', 'seovault-ai' ); ?>
				</span>
			</p>
		</div>
		<?php
	}


	/**
	 * Render the optional migration notice banner.
	 *
	 * @param array $banner Migration banner view model.
	 * @return void
	 */
	private static function render_migration_banner( array $banner ) {
		if ( empty( $banner['show'] ) ) {
			return;
		}
		?>
		<div class="notice notice-info inline seovaultseo-schema-migration-banner" role="status">
			<p><strong><?php echo esc_html( $banner['title'] ); ?></strong></p>
			<p><?php echo esc_html( $banner['message'] ); ?></p>
			<p>
				<a class="button button-secondary" href="<?php echo esc_url( $banner['review_url'] ); ?>">
					<?php echo esc_html( $banner['review_label'] ); ?>
				</a>
			</p>
		</div>
		<?php
	}

	/**
	 * Render Schema output status and compatibility copy.
	 *
	 * @param array $section Output section view model.
	 * @return void
	 */
	private static function render_output_section( array $section ) {
		?>
		<section class="seovaultseo-schema-section" id="seovaultseo-schema-output" aria-labelledby="seovaultseo-schema-output-title">
			<h3 id="seovaultseo-schema-output-title"><?php echo esc_html( $section['title'] ); ?></h3>
			<p class="seovaultseo-schema-status">
				<strong><?php echo esc_html( $section['status_label'] ); ?></strong>
			</p>
			<p><?php echo esc_html( $section['explanation'] ); ?></p>
			<p>
				<label class="seovaultseo-check seovaultseo-schema-v2-toggle">
					<input
						type="checkbox"
						id="seovaultseo-schema-v2-enabled"
						class="seovaultseo-schema-v2-field"
						data-v2-field="schema_enabled"
						<?php checked( ! empty( $section['enabled'] ) ); ?>
					/>
					<span></span>
					<span><?php echo esc_html( $section['toggle_label'] ); ?></span>
				</label>
			</p>
			<p class="description"><?php echo esc_html( $section['compatibility'] ); ?></p>
			<p>
				<a href="<?php echo esc_url( $section['docs_url'] ); ?>" target="_blank" rel="noopener noreferrer">
					<?php echo esc_html( $section['docs_label'] ); ?>
				</a>
			</p>
		</section>
		<?php
	}

	/**
	 * Render automatic/foundational schema rows.
	 *
	 * @param array $section Automatic section view model.
	 * @return void
	 */
	private static function render_automatic_section( array $section ) {
		?>
		<section class="seovaultseo-schema-section" id="seovaultseo-schema-automatic" aria-labelledby="seovaultseo-schema-automatic-title">
			<h3 id="seovaultseo-schema-automatic-title"><?php echo esc_html( $section['title'] ); ?></h3>
			<p><?php echo esc_html( $section['intro'] ); ?></p>
			<?php if ( ! empty( $section['woo_inactive_note'] ) ) : ?>
				<p class="description"><?php echo esc_html( $section['woo_inactive_note'] ); ?></p>
			<?php endif; ?>

			<p>
				<label for="seovaultseo-schema-v2-identity">
					<strong><?php echo esc_html( $section['identity_label'] ); ?></strong>
				</label>
				<select
					id="seovaultseo-schema-v2-identity"
					class="seovaultseo-schema-v2-field"
					data-v2-field="publisher_identity"
				>
					<?php foreach ( $section['identity_options'] as $value => $label ) : ?>
						<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $section['publisher_identity'], $value ); ?>>
							<?php echo esc_html( $label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
			</p>

			<ul class="seovaultseo-schema-automatic-list">
				<?php foreach ( $section['rows'] as $row ) : ?>
					<li class="seovaultseo-schema-automatic-row" data-automatic-id="<?php echo esc_attr( $row['id'] ); ?>">
						<strong><?php echo esc_html( $row['label'] ); ?></strong>
						<p class="description"><?php echo esc_html( $row['when'] ); ?></p>
						<?php if ( ! empty( $row['toggle'] ) && is_array( $row['toggle'] ) ) : ?>
							<p>
								<label class="seovaultseo-check seovaultseo-schema-v2-toggle">
									<input
										type="checkbox"
										class="seovaultseo-schema-v2-field seovaultseo-schema-v2-automatic"
										data-v2-field="automatic"
										data-automatic-key="<?php echo esc_attr( $row['toggle']['key'] ); ?>"
										<?php checked( ! empty( $row['toggle']['enabled'] ) ); ?>
									/>
									<span></span>
									<span><?php echo esc_html( $row['toggle']['label'] ); ?></span>
								</label>
							</p>
						<?php endif; ?>
						<p>
							<a href="<?php echo esc_url( $row['settings_url'] ); ?>">
								<?php echo esc_html( $row['settings_label'] ); ?>
							</a>
						</p>
					</li>
				<?php endforeach; ?>
			</ul>
		</section>
		<?php
	}

	/**
	 * Render additional schema type catalogue rows.
	 *
	 * @param array $section Additional types section view model.
	 * @return void
	 */
	private static function render_additional_section( array $section ) {
		?>
		<section class="seovaultseo-schema-section" id="seovaultseo-schema-additional" aria-labelledby="seovaultseo-schema-additional-title">
			<h3 id="seovaultseo-schema-additional-title"><?php echo esc_html( $section['title'] ); ?></h3>
			<p><?php echo esc_html( $section['intro'] ); ?></p>
			<p>
				<label class="seovaultseo-check seovaultseo-schema-v2-toggle">
					<input
						type="checkbox"
						class="seovaultseo-schema-v2-field"
						data-v2-field="catalogue_show_extension_types"
						<?php checked( ! empty( $section['show_extension_types'] ) ); ?>
					/>
					<span></span>
					<span><?php echo esc_html( $section['show_extension_types_label'] ); ?></span>
				</label>
			</p>
			<ul class="seovaultseo-schema-additional-list">
				<?php foreach ( $section['items'] as $item ) : ?>
					<li
						class="seovaultseo-schema-additional-item<?php echo ! empty( $item['extension'] ) ? ' seovaultseo-schema-additional-extension' : ''; ?>"
						data-schema-type="<?php echo esc_attr( $item['type'] ); ?>"
					>
						<strong><?php echo esc_html( $item['label'] ); ?></strong>
						<code><?php echo esc_html( $item['type'] ); ?></code>
						<p><?php echo esc_html( $item['availability'] ); ?></p>
						<p class="description"><?php echo esc_html( $item['how'] ); ?></p>
						<p class="seovaultseo-schema-additional-actions">
							<a href="<?php echo esc_url( $item['learn_url'] ); ?>" target="_blank" rel="noopener noreferrer">
								<?php echo esc_html( $item['learn_label'] ); ?>
							</a>
							<span aria-hidden="true"> · </span>
							<a href="<?php echo esc_url( $item['template_url'] ); ?>">
								<?php echo esc_html( $item['template_label'] ); ?>
							</a>
							<span aria-hidden="true"> · </span>
							<a href="<?php echo esc_url( $item['posts_url'] ); ?>">
								<?php echo esc_html( $item['posts_label'] ); ?>
							</a>
						</p>
					</li>
				<?php endforeach; ?>
			</ul>
		</section>
		<?php
	}

	/**
	 * Render schema templates summary and manage links.
	 *
	 * @param array $section Templates section view model.
	 * @return void
	 */
	private static function render_templates_section( array $section ) {
		?>
		<section class="seovaultseo-schema-section" id="seovaultseo-schema-templates" aria-labelledby="seovaultseo-schema-templates-title">
			<h3 id="seovaultseo-schema-templates-title"><?php echo esc_html( $section['title'] ); ?></h3>
			<p><?php echo esc_html( $section['intro'] ); ?></p>
			<p class="description"><?php echo esc_html( $section['constraint'] ); ?></p>
			<p>
				<?php
				echo esc_html(
					sprintf(
						/* translators: 1: published count, 2: draft count. */
						__( 'Published templates: %1$d. Drafts: %2$d.', 'seovault-ai' ),
						(int) $section['published'],
						(int) $section['drafts']
					)
				);
				?>
			</p>
			<p>
				<a class="button button-secondary" href="<?php echo esc_url( $section['manage_url'] ); ?>">
					<?php echo esc_html( $section['manage_label'] ); ?>
				</a>
				<a class="button button-primary" href="<?php echo esc_url( $section['create_url'] ); ?>">
					<?php echo esc_html( $section['create_label'] ); ?>
				</a>
			</p>
			<?php
			// Keep the no-JS templates list toggle container for progressive enhancement hooks.
			?>
			<div id="seovaultseo-schema-templates-container" class="seovaultseo-schema-templates-embed" hidden></div>
		</section>
		<?php
	}

	/**
	 * Render advanced ruleset, custom JSON, hooks, and migration controls.
	 *
	 * @param array $section Advanced section view model.
	 * @return void
	 */
	private static function render_advanced_section( array $section ) {
		?>
		<section class="seovaultseo-schema-section" id="seovaultseo-schema-advanced" aria-labelledby="seovaultseo-schema-advanced-title">
			<h3 id="seovaultseo-schema-advanced-title"><?php echo esc_html( $section['title'] ); ?></h3>

			<h4><?php echo esc_html( $section['ruleset_label'] ); ?></h4>
			<p>
				<code><?php echo esc_html( (string) $section['ruleset_version'] ); ?></code>
			</p>

			<h4><?php echo esc_html( $section['custom_json_title'] ); ?></h4>
			<p><?php echo esc_html( $section['custom_json_text'] ); ?></p>

			<h4><?php echo esc_html( $section['hooks_title'] ); ?></h4>
			<p><?php echo esc_html( $section['hooks_text'] ); ?></p>
			<?php if ( ! empty( $section['hooks_list'] ) && is_array( $section['hooks_list'] ) ) : ?>
				<ul class="seovaultseo-schema-hooks-list">
					<?php foreach ( $section['hooks_list'] as $hook_name ) : ?>
						<li><code><?php echo esc_html( (string) $hook_name ); ?></code></li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>
			<h4><?php echo esc_html( $section['migration_title'] ); ?></h4>
			<p><?php echo esc_html( $section['migration_text'] ); ?></p>
			<?php if ( ! empty( $section['show_legacy_link'] ) ) : ?>
				<p class="description"><?php echo esc_html( $section['legacy_note'] ); ?></p>
				<p>
					<a class="button button-secondary" href="<?php echo esc_url( $section['migration_url'] ); ?>">
						<?php echo esc_html( $section['migration_label'] ); ?>
					</a>
				</p>
			<?php endif; ?>
		</section>
		<?php
	}
}
