<?php

namespace RadiusTheme\RadiusBooking\Models;

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

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

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected array $fillable = array(
		'user_id',
		'wp_user_id',
		'listing_id',
		'source_type',
		'first_name',
		'last_name',
		'phone',
		'image_id',
		'gender',
		'time_zone',
		'note',
		'badge_id',
		'location_id',
		'translations',
	);

	/**
	 * Gender is declared NOT NULL with no default in UserProfilesTable, so a
	 * strict-mode MySQL install rejects inserts that omit it. Default to empty
	 * string here so callers (including the Classified Listing integration) can
	 * create a profile without having to remember to pass gender explicitly.
	 */
	protected array $attributes = array(
		'gender' => '',
	);

	/**
	 * Get the user associated with the profile.
	 *
	 * @return \RadiusTheme\RadiusBooking\Core\ORM\Relations\BelongsTo
	 */
	public function user() {
		return $this->belongsTo( User::class, 'user_id', 'id' );
	}

	/**
	 * Retrieve the fillable attributes of the model.
	 *
	 * @return array The array of fillable attributes.
	 */
	public function getFillable() {
		return $this->fillable;
	}

	/**
	 * Retrieves the URL of the image associated with the current object.
	 *
	 * @return string The URL of the image if available, or an empty string if no image ID is set.
	 */
	public function getImageUrl(): string {
		$image_id = $this->attributes['image_id'];
		if ( ! $image_id ) {
			return '';
		}

		return wp_get_attachment_image_url( $image_id, 'full' );
	}

	/**
	 * Retrieves the first name associated with the current object.
	 *
	 * @return string The first name if available, or an empty string if not set.
	 */
	public function getFirstName() {
		return $this->attributes['first_name'] ?? '';
	}

	/**
	 * Retrieves the last name associated with the current object.
	 *
	 * @return string The last name if it is set, or an empty string if not available.
	 */
	public function getLastName() {
		return $this->attributes['last_name'] ?? '';
	}

	/**
	 * Generates and returns the full name by concatenating the first name
	 * and last name with a space in between.
	 *
	 * @return string The full name constructed from the first and last names.
	 */
	public function fullName() {
		return $this->getFirstName() . ' ' . $this->getLastName();
	}

	public function getUserId(  ) {
		return $this->attributes['user_id'];
	}
}
