using Hangfire.Server;
using System;

namespace OnlineSalesAutoCrop.CoreAPI.Configurations
{
	/// <summary>
	/// 
	/// </summary>
	/// <param name="startHour"></param>
	/// <param name="endHour"></param>
	public class TimeRestrictionServerFilter(int startHour, int endHour) : IServerFilter
	{
		private readonly int _endHour = endHour;
		private readonly int _startHour = startHour;

		/// <summary>
		///     
		/// </summary>
		/// <param name="context"></param>
		public void OnPerforming(PerformingContext context)
		{
			var now = DateTime.Now.Hour; // Use UTC or local time consistently

			// Check if the current hour is within the restricted range (e.g., 1 AM to 7 AM)
			if (now >= _startHour && now < _endHour)
			{
				// Abort the processing of the current job
				context.Canceled = true;
				// Optional: Log that the job was skipped due to time restrictions
			}
		}

		/// <summary>
		///     
		/// </summary>
		/// <param name="context"></param>
		public void OnPerformed(PerformedContext context)
		{
			// No action needed after the job is performed
		}
	}
}