<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Digital_Product_Pricing extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'digital';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-digital-product-pricing',
			'title'         => 'Digital Product Pricing',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-digital-components' ),
			'keywords'      => array( 'digital', 'product', 'price', 'pricing', 'table', 'subscription', 'meta', 'sifency' ),
			'style_depends' => array( 'sifency-digital-style' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_single_price_style',
			array(
				'label' => esc_html__( 'Single Price Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_regular_price',
			array(
				'label' => 'Regular Price',
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_control(
			'regular_price_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-single-price .regular-price' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'regular_price_typography',
				'selector' => '{{WRAPPER}} .sifency-single-price .regular-price',
			)
		);
		$this->add_control(
			'heading_sale_price',
			array(
				'label'     => 'Sale Price',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'sale_price_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-single-price .sale-price' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'sale_price_typography',
				'selector' => '{{WRAPPER}} .sifency-single-price .sale-price',
			)
		);
		$this->add_responsive_control(
			'price_spacing',
			array(
				'label'     => 'Spacing',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-single-price' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();

		// --- SUBSCRIPTION TABLE STYLE ---
		$this->start_controls_section(
			'section_subscription_style',
			array(
				'label' => esc_html__( 'Subscription Table', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'columns',
			array(
				'label'     => 'Columns',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'default'   => '3',
				'selectors' => array( '{{WRAPPER}} .sifency-pricing-table' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ),
			)
		);
		$this->add_responsive_control(
			'table_gap',
			array(
				'label'     => 'Gap',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-pricing-table' => 'gap: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->start_controls_tabs( 'tier_tabs_style' );
		// Normal Tier
		$this->start_controls_tab( 'tier_normal', array( 'label' => 'Normal Tier' ) );
		$this->add_control(
			'tier_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-pricing-tier' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'tier_border',
				'selector' => '{{WRAPPER}} .sifency-pricing-tier',
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tier_shadow',
				'selector' => '{{WRAPPER}} .sifency-pricing-tier',
			)
		);
		$this->end_controls_tab();
		// Highlighted Tier
		$this->start_controls_tab( 'tier_highlight', array( 'label' => 'Highlighted Tier' ) );
		$this->add_control(
			'tier_highlight_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-pricing-tier--highlight' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'tier_highlight_border',
				'selector' => '{{WRAPPER}} .sifency-pricing-tier--highlight',
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tier_highlight_shadow',
				'selector' => '{{WRAPPER}} .sifency-pricing-tier--highlight',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_responsive_control(
			'tier_padding',
			array(
				'label'     => 'Padding',
				'type'      => Controls_Manager::DIMENSIONS,
				'selectors' => array( '{{WRAPPER}} .sifency-pricing-tier' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ),
				'separator' => 'before',
			)
		);
		$this->end_controls_section();

		// --- SUBSCRIPTION CONTENT STYLE ---
		$this->start_controls_section(
			'section_subscription_content_style',
			array(
				'label' => esc_html__( 'Subscription Content', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_tier_name',
			array(
				'label' => 'Tier Name',
				'type'  => Controls_Manager::HEADING,
			)
		);
		$this->add_control(
			'tier_name_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tier-name' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'tier_name_typography',
				'selector' => '{{WRAPPER}} .sifency-tier-name',
			)
		);

		$this->add_control(
			'heading_tier_price',
			array(
				'label'     => 'Price',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'tier_price_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tier-price' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'tier_price_typography',
				'selector' => '{{WRAPPER}} .sifency-tier-price',
			)
		);

		$this->add_control(
			'heading_tier_features',
			array(
				'label'     => 'Features',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'tier_features_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tier-features li' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'tier_features_typography',
				'selector' => '{{WRAPPER}} .sifency-tier-features li',
			)
		);

		$this->add_control(
			'heading_tier_button',
			array(
				'label'     => 'Button',
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);
		$this->add_control(
			'tier_button_text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tier-button' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'tier_button_bg_color',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-tier-button' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$pricing_model = sifency_get_field( 'digital_pricing_model' );

		if ( empty( $pricing_model ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$pricing_model = 'single';
		}

		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-pricing-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<?php
			if ( 'subscription' === $pricing_model ) {
				$this->render_subscription_table();
			} else {
				$this->render_single_price();
			}
			?>
		</div>
		<?php
	}

	protected function render_single_price() {
		$regular_price = sifency_get_field( 'digital_price' );
		$sale_price    = sifency_get_field( 'digital_sale_price' );

		if ( empty( $regular_price ) && empty( $sale_price ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$regular_price = '$29';
				$sale_price    = '$19';
			} else {
				return;
			}
		}

		echo '<div class="sifency-single-price">';
		if ( ! empty( $sale_price ) ) {
			echo '<span class="regular-price"><del>' . esc_html( $regular_price ) . '</del></span>';
			echo '<span class="sale-price">' . esc_html( $sale_price ) . '</span>';
		} elseif ( ! empty( $regular_price ) ) {
			echo '<span class="sale-price">' . esc_html( $regular_price ) . '</span>';
		}
		echo '</div>';
	}

	protected function render_subscription_table() {
		$tiers = sifency_get_field( 'digital_subscription_tiers' );

		if ( empty( $tiers ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$tiers = array(
					array(
						'tier_name'               => 'Personal',
						'tier_price'              => '$29',
						'tier_billing_cycle'      => '/ year',
						'tier_features'           => '<ul><li>Feature 1</li><li>Feature 2</li></ul>',
						'tier_purchase_link'      => '#',
						'tier_purchase_link_text' => 'Get Personal',
						'tier_highlight'          => 'no',
					),
					array(
						'tier_name'               => 'Business',
						'tier_price'              => '$99',
						'tier_billing_cycle'      => '/ year',
						'tier_features'           => '<ul><li>All Personal Features</li><li>Team Access</li><li>Priority Support</li></ul>',
						'tier_purchase_link'      => '#',
						'tier_purchase_link_text' => 'Get Business',
						'tier_highlight'          => 'yes',
					),
					array(
						'tier_name'               => 'Enterprise',
						'tier_price'              => 'Contact Us',
						'tier_billing_cycle'      => '',
						'tier_features'           => '<ul><li>All Business Features</li><li>Dedicated Account Manager</li></ul>',
						'tier_purchase_link'      => '#',
						'tier_purchase_link_text' => 'Contact Sales',
						'tier_highlight'          => 'no',
					),
				);
			} else {
				return;
			}
		}

		echo '<div class="sifency-pricing-table">';
		foreach ( $tiers as $tier ) {
			$tier_class = 'sifency-pricing-tier';
			if ( ( $tier['tier_highlight'] ?? 'no' ) === 'yes' ) {
				$tier_class .= ' sifency-pricing-tier--highlight';
			}

			// Get the button text, with a fallback
			$button_text = ! empty( $tier['tier_purchase_link_text'] ) ? $tier['tier_purchase_link_text'] : esc_html__( 'Choose Plan', 'sifency-addons' );

			echo '<div class="' . esc_attr( $tier_class ) . '">';

			if ( ! empty( $tier['tier_name'] ) ) {
				echo '<h3 class="sifency-tier-name">' . esc_html( $tier['tier_name'] ) . '</h3>';
			}

			if ( ! empty( $tier['tier_price'] ) ) {
				echo '<div class="sifency-tier-price">' . esc_html( $tier['tier_price'] ) . '<span class="sifency-tier-cycle">' . esc_html( $tier['tier_billing_cycle'] ?? '' ) . '</span></div>';
			}

			if ( ! empty( $tier['tier_features'] ) ) {
				echo '<div class="sifency-tier-features">';
				echo wp_kses_post( apply_filters( 'the_content', $tier['tier_features'] ) );
				echo '</div>';
			}

			if ( ! empty( $tier['tier_purchase_link'] ) ) {
				echo '<a href="' . esc_url( $tier['tier_purchase_link'] ) . '" class="sifency-tier-button" target="_blank" rel="noopener noreferrer">' . esc_html( $button_text ) . '</a>';
			}

			echo '</div>';
		}
		echo '</div>';
	}
}
