<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\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_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Digital_Product_Pricing extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() { return 'sifency-digital-product-pricing'; }
	public function get_title() { return esc_html__( 'Digital Product Pricing', 'sifency-addons' ); }
	public function get_icon() { return 'eicon-meta-data'; }
	public function get_categories() { return [ 'sifency-digital-components' ]; }
	public function get_keywords() { return [ 'digital', 'product', 'price', 'pricing', 'table', 'subscription', 'meta', 'sifency' ]; }
	public function get_style_depends() { return [ 'sifency-digital-style' ]; }
	protected function register_controls() {
		// --- SINGLE PRICE STYLE ---
		$this->start_controls_section( 'section_single_price_style', [ 'label' => esc_html__( 'Single Price Style', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_control( 'heading_regular_price', [ 'label' => 'Regular Price', 'type' => Controls_Manager::HEADING ] );
		$this->add_control( 'regular_price_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-single-price .regular-price' => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'regular_price_typography', 'selector' => '{{WRAPPER}} .sifency-single-price .regular-price' ] );
		$this->add_control( 'heading_sale_price', [ 'label' => 'Sale Price', 'type' => Controls_Manager::HEADING, 'separator' => 'before' ] );
		$this->add_control( 'sale_price_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-single-price .sale-price' => 'color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'sale_price_typography', 'selector' => '{{WRAPPER}} .sifency-single-price .sale-price' ] );
		$this->add_responsive_control( 'price_spacing', [ 'label' => 'Spacing', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-single-price' => 'gap: {{SIZE}}{{UNIT}};' ] ] );
		$this->end_controls_section();

		// --- SUBSCRIPTION TABLE STYLE ---
		$this->start_controls_section( 'section_subscription_style', [ 'label' => esc_html__( 'Subscription Table', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_responsive_control( 'columns', [ 'label' => 'Columns', 'type' => Controls_Manager::SELECT, 'options' => ['1'=>'1','2'=>'2','3'=>'3','4'=>'4'], 'default' => '3', 'selectors' => [ '{{WRAPPER}} .sifency-pricing-table' => 'grid-template-columns: repeat({{VALUE}}, 1fr);' ] ] );
		$this->add_responsive_control( 'table_gap', [ 'label' => 'Gap', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-pricing-table' => 'gap: {{SIZE}}{{UNIT}};' ] ] );
		
		$this->start_controls_tabs( 'tier_tabs_style' );
		// Normal Tier
		$this->start_controls_tab( 'tier_normal', ['label' => 'Normal Tier'] );
		$this->add_control( 'tier_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-pricing-tier' => 'background-color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'tier_border', 'selector' => '{{WRAPPER}} .sifency-pricing-tier' ] );
		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'tier_shadow', 'selector' => '{{WRAPPER}} .sifency-pricing-tier' ] );
		$this->end_controls_tab();
		// Highlighted Tier
		$this->start_controls_tab( 'tier_highlight', ['label' => 'Highlighted Tier'] );
		$this->add_control( 'tier_highlight_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-pricing-tier--highlight' => 'background-color: {{VALUE}};' ] ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'tier_highlight_border', 'selector' => '{{WRAPPER}} .sifency-pricing-tier--highlight' ] );
		$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'tier_highlight_shadow', 'selector' => '{{WRAPPER}} .sifency-pricing-tier--highlight' ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_responsive_control( 'tier_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{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', [ 'label' => esc_html__( 'Subscription Content', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_control('heading_tier_name', ['label' => 'Tier Name', 'type' => Controls_Manager::HEADING]);
		$this->add_control('tier_name_color', ['label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-tier-name' => 'color: {{VALUE}};']]);
		$this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'tier_name_typography', 'selector' => '{{WRAPPER}} .sifency-tier-name']);
		
		$this->add_control('heading_tier_price', ['label' => 'Price', 'type' => Controls_Manager::HEADING, 'separator'=>'before']);
		$this->add_control('tier_price_color', ['label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-tier-price' => 'color: {{VALUE}};']]);
		$this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'tier_price_typography', 'selector' => '{{WRAPPER}} .sifency-tier-price']);

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

		$this->add_control('heading_tier_button', ['label' => 'Button', 'type' => Controls_Manager::HEADING, 'separator'=>'before']);
		$this->add_control('tier_button_text_color', ['label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-tier-button' => 'color: {{VALUE}};']]);
		$this->add_control('tier_button_bg_color', ['label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => ['{{WRAPPER}} .sifency-tier-button' => 'background-color: {{VALUE}};']]);
		$this->end_controls_section();

		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$pricing_model = sifency_get_field( 'digital_pricing_model' );
		
		if ( empty( $pricing_model ) && \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$pricing_model = 'single';
		}

		?>
		<div 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 = [
					[
						'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',
					],
					[
						'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',
					],
					[
						'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>';
	}
}