<?php

namespace SugarCalendarBookings\Availability;

use JsonSerializable;
use DateTimeImmutable;
use SugarCalendarBookings\Employees\EmployeeModel;

/**
 * Working time slot.
 *
 * Always in UTC.
 *
 * @since 1.0.0
 */
class WorkingTimeSlot {

	/**
	 * Start time in UTC.
	 *
	 * @since 1.0.0
	 *
	 * @var DateTimeImmutable
	 */
	protected $start;

	/**
	 * End time in UTC.
	 *
	 * @since 1.0.0
	 *
	 * @var DateTimeImmutable
	 */
	protected $end;

	/**
	 * Employees.
	 *
	 * Employees that are working during this time slot.
	 *
	 * @since 1.0.0
	 *
	 * @var array<EmployeeModel>
	 */
	protected $employees;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param DateTimeImmutable    $start     Start time.
	 * @param DateTimeImmutable    $end       End time.
	 * @param array<EmployeeModel> $employees Employees.
	 */
	public function __construct( $start, $end, $employees ) {

		$this->start     = $start;
		$this->end       = $end;
		$this->employees = $employees;
	}

	/**
	 * Get start time in UTC.
	 *
	 * @since 1.0.0
	 *
	 * @return DateTimeImmutable Start time.
	 */
	public function start() {

		return $this->start;
	}

	/**
	 * Get end time in UTC.
	 *
	 * @since 1.0.0
	 *
	 * @return DateTimeImmutable End time.
	 */
	public function end() {

		return $this->end;
	}

	/**
	 * Get employees.
	 *
	 * @since 1.0.0
	 *
	 * @return array<EmployeeModel> Employees.
	 */
	public function get_employees() {

		return $this->employees;
	}

	/**
	 * Create a working time slot from a start time and duration.
	 *
	 * @since 1.0.0
	 *
	 * @param DateTimeImmutable    $start     Start time.
	 * @param int                  $duration  Duration.
	 * @param array<EmployeeModel> $employees Employees.
	 *
	 * @return WorkingTimeSlot Time slot.
	 */
	public static function from_duration( $start, $duration, $employees ) {

		$end = $start->modify( '+' . $duration . ' minutes' );

		return new self( $start, $end, $employees );
	}
}
