<?php
/**
 * Vehicle Card widget — single-listing card.
 *
 * Resolution: explicit `vehicle_id` setting → Elementor preview post (when in
 * the editor / preview frame) → global `get_the_ID()` (template builder).
 * Falls through to VehicleCardBlock so the markup matches the Gutenberg block.
 *
 * @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_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Blocks\VehicleCardBlock;

class VehicleCardWidget extends Widget_Base {

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

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

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

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

	public function get_keywords(): array {
		return array( 'vehicle', 'card', 'listing' );
	}

	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(
			'vehicle_id',
			array(
				'label'       => __( 'Vehicle', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => WidgetHelpers::vehicleOptions( __( '— Use current vehicle —', 'trx-auto-dealer' ) ),
				'default'     => '',
				'description' => __( 'Leave empty to render the current vehicle (single page or Query Loop).', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->add_control(
			'show_meta',
			array(
				'label'        => __( 'Show Meta (year, mileage, fuel)', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'show_label',
			array(
				'label'        => __( 'Show Status Label', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'show_favorite',
			array(
				'label'        => __( 'Show Favorite Button', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->add_control(
			'show_compare',
			array(
				'label'        => __( 'Show Compare Button', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'return_value' => 'yes',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style',
			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-card' => 'background-color: {{VALUE}};',
				),
			)
		);

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

		$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-card' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

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

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

		$this->add_control(
			'price_color',
			array(
				'label'     => __( 'Price Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-card__price' => 'color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$id       = (int) ( $settings['vehicle_id'] ?? 0 );
		if ( $id <= 0 ) {
			$id = ElementorPreviewContext::resolveVehicleId();
		}

		$attributes = array(
			'vehicleId'    => $id,
			'showMeta'     => 'yes' === ( $settings['show_meta'] ?? 'yes' ),
			'showLabel'    => 'yes' === ( $settings['show_label'] ?? 'yes' ),
			'showFavorite' => 'yes' === ( $settings['show_favorite'] ?? 'yes' ),
			'showCompare'  => 'yes' === ( $settings['show_compare'] ?? 'yes' ),
		);

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