"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArchiveScheduler = void 0;
const node_cron_1 = __importDefault(require("node-cron"));
const logger_js_1 = __importDefault(require("../logger.js"));
const archive_js_1 = require("./archive.js");
class ArchiveScheduler {
    task = null;
    dbPath;
    config;
    constructor(dbPath, config) {
        this.dbPath = dbPath;
        this.config = config;
    }
    /**
     * Start the archive scheduler
     * Performs crash recovery check on startup
     */
    async start() {
        if (!this.config.archiveEnabled) {
            logger_js_1.default.info("[Archive] archive scheduler disabled by config");
            return;
        }
        logger_js_1.default.info("[Archive] starting archive scheduler", {
            schedule: this.config.archiveSchedule,
            activeDays: this.config.activeDays,
            maxFiles: this.config.archiveMaxFiles,
        });
        // Crash recovery: check for incomplete archive from previous run
        try {
            const wasIncomplete = await (0, archive_js_1.checkIncompleteArchive)(this.dbPath);
            if (wasIncomplete) {
                logger_js_1.default.info("[Archive] recovered from incomplete archive operation");
            }
        }
        catch (error) {
            logger_js_1.default.error("[Archive] crash recovery check failed", { error });
            // Continue starting scheduler even if recovery check fails
        }
        this.task = node_cron_1.default.schedule(this.config.archiveSchedule, async () => {
            try {
                logger_js_1.default.info("[Archive] starting scheduled archive...");
                const result = await (0, archive_js_1.archiveOldTraces)(this.dbPath, this.config);
                if (result.success) {
                    if (result.archivedTraces > 0) {
                        logger_js_1.default.info("[Archive] completed successfully", {
                            archivedTraces: result.archivedTraces,
                            archiveFile: result.archiveFile,
                            duration: `${result.duration}ms`,
                        });
                    }
                    else {
                        logger_js_1.default.info("[Archive] no traces to archive");
                    }
                }
                else {
                    logger_js_1.default.error("[Archive] failed", { error: result.error });
                }
            }
            catch (error) {
                logger_js_1.default.error("[Archive] unexpected error", { error });
            }
        });
        this.task.start();
        logger_js_1.default.info("[Archive] archive scheduler started");
    }
    stop() {
        if (this.task) {
            this.task.stop();
            this.task = null;
            logger_js_1.default.info("[Archive] archive scheduler stopped");
        }
    }
    /**
     * Trigger archive operation immediately (for testing purposes only)
     * @internal
     */
    async triggerNow() {
        return (0, archive_js_1.archiveOldTraces)(this.dbPath, this.config).then(() => { });
    }
}
exports.ArchiveScheduler = ArchiveScheduler;
