<?php
/**
 * EventRegistration Model
 *
 * @package RadiusTheme\RadiusBooking\Models
 * @since   1.2.0
 */

namespace RadiusTheme\RadiusBooking\Models;

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

use RadiusTheme\RadiusBooking\Abstracts\BaseModel;

/**
 * Class EventRegistration
 */
class EventRegistration extends BaseModel {

	protected string $table = 'radius_booking_event_registrations';

	protected array $fillable = array(
		'event_id',
		'ticket_type_id',
		'appointment_id',
		'customer_id',
		'registration_number',
		'quantity',
		'attendee_info',
		'status',
		'waitlist_position',
		'check_in_token',
		'checked_in_at',
	);

	protected array $casts = array(
		'event_id'          => 'integer',
		'ticket_type_id'    => 'integer',
		'appointment_id'    => 'integer',
		'customer_id'       => 'integer',
		'quantity'          => 'integer',
		'attendee_info'     => 'json',
		'waitlist_position' => 'integer',
	);

	public function event() {
		return $this->belongsTo( Event::class, 'event_id' );
	}

	public function ticketType() {
		return $this->belongsTo( EventTicketType::class, 'ticket_type_id' );
	}

	public function appointment() {
		return $this->belongsTo( Appointment::class, 'appointment_id' );
	}

	protected function onCreating(): void {
		if ( empty( $this->registration_number ) ) {
			$this->registration_number = strtoupper( wp_generate_password( 10, false, false ) );
		}
		if ( empty( $this->check_in_token ) ) {
			$this->check_in_token = wp_generate_password( 20, false, false );
		}
	}

	public function get_id() {
		return $this->attributes['id'] ?? null;
	}
}
