<?php
/**
 * Campaign Data Meta Box
 *
 * Renders a WooCommerce-style tabbed settings panel on the campaign edit
 * screen, replacing the individual side meta boxes. Other plugins can add
 * their own tabs via the `ambh_campaign_data_tabs` filter.
 *
 * @package AmbassadorHub\Campaigns\Admin
 */

namespace AmbassadorHub\Campaigns\Admin;

use AmbassadorHub\Campaigns\CampaignsModule;

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

/**
 * Registers and renders the campaign settings tabbed meta box.
 */
final class CampaignDataMetaBox {

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'add_meta_boxes', array( self::class, 'register' ) );
		add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_assets' ) );
	}

	/**
	 * Register the meta box.
	 *
	 * @return void
	 */
	public static function register(): void {
		add_meta_box(
			'ambh-campaign-data',
			__( 'Campaign Settings', 'ambassadorhub' ),
			array( self::class, 'render' ),
			CampaignsModule::POST_TYPE,
			'normal',
			'high'
		);
	}

	/**
	 * Enqueue scripts and styles on the campaign edit screen.
	 *
	 * @param string $hook_suffix Current admin page hook.
	 * @return void
	 */
	public static function enqueue_assets( string $hook_suffix ): void {
		if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}

		$screen = get_current_screen();
		if ( ! $screen || CampaignsModule::POST_TYPE !== $screen->post_type ) {
			return;
		}

		$base = plugins_url( 'assets/js/', dirname( __DIR__ ) . '/CampaignsModule.php' );

		wp_enqueue_script( 'ambh-campaign-dates', $base . 'campaign-dates.js', array( 'jquery' ), '1.0', true );
		wp_enqueue_script( 'ambh-campaign-data-tabs', $base . 'campaign-data-tabs.js', array(), '1.0', true );
		wp_enqueue_style( 'ambh-admin' );
	}

	/**
	 * Render the tabbed meta box.
	 *
	 * @param \WP_Post $post Current post.
	 * @return void
	 */
	public static function render( \WP_Post $post ): void {
		/**
		 * Filter the tabs shown in the Campaign Settings meta box.
		 *
		 * Each entry is keyed by a unique tab slug and contains:
		 *   string   label    — translated display name
		 *   string   icon     — dashicons CSS class (e.g. 'dashicons-calendar-alt')
		 *   callable callback — receives \WP_Post $post
		 *   int      priority — lower numbers render first
		 *
		 * @param array<string, array{label: string, icon: string, callback: callable, priority: int}> $tabs
		 */
		$tabs = apply_filters(
			'ambh_campaign_data_tabs',
			array(
				'general'             => array(
					'label'    => __( 'General', 'ambassadorhub' ),
					'icon'     => 'dashicons-calendar-alt',
					'callback' => array( self::class, 'render_general_tab' ),
					'priority' => 10,
				),
				'fundraiser_settings' => array(
					'label'    => __( 'Fundraiser Settings', 'ambassadorhub' ),
					'icon'     => 'dashicons-groups',
					'callback' => array( NestingMetaBox::class, 'render' ),
					'priority' => 20,
				),
			)
		);

		uasort( $tabs, fn( $a, $b ) => $a['priority'] <=> $b['priority'] );

		$first_tab = array_key_first( $tabs );
		?>
		<div class="ambh-campaign-data-wrap">
			<ul class="ambh-campaign-tabs">
				<?php
				foreach ( $tabs as $tab_id => $tab ) :
					$is_active = $tab_id === $first_tab;
					?>
				<li class="ambh-campaign-tab<?php echo $is_active ? ' active' : ''; ?>">
					<a href="#ambh-tab-<?php echo esc_attr( $tab_id ); ?>">
						<?php if ( ! empty( $tab['icon'] ) ) : ?>
						<span class="dashicons <?php echo esc_attr( $tab['icon'] ); ?>"></span>
						<?php endif; ?>
						<?php echo esc_html( $tab['label'] ); ?>
					</a>
				</li>
				<?php endforeach; ?>
			</ul>
			<div class="ambh-campaign-tab-panels">
				<?php
				foreach ( $tabs as $tab_id => $tab ) :
					$is_active = $tab_id === $first_tab;
					?>
				<div id="ambh-tab-<?php echo esc_attr( $tab_id ); ?>"
					class="ambh-campaign-tab-panel<?php echo $is_active ? ' active' : ''; ?>">
					<?php
					if ( is_callable( $tab['callback'] ) ) {
						call_user_func( $tab['callback'], $post );
					}
					?>
				</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the General tab (campaign dates + financial goal).
	 *
	 * @param \WP_Post $post Current post.
	 * @return void
	 */
	public static function render_general_tab( \WP_Post $post ): void {
		MetaBoxes::render_campaign_dates_meta_box( $post );
		echo '<hr class="ambh-tab-divider">';
		MetaBoxes::render_campaign_financial_goal_meta_box( $post );
	}
}
