<?php
/**
 * Vehicle Grid widget — server-side rendered listings grid.
 *
 * Delegates to VehicleGridBlock so the same row set is returned for the same
 * inputs across the REST API, archive page, Gutenberg block, and this widget
 * (ADR-03). Per-element display toggles cascade to every card via the shared
 * `.trx-ad-card-display--*` modifier classes.
 *
 * @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\VehicleGridBlock;

class VehicleGridWidget extends Widget_Base {

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

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

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

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

	public function get_keywords(): array {
		return array( 'vehicle', 'grid', 'listings', 'inventory' );
	}

	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_query',
			array(
				'label' => __( 'Query', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'per_page',
			array(
				'label'       => __( 'Listings per Page', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'max'         => 48,
				'default'     => \ThemeREX\AutoDealer\Search\FilterParams::configuredPerPage(),
				'description' => __( 'Defaults to the plugin\'s "Listings per page" setting.', 'trx-auto-dealer' ),
			)
		);

		$this->add_responsive_control(
			'columns',
			array(
				'label'   => __( 'Columns', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '3',
				'options' => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				),
			)
		);

		$this->add_control(
			'orderby',
			array(
				'label'   => __( 'Order By', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'date',
				'options' => array(
					'date'          => __( 'Most Recent', 'trx-auto-dealer' ),
					'price'         => __( 'Price', 'trx-auto-dealer' ),
					'mileage'       => __( 'Mileage', 'trx-auto-dealer' ),
					'year'          => __( 'Year', 'trx-auto-dealer' ),
					'price_reduced' => __( 'Price Reduced First', 'trx-auto-dealer' ),
					'title'         => __( 'Title', 'trx-auto-dealer' ),
				),
			)
		);

		$this->add_control(
			'order',
			array(
				'label'   => __( 'Order', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'DESC',
				'options' => array(
					'ASC'  => __( 'Ascending', 'trx-auto-dealer' ),
					'DESC' => __( 'Descending', 'trx-auto-dealer' ),
				),
			)
		);

		$this->add_control(
			'featured',
			array(
				'label'        => __( 'Featured Only', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_filters',
			array(
				'label' => __( 'Filters', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'make',
			array(
				'label'   => __( 'Make', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT2,
				'options' => WidgetHelpers::termOptions( 'trx_ad_make', __( 'Any make', 'trx-auto-dealer' ) ),
				'default' => '',
			)
		);

		$this->add_control(
			'body_style',
			array(
				'label'   => __( 'Body Style', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT2,
				'options' => WidgetHelpers::termOptions( 'trx_ad_body_style', __( 'Any body style', 'trx-auto-dealer' ) ),
				'default' => '',
			)
		);

		$this->add_control(
			'condition',
			array(
				'label'   => __( 'Condition', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT2,
				'options' => WidgetHelpers::termOptions( 'trx_ad_condition', __( 'Any condition', 'trx-auto-dealer' ) ),
				'default' => '',
			)
		);

		$this->add_control(
			'status',
			array(
				'label'   => __( 'Status', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '',
				'options' => array(
					''            => __( 'All', 'trx-auto-dealer' ),
					'available'   => __( 'Available', 'trx-auto-dealer' ),
					'reserved'    => __( 'Reserved', 'trx-auto-dealer' ),
					'sold'        => __( 'Sold', 'trx-auto-dealer' ),
					'coming_soon' => __( 'Coming Soon', 'trx-auto-dealer' ),
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_display',
			array(
				'label' => __( 'Card Display', 'trx-auto-dealer' ),
			)
		);

		$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 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();

		// Style: Card.
		$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-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_responsive_control(
			'card_gap',
			array(
				'label'     => __( 'Card Gap', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-archive__grid' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		// Style: Title + price.
		$this->start_controls_section(
			'section_style_text',
			array(
				'label' => __( 'Title & Price', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$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, {{WRAPPER}} .trx-ad-price__value' => 'color: {{VALUE}};',
				),
			)
		);

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

		$this->end_controls_section();
	}

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

		$attributes = array(
			'perPage'      => (int) ( $settings['per_page'] ?? \ThemeREX\AutoDealer\Search\FilterParams::configuredPerPage() ),
			'columns'      => (int) ( $settings['columns'] ?? 3 ),
			'orderby'      => (string) ( $settings['orderby'] ?? 'date' ),
			'order'        => (string) ( $settings['order'] ?? 'DESC' ),
			'featured'     => 'yes' === ( $settings['featured'] ?? '' ),
			'make'         => (string) ( $settings['make'] ?? '' ),
			'bodyStyle'    => (string) ( $settings['body_style'] ?? '' ),
			'condition'    => (string) ( $settings['condition'] ?? '' ),
			'status'       => (string) ( $settings['status'] ?? '' ),
			'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 VehicleGridBlock() )->render( $attributes ), \ThemeREX\AutoDealer\allowedHtml() );
	}
}
