<?php

namespace WC_Affiliate\Controllers\Front;

defined( 'ABSPATH' ) || exit;

/**
 * Front-end Shortcode Controller for WC Affiliate Plugin.
 */
class Shortcode {

	/**
	 * Controller constructor
	 *
	 * Initializes the frontend shortcode controller and registers necessary hooks
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register hooks for shortcode controller
	 *
	 * @return void
	 */
	protected function register_hooks(): void {
		add_action( 'init', array( $this, 'register_shortcodes' ) );
	}

	/**
	 * Register all shortcodes
	 *
	 * Registers all available shortcodes for the WC Affiliate plugin frontend
	 *
	 * @return void
	 */
	public function register_shortcodes(): void {
		/**
		 * Action hook fired before shortcode registration.
		 */
		do_action( 'wc_affiliate_register_shortcodes_before' );

		// Core shortcodes with existing views.
		add_shortcode( 'wc-affiliate-dashboard', array( $this, 'affiliate_dashboard' ) );
		add_shortcode( 'wc-affiliate-application-form', array( $this, 'affiliate_application_form' ) );

		// Legacy compatibility shortcodes.
		add_shortcode( 'wc-affiliate-registration', array( $this, 'affiliate_registration' ) );
		add_shortcode( 'wc-affiliate-login', array( $this, 'affiliate_login' ) );

		/**
		 * Action hook fired after shortcode registration.
		 */
		do_action( 'wc_affiliate_register_shortcodes_after' );
	}

	/**
	 * Affiliate Dashboard Shortcode
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string Dashboard HTML
	 */
	public function affiliate_dashboard( array $atts ): string {
		/**
		 * Action hook fired before dashboard shortcode rendering.
		 *
		 * @param array $atts Shortcode attributes.
		 */
		do_action( 'wc_affiliate_dashboard_shortcode_before', $atts );

		$atts = shortcode_atts(
			array(
				'show_tabs'   => 'summary,visits,referrals,transactions,profile,payout',
				'default_tab' => 'summary',
			),
			$atts,
			'wc-affiliate-dashboard'
		);

		// Check if user is admin - not allowed in affiliate program.
		if ( ! current_user_can( 'manage_options' ) ) {
			// authenticate.
			$status = wca_get_affiliate_status();
			$status = ! empty( $status ) ? $status : 'apply';

			$is_mail_verified = wca_is_mail_verified();

			if ( ! is_user_logged_in() ) {
				$template = 'login';
			} elseif ( 'active' === $status ) {
				$template = 'affiliate-dashboard';
			} elseif ( ! $is_mail_verified ) {
				$template = 'email-varification';
			} else {
				$template = $status;
			}

			// If user is not logged in or is not active affiliate.
			if ( 'affiliate-dashboard' !== $template ) {
				if ( 'apply' === $template ) {
					$result = wca_get_template( 'application-form', 'views/front/shortcodes', array( 'atts' => $atts ) );
				} elseif ( 'login' === $template ) {
					$result = wca_get_template( 'login', 'views/front/shortcodes', array( 'atts' => $atts ) );
				} else {
					$result = wca_get_template( $template, 'views/front/authenticate', array( 'atts' => $atts ) );
				}

				/**
				 * Action hook fired after non-dashboard shortcode rendering.
				 *
				 * @param string $result Rendered HTML.
				 * @param array  $atts   Shortcode attributes.
				 */
				do_action( 'wc_affiliate_dashboard_shortcode_non_dashboard', $result, $atts );

				return $result;
			}
		}

		$user_id = get_current_user_id();

		$dashboard_html = wca_get_template(
			'affiliate-dashboard',
			'views/front',
			array(
				'user_id'             => $user_id,
				'is_active_affiliate' => wca_is_active_affiliate( $user_id ),
				'atts'                => $atts,
			),
			true
		);

		// Allow filtering dashboard HTML.
		$result = apply_filters( 'wc_affiliate_dashboard_html', $dashboard_html, $user_id, $atts );

		/**
		 * Action hook fired after dashboard shortcode rendering.
		 *
		 * @param string $result  Rendered HTML.
		 * @param int    $user_id User ID.
		 * @param array  $atts    Shortcode attributes.
		 */
		do_action( 'wc_affiliate_dashboard_shortcode_after', $result, $user_id, $atts );

		return $result;
	}

	/**
	 * Affiliate Registration Shortcode
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string Registration form HTML
	 */
	public function affiliate_registration( array $atts ): string {
		$atts = shortcode_atts(
			array(
				'redirect_url'    => '',
				'show_login_link' => 'yes',
				'form_style'      => 'default',
			),
			$atts,
			'wc-affiliate-registration'
		);

		// Check if user is admin - not allowed in affiliate program.
		if ( current_user_can( 'manage_options' ) ) {
			return wca_get_template( 'not-allowed', 'views/front/authenticate' );
		}

		// Use the existing application-form.php view (legacy compatibility).
		$form_html = wca_get_template( 'application-form', 'views/front/shortcodes', array( 'atts' => $atts ) );

		return apply_filters( 'wc_affiliate_registration_form_html', $form_html, $atts );
	}

	/**
	 * Affiliate Application Form Shortcode
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string Application form HTML
	 */
	public function affiliate_application_form( array $atts ): string {
		$atts = shortcode_atts(
			array(
				'redirect_url'    => '',
				'show_login_link' => 'yes',
				'form_style'      => 'default',
			),
			$atts,
			'wc-affiliate-application-form'
		);

		// Check if user is admin - not allowed in affiliate program.
		if ( current_user_can( 'manage_options' ) ) {
			return wca_get_template( 'not-allowed', 'views/front/authenticate' );
		}

		// Use the existing application-form.php view which handles all the logic.
		$form_html = wca_get_template( 'application-form', 'views/front/shortcodes', array( 'atts' => $atts ) );

		return apply_filters( 'wc_affiliate_application_form_html', $form_html, $atts );
	}

	/**
	 * Affiliate Login Shortcode
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string Login form HTML
	 */
	public function affiliate_login( array $atts ): string {
		$atts = shortcode_atts(
			array(
				'redirect_url'       => '',
				'show_register_link' => 'yes',
				'form_style'         => 'default',
			),
			$atts,
			'wc-affiliate-login'
		);

		// Check if user is admin - not allowed in affiliate program.
		if ( current_user_can( 'manage_options' ) ) {
			return wca_get_template( 'not-allowed', 'views/front/authenticate' );
		}

		// If user is already logged in.
		if ( is_user_logged_in() ) {
			$dashboard_url = wca_get_dashboard( 'link' ) ?: home_url();

			return wca_get_template(
				'login-message',
				'views/front',
				array(
					'message'       => __( 'You are already logged in.', 'wc-affiliate' ),
					'dashboard_url' => $dashboard_url,
					'link_text'     => __( 'Go to Dashboard', 'wc-affiliate' ),
				)
			);
		}

		$form_html = wca_get_template( 'login', 'views/front/shortcodes', array( 'atts' => $atts ) );

		return apply_filters( 'wc_affiliate_login_form_html', $form_html, $atts );
	}
}
