<?php

namespace SugarCalendarBookings\Availability;

use SugarCalendarBookings\Options;
use SugarCalendarBookings\Plugin;

/**
 * Date Overrides Service.
 *
 * Handles business logic for date overrides.
 *
 * @since 1.1.0
 */
class DateOverridesService {

	/**
	 * Get all date overrides.
	 *
	 * @since 1.1.0
	 *
	 * @return array Array of date overrides.
	 */
	public function get_all() {

		$options          = Plugin::get( Options::class );
		$working_schedule = $options->get( 'availability.working_schedule' );
		$overrides        = $working_schedule['overrides'] ?? [];

		return $overrides;
	}

	/**
	 * Sort overrides by start date (earliest to latest).
	 *
	 * @since 1.1.0
	 *
	 * @param array $overrides Array of overrides.
	 *
	 * @return array Sorted overrides.
	 */
	public function sort_by_date( $overrides ) {

		if ( empty( $overrides ) ) {
			return $overrides;
		}

		usort(
			$overrides,
			function ( $a, $b ) {
				$date_a = $a['start_date'] ?? '';
				$date_b = $b['start_date'] ?? '';

				return strcmp( $date_a, $date_b );
			}
		);

		return $overrides;
	}
}
