<?php
/**
 * People & Team management page
 *
 * Pick the team post type and see per-member schema completeness (photo,
 * job title — the same data the dashboard health checks report).
 * Edits the same option as the setup wizard's People step.
 *
 * @package BW_Schema
 * @since 3.0.0-babel
 */

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

class BW_Schema_Page_People extends BW_Schema_Page {

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

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

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

		$post_type = isset( $_POST['bw_schema_team_post_type'] )
			? sanitize_key( $_POST['bw_schema_team_post_type'] ) : '';

		if ( '' !== $post_type && ! post_type_exists( $post_type ) ) {
			$this->add_error( __( 'That post type does not exist.', 'bw-schema' ) );
			return;
		}

		BW_Schema_Service_People::set_team_post_type( $post_type );
		BW_Schema_Cache::invalidate();

		$this->add_message(
			'' === $post_type
				? __( 'Team disabled — no pages will emit Person schema.', 'bw-schema' )
				: __( 'Team post type saved. Member pages now emit Person schema.', 'bw-schema' )
		);
	}

	/**
	 * Render page content
	 *
	 * @return void
	 */
	protected function render() {
		echo '<p class="description">';
		echo esc_html__( 'Team member pages emit Person schema linked to your organization, and power author markup on posts.', 'bw-schema' );
		echo '</p>';

		$this->render_team_post_type_form();
		$this->render_team_overview();
	}

	/**
	 * Render the team post type selector
	 *
	 * @return void
	 */
	private function render_team_post_type_form() {
		$current = BW_Schema_Service_People::get_team_post_type();

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

		echo '<form method="post">';
		wp_nonce_field( 'bw_schema_people', 'bw_schema_people_nonce' );

		echo '<table class="form-table" role="presentation">';
		echo '<tr><th scope="row"><label for="bw_schema_team_post_type">' . esc_html__( 'Post type', 'bw-schema' ) . '</label></th><td>';
		echo '<select id="bw_schema_team_post_type" name="bw_schema_team_post_type">';
		echo '<option value="">' . esc_html__( 'No team section on this site', 'bw-schema' ) . '</option>';

		foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $post_type ) {
			if ( in_array( $post_type->name, array( 'attachment', 'post', 'page' ), true ) ) {
				continue;
			}
			$count     = wp_count_posts( $post_type->name );
			$published = isset( $count->publish ) ? (int) $count->publish : 0;

			echo '<option value="' . esc_attr( $post_type->name ) . '" ' . selected( $current, $post_type->name, false ) . '>';
			printf(
				/* translators: 1: post type label, 2: post type slug, 3: published count */
				esc_html__( '%1$s (%2$s) — %3$d published', 'bw-schema' ),
				esc_html( $post_type->labels->name ),
				esc_html( $post_type->name ),
				(int) $published
			);
			echo '</option>';
		}

		echo '</select>';
		echo '<p class="description">' . esc_html__( 'Each published entry in this post type gets Person schema on its page.', 'bw-schema' ) . '</p>';
		echo '</td></tr>';
		echo '</table>';

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

	/**
	 * Render the team overview with per-member schema completeness
	 *
	 * Photo and job title are the two Person properties the dashboard
	 * health checks track — this table shows exactly who is missing what.
	 *
	 * @return void
	 */
	private function render_team_overview() {
		if ( ! BW_Schema_Service_People::is_team_enabled() ) {
			return;
		}

		$members = BW_Schema_Service_People::get_team_members();
		$count   = count( $members );

		echo '<div class="bw-schema-card">';
		echo '<h3 class="bw-schema-section-heading">';
		printf(
			/* translators: %d: number of team members */
			esc_html( _n( 'Team Members (%d)', 'Team Members (%d)', $count, 'bw-schema' ) ),
			(int) $count
		);
		echo '</h3>';

		if ( 0 === $count ) {
			echo '<p>' . esc_html__( 'No published team members yet. Add some and their pages will emit Person schema automatically.', 'bw-schema' ) . '</p>';
		} else {
			echo '<table class="widefat striped">';
			echo '<thead><tr>';
			echo '<th>' . esc_html__( 'Name', 'bw-schema' ) . '</th>';
			echo '<th>' . esc_html__( 'Job title', 'bw-schema' ) . '</th>';
			echo '<th>' . esc_html__( 'Photo', 'bw-schema' ) . '</th>';
			echo '<th>' . esc_html__( 'Schema', 'bw-schema' ) . '</th>';
			echo '<th></th>';
			echo '</tr></thead><tbody>';

			foreach ( $members as $member ) {
				$job_title = get_post_meta( $member->ID, 'title', true );
				if ( ! $job_title ) {
					$job_title = get_post_meta( $member->ID, '_job_title', true );
				}
				$has_photo = has_post_thumbnail( $member->ID );
				$complete  = $has_photo && $job_title;

				echo '<tr>';
				echo '<td><strong>' . esc_html( $member->post_title ) . '</strong></td>';
				echo '<td>' . ( $job_title ? esc_html( $job_title ) : '<span class="bw-schema-missing">' . esc_html__( 'missing', 'bw-schema' ) . '</span>' ) . '</td>';
				echo '<td>' . ( $has_photo ? '<span class="bw-schema-ok">✓</span>' : '<span class="bw-schema-missing">' . esc_html__( 'missing', 'bw-schema' ) . '</span>' ) . '</td>';
				echo '<td>' . ( $complete
					? '<span class="bw-schema-ok">✓ ' . esc_html__( 'Complete', 'bw-schema' ) . '</span>'
					: '<span class="bw-schema-partial">' . esc_html__( 'Partial', 'bw-schema' ) . '</span>' ) . '</td>';
				echo '<td><a href="' . esc_url( get_edit_post_link( $member->ID ) ) . '">' . esc_html__( 'Edit', 'bw-schema' ) . '</a> · ';
				echo '<a href="' . esc_url( get_permalink( $member->ID ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'View', 'bw-schema' ) . '</a></td>';
				echo '</tr>';
			}

			echo '</tbody></table>';

			echo '<p class="description" style="margin-top: 12px;">';
			echo esc_html__( 'Job title comes from the member\'s "title" custom field. Photo is the featured image. Partial members still emit Person schema — just with fewer properties.', 'bw-schema' );
			echo '</p>';
		}

		$team_cpt = BW_Schema_Service_People::get_team_post_type();
		echo '<p><a href="' . esc_url( admin_url( 'edit.php?post_type=' . $team_cpt ) ) . '" class="button">';
		echo esc_html__( 'Manage Team Members', 'bw-schema' );
		echo '</a></p>';

		echo '</div>';
	}
}
