/**
 * Auto-Archive Watcher Service
 *
 * Periodically polls for completed features (Maintain lifecycle) that have
 * exceeded the configured auto-archive delay, and archives them automatically.
 *
 * Follows the same singleton + start/stop pattern as NotificationWatcherService.
 *
 * The poll interval is 60 seconds — auto-archiving is not time-critical and
 * a minute-level granularity keeps DB load minimal.
 */
import { AutoArchiveCompletedUseCase } from '../../../application/use-cases/features/auto-archive-completed.use-case.js';
import { ArchiveFeatureUseCase } from '../../../application/use-cases/features/archive-feature.use-case.js';
import { getSettings } from '../settings.service.js';
const DEFAULT_POLL_INTERVAL_MS = 60_000;
const DEFAULT_DELAY_MINUTES = 10;
export class AutoArchiveWatcherService {
    featureRepo;
    pollIntervalMs;
    intervalId = null;
    constructor(featureRepo, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS) {
        this.featureRepo = featureRepo;
        this.pollIntervalMs = pollIntervalMs;
    }
    isRunning() {
        return this.intervalId !== null;
    }
    start() {
        if (this.intervalId !== null)
            return;
        // Run first poll immediately
        void this.poll();
        this.intervalId = setInterval(() => {
            void this.poll();
        }, this.pollIntervalMs);
    }
    stop() {
        if (this.intervalId !== null) {
            clearInterval(this.intervalId);
            this.intervalId = null;
        }
    }
    async poll() {
        try {
            const settings = getSettings();
            const delayMinutes = settings.workflow.autoArchiveDelayMinutes ?? DEFAULT_DELAY_MINUTES;
            if (delayMinutes <= 0)
                return;
            const archiveFeature = new ArchiveFeatureUseCase(this.featureRepo);
            const useCase = new AutoArchiveCompletedUseCase(this.featureRepo, archiveFeature);
            await useCase.execute(delayMinutes);
        }
        catch {
            // Settings not initialized or DB not ready — skip this poll cycle
        }
    }
}
// --- Singleton accessors (follows NotificationWatcher pattern) ---
let watcherInstance = null;
/**
 * Initialize the auto-archive watcher singleton.
 * Must be called once during web server startup.
 *
 * @throws Error if the watcher is already initialized
 */
export function initializeAutoArchiveWatcher(featureRepo, pollIntervalMs) {
    if (watcherInstance !== null) {
        throw new Error('Auto-archive watcher already initialized. Cannot re-initialize.');
    }
    watcherInstance = new AutoArchiveWatcherService(featureRepo, pollIntervalMs);
}
/**
 * Get the auto-archive watcher singleton.
 *
 * @returns The auto-archive watcher service
 * @throws Error if the watcher hasn't been initialized yet
 */
export function getAutoArchiveWatcher() {
    if (watcherInstance === null) {
        throw new Error('Auto-archive watcher not initialized. Call initializeAutoArchiveWatcher() during web server startup.');
    }
    return watcherInstance;
}
/**
 * Check if the auto-archive watcher has been initialized.
 */
export function hasAutoArchiveWatcher() {
    return watcherInstance !== null;
}
/**
 * Reset the auto-archive watcher singleton (for testing purposes only).
 * Stops the watcher if running before resetting.
 *
 * @internal
 */
export function resetAutoArchiveWatcher() {
    if (watcherInstance !== null) {
        watcherInstance.stop();
    }
    watcherInstance = null;
}
