/**
 * Copyright (c) 2025 SoftCorporation LLC. All rights reserved.
 *
 * The Software License, Version 1.0
 *
 * SoftCorporation LLC. grants you ("Licensee") a non-exclusive, royalty free,
 * license to use, modify and redistribute this software in source and binary
 * code form, provided that the following conditions are met:
 *
 * 1. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        SoftCorporation LLC. (http://www.softcorporation.com)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 2. This software and SoftCorporation name must not be used to
 *    promote products derived from the software without prior
 *    written permission. For written permission, please contact
 *    info@softcorporation.com.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * IN NO EVENT SHALL THE SOFTCORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).
 *
 */
package com.softcorporation.sandi.index.scheduler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.softcorporation.sandi.index.service.TypeAheadIndexerService;

/**
 * Scheduler for type-ahead indexing
 * Runs every 10 minutes to update type-ahead collections with new suggestions
 */
@Component
public class TypeAheadScheduler {

    private static final Logger LOGGER = LoggerFactory.getLogger(TypeAheadScheduler.class);

    @Autowired
    private TypeAheadIndexerService typeAheadIndexerService;

    /**
     * Enable/disable type-ahead indexing scheduler
     */
    @Value("${sandi.typeahead.scheduler.enabled:true}")
    private boolean schedulerEnabled;

    /**
     * Scheduled task to index type-ahead suggestions
     * Schedule configured via sandi.typeahead.scheduler.cron property (default: Sunday midnight)
     */
//    @Scheduled(cron = "${sandi.typeahead.scheduler.cron:0 0 0 * * SUN}")
    public void indexTypeAheadScheduled() {
        if (!schedulerEnabled) {
            LOGGER.debug("Type-ahead scheduler is disabled");
            return;
        }

        try {
            LOGGER.info("Type-ahead scheduler: Starting indexing job");
            typeAheadIndexerService.indexAllTypeAhead();
            LOGGER.info("Type-ahead scheduler: Indexing job completed");
        } catch (Exception e) {
            LOGGER.error("Type-ahead scheduler: Indexing job failed: {}", e.getMessage(), e);
        }
    }

    /**
     * Scheduled task to index type-ahead suggestions
     * Runs every hour (3,600,000 milliseconds)
     */
//    @Scheduled(fixedDelay = 600000, initialDelay = 60000)
//    @Scheduled(fixedDelay = 3600000, initialDelay = 60000)
    public void indexTypeAheadPeriodic() {
        if (!schedulerEnabled) {
            LOGGER.debug("Type-ahead scheduler is disabled");
            return;
        }

        try {
            LOGGER.debug("Type-ahead periodic scheduler: Starting indexing job");
            typeAheadIndexerService.indexAllTypeAhead();
            LOGGER.debug("Type-ahead periodic scheduler: Indexing job completed");
        } catch (Exception e) {
            LOGGER.error("Type-ahead periodic scheduler: Indexing job failed: {}", e.getMessage(), e);
        }
    }

}
