<?php

/**
 * User Weekday Relation Sync Service
 */
namespace RadiusTheme\RadiusBooking\Services\EmployeeServices;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
use Exception;
use RadiusTheme\RadiusBooking\Repositories\UserRepository;

/**
 * Class UserWeekdayRelationSyncService
 *
 * Handles synchronization of weekday relations (break times and time periods)
 *
 * @package RadiusTheme\RadiusBooking\Services\UserServices
 * @since 1.0.0
 */
class WeekdayRelationSyncService {

	/**
	 * The user repository instance.
	 *
	 * @var UserRepository $userRepo  User repository instance.
	 */
	private UserRepository $userRepo;

	/**
	 * The break time sync service instance.
	 *
	 * @var BreakTimeSyncService $breakTimeSyncService  Break time sync service instance.
	 */
	private BreakTimeSyncService $breakTimeSyncService;

	/**
	 * The time period sync service instance.
	 *
	 * @var TimePeriodSyncService $timePeriodSyncService  Time period sync service instance.
	 */
	private TimePeriodSyncService $timePeriodSyncService;

	/**
	 * Constructor
	 *
	 * @param UserRepository $userRepo  User repository instance.
	 * @param BreakTimeSyncService $breakTimeSyncService  Break time sync service instance.
	 * @param TimePeriodSyncService $timePeriodSyncService  Time period sync service instance.
	 */
	public function __construct(
		UserRepository $userRepo,
		BreakTimeSyncService $breakTimeSyncService,
		TimePeriodSyncService $timePeriodSyncService
	) {
		$this->userRepo              = $userRepo;
		$this->breakTimeSyncService  = $breakTimeSyncService;
		$this->timePeriodSyncService = $timePeriodSyncService;
	}

	/**
	 * Synchronizes weekday relations including break times and time periods for a user.
	 *
	 * @param int $userId The ID of the user for whom weekday relations are being synced.
	 * @param int $weekdayId The ID of the weekday being processed.
	 * @param array $weekday Data associated with the weekday, including time breaks and time periods.
	 *
	 * @return void
	 */
	public function syncWeekdayRelations( int $userId, int $weekdayId, array $weekday ): void {
		// Sync break times
		if ( isset( $weekday['time_breaks'] ) ) {
			$this->breakTimeSyncService->syncUserBreakTime( $userId, $weekdayId, $weekday['time_breaks'] );
		}
		// Sync time periods
		if ( isset( $weekday['time_periods'] ) ) {
			$this->timePeriodSyncService->syncUserTimePeriod( $userId, $weekdayId, $weekday['time_periods'] );
		}
	}
}
