<?php
namespace WPEventGenius\Common\CustomPostTypes\Event;

use WPEventGenius\Common\Event\EventPost;

if ( ! defined( 'ABSPATH' ) ) {
	exit; 
}

class EventBase {

	public function __construct(){

	}

	public function init_custom_hooks() {
		add_action( 'wp_ajax_nopriv_evge_get_registration_content', array( $this, 'get_registration_content' ), 10, 1 );
		add_action( 'wp_ajax_evge_get_registration_content', array( $this, 'get_registration_content' ), 10, 1 );

		add_action( 'wp_ajax_nopriv_evge_get_already_registered_content', array( $this, 'get_already_registered_content' ), 10, 1 );
		add_action( 'wp_ajax_evge_get_already_registered_content', array( $this, 'get_already_registered_content' ), 10, 1 );
	}

	public function is_cpt_page() {
		return is_post_type_archive( EVGE_EVENT_POST_TYPE )  || get_post_type() === EVGE_EVENT_POST_TYPE;
	}


	public function maybe_alter_template( $template ) {
		
	}

	public function maybe_alter_query( $query ) {
		return $query;
	}

	public function enqueue( $screen ) {
		if ( ! $this->is_cpt_page() ) {
			return;
		}
		EVGE()->style_service()->enqueue_style('evge_common');
		EVGE()->style_service()->enqueue_style('evge_single_post');
		EVGE()->style_service()->enqueue_style('evge_registration_form');
        EVGE()->style_service()->enqueue_style('evge_attendee_list');

		EVGE()->script_service()->enqueue_script('evge_common');
		EVGE()->script_service()->enqueue_script('evge_event');
		EVGE()->script_service()->enqueue_script('evge_registration_form');
		EVGE()->script_service()->enqueue_script('evge_single_post');
		EVGE()->script_service()->enqueue_script('evge_attendee_list');

	}

	public function get_registration_content() {
		//phpcs:ignore WordPress.Security.NonceVerification.Missing
		// Support both single event_id (backward compatible) and event_ids array (for bulk registration)
		$event_ids = array();
		
		if ( ! empty( $_POST['event_ids'] ) && is_array( $_POST['event_ids'] ) ) {
			// Bulk registration: multiple event IDs
		//phpcs:ignore WordPress.Security.NonceVerification.Missing
			$event_ids = array_map( 'absint', $_POST['event_ids'] );
			$event_ids = array_filter( $event_ids ); // Remove any invalid IDs
		} elseif ( ! empty( $_POST['event_id'] ) ) {
			// Single event (backward compatible)
			//phpcs:ignore WordPress.Security.NonceVerification.Missing
			$event_ids = array( absint( $_POST['event_id'] ) );
		}
		
		if ( empty( $event_ids ) ) {
			wp_send_json_error( array(
				'error_message' => __( 'No event ID(s) provided.', 'event-genius' )
			) );
			return;
		}

		// Use factory to create appropriate EventPost instance
		// Factory will return BulkEventPost for multiple events (Premium tier) or EventPost for single
		$factory = new \WPEventGenius\Common\Services\RegistrationObjectFactory();
		$event_post = $factory->create_event_post( $event_ids );

		// Check if current user can register for the primary event
		$event_goer = EVGE()->event_goer();
		EVGE()->event_goer()->set_event( $event_post );

		if ( ! $event_goer || ! $event_goer->can_register_for_event() ) {
			$error_message = __( 'You do not have permission to register for this event. Please log in if required.', 'event-genius' );
			wp_send_json_error( array(
				'error_message' => $error_message
			) );
			return;
		}

		// Validate all events if multiple are provided
		if ( count( $event_ids ) > 1 ) {
			foreach ( $event_ids as $event_id ) {
				if ( ! \WPEventGenius\Common\Utils\Utils::is_event_visible_to_user( $event_id ) ) {
					wp_send_json_error( array(
						'error_message' => sprintf( __( 'Event ID %d is not visible or accessible.', 'event-genius' ), $event_id )
					) );
					return;
				}
			}
		}

		do_action( 'evge_before_registration_content', $event_post, $event_ids );

		$return = array(
			'html' => ''
		);

		// Initialize registration_data for the template
		// This will be an empty array for new registrations, or populated for edit mode
		$registration_data = apply_filters( 'evge_registration_data', array(), $event_post );

		// Pass event_ids to template via a variable
		// For backward compatibility, single event_id still works
		$is_bulk_registration = count( $event_ids ) > 1;

		// Sanitized per-event registration types for form hidden fields and display.
		// POST can be flat (event_id => type_id from JS) or nested rows (event_id => [ [ type_id, quantity ], ... ]).
		$bulk_registration_types      = array(); // eid => first type_id (for order summary labels).
		$bulk_registration_type_rows = array(); // eid => list of [ 'type_id' => int, 'quantity' => int ].
		if ( $is_bulk_registration && ! empty( $_POST['bulk_registration_types'] ) && is_array( $_POST['bulk_registration_types'] ) ) {
			foreach ( $event_ids as $eid ) {
				// phpcs:ignore WordPress.Security.NonceVerification.Missing
				$raw = isset( $_POST['bulk_registration_types'][ $eid ] ) ? wp_unslash( $_POST['bulk_registration_types'][ $eid ] ) : null;
				$rows = $this->parse_bulk_registration_types_post_rows( $raw );
				if ( ! empty( $rows ) ) {
					$bulk_registration_type_rows[ $eid ] = $rows;
					$bulk_registration_types[ $eid ]     = (int) $rows[0]['type_id'];
				} else {
					$bulk_registration_types[ $eid ] = 0;
				}
			}
		}

		// Resolve bulk registration type IDs to names for display in order summary.
		// IMPORTANT: Only show type when event has 2+ active types. Single-type events
		// are treated as \"no registration types\" in displays.
		$bulk_registration_type_names  = array();
		$bulk_registration_type_costs = array();
		if ( $is_bulk_registration && ! empty( $_POST['bulk_registration_types'] ) && is_array( $_POST['bulk_registration_types'] ) ) {
			$database              = new \WPEventGenius\Common\Database();
			$payment_items_service = new \WPEventGenius\Common\Services\PaymentItemsService( $database );

			foreach ( $event_ids as $eid ) {
				$single_event = new EventPost( $eid );
				$active_types = $single_event->get_active_registration_types();

				$type_id = isset( $bulk_registration_types[ $eid ] ) ? (int) $bulk_registration_types[ $eid ] : 0;
				$type_row = null;
				if ( $type_id > 0 ) {
					$type_row = $database->get_event_registration_type( $type_id );
				}

				// Only derive a display name when event truly has multiple active types.
				if ( count( $active_types ) > 1 ) {
					$name = '';
					if ( $type_row && isset( $type_row['name'] ) ) {
						$name = trim( (string) $type_row['name'] );
					}
					if ( $name === '' && ! empty( $active_types ) && isset( $active_types[0]['name'] ) ) {
						$name = trim( (string) $active_types[0]['name'] );
					}
					if ( $name !== '' ) {
						$bulk_registration_type_names[ $eid ] = $name;
					}
				}

				// Derive per-event cost from the selected registration type when available.
				if ( $type_row ) {
					$bulk_registration_type_costs[ $eid ] = $payment_items_service->get_effective_type_price_dollars( $eid, $type_row );
				}
			}
		}

		$template_path = EVGE()->template_manager()->locate_template('registration/forms/registration/registration-modal-content.php');
		if ($template_path) {
			ob_start();
			include $template_path;
			$return['html'] = ob_get_contents();
			ob_end_clean();
		}

		wp_send_json_success( $return );
	}

	public function get_already_registered_content() {
		//phpcs:ignore WordPress.Security.NonceVerification.Missing
		if ( empty( $_POST['event_id'] ) ) {
			wp_send_json_error();
		}
		//phpcs:ignore WordPress.Security.NonceVerification.Missing
		$event_id = isset($_POST['event_id']) ? absint($_POST['event_id']) : 0;

		// Should check: Utils::is_event_visible_to_user( $event_id ) before proceeding
		if ( ! \WPEventGenius\Common\Utils\Utils::is_event_visible_to_user( $event_id ) ) {
			wp_send_json_error( array(
				'error_message' => __( 'You do not have permission to access this event.', 'event-genius' )
			) );
			return;
		}

		$event_post = new EventPost( $event_id );

		$return = array(
			'html' => '',
		);

		$template_path = EVGE()->template_manager()->locate_template('registration/forms/cancel/already-registered-modal-content.php');
		if ($template_path) {
			ob_start();
			include $template_path;
			$return['html'] = ob_get_contents();
			ob_end_clean();
		}

		wp_send_json_success( $return );
	}

	/**
	 * Normalize bulk_registration_types[ event_id ] from POST into type/quantity rows.
	 *
	 * @param mixed $raw Numeric type id, or array of rows with type_id and quantity (simple_count).
	 * @return array<int, array{ type_id: int, quantity: int }>
	 */
	private function parse_bulk_registration_types_post_rows( $raw ) {
		$rows = array();
		if ( is_array( $raw ) ) {
			foreach ( $raw as $row ) {
				if ( ! is_array( $row ) || ! isset( $row['type_id'] ) ) {
					continue;
				}
				$tid = absint( $row['type_id'] );
				$qty = isset( $row['quantity'] ) ? max( 0, absint( $row['quantity'] ) ) : 0;
				if ( $tid > 0 && $qty > 0 ) {
					$rows[] = array(
						'type_id'  => $tid,
						'quantity' => $qty,
					);
				}
			}
		} elseif ( is_numeric( $raw ) && (int) $raw > 0 ) {
			$rows[] = array(
				'type_id'  => (int) $raw,
				'quantity' => 1,
			);
		}
		return $rows;
	}

}