<?php

namespace SugarCalendarBookings\Integrations\ExternalBusyTimes;

use DateTimeImmutable;
use SugarCalendarBookings\Employees\EmployeeModel;
use SugarCalendarBookings\Integrations\OAuthRelay\OAuthConnectionModel;
use SugarCalendarBookings\Support\Database\AbstractModel;

/**
 * External busy time model.
 *
 * @since 1.4.0
 *
 * @property int                    $id
 * @property int|null               $employee_id
 * @property int                    $oauth_connection_id
 * @property string                 $provider
 * @property string                 $calendar_id
 * @property string                 $provider_event_id
 * @property string                 $title
 * @property DateTimeImmutable      $start_at
 * @property DateTimeImmutable      $end_at
 * @property int                    $is_all_day
 * @property DateTimeImmutable      $created_at
 * @property DateTimeImmutable      $updated_at
 */
class ExternalBusyTimeModel extends AbstractModel {

	/**
	 * The database table name without prefix.
	 *
	 * @since 1.4.0
	 *
	 * @var string
	 */
	protected static $table_name = 'external_busy_times';

	/**
	 * The model type identifier.
	 *
	 * @since 1.4.0
	 *
	 * @var string
	 */
	protected static $type = 'external_busy_time';

	/**
	 * Column formats for the model.
	 *
	 * @since 1.4.0
	 *
	 * @var array<string, string>
	 */
	protected static $column_formats = [
		'id'                  => '%d',
		'employee_id'         => '%d',
		'oauth_connection_id' => '%d',
		'provider'            => '%s',
		'calendar_id'         => '%s',
		'provider_event_id'   => '%s',
		'title'               => '%s',
		'start_at'            => '%s',
		'end_at'              => '%s',
		'is_all_day'          => '%d',
		'created_at'          => '%s',
		'updated_at'          => '%s',
	];

	/**
	 * The attributes that should be cast.
	 *
	 * @since 1.4.0
	 *
	 * @var array<string, string>
	 */
	protected static $casts = [
		'id'                  => 'integer',
		'employee_id'         => 'integer',
		'oauth_connection_id' => 'integer',
		'is_all_day'          => 'integer',
		'start_at'            => 'datetime',
		'end_at'              => 'datetime',
		'created_at'          => 'datetime',
		'updated_at'          => 'datetime',
	];

	/**
	 * Column defaults for the model.
	 *
	 * @since 1.4.0
	 *
	 * @return array<string, mixed>
	 */
	protected static function column_defaults() {

		return [
			'title'      => '',
			'is_all_day' => 0,
		];
	}

	/**
	 * Employee relationship.
	 *
	 * @since 1.4.0
	 *
	 * @return EmployeeModel|null
	 */
	protected function employee() {

		return $this->belongs_to( EmployeeModel::class );
	}

	/**
	 * OAuth connection relationship.
	 *
	 * @since 1.4.0
	 *
	 * @return OAuthConnectionModel|null
	 */
	protected function oauth_connection() {

		return $this->belongs_to( OAuthConnectionModel::class );
	}
}
