<?php
/**
 * Content settings page
 *
 * Which post types emit Article schema, and whether breadcrumb schema is
 * emitted. Edits the same options as the setup wizard's Content step
 * (BW_Schema_Service_Content) — no parallel state.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Page_Content extends BW_Schema_Page {

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

	/**
	 * Handle form submission
	 *
	 * @return void
	 */
	protected function handle_save() {
		if ( ! isset( $_POST['bw_schema_content_nonce'] ) ) {
			return;
		}

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

		$eligible = BW_Schema_Service_Content::get_eligible_post_types();

		$selected = isset( $_POST['bw_schema_article_post_types'] ) && is_array( $_POST['bw_schema_article_post_types'] )
			? array_map( 'sanitize_key', wp_unslash( $_POST['bw_schema_article_post_types'] ) )
			: array();
		$selected = array_values( array_intersect( $selected, array_keys( $eligible ) ) );

		BW_Schema_Service_Content::set_article_post_types( $selected );
		BW_Schema_Service_Content::set_breadcrumbs_enabled( ! empty( $_POST['bw_schema_enable_breadcrumbs'] ) );

		// Invalidate schema caches so the frontend reflects the change
		BW_Schema_Cache::invalidate();

		$this->add_message( __( 'Content settings saved.', 'bw-schema' ) );
	}

	/**
	 * Render page content
	 *
	 * @return void
	 */
	protected function render() {
		echo '<p class="description">';
		echo esc_html__( 'Choose which content emits schema markup. Team pages always emit Person schema — configure the team post type on the People page.', 'bw-schema' );
		echo '</p>';

		$eligible = BW_Schema_Service_Content::get_eligible_post_types();
		$selected = BW_Schema_Service_Content::get_article_post_types();

		echo '<form method="post">';
		wp_nonce_field( 'bw_schema_content', 'bw_schema_content_nonce' );

		echo '<div class="bw-schema-card">';
		echo '<h3 class="bw-schema-section-heading">' . esc_html__( 'Article Schema', 'bw-schema' ) . '</h3>';
		echo '<p class="description">' . esc_html__( 'Selected post types emit Article schema on their pages — headline, author, dates, and featured image.', 'bw-schema' ) . '</p>';

		echo '<table class="form-table" role="presentation">';
		echo '<tr><th scope="row">' . esc_html__( 'Post types', 'bw-schema' ) . '</th><td><fieldset>';
		foreach ( $eligible as $slug => $label ) {
			echo '<label class="bw-schema-checkbox-row">';
			echo '<input type="checkbox" name="bw_schema_article_post_types[]" value="' . esc_attr( $slug ) . '" ' . checked( in_array( $slug, $selected, true ), true, false ) . '> ';
			echo esc_html( $label ) . ' <code>' . esc_html( $slug ) . '</code>';
			echo '</label>';
		}
		echo '</fieldset></td></tr>';
		echo '</table>';
		echo '</div>';

		echo '<div class="bw-schema-card">';
		echo '<h3 class="bw-schema-section-heading">' . esc_html__( 'Breadcrumbs', 'bw-schema' ) . '</h3>';

		echo '<table class="form-table" role="presentation">';
		echo '<tr><th scope="row">' . esc_html__( 'Breadcrumb schema', 'bw-schema' ) . '</th><td>';
		echo '<label>';
		echo '<input type="checkbox" name="bw_schema_enable_breadcrumbs" value="1" ' . checked( BW_Schema_Service_Content::is_breadcrumbs_enabled(), true, false ) . '> ';
		echo esc_html__( 'Help search engines show a page trail (Home › About › Team) under your search results (recommended)', 'bw-schema' );
		echo '</label>';
		echo '</td></tr>';
		echo '</table>';
		echo '</div>';

		submit_button( __( 'Save Changes', 'bw-schema' ) );
		echo '</form>';
	}
}
