<?php
/**
 * Card Oracle Elementor widget.
 *
 * Loaded only when Elementor is active. Lets editors drop a Card Oracle
 * reading into a page via the Elementor sidebar instead of pasting the
 * shortcode. The widget delegates rendering to the [card-oracle] shortcode
 * so the visual output stays identical between editor preview and front end.
 *
 * @package CardOracle
 * @since   1.2.6
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

/**
 * Class Card_Oracle_Elementor_Widget
 *
 * @since 1.2.6
 */
class Card_Oracle_Elementor_Widget extends \Elementor\Widget_Base {

	/**
	 * Internal widget name (used as a slug in elementor).
	 *
	 * @return string
	 */
	public function get_name() {
		return 'card-oracle-reading';
	}

	/**
	 * Human-readable title shown in the Elementor sidebar.
	 *
	 * @return string
	 */
	public function get_title() {
		return esc_html__( 'Card Oracle Reading', 'card-oracle' );
	}

	/**
	 * Sidebar icon (Elementor icon font).
	 *
	 * @return string
	 */
	public function get_icon() {
		return 'eicon-product-stack';
	}

	/**
	 * Categories the widget appears under.
	 *
	 * @return string[]
	 */
	public function get_categories() {
		return array( 'general' );
	}

	/**
	 * Keywords for the Elementor search box.
	 *
	 * @return string[]
	 */
	public function get_keywords() {
		return array( 'card', 'oracle', 'tarot', 'reading', 'divination' );
	}

	/**
	 * Build the list of co_readings for the select control.
	 *
	 * @return array<int|string, string> Map of post ID to title; '' = "Select a reading".
	 */
	protected function get_readings_options() {
		$options = array( '' => esc_html__( '— Select a reading —', 'card-oracle' ) );

		if ( ! function_exists( 'get_posts' ) ) {
			return $options;
		}

		$readings = get_posts(
			array(
				'post_type'      => 'co_readings',
				'post_status'    => 'publish',
				'posts_per_page' => -1,
				'orderby'        => 'title',
				'order'          => 'ASC',
				'fields'         => 'ids',
				'no_found_rows'  => true,
			)
		);

		if ( ! empty( $readings ) ) {
			foreach ( $readings as $reading_id ) {
				$options[ (string) $reading_id ] = get_the_title( $reading_id );
			}
		}

		return $options;
	}

	/**
	 * Register sidebar controls.
	 *
	 * @return void
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'card_oracle_reading_section',
			array(
				'label' => esc_html__( 'Reading', 'card-oracle' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'reading_id',
			array(
				'label'       => esc_html__( 'Reading', 'card-oracle' ),
				'type'        => \Elementor\Controls_Manager::SELECT,
				'options'     => $this->get_readings_options(),
				'default'     => '',
				'description' => esc_html__( 'Pick which Card Oracle reading to display in this section.', 'card-oracle' ),
			)
		);

		$this->add_control(
			'deck_layout',
			array(
				'label'       => esc_html__( 'Deck Layout (optional)', 'card-oracle' ),
				'type'        => \Elementor\Controls_Manager::SELECT,
				'options'     => array(
					''            => esc_html__( 'Use reading default', 'card-oracle' ),
					'standard'    => esc_html__( 'Standard', 'card-oracle' ),
					'overlapping' => esc_html__( 'Overlapping', 'card-oracle' ),
					'spread'      => esc_html__( 'Spread', 'card-oracle' ),
					'circular'    => esc_html__( 'Circular', 'card-oracle' ),
				),
				'default'     => '',
				'description' => esc_html__( 'Override the reading\'s configured deck layout for this placement only.', 'card-oracle' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render output by delegating to the [card-oracle] shortcode.
	 *
	 * @return void
	 */
	protected function render() {
		$settings   = $this->get_settings_for_display();
		$reading_id = isset( $settings['reading_id'] ) ? absint( $settings['reading_id'] ) : 0;

		if ( 0 === $reading_id ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<p>' . esc_html__( 'Pick a reading in the sidebar to render the Card Oracle preview here.', 'card-oracle' ) . '</p>';
			}
			return;
		}

		$attrs = sprintf( 'id="%d"', $reading_id );

		if ( ! empty( $settings['deck_layout'] ) ) {
			$attrs .= sprintf( ' deck_layout="%s"', esc_attr( $settings['deck_layout'] ) );
		}

		echo do_shortcode( '[card-oracle ' . $attrs . ']' );
	}
}
