<?php

namespace Booktics\Appointment\Schedule;

/**
 * Strategy class that generates time slots based only on service schedule entry.
 * This class implements Time_Slot_Strategy interface to provide time slot generation
 * logic that considers only service availability, ignoring team member schedules.
 *
 * @package Booktics\Appointment\Schedule
 * @since 1.0.0
 */
class Service_Only_Strategy implements Time_Slot_Strategy {

    /**
     * Generate available time slots based on service schedule entry.
     *
     * @param Schedule_Entry|null $service_entry The service schedule entry
     * @param Schedule_Entry|null $team_entry The team member schedule entry (unused in this strategy)
     * @param Schedule_Entry|null $default_entry The default schedule entry (unused in this strategy)
     * @param Time_Slot_Manager $manager The time slot manager instance
     * @param \DateTime $date The 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();

        // Use service schedule
        foreach ( $service_entries as $service_entry ) {
            if ( ! $service_entry ) {
                continue;
            }
            $start_time = strtotime( $service_entry->get_start_time() );
            $end_time   = strtotime( $service_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;
    }
}
