<?php
namespace Booktics\Appointment\Schedule;

use DateTime;

/**
 * Schedule Entry Class
 */
class Schedule_Entry {
    /**
     * Store start time
     *
     * @var string
     */
    private string $start_time;

    /**
     * Store end time
     *
     * @var string
     */
    private string $end_time;

    /**
     * Store day of week
     *
     * @var int
     */
    private int $day_of_week;

    /**
     * Store custom date
     *
     * @var string|null
     */
    private ?string $custom_date;

    /**
     * Store holiday checking
     *
     * @var bool
     */
    private bool $is_holiday;

    /**
     * Constructor for ScheduleEntry
     *
     * @param   array  $data  Schedule data
     *
     * @return  void
     */
    public function __construct(array $data) {
        $this->start_time  = booktics_time_from_gmt( $data['start_time'], $data['custom_date'] ?? 0 );
        $this->end_time    = booktics_time_from_gmt( $data['end_time'], $data['custom_date'] ?? 0 );
        $this->day_of_week = (int) $data['week_day'];
        $this->custom_date = $data['custom_date'] ?? null;
        $this->is_holiday  = $this->start_time == 0 && $this->end_time == 0;
    }

    /**
     * Get start time
     *
     * @return string Start time value
     */
    public function get_start_time(): string {
        return $this->start_time;
    }

    /**
     * Get end time
     *
     * @return string End time value
     */
    public function get_end_time(): string {
        return $this->end_time;
    }

    /**
     * Get day of week
     *
     * @return int Day of week value
     */
    public function get_day_of_week(): int {
        return $this->day_of_week;
    }

    /**
     * Get custom date
     *
     * @return string|null Custom date value
     */
    public function get_custom_date(): ?string {
        return $this->custom_date;
    }

    /**
     * Check if schedule entry is holiday
     *
     * @return bool True if holiday, false otherwise
     */
    public function is_holiday(): bool {
        return $this->is_holiday;
    }

    /**
     * Compare date
     *
     * @param   DateTime  $date  Schedule date time
     *
     * @return  bool    Return True if match date otherwise false
     */
    public function matches_date( DateTime $date ): bool {
        $dow = intval($date->format('N')); // 1 = Monday
        return $this->custom_date === $date->format('Y-m-d') ||
            ( $this->custom_date == 0 && (int)$this->day_of_week == $dow );
    }
}

