<?php
/**
 * Widget Name: Post Comments
 * Description: Display and style the WordPress comment list and comment form.
 *              Includes comment count, custom texts, avatar, form fields,
 *              reply button, cancel reply, and submit button styling.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.2.0
 */

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

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

/**
 * Class Captwiki_Post_Comments
 *
 * @since 1.2.0
 */
class Captwiki_Post_Comments extends Widget_Base {

	public function get_name(): string {
		return 'captwiki-post-comments';
	}

	public function get_title(): string {
		return esc_html__( 'Post Comments', 'captain-widgets-kit' );
	}

	public function get_icon(): string {
		return 'eicon-comments captwiki-el-logo';
	}

	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	public function get_keywords(): array {
		return array( 'post comments', 'comments', 'comment form', 'discussion', 'reply', 'captain-widgets-kit' );
	}

	public function is_dynamic_content(): bool {
		return true;
	}

	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/* ── Shared selectors ── */
	private function cw_field_sel(): string {
		return '{{WRAPPER}} .cw-post-comments #commentform #author,
			{{WRAPPER}} .cw-post-comments #commentform #email,
			{{WRAPPER}} .cw-post-comments #commentform #url,
			{{WRAPPER}} .cw-post-comments form.comment-form textarea#comment';
	}

	private function cw_comment_item_sel(): string {
		return '{{WRAPPER}} .cw-post-comments .comment-list > .comment:not(.parent),
			{{WRAPPER}} .cw-post-comments .comment-list ul.children li,
			{{WRAPPER}} .cw-post-comments .comment-list > .comment.parent > .comment-body';
	}

	private function cw_avatar_sel(): string {
		return '{{WRAPPER}} .cw-post-comments .comment-list li.comment > .comment-body img.avatar,
			{{WRAPPER}} .cw-post-comments .comment-list li.pingback > .comment-body img.avatar';
	}

	protected function register_controls(): void {

		/* ══════════════════════════════════════════
		 * CONTENT TAB
		 * ══════════════════════════════════════════ */

		// ── Section: General Settings ────────────
		$this->start_controls_section(
			'section_general',
			array(
				'label' => esc_html__( 'General Settings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'title',
			array(
				'label'       => esc_html__( 'Section Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Leave Your Comment', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Share Your Thoughts', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'leave_reply_text',
			array(
				'label'       => esc_html__( 'Reply Heading Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Leave A Reply To', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Replying to', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
				'description' => esc_html__( 'Shown when a user clicks "Reply" on a comment.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'cancel_reply_text',
			array(
				'label'       => esc_html__( 'Cancel Reply Link Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Cancel Reply', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Go Back', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->end_controls_section();

		// ── Section: Comment Form ────────────────
		$this->start_controls_section(
			'section_form_settings',
			array(
				'label' => esc_html__( 'Comment Form', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'placeholder_text',
			array(
				'label'       => esc_html__( 'Textarea Placeholder', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Write your comment here…', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'Enter placeholder text', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->add_control(
			'btn_text',
			array(
				'label'       => esc_html__( 'Submit Button Label', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Post Comment', 'captain-widgets-kit' ),
				'placeholder' => esc_html__( 'e.g. Submit Now', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$this->end_controls_section();

		// ── Section: Display Options ─────────────
		$this->start_controls_section(
			'section_display',
			array(
				'label' => esc_html__( 'Display Options', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_comment_count',
			array(
				'label'        => esc_html__( 'Show Comment Count', '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',
				'description'  => esc_html__( 'Displays the total comment count next to the section title.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'divider_col_width',
			array(
				'type'      => Controls_Manager::DIVIDER,
				'separator' => 'before',
			)
		);

		$this->add_control(
			'custom_width_enable',
			array(
				'label'        => esc_html__( 'Custom Column Widths', '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__( 'Set individual percentage widths for each form field. Fields float side-by-side on desktop.', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'custom_width_comment',
			array(
				'label'     => esc_html__( 'Textarea Width (%)', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'min' => 10, 'max' => 100, 'step' => 1 ) ),
				'default'   => array( 'size' => 100 ),
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments form.comment-form .cw-comment-row' => 'width: {{SIZE}}%; float: left; margin-right: 15px;',
				),
				'condition' => array( 'custom_width_enable' => 'yes' ),
			)
		);

		$this->add_control(
			'custom_width_author',
			array(
				'label'     => esc_html__( 'Name Field Width (%)', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'min' => 10, 'max' => 100, 'step' => 1 ) ),
				'default'   => array( 'size' => 45 ),
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments form.comment-form .comment-form-author' => 'width: {{SIZE}}%; float: left; margin-right: 15px;',
				),
				'condition' => array( 'custom_width_enable' => 'yes' ),
			)
		);

		$this->add_control(
			'custom_width_email',
			array(
				'label'     => esc_html__( 'Email Field Width (%)', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'min' => 10, 'max' => 100, 'step' => 1 ) ),
				'default'   => array( 'size' => 45 ),
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments form.comment-form .comment-form-email' => 'width: {{SIZE}}%; float: left; margin-right: 15px;',
				),
				'condition' => array( 'custom_width_enable' => 'yes' ),
			)
		);

		$this->add_control(
			'custom_width_website',
			array(
				'label'     => esc_html__( 'Website Field Width (%)', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'min' => 10, 'max' => 100, 'step' => 1 ) ),
				'default'   => array( 'size' => 45 ),
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments form.comment-form .comment-form-url' => 'width: {{SIZE}}%; float: left; margin-right: 15px;',
				),
				'condition' => array( 'custom_width_enable' => 'yes' ),
			)
		);

		$this->end_controls_section();

		/* ══════════════════════════════════════════
		 * STYLE TAB
		 * ══════════════════════════════════════════ */

		// ── Section: Section Title ───────────────
		$this->start_controls_section(
			'section_style_title',
			array(
				'label' => esc_html__( 'Section Title', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments .cw-comment-section-title,
					{{WRAPPER}} .cw-post-comments #respond.comment-respond h3#reply-title' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments .cw-comment-section-title,
					{{WRAPPER}} .cw-post-comments #respond.comment-respond h3#reply-title',
			)
		);

		$this->add_responsive_control(
			'title_margin',
			array(
				'label'      => esc_html__( 'Bottom Spacing', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 80 ) ),
				'selectors'  => array(
					'{{WRAPPER}} .cw-post-comments .cw-comment-section-title,
					{{WRAPPER}} .cw-post-comments #respond.comment-respond h3#reply-title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// ── Section: Commenter Avatar ────────────
		$this->start_controls_section(
			'section_style_avatar',
			array(
				'label' => esc_html__( 'Commenter Avatar', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'avatar_size',
			array(
				'label'      => esc_html__( 'Avatar Size', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 20, 'max' => 150 ) ),
				'default'    => array( 'size' => 60, 'unit' => 'px' ),
				'selectors'  => array(
					$this->cw_avatar_sel() => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'avatar_gap',
			array(
				'label'      => esc_html__( 'Gap From Content', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 40 ) ),
				'default'    => array( 'size' => 16, 'unit' => 'px' ),
				'selectors'  => array(
					$this->cw_avatar_sel() => 'margin-right: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( 'tabs_avatar' );

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

			$this->add_responsive_control(
				'avatar_border_radius',
				array(
					'label'      => esc_html__( 'Shape (Border Radius)', 'captain-widgets-kit' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'default'    => array( 'top' => '50', 'right' => '50', 'bottom' => '50', 'left' => '50', 'unit' => '%' ),
					'selectors'  => array(
						$this->cw_avatar_sel() => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'avatar_border',
					'selector' => $this->cw_avatar_sel(),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'avatar_shadow',
					'selector' => $this->cw_avatar_sel(),
				)
			);

			$this->end_controls_tab();

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

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

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'avatar_border_hover',
					'selector' => $this->cw_avatar_sel() . ':hover',
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'avatar_shadow_hover',
					'selector' => $this->cw_avatar_sel() . ':hover',
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// ── Section: Comment Info (name, date, text) ─
		$this->start_controls_section(
			'section_style_comment_info',
			array(
				'label' => esc_html__( 'Comment Info', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		// Author name
		$this->add_control(
			'heading_author_name',
			array(
				'label'     => esc_html__( 'Author Name', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::HEADING,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'author_name_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn .url,
					{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn',
			)
		);

		$this->start_controls_tabs( 'tabs_author_name' );

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

			$this->add_control(
				'author_name_color',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn .url,
						{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'author_name_color_hover',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn .url:hover,
						{{WRAPPER}} .cw-post-comments .comment-author.vcard .fn:hover' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_date_meta', array( 'type' => Controls_Manager::DIVIDER ) );

		// Date / meta
		$this->add_control(
			'heading_date_meta',
			array(
				'label' => esc_html__( 'Date & Meta', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'date_meta_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments .comment-meta .comment-metadata a',
			)
		);

		$this->start_controls_tabs( 'tabs_date_meta' );

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

			$this->add_control(
				'date_meta_color',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-meta .comment-metadata a' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'date_meta_color_hover',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-meta .comment-metadata a:hover' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_control( 'divider_comment_body', array( 'type' => Controls_Manager::DIVIDER ) );

		// Comment body text
		$this->add_control(
			'heading_comment_body',
			array(
				'label' => esc_html__( 'Comment Text', 'captain-widgets-kit' ),
				'type'  => Controls_Manager::HEADING,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'comment_body_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .comment-content,
					{{WRAPPER}} .cw-post-comments .comment-list .comment-content p',
			)
		);

		$this->start_controls_tabs( 'tabs_comment_body' );

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

			$this->add_control(
				'comment_body_color',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .comment-content,
						{{WRAPPER}} .cw-post-comments .comment-list .comment-content p' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'comment_body_color_hover',
				array(
					'label'     => esc_html__( 'Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .comment-content:hover,
						{{WRAPPER}} .cw-post-comments .comment-list .comment-content:hover p' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// ── Section: Comment Card (item box) ─────
		$this->start_controls_section(
			'section_style_comment_card',
			array(
				'label' => esc_html__( 'Comment Card', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

		$this->add_responsive_control(
			'card_gap',
			array(
				'label'      => esc_html__( 'Gap Between Cards', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array( 'px' => array( 'min' => 0, 'max' => 80 ) ),
				'selectors'  => array(
					$this->cw_comment_item_sel() => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'card_background',
				'label'    => esc_html__( 'Card Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => $this->cw_comment_item_sel(),
				'exclude'  => array( 'image' ),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'label'    => esc_html__( 'Card Border', 'captain-widgets-kit' ),
				'selector' => $this->cw_comment_item_sel(),
			)
		);

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

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'card_shadow',
				'label'    => esc_html__( 'Card Shadow', 'captain-widgets-kit' ),
				'selector' => $this->cw_comment_item_sel(),
			)
		);

		$this->end_controls_section();

		// ── Section: Reply Button ────────────────
		$this->start_controls_section(
			'section_style_reply_btn',
			array(
				'label' => esc_html__( 'Reply Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'reply_btn_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a',
			)
		);

		$this->add_responsive_control(
			'reply_btn_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .cw-post-comments .comment-list .reply a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( 'tabs_reply_btn' );

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

			$this->add_control(
				'reply_btn_text_color',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .reply a' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'reply_btn_bg',
					'types'    => array( 'classic', 'gradient' ),
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a',
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'reply_btn_border',
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a',
				)
			);

			$this->add_responsive_control(
				'reply_btn_border_radius',
				array(
					'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .reply a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'reply_btn_shadow',
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a',
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'reply_btn_text_color_hover',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .reply a:hover' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'reply_btn_bg_hover',
					'types'    => array( 'classic', 'gradient' ),
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a:hover',
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'reply_btn_border_hover',
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a:hover',
				)
			);

			$this->add_responsive_control(
				'reply_btn_border_radius_hover',
				array(
					'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .cw-post-comments .comment-list .reply a:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'reply_btn_shadow_hover',
					'selector' => '{{WRAPPER}} .cw-post-comments .comment-list .reply a:hover',
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// ── Section: Cancel Reply ────────────────
		$this->start_controls_section(
			'section_style_cancel',
			array(
				'label' => esc_html__( 'Cancel Reply Link', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'cancel_typography',
				'selector' => '{{WRAPPER}} .cw-post-comments #respond.comment-respond #cancel-comment-reply-link',
			)
		);

		$this->start_controls_tabs( 'tabs_cancel' );

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

			$this->add_control(
				'cancel_color',
				array(
					'label'     => esc_html__( 'Link Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments #respond.comment-respond #cancel-comment-reply-link' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'cancel_color_hover',
				array(
					'label'     => esc_html__( 'Link Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments #respond.comment-respond #cancel-comment-reply-link:hover' => 'color: {{VALUE}};',
					),
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// ── Section: Form Fields ─────────────────
		$this->start_controls_section(
			'section_style_fields',
			array(
				'label' => esc_html__( 'Form Fields', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'field_label_color',
			array(
				'label'     => esc_html__( 'Label Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments #respond #commentform label' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'field_label_typography',
				'label'     => esc_html__( 'Label Typography', 'captain-widgets-kit' ),
				'selector'  => '{{WRAPPER}} .cw-post-comments #respond #commentform label',
				'separator' => 'after',
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'field_input_typography',
				'label'    => esc_html__( 'Input Typography', 'captain-widgets-kit' ),
				'selector' => $this->cw_field_sel(),
			)
		);

		$this->add_responsive_control(
			'field_padding',
			array(
				'label'      => esc_html__( 'Input Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$this->cw_field_sel() => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'separator'  => 'after',
			)
		);

		$this->start_controls_tabs( 'tabs_fields' );

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

			$this->add_control(
				'field_text_color',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$this->cw_field_sel() => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'field_bg',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $this->cw_field_sel(),
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'field_border',
					'selector' => $this->cw_field_sel(),
				)
			);

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

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'field_shadow',
					'selector' => $this->cw_field_sel(),
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'field_text_color_focus',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						$this->cw_field_sel() . ':focus' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'field_bg_focus',
					'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
					'types'    => array( 'classic', 'gradient' ),
					'selector' => $this->cw_field_sel() . ':focus',
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'field_border_focus',
					'selector' => $this->cw_field_sel() . ':focus',
				)
			);

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

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'field_shadow_focus',
					'selector' => $this->cw_field_sel() . ':focus',
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		// ── Section: Submit Button ───────────────
		$this->start_controls_section(
			'section_style_submit',
			array(
				'label' => esc_html__( 'Submit Button', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'submit_align',
			array(
				'label'     => esc_html__( 'Button Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'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' ),
				),
				'default'   => 'left',
				'selectors' => array(
					'{{WRAPPER}} .cw-post-comments #comments .form-submit' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'submit_typography',
				'label'    => esc_html__( 'Button Typography', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit',
			)
		);

		$this->add_responsive_control(
			'submit_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .cw-post-comments #commentform #submit' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'submit_margin',
			array(
				'label'      => esc_html__( 'Margin', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .cw-post-comments #commentform #submit' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'separator'  => 'after',
			)
		);

		$this->start_controls_tabs( 'tabs_submit' );

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

			$this->add_control(
				'submit_text_color',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments #commentform #submit' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'submit_bg',
					'types'    => array( 'classic', 'gradient' ),
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit',
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'submit_border',
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit',
				)
			);

			$this->add_responsive_control(
				'submit_border_radius',
				array(
					'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .cw-post-comments #commentform #submit' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'submit_shadow',
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit',
				)
			);

			$this->end_controls_tab();

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

			$this->add_control(
				'submit_text_color_hover',
				array(
					'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
					'type'      => Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} .cw-post-comments #commentform #submit:hover' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Background::get_type(),
				array(
					'name'     => 'submit_bg_hover',
					'types'    => array( 'classic', 'gradient' ),
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit:hover',
					'exclude'  => array( 'image' ),
				)
			);

			$this->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'     => 'submit_border_hover',
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit:hover',
				)
			);

			$this->add_responsive_control(
				'submit_border_radius_hover',
				array(
					'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
					'type'       => Controls_Manager::DIMENSIONS,
					'size_units' => array( 'px', '%' ),
					'selectors'  => array(
						'{{WRAPPER}} .cw-post-comments #commentform #submit:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Box_Shadow::get_type(),
				array(
					'name'     => 'submit_shadow_hover',
					'selector' => '{{WRAPPER}} .cw-post-comments #commentform #submit:hover',
				)
			);

			$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	/**
	 * Build comment_form() args from widget settings.
	 *
	 * @since 1.2.0
	 */
	private function captwiki_build_comment_args(): array {

		$settings = $this->get_settings_for_display();

		$title       = ! empty( $settings['title'] ) ? $settings['title'] : esc_html__( 'Leave Your Comment', 'captain-widgets-kit' );
		$btn_text    = ! empty( $settings['btn_text'] ) ? $settings['btn_text'] : esc_html__( 'Post Comment', 'captain-widgets-kit' );
		$placeholder = ! empty( $settings['placeholder_text'] ) ? $settings['placeholder_text'] : esc_html__( 'Write your comment here…', 'captain-widgets-kit' );
		$leave_txt   = ! empty( $settings['leave_reply_text'] ) ? $settings['leave_reply_text'] : esc_html__( 'Leave A Reply To', 'captain-widgets-kit' );
		$cancel_txt  = ! empty( $settings['cancel_reply_text'] ) ? $settings['cancel_reply_text'] : esc_html__( 'Cancel Reply', 'captain-widgets-kit' );

		// Parent comment author for reply-to heading.
		$parent_id     = isset( $_GET['replytocom'] ) ? absint( $_GET['replytocom'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$parent_author = '';
		if ( $parent_id ) {
			$parent_comment = get_comment( $parent_id );
			if ( $parent_comment ) {
				$parent_author = $parent_comment->comment_author;
			}
		}

		// Logged-in user.
		$user          = wp_get_current_user();
		$user_identity = $user->exists() ? $user->display_name : '';

		return array(
			'id_form'              => 'commentform',
			'class_form'           => 'comment-form',
			'id_submit'            => 'submit',
			'title_reply'          => esc_html( $title ),
			'title_reply_to'       => $parent_id
				? sprintf(
					/* translators: 1: Reply heading text, 2: Parent comment author name */
					esc_html__( '%1$s %2$s', 'captain-widgets-kit' ),
					esc_html( $leave_txt ),
					esc_html( $parent_author )
				)
				: esc_html( $leave_txt ),
			'cancel_reply_link'    => esc_html( $cancel_txt ),
			'label_submit'         => esc_html( $btn_text ),
			'comment_notes_before' => '',
			'comment_notes_after'  => '',
			'comment_field'        => '<div class="cw-comment-row"><label><textarea id="comment" name="comment" cols="45" rows="6" placeholder="' . esc_attr( $placeholder ) . '" aria-required="true"></textarea></label></div>',
			'must_log_in'          => '<p class="must-log-in">' .
				sprintf(
					/* translators: 1: Opening <a> tag, 2: Closing </a> tag */
					esc_html__( 'You must be %1$slogged in%2$s to post a comment.', 'captain-widgets-kit' ),
					'<a href="' . esc_url( wp_login_url( get_permalink() ) ) . '">',
					'</a>'
				) . '</p>',
			'logged_in_as'         => '<p class="logged-in-as">' .
				sprintf(
					/* translators: 1: User display name, 2: Profile link open, 3: Profile link close, 4: Logout link open, 5: Logout link close */
					esc_html__( 'Logged in as %1$s%2$s. %3$sLog out?%4$s', 'captain-widgets-kit' ),
					esc_html( $user_identity ),
					'<a href="' . esc_url( admin_url( 'profile.php' ) ) . '"></a>',
					'<a href="' . esc_url( wp_logout_url( get_permalink() ) ) . '" title="' . esc_attr__( 'Log out of this account', 'captain-widgets-kit' ) . '">',
					'</a>'
				) . '</p>',
		);
	}

	/**
	 * Render widget HTML.
	 *
	 * @since 1.2.0
	 */
	protected function render(): void {

		$settings = $this->get_settings_for_display();

		// Editor placeholder — comment_form() needs a real post context.
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			echo '<div class="cw-post-comments cw-editor-placeholder">';
			echo '<p style="padding:24px;text-align:center;color:#94a3b8;">';
			echo '<span style="font-size:36px;display:block;margin-bottom:10px;">💬</span>';
			esc_html_e( 'Post Comments — preview visible on the frontend.', 'captain-widgets-kit' );
			echo '</p>';
			echo '</div>';
			return;
		}

		$post_id       = get_queried_object_id();
		$comments      = get_comments( array( 'post_id' => $post_id ) );
		$show_count    = ! empty( $settings['show_comment_count'] ) && 'yes' === $settings['show_comment_count'];
		$comment_count = $show_count ? get_comments_number( $post_id ) : 0;

		$list_args = array(
			'style'      => 'ul',
			'short_ping' => true,
			'avatar_size' => 100,
		);

		ob_start();

		echo '<div class="cw-post-comments">';
		echo '<div id="comments" class="comments-area">';

		if ( get_comments_number( $post_id ) > 0 ) {
			echo '<ul class="comment-list">';
			echo '<li>';
			if ( $show_count && $comment_count ) {
				/* translators: %d: Number of comments */
				echo '<div class="cw-comment-section-title">' . esc_html__( 'Comments', 'captain-widgets-kit' ) . ' (' . absint( $comment_count ) . ')</div>';
			} else {
				echo '<div class="cw-comment-section-title">' . esc_html__( 'Comments', 'captain-widgets-kit' ) . '</div>';
			}
			echo '</li>';
			wp_list_comments( $list_args, $comments );
			echo '</ul>';
		}

		comment_form( $this->captwiki_build_comment_args(), $post_id );

		echo '</div>'; // #comments
		echo '</div>'; // .cw-post-comments

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo ob_get_clean();
	}
}
