<?php

namespace SugarCalendarBookings\Integrations\CalendarEvents;

use SugarCalendarBookings\Appointments\AppointmentModel;
use SugarCalendarBookings\Integrations\CalendarEventProviderInterface;
use SugarCalendarBookings\Integrations\IntegrationCapabilityRegistry;

/**
 * Deliverable connection resolver.
 *
 * Snapshots deliverable connections for every available calendar event
 * provider in a single registry pass.
 *
 * @since 1.5.0
 */
class DeliverableConnectionResolver {

	/**
	 * Integration capability registry.
	 *
	 * @since 1.5.0
	 *
	 * @var IntegrationCapabilityRegistry
	 */
	private IntegrationCapabilityRegistry $registry;

	/**
	 * Constructor.
	 *
	 * @since 1.5.0
	 *
	 * @param IntegrationCapabilityRegistry $registry Capability registry.
	 */
	public function __construct( IntegrationCapabilityRegistry $registry ) {

		$this->registry = $registry;
	}

	/**
	 * Snapshot deliverable connections for every available calendar event provider.
	 *
	 * @since 1.5.0
	 *
	 * @param AppointmentModel $appointment Target appointment.
	 *
	 * @return DeliverableConnectionMap
	 */
	public function for_appointment( AppointmentModel $appointment ): DeliverableConnectionMap {

		$providers = $this->registry->get_available( CalendarEventProviderInterface::class );

		$connections_by_slug = [];

		foreach ( $providers as $provider ) {

			if ( ! $provider instanceof CalendarEventProviderInterface ) {
				continue;
			}

			$connections_by_slug[ $provider->get_provider_slug() ] = $provider->get_deliverable_connections( $appointment );
		}

		return new DeliverableConnectionMap( $connections_by_slug );
	}
}
