<?php
/**
 * Campaign Nesting Meta Box
 *
 * Renders and saves the "Allow Teams" toggle on the campaign edit screen.
 * The global maximum depth is fixed at 3 (two team levels + one individual).
 * Labels are not configurable: grouped fundraisers are always called "Team".
 *
 * Meta key stored on the campaign post:
 *   _ambh_teams_enabled  – '1' | '0'
 *
 * @package AmbassadorHub\Campaigns\Admin
 */

namespace AmbassadorHub\Campaigns\Admin;

use AmbassadorHub\Campaigns\CampaignsModule;
use AmbassadorHub\Fundraisers\FundraisersModule;

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

/**
 * Handles the Fundraiser Teams meta box on the campaign post-edit screen.
 */
final class NestingMetaBox {

	/** Nonce action used to verify meta box form submissions. */
	private const NONCE_ACTION = 'ambh_save_nesting_settings';

	/** Nonce field name embedded in the meta box form. */
	private const NONCE_FIELD = 'ambh_nesting_nonce';

	// -------------------------------------------------------------------------
	// Bootstrap
	// -------------------------------------------------------------------------

	/**
	 * Register hooks. Called from CampaignsModule::init().
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'save_post_' . CampaignsModule::POST_TYPE, array( self::class, 'save' ), 10, 1 );
	}

	// -------------------------------------------------------------------------
	// Rendering
	// -------------------------------------------------------------------------

	/**
	 * Render the meta box HTML.
	 *
	 * @param \WP_Post $post The campaign post being edited.
	 *
	 * @return void
	 */
	public static function render( \WP_Post $post ): void {
		$enabled          = (bool) get_post_meta( $post->ID, FundraisersModule::META_TEAMS_ENABLED, true );
		$end_date_enabled = (bool) get_post_meta( $post->ID, FundraisersModule::META_FUNDRAISER_END_DATE_ENABLED, true );

		wp_nonce_field( self::NONCE_ACTION, self::NONCE_FIELD );
		?>
		<p>
			<label>
				<input
					type="checkbox"
					name="_ambh_teams_enabled"
					value="1"
					<?php checked( $enabled ); ?>
				/>
				<?php esc_html_e( 'Allow ambassadors to create teams', 'ambassadorhub' ); ?>
			</label>
		</p>
		<p class="description">
			<?php esc_html_e( 'When enabled, ambassadors can create a Team fundraiser and optionally allow others to nest a sub-team inside it.', 'ambassadorhub' ); ?>
		</p>
		<p style="margin-top: 12px;">
			<label>
				<input
					type="checkbox"
					name="_ambh_fundraiser_end_date_enabled"
					value="1"
					<?php checked( $end_date_enabled ); ?>
				/>
				<?php esc_html_e( 'Allow ambassadors to set an end date for their fundraisers', 'ambassadorhub' ); ?>
			</label>
		</p>
		<p class="description">
			<?php esc_html_e( 'When enabled, ambassadors can optionally set a personal end date. The fundraiser will automatically end on that date (or on the campaign end date if earlier).', 'ambassadorhub' ); ?>
		</p>
		<?php
	}

	// -------------------------------------------------------------------------
	// Saving
	// -------------------------------------------------------------------------

	/**
	 * Save nesting meta when the campaign post is saved.
	 *
	 * @param int $post_id Campaign post ID.
	 *
	 * @return void
	 */
	public static function save( int $post_id ): void {
		// Skip autosaves and revisions.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}
		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		// Verify nonce.
		$nonce = isset( $_POST[ self::NONCE_FIELD ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::NONCE_FIELD ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified on next line.
		if ( ! wp_verify_nonce( $nonce, self::NONCE_ACTION ) ) {
			return;
		}

		// Check capability.
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		$enabled = isset( $_POST['_ambh_teams_enabled'] ) ? '1' : '0'; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above.
		update_post_meta( $post_id, FundraisersModule::META_TEAMS_ENABLED, $enabled );

		$end_date_enabled = isset( $_POST['_ambh_fundraiser_end_date_enabled'] ) ? '1' : '0'; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified above.
		update_post_meta( $post_id, FundraisersModule::META_FUNDRAISER_END_DATE_ENABLED, $end_date_enabled );
	}
}
