<?php
/**
 * Card Oracle Divi Builder module.
 *
 * Loaded only when Divi (or any theme/plugin that ships ET_Builder_Module) is
 * active. Lets editors drop a Card Oracle reading into a Divi layout from the
 * module sidebar instead of pasting the shortcode. Rendering delegates to the
 * existing [card-oracle] shortcode so the visual output stays identical
 * between Divi visual builder and the front end.
 *
 * @package CardOracle
 * @since   1.2.6
 */

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

if ( ! class_exists( 'ET_Builder_Module' ) ) {
	return;
}

/**
 * Class Card_Oracle_Divi_Module
 *
 * @since 1.2.6
 *
 * phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
 *   Divi's parent class declares public properties in camelCase / mixed case.
 */
class Card_Oracle_Divi_Module extends ET_Builder_Module {

	/**
	 * Module slug Divi uses for serialization (et_pb_co_reading).
	 *
	 * @var string
	 */
	public $slug = 'et_pb_co_reading';

	/**
	 * Indicates full Visual Builder support.
	 *
	 * @var string
	 */
	public $vb_support = 'on';

	/**
	 * Optional credits shown in the Divi builder.
	 *
	 * @var array
	 */
	protected $module_credits = array(
		'module_uri' => 'https://chillichalli.com/card-oracle',
		'author'     => 'ChilliChalli',
		'author_uri' => 'https://chillichalli.com',
	);

	/**
	 * Module init: name and pluralization.
	 *
	 * @return void
	 */
	public function init() {
		$this->name   = esc_html__( 'Card Oracle Reading', 'card-oracle' );
		$this->plural = esc_html__( 'Card Oracle Readings', 'card-oracle' );
	}

	/**
	 * Build the reading_id select options keyed by post ID.
	 *
	 * @return array<string, string>
	 */
	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;
	}

	/**
	 * Define the module settings fields.
	 *
	 * @return array
	 */
	public function get_fields() {
		return array(
			'reading_id'  => array(
				'label'           => esc_html__( 'Reading', 'card-oracle' ),
				'type'            => 'select',
				'option_category' => 'basic_option',
				'options'         => $this->get_readings_options(),
				'description'     => esc_html__( 'Pick which Card Oracle reading to display.', 'card-oracle' ),
				'toggle_slug'     => 'main_content',
			),
			'deck_layout' => array(
				'label'           => esc_html__( 'Deck Layout (optional)', 'card-oracle' ),
				'type'            => 'select',
				'option_category' => 'basic_option',
				'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' ),
				),
				'description'     => esc_html__( 'Override the reading\'s configured deck layout for this placement only.', 'card-oracle' ),
				'toggle_slug'     => 'main_content',
			),
		);
	}

	/**
	 * Render module output by delegating to the [card-oracle] shortcode.
	 *
	 * @param array  $unprocessed_props Raw module props (unused).
	 * @param string $content           Shortcode content (unused).
	 * @param string $render_slug       Slug Divi is rendering (unused).
	 * @return string Rendered HTML.
	 */
	public function render( $unprocessed_props, $content, $render_slug ) {
		$reading_id  = isset( $this->props['reading_id'] ) ? absint( $this->props['reading_id'] ) : 0;
		$deck_layout = isset( $this->props['deck_layout'] ) ? (string) $this->props['deck_layout'] : '';

		if ( 0 === $reading_id ) {
			if ( function_exists( 'et_fb_is_enabled' ) && et_fb_is_enabled() ) {
				return '<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 ( '' !== $deck_layout ) {
			$attrs .= sprintf( ' deck_layout="%s"', esc_attr( $deck_layout ) );
		}

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

new Card_Oracle_Divi_Module();
