<?php

namespace RadiusTheme\RadiusBooking\Models;

use RadiusTheme\RadiusBooking\Abstracts\BaseModel;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
/**
 * Class CustomerBooking
 *
 * @package RadiusTheme\RadiusBooking\Models
 *
 * @property int $id
 * @property string $name
 */
class CustomerBooking extends BaseModel {

	/**
	 * The database table associated with the model.
	 *
	 * @var string
	 */
	protected string $table = 'radius_booking_customer_bookings';

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected array $fillable = array(
		'appointment_id',
		'customer_id',
		'status',
		'price',
		'persons',
		'coupon_id',
		'token',
		'customer_info',
		'custom_info',
		'utc_offset',
		'duration',
	);

	/**
	 * The validation rules.
	 *
	 * @var array
	 */

	protected array $casts = array(
		'customer_info' => 'json',
		'custom_info'   => 'json',
	);

	/**
	 * Retrieves the 'id' attribute from the attributes array.
	 *
	 * @return mixed|null The value of the 'id' attribute if it exists, or null otherwise.
	 */
	public function get_id() {
		return $this->attributes['id'] ?? null;
	}

	/**
	 * Defines a relationship to the Appointment model.
	 *
	 */
	public function appointments() {
		return $this->belongsTo( 'RadiusTheme\RadiusBooking\Models\Appointment', 'appointment_id', 'id' );
	}

	/**
	 * Retrieves the appointment ID from the attributes array.
	 *
	 * @return mixed Returns the appointment ID if set, or null if not set.
	 */
	public function get_appointment_id() {
		return $this->attributes['appointment_id'] ?? null;
	}

	/**
	 * Retrieves the booking status from the attributes property.
	 *
	 * @return mixed The current booking status stored in the attributes.
	 */
	public function get_booking_status() {
		return $this->attributes['status'];
	}

	/**
	 * Retrieves the status of an appointment based on the appointment_id attribute.
	 *
	 * @return mixed The status of the appointment if found, or null if the appointment does not exist.
	 */
	public function get_appointment_status() {
		$appointment = Appointment::find( $this->attributes['appointment_id'] );
		if ( $appointment ) {
			return $appointment->status;
		}

		return null;
	}
}
