<?php
namespace Booktics\Appointment\Schedule;

/**
 * Strategy class for generating merged time slots based on service and default schedules.
 *
 * @package Booktics\Appointment\Schedule
 */
class Merged_Service_Default_Strategy implements Time_Slot_Strategy {
    /**
     * Generates time slots by merging service and default schedule entries.
     *
     * @param Schedule_Entry|null $service_entry The service schedule entry
     * @param Schedule_Entry|null $team_entry The team schedule entry
     * @param Schedule_Entry|null $default_entry The default schedule entry
     * @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();
        foreach ( $service_entries as $service_entry ) {
            if ( ! $service_entry || $service_entry->is_holiday() ) {
                continue;
            }
            foreach ( $default_entries as $default_entry ) {
                if ( ! $default_entry || $default_entry->is_holiday() ) {
                    continue;
                }
                $service_start = strtotime( $service_entry->get_start_time() );
                $service_end   = strtotime( $service_entry->get_end_time() );
                $default_start = strtotime( $default_entry->get_start_time() );
                $default_end   = strtotime( $default_entry->get_end_time() );

                $start_time = max( $service_start, $default_start );
                $end_time   = min( $service_end, $default_end );

                if ( $start_time >= $end_time ) {
                    continue;
                }

                $slot_length   = $manager->get_slot_length() * 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 + $slot_length <= $end_time ) {
                    $slot_start = gmdate( 'H:i', $current_start );
                    $slot_end   = gmdate( 'H:i', $current_start + $slot_length );
                    $slots[]    = new Time_Slot( $slot_start, $slot_end, true );
                    $current_start += $slot_length + $gap;
                }
            }
        }
        return $slots;
    }
}
