<?php

namespace Booktics\Appointment\Schedule;

/**
 * Strategy class for generating time slots based solely on team member availability.
 * This strategy ignores service schedule and uses only team member's schedule to generate slots.
 *
 * @package Booktics\Appointment\Schedule
 */
class Team_Member_Only_Strategy implements Time_Slot_Strategy {
    /**
     * Generates time slots based on team member's schedule.
     *
     * @param Schedule_Entry|null $service_entry Service schedule entry (not used in this strategy)
     * @param Schedule_Entry|null $team_entry Team member's schedule entry
     * @param Schedule_Entry|null $default_entry Default schedule entry (not used in this strategy)
     * @param Time_Slot_Manager $manager Slot manager instance to get slot configuration
     * @param \DateTime $date Date for which to generate slots
     *
     * @return array Array of Time_Slot objects representing available time slots
     */
    public function generate_slots(
        array $service_entries,
        array $team_entries,
        array $default_entries,
        Time_Slot_Manager $manager,
        \DateTime $date
    ): array {
        $slots = array();
        foreach ( $team_entries as $team_entry ) {
            if ( ! $team_entry || $team_entry->is_holiday() ) {
                continue;
            }
            $start_time = strtotime( $team_entry->get_start_time() );
            $end_time   = strtotime( $team_entry->get_end_time() );
            $slot_length = $manager->get_slot_length() * 60;
            $service_duration = $manager->get_service_duration() * 60;
            $before_buffer = $manager->get_before_buffer_time() * 60;
            $after_buffer  = $manager->get_after_buffer_time() * 60;
            $gap = $before_buffer + $after_buffer;
            $current_start = $start_time;
            while ( $current_start + $service_duration <= $end_time ) {
                $slot_start = gmdate( 'H:i', $current_start );
                $slot_end   = gmdate( 'H:i', $current_start + $service_duration );
                $max_capacity = $manager->get_service_max_capacity_for_timeslot(); // Get max capacity from group booking

                $slots[]    = new Time_Slot( $slot_start, $slot_end, true, $max_capacity );

                $current_start += $slot_length + $gap;
            }
        }
        return $slots;
    }
}
