<?php
/**
 * EventRegistrationResource
 *
 * Transforms EventRegistration model data into API response format.
 *
 * @package RadiusTheme\RadiusBooking\Resources
 * @since   1.2.0
 */

namespace RadiusTheme\RadiusBooking\Resources;

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

use RadiusTheme\RadiusBooking\Abstracts\BaseResource;
use RadiusTheme\RadiusBooking\Models\Event;
use RadiusTheme\RadiusBooking\Models\EventTicketType;

/**
 * Class EventRegistrationResource
 */
class EventRegistrationResource extends BaseResource {

	/**
	 * Transform an event registration into an API-friendly array.
	 *
	 * @param mixed $registration The event registration model or object.
	 *
	 * @return array
	 */
	public function transform( $registration ): array {
		$customer_info = $this->getCustomerInfo( $registration->appointment_id );

		return array(
			'id'                  => $registration->id,
			'event_id'            => $registration->event_id,
			'event_name'          => $this->getEventName( $registration->event_id ),
			'ticket_type_id'      => $registration->ticket_type_id,
			'ticket_name'         => $this->getTicketName( $registration->ticket_type_id ),
			'appointment_id'      => $registration->appointment_id,
			'customer_id'         => $registration->customer_id,
			'customer_name'       => $customer_info['name'] ?? null,
			'customer_email'      => $customer_info['email'] ?? null,
			'registration_number' => $registration->registration_number,
			'quantity'            => $registration->quantity,
			'attendee_info'       => $registration->attendee_info,
			'status'              => $registration->status,
			'waitlist_position'   => $registration->waitlist_position,
			'check_in_token'      => $registration->check_in_token,
			'checked_in_at'       => $registration->checked_in_at ?? null,
			'created_at'          => $registration->created_at ?? null,
		);
	}

	/**
	 * Get customer name and email from appointment booking.
	 *
	 * @param int|null $appointment_id The appointment ID.
	 *
	 * @return array
	 */
	private function getCustomerInfo( $appointment_id ): array {
		if ( ! $appointment_id ) {
			return array();
		}

		$booking = ( new \RadiusTheme\RadiusBooking\Repositories\BookingRepository() )
			->getBookingByAppointmentId( (int) $appointment_id, true );

		if ( $booking && isset( $booking->customer_info ) ) {
			return $booking->customer_info;
		}

		return array();
	}

	/**
	 * Get the event name by ID.
	 *
	 * @param int|null $event_id The event ID.
	 *
	 * @return string|null
	 */
	private function getEventName( $event_id ): ?string {
		if ( ! $event_id ) {
			return null;
		}
		$event = Event::find( $event_id );

		return $event ? $event->get_name() : null;
	}

	/**
	 * Get the ticket type name by ID.
	 *
	 * @param int|null $ticket_type_id The ticket type ID.
	 *
	 * @return string|null
	 */
	private function getTicketName( $ticket_type_id ): ?string {
		if ( ! $ticket_type_id ) {
			return null;
		}
		$ticket = EventTicketType::find( $ticket_type_id );

		return $ticket ? ( $ticket->name ?? null ) : null;
	}
}
