<?php
/**
 * File Path: includes/admin/class-mbs-metabox-arc-render.php
 *
 * Pre-Release / ARC Metabox — Registration & Render
 *
 * Handles metabox registration, asset enqueuing, and render data assembly
 * for the ARC metabox. Extracted from MBS_Metabox_Arc (REF-015, Session 137).
 *
 * @package MyBookShowroom
 * @since   1.1.0
 */

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

class MBS_Metabox_Arc_Render { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
	}

	/** Register the metabox. */

	/** Enqueue ARC assets on mbs_book edit screens only. */
	public function enqueue( string $hook ): void {
		if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}
		$screen = get_current_screen();
		if ( ! $screen || MBS_POST_TYPE !== $screen->post_type ) {
			return;
		}
		wp_enqueue_style(
			'mbs-admin-arc',
			MBS_ASSETS_URL . 'css/mbs-admin-arc.css',
			array(),
			(string) ( @filemtime( MBS_ASSETS_DIR . 'css/mbs-admin-arc.css' ) ?: MBS_VERSION )
		);
		foreach ( array( 'structure', 'reviewer', 'footer' ) as $component ) {
			wp_enqueue_style(
				'mbs-arc-' . $component,
				plugins_url( 'assets/css/admin/arc/arc-' . $component . '.css', MBS_DIR . 'bookshowroom.php' ),
				array( 'mbs-admin-arc' ),
				(string) ( @filemtime( MBS_ASSETS_DIR . 'css/admin/arc/arc-' . $component . '.css' ) ?: MBS_VERSION )
			);
		}
		wp_enqueue_script(
			'mbs-admin-arc',
			MBS_ASSETS_URL . 'js/adminjs/mbs-admin-arc.js',
			array( 'jquery' ),
			filemtime( MBS_ASSETS_DIR . 'js/adminjs/mbs-admin-arc.js' ),
			true
		);
		wp_localize_script(
			'mbs-admin-arc',
			'MbsArc',
			array(
				'ajaxUrl'    => admin_url( 'admin-ajax.php' ),
				'upgradeUrl' => esc_url( MBS_UPGRADE_URL ),
				'isPro'      => mbs_is_pro(),
				'tier'       => mbs_get_license_tier_label(),
			)
		);
	}

	/** Render the ARC metabox. Delegates HTML to templates/admin/metabox-arc.php (R-010). */
	public function render( WP_Post $post ): void {
		$post_id    = $post->ID;
		$is_pro     = mbs_is_pro();
		$is_pub     = mbs_is_pub();

		// Campaign row (mbs_arc_campaigns).
		$campaign    = MBS_Arc_Recipient_DAO::get_campaign( $post_id );
		$arc_status  = $campaign ? (string) $campaign->status                      : 'not_started';
		$copy_count  = $campaign ? (int)    $campaign->copy_count                  : 0;
		$dist_date   = $campaign ? (string) ( $campaign->distribution_date  ?? '' ) : '';
		$close_date  = $campaign ? (string) ( $campaign->close_date         ?? '' ) : '';
		$target_date = $campaign ? (string) ( $campaign->target_review_date ?? '' ) : '';

		// FREE enforcement: check if a DIFFERENT book is currently Active.
		$other_active = false;
		$disabled_msg = '';
		if ( ! $is_pro && 'active' !== $arc_status ) {
			$other_count = MBS_Arc_Recipient_DAO::count_active_books( $post_id );
			if ( $other_count > 0 ) {
				$other_active = true;
				$disabled_msg = sprintf(
					/* translators: %s: upgrade URL */
					__(
						'Another book is currently Active in ARC review. PRO allows unlimited books in ARC review simultaneously, tracks up to 25 reviewers per book with XLSX and CSV export. <a href="%s" target="_blank">Upgrade to PRO &#8594;</a>',
						'my-book-showroom'
					),
					esc_url( MBS_UPGRADE_URL )
				);
			}
		}

		// Reviewer rows and count limits.
		$reviewers          = MBS_Arc_Recipient_DAO::get_by_book( $post_id );
		$urls_by_recipient  = MBS_Arc_Recipient_DAO::get_urls_for_book( $post_id ); // R2-004
		$reviewer_count     = count( $reviewers );
		$limit          = $is_pub ? PHP_INT_MAX : ( $is_pro ? 25 : 5 );
		$limit_label    = $is_pub ? '' : ( $is_pro ? '25' : '5' );
		$at_limit       = ( $reviewer_count >= $limit );

		// F-027-3: Pending applicant data for the approval panel.
		$pending_applicants = $is_pro ? MBS_Arc_Applicant_DAO::get_pending_by_book( $post_id ) : array();
		$pending_count      = count( $pending_applicants );

		mbs_get_template_part( 'admin/metabox-arc', compact(
			'post_id', 'is_pro', 'is_pub',
			'arc_status', 'copy_count', 'dist_date', 'close_date', 'target_date',
			'other_active', 'disabled_msg',
			'reviewers', 'reviewer_count', 'limit', 'limit_label', 'at_limit',
			'urls_by_recipient',
			'pending_applicants', 'pending_count'
		) );
	}

	/**
	 * Render one editor section within the unified book editor shell.
	 *
	 * Passthrough — single-section handler (Sprint 169).
	 *
	 * @since  1.0.0
	 * @param  string  $section_key  Section key (ignored).
	 * @param  WP_Post $post         Current post object.
	 * @return void
	 */
	public function render_section( string $section_key, WP_Post $post ): void {
		$this->render( $post );
	}

	/**
	 * Save one editor section within the unified book editor shell.
	 *
	 * ARC saves via AJAX only (MBS_Metabox_Arc_Ajax). No form-submit path.
	 *
	 * @since  1.0.0
	 * @param  string  $section_key  Section key (ignored).
	 * @param  int     $post_id      Post ID.
	 * @param  WP_Post $post         Post object.
	 * @return void
	 */
	public function save_section( string $section_key, int $post_id, WP_Post $post ): void {
		// ARC saves via AJAX only (MBS_Metabox_Arc_Ajax). No form-submit path.
	}

}
