<?php
/**
 * Optional Elementor widget for the Junk Removal Cost Calculator.
 *
 * @package JunkRemovalCalculator
 */

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

if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

final class Junkreco_Elementor_Calculator_Widget extends \Elementor\Widget_Base {
	public function get_name(): string {
		return 'junkreco_calculator';
	}

	public function get_title(): string {
		return __( 'Junk Removal Calculator', 'junk-removal-cost-calculator' );
	}

	public function get_icon(): string {
		return 'eicon-calculator';
	}

	/**
	 * @return array<int, string>
	 */
	public function get_categories(): array {
		return array( 'junkreco-calculators' );
	}

	/**
	 * @return array<int, string>
	 */
	public function get_keywords(): array {
		return array( 'junk', 'removal', 'calculator', 'estimate', 'pricing' );
	}

	protected function register_controls(): void {
		$this->start_controls_section(
			'junkreco_content',
			array(
				'label' => __( 'Calculator Content', 'junk-removal-cost-calculator' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'title',
			array(
				'label'       => __( 'Title', 'junk-removal-cost-calculator' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => __( 'Calculate Your Junk Removal Cost', 'junk-removal-cost-calculator' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'intro',
			array(
				'label'       => __( 'Intro Text', 'junk-removal-cost-calculator' ),
				'type'        => \Elementor\Controls_Manager::TEXTAREA,
				'default'     => __( 'Choose item-based pricing, truck volume, or AI photo estimating, then continue with the booking link when the price looks right.', 'junk-removal-cost-calculator' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'booking_url',
			array(
				'label'       => __( 'Booking URL Override', 'junk-removal-cost-calculator' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'placeholder' => __( 'Leave blank to use plugin settings', 'junk-removal-cost-calculator' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'booking_mode',
			array(
				'label'   => __( 'Booking Action', 'junk-removal-cost-calculator' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => array(
					''         => __( 'Use plugin settings', 'junk-removal-cost-calculator' ),
					'form'     => __( 'Use built-in booking form', 'junk-removal-cost-calculator' ),
					'redirect' => __( 'Redirect to booking URL', 'junk-removal-cost-calculator' ),
				),
			)
		);

		$this->add_control(
			'pricing_methods',
			array(
				'label'   => __( 'Pricing Methods', 'junk-removal-cost-calculator' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => array(
					''                => __( 'Use plugin settings', 'junk-removal-cost-calculator' ),
					'specific,volume,ai' => __( 'All pricing methods', 'junk-removal-cost-calculator' ),
					'specific,volume' => __( 'Specific item-based and truck volume-based', 'junk-removal-cost-calculator' ),
					'specific'        => __( 'Specific item-based only', 'junk-removal-cost-calculator' ),
					'volume'          => __( 'Truck volume-based only', 'junk-removal-cost-calculator' ),
					'ai'              => __( 'AI Photo Estimator only', 'junk-removal-cost-calculator' ),
				),
			)
		);

		$this->add_control(
			'button_label',
			array(
				'label'       => __( 'Button Label', 'junk-removal-cost-calculator' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => __( 'Book Your Pickup', 'junk-removal-cost-calculator' ),
				'label_block' => true,
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$atts     = array();

		foreach ( array( 'title', 'intro', 'booking_url', 'booking_mode', 'pricing_methods', 'button_label' ) as $key ) {
			if ( isset( $settings[ $key ] ) && '' !== trim( (string) $settings[ $key ] ) ) {
				$atts[] = sprintf( '%s="%s"', $key, esc_attr( (string) $settings[ $key ] ) );
			}
		}

		echo do_shortcode( '[junkreco_calculator ' . implode( ' ', $atts ) . ']' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}
}
