<?php
/**
 * Widget Name: Pricing Box
 * Description: A professional pricing table / plan box with selectable visual styles,
 *              optional ribbon badge, plan header ( icon, title, subtitle ), price block
 *              with sale ( strike-through ) support, a feature list ( included / excluded
 *              rows ) and a call-to-action button. Only the selected style CSS file is
 *              loaded on the frontend.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Elementor\Icons_Manager;

/**
 * Class Captwiki_Pricing_Box
 *
 * @since 1.3.0
 */
class Captwiki_Pricing_Box extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-pricing';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Pricing Box', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-price-table captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'pricing', 'price', 'plan', 'table', 'package', 'subscription', 'captain-widgets-kit' );
	}

	/**
	 * Common stylesheet dependency.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_style_depends(): array {
		return array( 'captwiki_pricing' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.3.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Selectable visual styles.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Classic Card', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Elevated', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Gradient Header', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Minimal', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Filled Accent', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Register widget controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	protected function register_controls(): void {
		$this->register_header_controls();
		$this->register_price_controls();
		$this->register_features_controls();
		$this->register_button_controls();

		$this->register_general_style_controls();
		$this->register_badge_style_controls();
		$this->register_header_style_controls();
		$this->register_price_style_controls();
		$this->register_features_style_controls();
		$this->register_button_style_controls();
	}

	/**
	 * Content tab — header & badge controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_header_controls(): void {

		$this->start_controls_section(
			'section_header_content',
			array(
				'label' => esc_html__( 'Header', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$this->add_control(
			'is_featured',
			array(
				'label'        => esc_html__( 'Highlight ( Featured )', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
				'description'  => esc_html__( 'Visually emphasise this plan as the recommended one.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'show_badge',
			array(
				'label'        => esc_html__( 'Show Badge', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'badge_text',
			array(
				'label'     => esc_html__( 'Badge Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Popular', 'captain-widgets-kit' ),
				'dynamic'   => array( 'active' => true ),
				'condition' => array( 'show_badge' => 'yes' ),
			)
		);

		$this->add_control(
			'show_icon',
			array(
				'label'        => esc_html__( 'Show Icon', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'plan_icon',
			array(
				'label'       => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::ICONS,
				'default'     => array(
					'value'   => 'fas fa-rocket',
					'library' => 'fa-solid',
				),
				'skin'        => 'inline',
				'label_block' => false,
				'condition'   => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_control(
			'plan_title',
			array(
				'label'       => esc_html__( 'Plan Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Professional', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Professional', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'plan_subtitle',
			array(
				'label'       => esc_html__( 'Subtitle', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXTAREA,
				'rows'        => 2,
				'default'     => esc_html__( 'Best for growing teams and businesses.', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'title_tag',
			array(
				'label'     => esc_html__( 'Title HTML Tag', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'h3',
				'options'   => array(
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'div'  => 'div',
					'span' => 'span',
				),
				'separator' => 'before',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — price controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_price_controls(): void {

		$this->start_controls_section(
			'section_price_content',
			array(
				'label' => esc_html__( 'Price', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'currency',
			array(
				'label'   => esc_html__( 'Currency Symbol', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '$',
			)
		);

		$this->add_control(
			'currency_position',
			array(
				'label'   => esc_html__( 'Symbol Position', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'before',
				'options' => array(
					'before' => esc_html__( 'Before ( $49 )', 'captain-widgets-kit' ),
					'after'  => esc_html__( 'After ( 49$ )', 'captain-widgets-kit' ),
				),
			)
		);

		$this->add_control(
			'price',
			array(
				'label'   => esc_html__( 'Price', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '49',
				'dynamic' => array( 'active' => true ),
			)
		);

		$this->add_control(
			'period',
			array(
				'label'       => esc_html__( 'Period', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( '/month', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. /month', 'captain-widgets-kit' ),
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'show_original_price',
			array(
				'label'        => esc_html__( 'Show Original Price', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
				'separator'    => 'before',
			)
		);

		$this->add_control(
			'original_price',
			array(
				'label'       => esc_html__( 'Original Price', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '99',
				'description' => esc_html__( 'Shown with a strike-through to signal a discount.', 'captain-widgets-kit' ),
				'condition'   => array( 'show_original_price' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — features list controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_features_controls(): void {

		$this->start_controls_section(
			'section_features_content',
			array(
				'label' => esc_html__( 'Features', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'feature_text',
			array(
				'label'       => esc_html__( 'Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Feature item', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$repeater->add_control(
			'feature_state',
			array(
				'label'   => esc_html__( 'State', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'included',
				'options' => array(
					'included' => esc_html__( 'Included', 'captain-widgets-kit' ),
					'excluded' => esc_html__( 'Excluded', 'captain-widgets-kit' ),
				),
			)
		);

		$repeater->add_control(
			'feature_icon',
			array(
				'label'       => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::ICONS,
				'default'     => array(
					'value'   => 'fas fa-check',
					'library' => 'fa-solid',
				),
				'skin'        => 'inline',
				'label_block' => false,
			)
		);

		$repeater->add_control(
			'feature_tooltip',
			array(
				'label'       => esc_html__( 'Tooltip', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'label_block' => true,
				'description' => esc_html__( 'Optional hint shown on hover.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'features',
			array(
				'label'       => esc_html__( 'Feature List', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'feature_text' => esc_html__( 'Unlimited projects', 'captain-widgets-kit' ) ),
					array( 'feature_text' => esc_html__( '50 GB cloud storage', 'captain-widgets-kit' ) ),
					array( 'feature_text' => esc_html__( 'Priority email support', 'captain-widgets-kit' ) ),
					array(
						'feature_text'  => esc_html__( 'Dedicated account manager', 'captain-widgets-kit' ),
						'feature_state' => 'excluded',
						'feature_icon'  => array( 'value' => 'fas fa-times', 'library' => 'fa-solid' ),
					),
				),
				'title_field' => '{{{ feature_text }}}',
			)
		);

		$this->add_control(
			'show_feature_dividers',
			array(
				'label'        => esc_html__( 'Show Dividers', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — button & footer controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_button_controls(): void {

		$this->start_controls_section(
			'section_button_content',
			array(
				'label' => esc_html__( 'Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_button',
			array(
				'label'        => esc_html__( 'Show Button', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'     => esc_html__( 'Button Text', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Get Started', 'captain-widgets-kit' ),
				'dynamic'   => array( 'active' => true ),
				'condition' => array( 'show_button' => 'yes' ),
			)
		);

		$this->add_control(
			'button_link',
			array(
				'label'         => esc_html__( 'Link', 'captain-widgets-kit' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-link.com', 'captain-widgets-kit' ),
				'show_external' => true,
				'default'       => array( 'url' => '#' ),
				'dynamic'       => array( 'active' => true ),
				'condition'     => array( 'show_button' => 'yes' ),
			)
		);

		$this->add_control(
			'button_icon',
			array(
				'label'       => esc_html__( 'Button Icon', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::ICONS,
				'skin'        => 'inline',
				'label_block' => false,
				'condition'   => array( 'show_button' => 'yes' ),
			)
		);

		$this->add_control(
			'button_icon_position',
			array(
				'label'     => esc_html__( 'Icon Position', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'after',
				'options'   => array(
					'before' => esc_html__( 'Before', 'captain-widgets-kit' ),
					'after'  => esc_html__( 'After', 'captain-widgets-kit' ),
				),
				'condition' => array( 'show_button' => 'yes' ),
			)
		);

		$this->add_control(
			'footer_note',
			array(
				'label'       => esc_html__( 'Footer Note', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'No credit card required', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
				'separator'   => 'before',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * General (wrapper) style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_general_style_controls(): void {

		$this->start_controls_section(
			'section_general_style',
			array(
				'label' => esc_html__( 'General', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'content_align',
			array(
				'label'     => esc_html__( 'Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'center',
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'       => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '',
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-pricing' => '--captwiki-pb-accent: {{VALUE}};',
				),
				'description' => esc_html__( 'Drives badge, button, icons and style highlights.', 'captain-widgets-kit' ),
			)
		);

		$this->add_responsive_control(
			'max_width',
			array(
				'label'      => esc_html__( 'Max Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array( 'min' => 220, 'max' => 700 ),
					'%'  => array( 'min' => 20, 'max' => 100 ),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing' => 'max-width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'wrapper_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-pricing',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'wrapper_border',
				'label'    => esc_html__( 'Border', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-pricing',
			)
		);

		$this->add_responsive_control(
			'wrapper_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'wrapper_shadow',
				'label'    => esc_html__( 'Box Shadow', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-pricing',
			)
		);

		$this->add_responsive_control(
			'wrapper_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Badge style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_badge_style_controls(): void {

		$this->start_controls_section(
			'section_badge_style',
			array(
				'label'     => esc_html__( 'Badge', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_badge' => 'yes' ),
			)
		);

		$this->add_control(
			'badge_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__badge' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'badge_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__badge' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'badge_typography',
				'selector' => '{{WRAPPER}} .captwiki-pricing__badge',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Header style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_header_style_controls(): void {

		$this->start_controls_section(
			'section_header_style',
			array(
				'label' => esc_html__( 'Header', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'icon_heading',
			array(
				'label'     => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'condition' => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__icon'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__icon svg' => 'fill: {{VALUE}};',
				),
				'condition' => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			'icon_size',
			array(
				'label'      => esc_html__( 'Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 14, 'max' => 100 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing__icon' => 'font-size: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array( 'show_icon' => 'yes' ),
			)
		);

		$this->add_control(
			'title_heading',
			array(
				'label'     => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__title' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .captwiki-pricing__title',
			)
		);

		$this->add_control(
			'subtitle_heading',
			array(
				'label'     => esc_html__( 'Subtitle', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'subtitle_color',
			array(
				'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__subtitle' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'subtitle_typography',
				'selector' => '{{WRAPPER}} .captwiki-pricing__subtitle',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Price style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_price_style_controls(): void {

		$this->start_controls_section(
			'section_price_style',
			array(
				'label' => esc_html__( 'Price', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'price_color',
			array(
				'label'     => esc_html__( 'Price Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__amount'   => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__currency' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'price_typography',
				'label'    => esc_html__( 'Price Typography', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-pricing__amount',
			)
		);

		$this->add_control(
			'currency_size',
			array(
				'label'      => esc_html__( 'Currency Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em' ),
				'range'      => array(
					'%'  => array( 'min' => 20, 'max' => 100 ),
					'px' => array( 'min' => 10, 'max' => 80 ),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing__currency' => 'font-size: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'period_color',
			array(
				'label'     => esc_html__( 'Period Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__period' => 'color: {{VALUE}};',
				),
				'separator' => 'before',
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'period_typography',
				'label'    => esc_html__( 'Period Typography', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-pricing__period',
			)
		);

		$this->add_control(
			'original_color',
			array(
				'label'     => esc_html__( 'Original Price Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__original' => 'color: {{VALUE}};',
				),
				'separator' => 'before',
				'condition' => array( 'show_original_price' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Features style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_features_style_controls(): void {

		$this->start_controls_section(
			'section_features_style',
			array(
				'label' => esc_html__( 'Features', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'feature_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__feature-text' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'feature_excluded_color',
			array(
				'label'     => esc_html__( 'Excluded Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__feature.is-excluded .captwiki-pricing__feature-text' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'feature_typography',
				'selector' => '{{WRAPPER}} .captwiki-pricing__feature-text',
			)
		);

		$this->add_control(
			'feature_icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__feature-icon'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__feature-icon svg' => 'fill: {{VALUE}};',
				),
				'separator' => 'before',
			)
		);

		$this->add_control(
			'feature_excluded_icon_color',
			array(
				'label'     => esc_html__( 'Excluded Icon Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__feature.is-excluded .captwiki-pricing__feature-icon'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__feature.is-excluded .captwiki-pricing__feature-icon svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'feature_spacing',
			array(
				'label'      => esc_html__( 'Row Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing__feature' => 'padding-top: {{SIZE}}{{UNIT}}; padding-bottom: {{SIZE}}{{UNIT}};',
				),
				'separator'  => 'before',
			)
		);

		$this->add_control(
			'divider_color',
			array(
				'label'     => esc_html__( 'Divider Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__feature' => 'border-bottom-color: {{VALUE}};',
				),
				'condition' => array( 'show_feature_dividers' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Button style controls.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	private function register_button_style_controls(): void {

		$this->start_controls_section(
			'section_button_style',
			array(
				'label'     => esc_html__( 'Button', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_button' => 'yes' ),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'button_typography',
				'selector' => '{{WRAPPER}} .captwiki-pricing__btn',
			)
		);

		$this->add_responsive_control(
			'button_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing__btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'button_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-pricing__btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'button_full_width',
			array(
				'label'        => esc_html__( 'Full Width', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'No', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'selectors'    => array(
					'{{WRAPPER}} .captwiki-pricing__btn' => 'display: flex; width: 100%;',
				),
			)
		);

		$this->start_controls_tabs( 'button_color_tabs' );

		$this->start_controls_tab(
			'button_normal_tab',
			array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'button_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__btn'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__btn svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'button_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__btn' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'button_hover_tab',
			array( 'label' => esc_html__( 'Hover', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'button_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__btn:hover'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-pricing__btn:hover svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'button_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-pricing__btn:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	/**
	 * Render the price block.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_price( array $settings ): void {
		$currency = trim( (string) $settings['currency'] );
		$price    = trim( (string) $settings['price'] );
		$period   = trim( (string) $settings['period'] );
		$pos      = 'after' === $settings['currency_position'] ? 'after' : 'before';

		if ( '' === $price && '' === $currency ) {
			return;
		}

		echo '<div class="captwiki-pricing__price">';

		if ( 'yes' === $settings['show_original_price'] && '' !== trim( (string) $settings['original_price'] ) ) {
			$orig = trim( (string) $settings['original_price'] );
			$orig = 'after' === $pos ? $orig . $currency : $currency . $orig;
			echo '<span class="captwiki-pricing__original">' . esc_html( $orig ) . '</span>';
		}

		echo '<span class="captwiki-pricing__price-row">';

		if ( '' !== $currency && 'before' === $pos ) {
			echo '<span class="captwiki-pricing__currency">' . esc_html( $currency ) . '</span>';
		}

		echo '<span class="captwiki-pricing__amount">' . esc_html( $price ) . '</span>';

		if ( '' !== $currency && 'after' === $pos ) {
			echo '<span class="captwiki-pricing__currency">' . esc_html( $currency ) . '</span>';
		}

		if ( '' !== $period ) {
			echo '<span class="captwiki-pricing__period">' . esc_html( $period ) . '</span>';
		}

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

	/**
	 * Render the feature list.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_features( array $settings ): void {
		if ( empty( $settings['features'] ) || ! is_array( $settings['features'] ) ) {
			return;
		}

		$divider_class = 'yes' === $settings['show_feature_dividers'] ? ' captwiki-pricing__features--divided' : '';

		echo '<ul class="captwiki-pricing__features' . esc_attr( $divider_class ) . '">';

		foreach ( $settings['features'] as $feature ) {
			$text = isset( $feature['feature_text'] ) ? trim( (string) $feature['feature_text'] ) : '';

			if ( '' === $text ) {
				continue;
			}

			$excluded = isset( $feature['feature_state'] ) && 'excluded' === $feature['feature_state'];
			$state    = $excluded ? ' is-excluded' : ' is-included';
			$tooltip  = isset( $feature['feature_tooltip'] ) ? trim( (string) $feature['feature_tooltip'] ) : '';
			$title    = '' !== $tooltip ? ' title="' . esc_attr( $tooltip ) . '"' : '';

			echo '<li class="captwiki-pricing__feature' . esc_attr( $state ) . '"' . $title . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

			if ( ! empty( $feature['feature_icon']['value'] ) ) {
				echo '<span class="captwiki-pricing__feature-icon" aria-hidden="true">';
				Icons_Manager::render_icon( $feature['feature_icon'], array( 'aria-hidden' => 'true' ) );
				echo '</span>';
			}

			echo '<span class="captwiki-pricing__feature-text">' . esc_html( $text ) . '</span>';
			echo '</li>';
		}

		echo '</ul>';
	}

	/**
	 * Render the call-to-action button.
	 *
	 * @since 1.3.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_button( array $settings ): void {
		if ( 'yes' !== $settings['show_button'] ) {
			return;
		}

		$text = trim( (string) $settings['button_text'] );

		if ( '' === $text ) {
			return;
		}

		$url    = ! empty( $settings['button_link']['url'] ) ? $settings['button_link']['url'] : '#';
		$target = ! empty( $settings['button_link']['is_external'] ) ? ' target="_blank"' : '';
		$rel    = ! empty( $settings['button_link']['nofollow'] ) ? ' rel="nofollow"' : '';

		$has_icon = ! empty( $settings['button_icon']['value'] );
		$icon_pos = 'before' === $settings['button_icon_position'] ? 'before' : 'after';

		echo '<a class="captwiki-pricing__btn" href="' . esc_url( $url ) . '"' . $target . $rel . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( $has_icon && 'before' === $icon_pos ) {
			echo '<span class="captwiki-pricing__btn-icon captwiki-pricing__btn-icon--before">';
			Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		echo '<span class="captwiki-pricing__btn-text">' . esc_html( $text ) . '</span>';

		if ( $has_icon && 'after' === $icon_pos ) {
			echo '<span class="captwiki-pricing__btn-icon captwiki-pricing__btn-icon--after">';
			Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		echo '</a>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.3.0
	 * @return void
	 */
	protected function render(): void {
		$settings = $this->get_settings_for_display();

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-pricing-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/pricing/captwiki-pricing-' . $style . '.css',
			array( 'captwiki_pricing' ),
			CAPTWIKI_VERSION
		);

		$featured   = 'yes' === $settings['is_featured'] ? ' is-featured' : '';
		$title_tag  = in_array( $settings['title_tag'], array( 'h2', 'h3', 'h4', 'h5', 'div', 'span' ), true ) ? $settings['title_tag'] : 'h3';
		$title      = trim( (string) $settings['plan_title'] );
		$subtitle   = trim( (string) $settings['plan_subtitle'] );
		$note       = trim( (string) $settings['footer_note'] );
		$unique     = 'captwiki-widget-' . $this->get_id();

		echo '<div class="captwiki-pricing captwiki-' . esc_attr( $style ) . $featured . ' ' . esc_attr( $unique ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( 'yes' === $settings['show_badge'] && '' !== trim( (string) $settings['badge_text'] ) ) {
			echo '<span class="captwiki-pricing__badge">' . esc_html( $settings['badge_text'] ) . '</span>';
		}

		echo '<div class="captwiki-pricing__header">';

		if ( 'yes' === $settings['show_icon'] && ! empty( $settings['plan_icon']['value'] ) ) {
			echo '<span class="captwiki-pricing__icon" aria-hidden="true">';
			Icons_Manager::render_icon( $settings['plan_icon'], array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		if ( '' !== $title ) {
			echo '<' . esc_attr( $title_tag ) . ' class="captwiki-pricing__title">' . esc_html( $title ) . '</' . esc_attr( $title_tag ) . '>';
		}

		if ( '' !== $subtitle ) {
			echo '<p class="captwiki-pricing__subtitle">' . esc_html( $subtitle ) . '</p>';
		}

		echo '</div>';

		$this->captwiki_render_price( $settings );
		$this->captwiki_render_features( $settings );

		echo '<div class="captwiki-pricing__footer">';
			$this->captwiki_render_button( $settings );

		if ( '' !== $note ) {
			echo '<p class="captwiki-pricing__note">' . esc_html( $note ) . '</p>';
		}

		echo '</div>';

		echo '</div>';
	}
}
