<?php
/**
 * Tools & Settings page
 *
 * Advanced tools, cache management, and configuration.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Page_Tools extends BW_Schema_Page {

	/**
	 * Initialize page properties
	 *
	 * @return void
	 */
	protected function init() {
		$this->slug   = 'bw-schema-tools';
		$this->title  = __( 'Tools & Settings', 'bw-schema' );
		$this->icon   = 'dashicons-admin-tools';
		$this->parent_slug = 'bw-schema';
	}

	/**
	 * Render page content
	 *
	 * @return void
	 */
	protected function render() {
		echo '<p class="description">';
		echo esc_html__( 'Advanced tools for managing cache, detecting conflicts, and exporting/importing configuration.', 'bw-schema' );
		echo '</p>';

		// Setup wizard
		$this->render_setup_section();

		// Cache management
		$this->render_cache_section();

		// Conflict detection
		$this->render_conflict_section();

		// Import/Export
		$this->render_import_export_section();
	}

	/**
	 * Render setup wizard section
	 *
	 * @return void
	 */
	private function render_setup_section() {
		echo '<h2>' . esc_html__( 'Setup Wizard', 'bw-schema' ) . '</h2>';

		echo '<p>';
		if ( BW_Schema_Page_Setup::is_complete() ) {
			echo esc_html__( 'First-time configuration was completed. You can re-run the wizard anytime — it edits the same settings as the regular pages.', 'bw-schema' );
		} else {
			echo esc_html__( 'A four-step guided setup for organization, contact, people, and content schema.', 'bw-schema' );
		}
		echo '</p>';

		echo '<p><a class="button" href="' . esc_url( admin_url( 'admin.php?page=bw-schema-setup' ) ) . '">';
		echo BW_Schema_Page_Setup::is_complete()
			? esc_html__( 'Re-run Setup Wizard', 'bw-schema' )
			: esc_html__( 'Start Setup Wizard', 'bw-schema' );
		echo '</a></p>';
	}

	/**
	 * Render cache management section
	 *
	 * @return void
	 */
	private function render_cache_section() {
		echo '<h2>' . esc_html__( 'Schema Cache', 'bw-schema' ) . '</h2>';

		$health = BW_Schema_Service_Cache::get_health();

		echo '<table class="form-table">';

		echo '<tr>';
		echo '<th>' . esc_html__( 'Cache Status', 'bw-schema' ) . '</th>';
		echo '<td>';

		if ( $health['is_enabled'] ) {
			echo '<span style="color: #00a32a; font-weight: bold;">✓ ' . esc_html__( 'Enabled', 'bw-schema' ) . '</span>';
		} else {
			echo '<span style="color: #999;">○ ' . esc_html__( 'Disabled', 'bw-schema' ) . '</span>';
		}

		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<th>' . esc_html__( 'Cache Size', 'bw-schema' ) . '</th>';
		echo '<td>';
		echo esc_html( BW_Schema_Service_Cache::get_size_display() );
		echo '</td>';
		echo '</tr>';

		echo '<tr>';
		echo '<th>' . esc_html__( 'Cached Items', 'bw-schema' ) . '</th>';
		echo '<td>';
		echo esc_html( $health['cache_count'] );
		echo '</td>';
		echo '</tr>';

		echo '</table>';

		echo '<form method="post">';
		wp_nonce_field( 'bw_schema_cache', 'bw_schema_cache_nonce' );
		submit_button( __( 'Clear Cache', 'bw-schema' ), 'secondary', 'bw_schema_cache_clear', true );
		echo '</form>';
	}

	/**
	 * Handle form submission (Yoast schema toggle)
	 *
	 * @return void
	 */
	protected function handle_save() {
		// Clear cache action
		if ( isset( $_POST['bw_schema_cache_nonce'] ) ) {
			if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['bw_schema_cache_nonce'] ) ), 'bw_schema_cache' ) ) {
				$this->add_error( __( 'Security check failed. Please try again.', 'bw-schema' ) );
				return;
			}

			BW_Schema_Service_Cache::clear_all();
			$this->add_message( __( 'Schema cache cleared. Markup regenerates on the next page load.', 'bw-schema' ) );
			return;
		}

		if ( ! isset( $_POST['bw_schema_conflicts_nonce'] ) ) {
			return;
		}

		if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['bw_schema_conflicts_nonce'] ) ), 'bw_schema_conflicts' ) ) {
			$this->add_error( __( 'Security check failed. Please try again.', 'bw-schema' ) );
			return;
		}

		$disable = ! empty( $_POST['bw_schema_disable_yoast'] );
		BW_Schema_Service_Conflicts::set_yoast_schema_disabled( $disable );

		$this->add_message(
			$disable
				? __( 'Yoast SEO schema output is now disabled. Solomon Schema is the only source of schema markup.', 'bw-schema' )
				: __( 'Yoast SEO schema output is re-enabled. Both plugins will emit schema markup — expect duplicate data warnings.', 'bw-schema' )
		);
	}

	/**
	 * Render conflict detection section
	 *
	 * @return void
	 */
	private function render_conflict_section() {
		echo '<h2>' . esc_html__( 'Plugin Conflicts', 'bw-schema' ) . '</h2>';

		$this->render_yoast_conflict();
		$this->render_other_conflicts();
	}

	/**
	 * Render the Yoast SEO status + suppression toggle
	 *
	 * @return void
	 */
	private function render_yoast_conflict() {
		if ( ! BW_Schema_Service_Conflicts::is_yoast_active() ) {
			echo '<p>' . esc_html__( 'Yoast SEO is not active on this site.', 'bw-schema' ) . '</p>';
			return;
		}

		$disabled = BW_Schema_Service_Conflicts::is_yoast_schema_disabled();
		$version  = BW_Schema_Service_Conflicts::get_yoast_version();

		echo '<p>';
		printf(
			/* translators: %s: Yoast SEO version */
			esc_html__( 'Yoast SEO %s is active. Yoast emits its own schema graph (Organization, WebSite, breadcrumbs) which duplicates Solomon Schema output.', 'bw-schema' ),
			esc_html( $version )
		);
		echo '</p>';

		if ( $disabled ) {
			echo '<p><span style="color: #00a32a; font-weight: 600;">✓ ' . esc_html__( 'Yoast schema output is disabled', 'bw-schema' ) . '</span> — ';
			echo esc_html__( 'Solomon Schema is the single source of structured data. Yoast still handles titles, meta tags, and sitemaps.', 'bw-schema' );
			echo '</p>';
		} else {
			echo '<p><span style="color: #d63638; font-weight: 600;">! ' . esc_html__( 'Duplicate schema output detected', 'bw-schema' ) . '</span> — ';
			echo esc_html__( 'Both plugins are emitting schema markup. Search engines see two conflicting Organization entities on every page.', 'bw-schema' );
			echo '</p>';
		}

		echo '<form method="post">';
		wp_nonce_field( 'bw_schema_conflicts', 'bw_schema_conflicts_nonce' );

		echo '<label for="bw_schema_disable_yoast">';
		echo '<input type="checkbox" id="bw_schema_disable_yoast" name="bw_schema_disable_yoast" value="1" ' . checked( $disabled, true, false ) . '> ';
		echo esc_html__( 'Disable Yoast SEO schema output (recommended)', 'bw-schema' );
		echo '</label>';

		echo '<p class="description">';
		echo esc_html__( 'Only Yoast\'s structured data is suppressed — SEO titles, meta descriptions, and XML sitemaps keep working normally.', 'bw-schema' );
		echo '</p>';

		submit_button( __( 'Save Conflict Settings', 'bw-schema' ), 'secondary', 'bw_schema_conflicts_submit' );
		echo '</form>';
	}

	/**
	 * Render warnings for other detected schema-emitting plugins
	 *
	 * @return void
	 */
	private function render_other_conflicts() {
		$others = BW_Schema_Service_Conflicts::detect_other_schema_plugins();

		if ( empty( $others ) ) {
			return;
		}

		echo '<h3>' . esc_html__( 'Other schema plugins detected', 'bw-schema' ) . '</h3>';
		echo '<ul style="list-style: disc; padding-left: 20px;">';
		foreach ( $others as $plugin ) {
			echo '<li><strong>' . esc_html( $plugin['name'] ) . '</strong> — ' . esc_html( $plugin['detail'] ) . '</li>';
		}
		echo '</ul>';
	}

	/**
	 * Render import/export section
	 *
	 * @return void
	 */
	private function render_import_export_section() {
		echo '<h2>' . esc_html__( 'Import / Export', 'bw-schema' ) . '</h2>';

		echo '<p class="description">';
		echo esc_html__( 'Configuration export/import is planned — not yet available.', 'bw-schema' );
		echo '</p>';
	}
}
