<?php

/**
 * Card Oracle Admin Wizard functionality
 *
 * Provides wizard functionality for quickly creating tarot/oracle readings,
 * including automated creation of positions, cards, and descriptions.
 *
 * @package Card_Oracle/Admin/Wizard
 * @version 1.1.1
 * @since   1.1.1
 */

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

/**
 * Card Oracle Admin Wizard Class.
 *
 * Handles the creation of complete readings through a guided wizard interface.
 * Automates the creation of readings, positions, cards, and their relationships.
 *
 * @since 1.1.1
 * @package Card_Oracle/Admin
 * @author  Christopher Graham <support@chillichalli.com>
 */
class CardOracleAdminWizard {

	/**
	 * Creates a complete reading with positions, cards, and descriptions.
	 *
	 * This method processes wizard settings to automatically create:
	 * - A new reading post
	 * - Associated position posts
	 * - Associated card posts
	 * - Description posts linking cards to positions
	 *
	 * @since 1.1.1
	 * @global CO_Logging $co_logs Global logging instance
	 * @global object     $co_notices Global notices instance
	 *
	 * @return void
	 */
	public static function create_reading() {
		global $co_logs, $co_notices;

		// Verify user has permission to create readings.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$co_logs->add( 'Wizard', 'Inserting data.', null, 'event' );

		$co_wizard_settings = get_option( 'co_wizard', array() );

		// Validate wizard settings data structure.
		if ( ! is_array( $co_wizard_settings ) ) {
			return;
		}

		if ( empty( $co_wizard_settings['reading'] ) || ! is_string( $co_wizard_settings['reading'] ) ) {
			return;
		}

		if ( empty( $co_wizard_settings['positions'] ) || ! is_array( $co_wizard_settings['positions'] ) ) {
			return;
		}

		if ( empty( $co_wizard_settings['cards'] ) || ! is_array( $co_wizard_settings['cards'] ) ) {
			return;
		}

		$cards_array     = array();
		$positions_array = array();
		$positions_count = 1;

		foreach ( $co_wizard_settings['positions'] as $position ) {
			if ( ! is_string( $position ) || '' === trim( $position ) ) {
				continue;
			}

			$position_item['name']  = $position;
			$position_item['order'] = $positions_count++;

			array_push( $positions_array, $position_item );
		}

		foreach ( $co_wizard_settings['cards'] as $card ) {
			if ( ! is_string( $card ) || '' === trim( $card ) ) {
				continue;
			}

			$card_item['name'] = $card;

			array_push( $cards_array, $card_item );
		}

		$data = array(
			'reading'   => array(
				'name' => $co_wizard_settings['reading'],
			),
			'positions' => $positions_array,
			'cards'     => $cards_array,
		);

		( new CardOracleDemoData() )->insert_data( $data );
	}

	/**
	 * Handles output of report.
	 */
	public static function wizard_validate() {
		global $co_notices;
		$message = '';

		$co_wizard_settings = get_option( 'co_wizard', array() );

		if ( empty( $co_wizard_settings['reading'] ) ) {
			$message .= esc_html__( 'Please add a title for your Reading.', 'card-oracle' );
		}

		if ( empty( $co_wizard_settings['positions'] ) ) {
			if ( ! empty( $message ) ) {
				$message .= '<br />';
			}
			$message .= esc_html__( 'Please add Positions to your Reading.', 'card-oracle' );
		}

		if ( empty( $co_wizard_settings['cards'] ) ) {
			if ( ! empty( $message ) ) {
				$message .= '<br />';
			}
			$message .= esc_html__( 'Please add Cards to your Reading.', 'card-oracle' );
		}

		if ( ! empty( $message ) ) {
			$co_notices->add( 'wizard-create', $message, 'error' );
			$co_notices->display();
			return false;
		}

		return true;
	}

	/**
	 * Validates the template reading request data.
	 *
	 * @since 1.2.0
	 *
	 * @param array $args {
	 *     Arguments required to create a template reading.
	 *
	 *     @type string $reading_name     The display name for the reading.
	 *     @type int    $position_total   Total number of placeholder positions to create.
	 *     @type int    $card_total       Total number of placeholder cards to create.
	 *     @type string $position_prefix  Optional prefix for generated position titles.
	 *     @type string $card_prefix      Optional prefix for generated card titles.
	 * }
	 * @return true|WP_Error
	 */
	public static function validate_template_reading( $args ) {
		$args   = wp_parse_args(
			$args,
			array(
				'reading_name'    => '',
				'position_total'  => 0,
				'card_total'      => 0,
				'position_prefix' => '',
				'card_prefix'     => '',
			)
		);
		$errors = new WP_Error();

		if ( '' === trim( $args['reading_name'] ) ) {
			$errors->add( 'co_template_reading_name', esc_html__( 'Please provide a name for the template reading.', 'card-oracle' ) );
		}

		if ( empty( $args['position_total'] ) || absint( $args['position_total'] ) < 1 ) {
			$errors->add( 'co_template_position_count', esc_html__( 'Template readings require at least one position.', 'card-oracle' ) );
		}

		if ( empty( $args['card_total'] ) || absint( $args['card_total'] ) < 1 ) {
			$errors->add( 'co_template_card_count', esc_html__( 'Template readings require at least one card.', 'card-oracle' ) );
		}

		if ( ! empty( $args['position_prefix'] ) && '' === trim( wp_strip_all_tags( $args['position_prefix'] ) ) ) {
			$errors->add( 'co_template_position_prefix', esc_html__( 'The position prefix must include visible characters or be left blank.', 'card-oracle' ) );
		}

		if ( ! empty( $args['card_prefix'] ) && '' === trim( wp_strip_all_tags( $args['card_prefix'] ) ) ) {
			$errors->add( 'co_template_card_prefix', esc_html__( 'The card prefix must include visible characters or be left blank.', 'card-oracle' ) );
		}

		if ( $errors->has_errors() ) {
			return $errors;
		}

		return true;
	}

	/**
	 * Creates a template reading with placeholder cards, positions, and descriptions.
	 *
	 * @since 1.2.0
	 *
	 * @param array $args Template arguments.
	 * @return void
	 */
	public static function create_template_reading( $args ) {
		global $co_logs;

		// Verify user has permission to create readings.
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$args = wp_parse_args(
			$args,
			array(
				'reading_name'    => '',
				'position_total'  => 0,
				'card_total'      => 0,
				'position_prefix' => '',
				'card_prefix'     => '',
			)
		);

		$reading_name   = sanitize_text_field( $args['reading_name'] );
		$position_total = max( 1, absint( $args['position_total'] ) );
		$card_total     = max( 1, absint( $args['card_total'] ) );

		$position_prefix = sanitize_text_field( $args['position_prefix'] );
		$card_prefix     = sanitize_text_field( $args['card_prefix'] );

		if ( '' === $position_prefix ) {
			$position_prefix = esc_html__( 'Position', 'card-oracle' );
		}

		if ( '' === $card_prefix ) {
			$card_prefix = esc_html__( 'Card', 'card-oracle' );
		}

		$positions = array();
		for ( $i = 1; $i <= $position_total; $i++ ) {
			$positions[] = array(
				'name'  => sprintf( '%s %d', $position_prefix, $i ),
				'order' => $i,
			);
		}

		$cards = array();
		for ( $i = 1; $i <= $card_total; $i++ ) {
			$cards[] = array(
				'name' => sprintf( '%s %d', $card_prefix, $i ),
			);
		}

		if ( '' === $reading_name ) {
			return;
		}

		$co_logs->add( 'Wizard', sprintf( 'Creating template reading "%s" with %d positions and %d cards.', $reading_name, $position_total, $card_total ), null, 'event' );

		// Allow third-parties to adjust template payload before it is persisted.
		$data = apply_filters(
			'card_oracle_template_wizard_data',
			array(
				'reading'   => array(
					'name' => $reading_name,
				),
				'positions' => $positions,
				'cards'     => $cards,
			),
			$args
		);

		( new CardOracleDemoData() )->insert_data( $data );
	}
}
