<?php

namespace modules\selfserve\traits;
require_once WD . '/modules/selfserve/helpers/selfserve_lane_status.php';
require_once WD . '/modules/selfserve/classes/selfserve_lane.php';
use modules\selfserve\classes\selfserve_lane;
use modules\selfserve\classes\selfserve_lane_command_arguments;
use modules\selfserve\helpers\selfserve_lane_command;
use modules\selfserve\helpers\selfserve_lane_status;

trait selfserve_lane_reservation_timer_t
{
    const DEFAULT_LANE_RESERVATION_TIME = 300 * 1000; // Default reservation time in milliseconds (5 minutes)
    /**
     * The timestamp of the reservation time of the lane
     * @var int|null $reservation_start_time
     */
    public ?int $reservation_start_time = null;

    /**
     * Get the lane reservation time
     * @return int|null The timestamp of the reservation time of the lane, or null if not set
     */
    public function getReservationStartTime(): ?int
    {
        $reservation_start_time = $this->getLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_RESERVATION_START_TIME);
        if (!empty($reservation_start_time)) {
            $this->reservation_start_time = $reservation_start_time;
        }
        return $this->reservation_start_time;
    }
    /**
     * Set the lane reservation time
     * @param int|null $timestamp The timestamp of the reservation time of the lane, or null to clear
     * @return selfserve_lane_reservation_timer_t|selfserve_lane
     */
    public function setReservationStartTime(?int $timestamp): self
    {
        $this->reservation_start_time = $timestamp;
        $this->setLaneCache($this->id, self::CACHE_SELFSERVE_LANE_KEY_RESERVATION_START_TIME, $this->reservation_start_time);
        return $this;
    }
    /**
     * Get the elapsed time since the reservation started
     * @return int|null The elapsed time in seconds since the reservation started, or null if reservation has not started
     */
    public function getElapsedReservationTime(): ?int
    {
        $reservation_start_time = $this->getReservationStartTime();
        if ($reservation_start_time === null) {
            return null; // Reservation has not started
        }
        return time() - $reservation_start_time;
    }
    /**
     * Get remaining reservation time
     * @return int|null The remaining reservation time in seconds, or null if the reservation has not started
     */
    public function getRemainingReservationTime(): ?int
    {
        $elapsed_time = $this->getElapsedReservationTime();
        if ($elapsed_time === null) {
            return null; // Reservation has not started
        }
        $remaining_time = (self::DEFAULT_LANE_RESERVATION_TIME / 1000) - $elapsed_time;
        return max($remaining_time, 0);
    }
    /**
     * Check if the reservation has expired
     * @return bool True if the reservation has expired, false otherwise
     */
    public function isReservationExpired(): bool
    {
        $remaining_time = $this->getRemainingReservationTime();
        if ($remaining_time === null) {
            return false; // Reservation has not started
        }
        return $remaining_time <= 0;
    }
}