/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.license.plugin.core;

import org.elasticsearch.license.core.License;
import org.elasticsearch.xpack.scheduler.SchedulerEngine;

import static org.elasticsearch.license.plugin.core.LicensesService.GRACE_PERIOD_DURATION;
import static org.elasticsearch.license.plugin.core.LicensesService.getLicenseState;

public class LicenseSchedule implements SchedulerEngine.Schedule {

    private final License license;

    LicenseSchedule(License license) {
        this.license = license;
    }

    @Override
    public long nextScheduledTimeAfter(long startTime, long time) {
        long nextScheduledTime = -1;
        switch (getLicenseState(license, time)) {
            case ENABLED:
                nextScheduledTime = license.expiryDate();
                break;
            case GRACE_PERIOD:
                nextScheduledTime = license.expiryDate() + GRACE_PERIOD_DURATION.getMillis();
                break;
            case DISABLED:
                if (license.issueDate() > time) {
                    nextScheduledTime = license.issueDate();
                }
                break;
        }
        return nextScheduledTime;
    }
}
@@ -1241,32 +1241,403 @@
 ate() %3E time) %7B%0A
+                    // when we encounter a license with a future issue date%0A                    // which can happen with autogenerated license,%0A                    // we want to schedule a notification on the license issue date%0A                    // so the license is notificed once it is valid%0A                    // see https://github.com/elastic/x-plugins/issues/983%0A
                 

add comment regarding scheduling license with future issue date