<?php
/**
 * Single Listing widget — full default vehicle layout via the
 * `parts/single-listing.php` template part. Renders byte-identical
 * output to the Gutenberg `trx-auto-dealer/single-listing` block and
 * the plugin's PHP single template, which is what makes the
 * auto-seeded Elementor starter match the PHP default.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Frontend\TemplateLoader;

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

class SingleListingWidget extends Widget_Base {

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

	public function get_title(): string {
		return __( 'Single Listing (Default)', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		// Must be a class that exists in Elementor's eicons font — an unknown
		// class renders as a blank tile in the widget panel (tester feedback #10).
		return 'eicon-single-post';
	}

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

	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 use the current vehicle (template builder + single page).', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->end_controls_section();
	}

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

		echo '<div class="trx-ad-elementor-single-listing">';
		TemplateLoader::getTemplatePart( 'single-listing', array( 'post_id' => $post_id ) );
		echo '</div>';
	}
}
