package de.votian.services.service;

import de.votian.services.config.LegacySchedulingProperties;
import de.votian.services.security.AuthSessionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class LegacyOperationsScheduler {

    private static final Logger log = LoggerFactory.getLogger(LegacyOperationsScheduler.class);

    private final LegacySchedulingProperties schedulingProperties;
    private final AuthSessionService authSessionService;
    private final LegacyCourierAutoLogoutService legacyCourierAutoLogoutService;
    private final AcceptanceProtocolAutomationService acceptanceProtocolAutomationService;

    public LegacyOperationsScheduler(LegacySchedulingProperties schedulingProperties,
                                     AuthSessionService authSessionService,
                                     LegacyCourierAutoLogoutService legacyCourierAutoLogoutService,
                                     AcceptanceProtocolAutomationService acceptanceProtocolAutomationService) {
        this.schedulingProperties = schedulingProperties;
        this.authSessionService = authSessionService;
        this.legacyCourierAutoLogoutService = legacyCourierAutoLogoutService;
        this.acceptanceProtocolAutomationService = acceptanceProtocolAutomationService;
    }

    @Scheduled(cron = "${votian.scheduling.auth-session-cleanup-cron:0 */5 * * * *}",
            zone = "${votian.scheduling.zone:Europe/Berlin}")
    public void cleanupExpiredAuthSessions() {
        if (!schedulingProperties.isEnabled()) {
            return;
        }
        AuthSessionService.CleanupResult result = authSessionService.cleanupExpiredSessions();
        if (result.totalExpiredSessions() > 0) {
            log.info("Expired {} auth session(s) and {} pending TOTP session(s).",
                    result.expiredAuthenticatedSessions(),
                    result.expiredPendingTotpSessions());
        }
    }

    @Scheduled(cron = "${votian.scheduling.courier-auto-logout-cron:0 */30 * * * *}",
            zone = "${votian.scheduling.zone:Europe/Berlin}")
    public void runCourierAutoLogout() {
        if (!schedulingProperties.isEnabled()) {
            return;
        }
        legacyCourierAutoLogoutService.logoutCouriersByAutorevokes();
    }

    @Scheduled(cron = "${votian.scheduling.acceptance-protocol-mail-cron:0 */5 * * * *}",
            zone = "${votian.scheduling.zone:Europe/Berlin}")
    public void runAcceptanceProtocolMailDispatch() {
        if (!schedulingProperties.isEnabled()) {
            return;
        }
        acceptanceProtocolAutomationService.dispatchPendingMailProtocols();
    }

    @Scheduled(cron = "${votian.scheduling.acceptance-protocol-letter-cron:30 */5 * * * *}",
            zone = "${votian.scheduling.zone:Europe/Berlin}")
    public void runAcceptanceProtocolLetterDispatch() {
        if (!schedulingProperties.isEnabled()) {
            return;
        }
        acceptanceProtocolAutomationService.dispatchPendingLetterProtocols();
    }
}
