<?php
/**
 * RadiusBooking API Route Registrar
 *
 * This class is responsible for registering API routes dynamically with the WordPress REST API.
 * It supports CRUD operations and allows for middleware to be applied globally or per route.
 *
 * @package RadiusTheme\RadiusBooking\Core\Api\Routes
 */

namespace RadiusTheme\RadiusBooking\Models;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
use RadiusTheme\RadiusBooking\Abstracts\BaseModel;
use RadiusTheme\RadiusBooking\Core\ORM\Relations\HasOne;
use RadiusTheme\RadiusBooking\Services\UserService;

/**
 * Class User
 *
 * Represents a user in the RadiusBooking system.
 *
 * @package RadiusTheme\RadiusBooking\Models
 *
 * @property int    $id
 * @property string $name
 * @property string $email
 * @property string|null $phone
 */
class User extends BaseModel {

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

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array $fillable
	 */
	protected array $fillable = array( 'user_type', 'email', 'password', 'status', 'token', 'created_by' );


	/**
	 * The relations to eager load on every query.
	 *
	 * @var array|\class-string[]
	 */
	protected array $relations = array(
		'profile' => UserProfile::class,
	);



	/**
	 * Get all appointments for the user.
	 *
	 * @return \RadiusTheme\RadiusBooking\Core\ORM\Relations\HasMany
	 */
	public function appointments() {
		return $this->hasMany( Appointment::class );
	}

	public function get_employee_appointments() {
		return $this->hasMany( Appointment::class, 'employee_id', 'id' );
	}

	/**
	 * Get the profile associated with the user.
	 *
	 * @return HasOne
	 */
	public function profile() {
		return $this->hasOne( UserProfile::class, 'user_id', 'id' );
	}

	/**
	 * Retrieves the ID attribute from the object's properties.
	 *
	 * @return int The ID value if available, or 0 if it is not set.
	 */
	public function get_id() {
		return $this->attributes['id'] ?? 0;
	}

	/**
	 * Get the Email
	 */
	public function get_email() {
		return $this->attributes['email'] ?? '';
	}

	/**
	 * Retrieves the user type from the attributes or returns 'customer' if not set.
	 *
	 * @return string The user type.
	 */
	public function get_type() {
		return $this->attributes['user_type'] ?? 'customer';
	}

	/**
	 * Retrieves the status from the attributes or returns 'active' if not set.
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	public function services() {
		return $this->hasMany( EmployeeService::class, 'user_id', 'id' );
	}

	// user daysOff

	/**
	 * Establishes a relationship to retrieve the days off associated with the employee.
	 */
	public function daysOff() {
		return $this->hasMany( EmployeeDayoff::class, 'user_id', 'id' );
	}

	/**
	 * Establishes a relationship to retrieve the weekdays associated with the employee.
	 */
	public function weekdays() {
		return $this->hasMany( EmployeeWeekday::class, 'user_id', 'id' );
	}

	/**
	 * Establishes a relationship to retrieve the special days associated with the employee.
	 */
	public function specialDays() {
		return $this->hasMany( EmployeeSpecialDay::class, 'user_id', 'id' );
	}

	/**
	 * Retrieves the total count of active customers.
	 */
	public static function totalCustomer() {
		return static::where( 'user_type', 'customer' )->where( 'status', 'active' )->count();
	}

	/**
	 * Retrieves the total count of active employees.
	 *
	 * @return int The total number of active employees.
	 */
	public static function totalEmployee() {
		return static::where( 'user_type', 'employee' )->where( 'status', 'active' )->count();
	}

	/**
	 * Determines if the current entity is of type 'employee'.
	 *
	 * @return bool Returns true if the entity type is 'employee', otherwise false.
	 */
	public function is_employee() {
		return $this->get_type() === 'employee' ? true : false;
	}

	/**
	 * Determines if the current entity is of type 'customer'.
	 *
	 * @return bool Returns true if the entity is a customer, otherwise false.
	 */
	public function is_customer(  ) {
		return $this->get_type() === 'customer' ? true : false;
	}
}
