<?php
/**
 * Dealer Info widget — dealer card with logo, contact, hours, map.
 *
 * Resolution: explicit `dealer_id` → linked vehicle's dealer (via specs row)
 *   → editor preview post → global post (template builder).
 * Delegates to DealerInfoBlock so per-field visibility toggles, modifier
 * classes, and template part are identical to the Gutenberg surface.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Blocks\DealerInfoBlock;

class DealerInfoWidget extends Widget_Base {

	public function get_name(): string {
		return 'trx_ad_dealer_info';
	}

	public function get_title(): string {
		return __( 'Dealer Info', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		return 'eicon-call-to-action';
	}

	public function get_categories(): array {
		return array( 'trx-auto-dealer' );
	}

	public function get_keywords(): array {
		return array( 'dealer', 'info', 'contact', 'card' );
	}

	public function get_style_depends(): array {
		return array( 'trx-ad-frontend' );
	}

	public function get_script_depends(): array {
		return array( 'trx-ad-frontend' );
	}

	protected function register_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Content', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'dealer_id',
			array(
				'label'       => __( 'Dealer', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => WidgetHelpers::dealerOptions( __( '— Resolve from context —', 'trx-auto-dealer' ) ),
				'default'     => '',
				'description' => __( 'Leave empty to resolve from the current vehicle or dealer post.', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'vehicle_id',
			array(
				'label'       => __( 'Or Vehicle', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => WidgetHelpers::vehicleOptions( __( '— None —', 'trx-auto-dealer' ) ),
				'default'     => '',
				'description' => __( 'Render the dealer linked to this vehicle.', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_visibility',
			array(
				'label' => __( 'Show / Hide Fields', 'trx-auto-dealer' ),
			)
		);

		$visibility = array(
			'show_logo'         => __( 'Logo', 'trx-auto-dealer' ),
			'show_heading'      => __( 'Dealer Name Heading', 'trx-auto-dealer' ),
			'show_phone'        => __( 'Phone', 'trx-auto-dealer' ),
			'show_email'        => __( 'Email', 'trx-auto-dealer' ),
			'show_address'      => __( 'Address', 'trx-auto-dealer' ),
			'show_website'      => __( 'Website', 'trx-auto-dealer' ),
			'show_hours'        => __( 'Hours', 'trx-auto-dealer' ),
			'show_map'          => __( 'Map', 'trx-auto-dealer' ),
			'show_description'  => __( 'Description', 'trx-auto-dealer' ),
			'show_call_btn'     => __( 'Call Button', 'trx-auto-dealer' ),
			'show_whatsapp_btn' => __( 'WhatsApp Button', 'trx-auto-dealer' ),
			'show_email_btn'    => __( 'Email Button', 'trx-auto-dealer' ),
			'show_dealer_link'  => __( 'Dealer Page Link', 'trx-auto-dealer' ),
		);
		foreach ( $visibility as $key => $label ) {
			$this->add_control(
				$key,
				array(
					'label'        => $label,
					'type'         => Controls_Manager::SWITCHER,
					'default'      => 'yes',
					'return_value' => 'yes',
				)
			);
		}

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_card',
			array(
				'label' => __( 'Card', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'card_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-dealer-card' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'selector' => '{{WRAPPER}} .trx-ad-dealer-card',
			)
		);

		$this->add_responsive_control(
			'card_padding',
			array(
				'label'      => __( 'Padding', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .trx-ad-dealer-card' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'card_radius',
			array(
				'label'      => __( 'Border Radius', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					'{{WRAPPER}} .trx-ad-dealer-card' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// The block renderer wraps the card in .wp-block-trx-auto-dealer-dealer-info,
		// which carries its own frame (background/border/radius/padding/shadow) in
		// frontend.css. Without these controls that outer frame is unstylable from
		// Elementor (tester feedback #6).
		$this->start_controls_section(
			'section_style_wrapper',
			array(
				'label' => __( 'Wrapper', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'wrapper_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'wrapper_border',
				'selector' => '{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info',
			)
		);

		$this->add_responsive_control(
			'wrapper_padding',
			array(
				'label'      => __( 'Padding', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'wrapper_radius',
			array(
				'label'      => __( 'Border Radius', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'wrapper_shadow',
				'selector' => '{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info',
			)
		);

		$this->add_control(
			'wrapper_frameless',
			array(
				'label'        => __( 'Remove Frame', 'trx-auto-dealer' ),
				'description'  => __( 'Strip the default background, border, padding, and shadow from the outer wrapper.', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'selectors'    => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-dealer-info' => 'background: none; border: 0; border-radius: 0; padding: 0; box-shadow: none;',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_text',
			array(
				'label' => __( 'Text', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => __( 'Heading Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-dealer-card__heading, {{WRAPPER}} .trx-ad-dealer-card__name, {{WRAPPER}} .trx-ad-dealer-card__name a' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .trx-ad-dealer-card__heading, {{WRAPPER}} .trx-ad-dealer-card__name',
			)
		);

		$this->add_control(
			'text_color',
			array(
				'label'     => __( 'Body Text Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-dealer-card__field, {{WRAPPER}} .trx-ad-dealer-card__text, {{WRAPPER}} .trx-ad-dealer-card__description' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => __( 'Icon Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-dealer-card__field svg' => 'color: {{VALUE}}; fill: currentColor;',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings  = $this->get_settings_for_display();
		$dealerId  = (int) ( $settings['dealer_id'] ?? 0 );
		$vehicleId = (int) ( $settings['vehicle_id'] ?? 0 );

		// Resolve a dealer from context when nothing is set explicitly — on the
		// frontend too, not just the editor, so the widget isn't blank on a
		// published page (resolveDealerId() falls back to the sole dealer on a
		// single-dealership site). When a vehicle is set, the block resolves the
		// dealer from it, so only fill in when neither is given (UAT #42).
		if ( $dealerId <= 0 && $vehicleId <= 0 ) {
			$dealerId = ElementorPreviewContext::resolveDealerId();
		}

		$attributes = array(
			'dealerId'        => $dealerId,
			'vehicleId'       => $vehicleId,
			'showLogo'        => 'yes' === ( $settings['show_logo'] ?? 'yes' ),
			'showHeading'     => 'yes' === ( $settings['show_heading'] ?? 'yes' ),
			'showPhone'       => 'yes' === ( $settings['show_phone'] ?? 'yes' ),
			'showEmail'       => 'yes' === ( $settings['show_email'] ?? 'yes' ),
			'showAddress'     => 'yes' === ( $settings['show_address'] ?? 'yes' ),
			'showWebsite'     => 'yes' === ( $settings['show_website'] ?? 'yes' ),
			'showHours'       => 'yes' === ( $settings['show_hours'] ?? 'yes' ),
			'showMap'         => 'yes' === ( $settings['show_map'] ?? 'yes' ),
			'showDescription' => 'yes' === ( $settings['show_description'] ?? 'yes' ),
			'showCallBtn'     => 'yes' === ( $settings['show_call_btn'] ?? 'yes' ),
			'showWhatsappBtn' => 'yes' === ( $settings['show_whatsapp_btn'] ?? 'yes' ),
			'showEmailBtn'    => 'yes' === ( $settings['show_email_btn'] ?? 'yes' ),
			'showDealerLink'  => 'yes' === ( $settings['show_dealer_link'] ?? 'yes' ),
		);

		echo wp_kses( ( new DealerInfoBlock() )->render( $attributes ), \ThemeREX\AutoDealer\allowedHtml() );
	}
}
