<?php
/**
 * Frontend asset registration and conditional enqueueing.
 *
 * @package Donatico\Frontend
 */

declare(strict_types=1);

namespace Donatico\Frontend;

use Donatico\Helpers\Currency;

/**
 * Registers form.css and form.js and enqueues them only on pages that
 * actually contain a Donatico shortcode.
 *
 * Two-stage strategy:
 *  1. wp_enqueue_scripts — checks has_shortcode() on the current singular post.
 *  2. FormRenderer::render() — calls enqueueNow() as a fallback for widget
 *     areas, non-singular templates, or page builders that run shortcodes
 *     outside the_content().
 */
class Assets {

	/** Shortcodes that require the form assets. */
	const SHORTCODES = array(
		'donatico_form',
		'donatico_total_raised',
		'donatico_donation_count',
		'donatico_recent_donors',
		'donatico_progress_bar',
	);

	/**
	 * Register scripts/styles and conditionally enqueue on the current singular post.
	 * Hooked to wp_enqueue_scripts.
	 *
	 * @return void
	 */
	public function enqueue(): void {
		$this->register();

		global $post;

		if ( ! is_singular() || ! $post instanceof \WP_Post ) {
			return;
		}

		foreach ( self::SHORTCODES as $shortcode ) {
			if ( has_shortcode( $post->post_content, $shortcode ) ) {
				$this->enqueueNow();
				return;
			}
		}
	}

	/**
	 * Register form assets without enqueueing.
	 * Safe to call multiple times — wp_register_* is idempotent.
	 *
	 * @return void
	 */
	public function register(): void {
		wp_register_style(
			'donatico-form',
			DONATICO_PLUGIN_URL . 'assets/css/form.css',
			array(),
			DONATICO_VERSION
		);

		wp_register_script(
			'donatico-form',
			DONATICO_PLUGIN_URL . 'assets/js/form.js',
			array(),
			DONATICO_VERSION,
			true // footer
		);

		wp_localize_script(
			'donatico-form',
			'donaticoData',
			$this->buildLocalizedData()
		);
	}

	/**
	 * Enqueue the already-registered assets.
	 * Called by register() on pages with a shortcode, and by FormRenderer
	 * as a late fallback for non-singular pages.
	 *
	 * @return void
	 */
	public function enqueueNow(): void {
		wp_enqueue_style( 'donatico-form' );
		wp_enqueue_script( 'donatico-form' );
	}

	/**
	 * Build the JS localisation payload.
	 *
	 * @return array<string, mixed>
	 */
	private function buildLocalizedData(): array {
		$currency = (string) get_option( 'donatico_currency', 'USD' );

		return array(
			'ajaxUrl'         => admin_url( 'admin-ajax.php' ),
			'currency'        => $currency,
			'currencySymbol'  => Currency::getSymbol( $currency ),
			'currencySymbols' => Currency::symbolsForJs(),
			'minAmount'       => 1,
			'i18n'            => array(
				'donate'        => __( 'Donate', 'donatico' ),
				'processing'    => __( 'Processing…', 'donatico' ),
				'error'         => __( 'Something went wrong. Please try again.', 'donatico' ),
				'amountMin'     => __( 'Please enter a valid amount (minimum 1).', 'donatico' ),
				'nameRequired'  => __( 'Please enter your name.', 'donatico' ),
				'emailRequired' => __( 'Please enter a valid email address.', 'donatico' ),
				'securePayment' => __( 'Secure payment via Stripe', 'donatico' ),
			),
		);
	}
}
