<?php
/**
 * Shortcodes Handler
 *
 * Registers and handles all plugin shortcodes by wrapping existing block renders.
 *
 * @package DonorCore\Shortcodes
 */

namespace DonorCore\Shortcodes;

defined( 'ABSPATH' ) || exit;

/**
 * Shortcodes Class
 */
class Shortcodes {

	/**
	 * Block registration instance.
	 *
	 * @var \DonorCore\Frontend\Block_Registration
	 */
	private $block_registration;

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->block_registration = new \DonorCore\Frontend\Block_Registration();
	}

	/**
	 * Initialize shortcodes.
	 */
	public function init() {
		// Primary unique shortcodes
		add_shortcode( 'donorcore_donation_form', array( $this, 'donation_form_shortcode' ) );
		add_shortcode( 'donorcore_campaign_progress', array( $this, 'campaign_progress_shortcode' ) );
		
		// Enqueue frontend assets when shortcodes are used
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
	}

	/**
	 * Enqueue frontend assets for shortcodes.
	 */
	public function enqueue_frontend_assets() {
		// Enqueue frontend styles
		wp_enqueue_style(
			'donor-core-frontend-blocks',
			DONOR_CORE_PLUGIN_URL . 'includes/css/frontend-blocks.css',
			array(),
			DONOR_CORE_VERSION
		);

		// Enqueue frontend JavaScript
		wp_enqueue_script(
			'donor-core-donation-form-frontend',
			DONOR_CORE_PLUGIN_URL . 'includes/js/donation-form.js',
			array( 'jquery' ),
			DONOR_CORE_VERSION,
			true
		);
	}

	/**
	 * Donation form shortcode.
	 * Wraps the existing block render method.
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string
	 */
	public function donation_form_shortcode( $atts ) {
		$atts = shortcode_atts(
			array(
				'campaign_id'    => '',
				'show_frequency' => 'true',
			),
			$atts,
			'donorcore_donation_form'
		);

		// Convert to block attributes format
		$block_attributes = array(
			'campaignId'    => absint( $atts['campaign_id'] ),
			'showFrequency' => filter_var( $atts['show_frequency'], FILTER_VALIDATE_BOOLEAN ),
		);

		// Use the existing block render method
		return $this->block_registration->render_donation_form( $block_attributes );
	}

	/**
	 * Campaign progress bar shortcode.
	 * Wraps the existing block render method.
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string
	 */
	public function campaign_progress_shortcode( $atts ) {
		$atts = shortcode_atts(
			array(
				'id'              => '',
				'show_amount'     => 'true',
				'show_percentage' => 'true',
			),
			$atts,
			'donorcore_campaign_progress'
		);

		// Convert to block attributes format
		$block_attributes = array(
			'campaignId'     => absint( $atts['id'] ),
			'showAmount'     => filter_var( $atts['show_amount'], FILTER_VALIDATE_BOOLEAN ),
			'showPercentage' => filter_var( $atts['show_percentage'], FILTER_VALIDATE_BOOLEAN ),
		);

		// Use the existing block render method
		return $this->block_registration->render_progress_bar( $block_attributes );
	}
}

