using e_suite.Scheduler.Extensions;
using eSuite.Core.Clock;

namespace e_suite.Scheduler.Scheduler;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class DailyAttribute : Attribute, ISchedule
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="hour">UTC Hour when job will be triggered</param>
    public DailyAttribute(int hour)
    {
        Hour = hour;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="hour">UTC Hour when job will be triggered</param>
    /// <param name="minute">UTC Minute when job will be triggered</param>
    public DailyAttribute(int hour, int minute)
    {
        Hour = hour;
        Minute = minute;
    }

    public int Hour { get; }
    public int Minute { get; }


    public DateTimeOffset NextScheduledExecution(IClock clock)
    {
        var actualTime = clock.GetNow;
        var scheduledTime = actualTime
            .SetToMidnight()
            .AddHours(Hour)
            .AddMinutes(Minute);

        if (scheduledTime < actualTime)
        {
            scheduledTime = scheduledTime.AddDays(1);
        }

        return scheduledTime;
    }

    
}