<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase, WordPress.Files.FileName.InvalidClassFileName
/**
 * Snooze template rendering helpers.
 *
 * @package Autoship\Services\Snooze\Implementations
 * @since   2.13.0
 */

namespace Autoship\Services\Snooze\Implementations;

use Autoship\Core\EnvironmentInterface;
use Autoship\Domain\Snooze\SnoozeStatus;
use Autoship\Services\Snooze\Interfaces\SnoozeOptionsProviderInterface;
use Autoship\Services\Snooze\Interfaces\SnoozeSettingsInterface;
use Autoship\Services\Templates\TemplateRendererInterface;

/**
 * Renders snooze-related markup on the orders list and order detail
 * pages, plus the post-snooze notification banner and the inline JS
 * config bootstrap.
 */
class SnoozeRenderer {

	/**
	 * Snooze settings reader.
	 *
	 * @var SnoozeSettingsInterface
	 */
	private SnoozeSettingsInterface $settings;

	/**
	 * Snooze options provider.
	 *
	 * @var SnoozeOptionsProviderInterface
	 */
	private SnoozeOptionsProviderInterface $options_provider;

	/**
	 * Template renderer.
	 *
	 * @var TemplateRendererInterface
	 */
	private TemplateRendererInterface $templates;

	/**
	 * Environment service.
	 *
	 * @var EnvironmentInterface
	 */
	private EnvironmentInterface $environment;

	/**
	 * Constructor.
	 *
	 * @param SnoozeSettingsInterface        $settings         Snooze settings reader.
	 * @param SnoozeOptionsProviderInterface $options_provider Options provider.
	 * @param TemplateRendererInterface      $templates        Template renderer.
	 * @param EnvironmentInterface           $environment      Environment service.
	 */
	public function __construct( SnoozeSettingsInterface $settings, SnoozeOptionsProviderInterface $options_provider, TemplateRendererInterface $templates, EnvironmentInterface $environment ) {
		$this->settings         = $settings;
		$this->options_provider = $options_provider;
		$this->templates        = $templates;
		$this->environment      = $environment;
	}

	/**
	 * Render the snooze form row in the orders list.
	 *
	 * Hooked to autoship_after_autoship_scheduled_orders_template_row.
	 *
	 * @param object $autoship_order       The scheduled order.
	 * @param int    $customer_id          The WC customer ID.
	 * @param int    $autoship_customer_id The Autoship customer ID.
	 */
	public function render_snooze_form_row( $autoship_order, $customer_id, $autoship_customer_id ): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		// Only show snooze form for Active orders (or snoozed orders for re-snooze).
		if ( 'Active' !== $autoship_order->status && ! SnoozeStatus::is_snoozed( $autoship_order ) ) {
			return;
		}

		$order_number   = $autoship_order->id;
		$snooze_options = apply_filters( 'autoship_snooze_frequencies', $this->options_provider->get_options() );
		$columns_count  = count( autoship_get_my_account_scheduled_orders_columns() );
		$is_snoozed     = SnoozeStatus::is_snoozed( $autoship_order );

		echo '<tr class="autoship-snooze-form-row" id="autoship-snooze-row-' . esc_attr( $order_number ) . '" style="display:none;">';
		echo '<td colspan="' . esc_attr( $columns_count ) . '">';

		$this->templates->render(
			'scheduled-orders/snooze-form-template',
			array(
				'order_number'   => $order_number,
				'snooze_options' => $snooze_options,
				'is_snoozed'     => $is_snoozed,
			)
		);

		echo '</td>';
		echo '</tr>';
	}

	/**
	 * Render the Resume Early confirmation panel after a snoozed order row.
	 *
	 * @param object $autoship_order       The scheduled order.
	 * @param int    $customer_id          The WC customer ID.
	 * @param int    $autoship_customer_id The Autoship customer ID.
	 */
	public function render_resume_early_panel_row( $autoship_order, $customer_id, $autoship_customer_id ): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		if ( ! SnoozeStatus::is_snoozed( $autoship_order ) ) {
			return;
		}

		$order_number  = $autoship_order->id;
		$columns_count = count( autoship_get_my_account_scheduled_orders_columns() );

		echo '<tr class="autoship-resume-early-panel-row" id="autoship-resume-early-row-' . esc_attr( $order_number ) . '" style="display:none;">';
		echo '<td colspan="' . esc_attr( $columns_count ) . '">';

		$this->templates->render(
			'scheduled-orders/resume-early-confirm-template',
			array(
				'order_number'   => $order_number,
				'autoship_order' => $autoship_order,
			)
		);

		echo '</td>';
		echo '</tr>';
	}

	/**
	 * Render the snooze form on the order detail page.
	 *
	 * Hooked at priority 12 — between the schedule form (with its action
	 * buttons) and the items/cart summary.
	 *
	 * @param array $autoship_order       The scheduled order array.
	 * @param int   $customer_id          The WC customer ID.
	 * @param int   $autoship_customer_id The Autoship customer ID.
	 */
	public function render_snooze_form_detail( $autoship_order, $customer_id, $autoship_customer_id ): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		$status = is_array( $autoship_order ) ? $autoship_order['status'] : $autoship_order->status;

		if ( 'Active' !== $status && ! SnoozeStatus::is_snoozed( $autoship_order ) ) {
			return;
		}

		$order_number   = is_array( $autoship_order ) ? $autoship_order['id'] : $autoship_order->id;
		$snooze_options = apply_filters( 'autoship_snooze_frequencies', $this->options_provider->get_options() );
		$is_snoozed     = SnoozeStatus::is_snoozed( $autoship_order );

		echo '<div class="autoship-snooze-form-row" id="autoship-snooze-row-' . esc_attr( $order_number ) . '" style="display:none;">';

		$this->templates->render(
			'scheduled-orders/snooze-form-template',
			array(
				'order_number'   => $order_number,
				'snooze_options' => $snooze_options,
				'is_snoozed'     => $is_snoozed,
			)
		);

		echo '</div>';
	}

	/**
	 * Render the Resume Early confirmation panel on the order detail page.
	 *
	 * @param array $autoship_order       The scheduled order array.
	 * @param int   $customer_id          The WC customer ID.
	 * @param int   $autoship_customer_id The Autoship customer ID.
	 */
	public function render_resume_early_panel_detail( $autoship_order, $customer_id, $autoship_customer_id ): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		if ( ! SnoozeStatus::is_snoozed( $autoship_order ) ) {
			return;
		}

		$order_number = is_array( $autoship_order ) ? $autoship_order['id'] : $autoship_order->id;

		echo '<div class="autoship-resume-early-panel-row" id="autoship-resume-early-row-' . esc_attr( $order_number ) . '" style="display:none;">';

		$this->templates->render(
			'scheduled-orders/resume-early-confirm-template',
			array(
				'order_number'   => $order_number,
				'autoship_order' => $autoship_order,
			)
		);

		echo '</div>';
	}

	/**
	 * Render the post-snooze notification banner.
	 *
	 * Reads a one-shot per-user transient set by the snooze AJAX handler
	 * and renders the "Your order has been snoozed…" amber banner. The
	 * transient is consumed (deleted) after rendering.
	 *
	 * @param array $autoship_order       The scheduled order array.
	 * @param int   $customer_id          The WC customer ID.
	 * @param int   $autoship_customer_id The Autoship customer ID.
	 */
	public function render_snooze_notification_banner( $autoship_order, $customer_id, $autoship_customer_id ): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		$order_number = is_array( $autoship_order ) ? $autoship_order['id'] : $autoship_order->id;
		$user_id      = $this->environment->get_current_user_id();

		if ( empty( $user_id ) ) {
			return;
		}

		$transient_key  = 'autoship_snooze_notice_' . $user_id . '_' . $order_number;
		$notice_payload = get_transient( $transient_key );

		if ( false === $notice_payload || ! is_array( $notice_payload ) ) {
			return;
		}

		// Consume the transient so the banner only appears once.
		delete_transient( $transient_key );

		$resume_date = isset( $notice_payload['resume_date'] ) ? $notice_payload['resume_date'] : '';

		$this->templates->render(
			'scheduled-orders/snooze-notification-banner-template',
			array( 'resume_date' => $resume_date )
		);
	}

	/**
	 * Inject the inline JS config used by the subscriber-facing snooze JS.
	 *
	 * Hooked to wp_enqueue_scripts after the main scheduled-orders script
	 * has been registered (priority 20).
	 */
	public function enqueue_scripts(): void {
		if ( ! $this->settings->is_enabled() ) {
			return;
		}

		$snooze_data = array(
			'ajaxUrl' => admin_url( 'admin-ajax.php' ),
			'nonce'   => wp_create_nonce( 'autoship-snooze-nonce' ),
			'i18n'    => array(
				'resumeOn'         => __( 'Your order will resume on %s', 'autoship' ),
				'confirmTitle'     => __( 'Are you sure you want to resume this order now?', 'autoship' ),
				'processing'       => __( 'Processing...', 'autoship' ),
				'error'            => __( 'An error occurred. Please try again.', 'autoship' ),
				'selectDuration'   => __( 'Please select a snooze duration before confirming.', 'autoship' ),
				'selectCustomDate' => __( 'Please select a resume date before confirming.', 'autoship' ),
				'customDateInPast' => __( 'The selected date must be in the future.', 'autoship' ),
			),
		);

		$inline_js = 'window.autoshipSnooze = ' . wp_json_encode( $snooze_data ) . ';';

		wp_add_inline_script( 'autoship-scheduled-orders', $inline_js, 'before' );
	}
}
