import { ScheduledNotes } from "../../../models/index.js";
import { createQueue, defaultJobOpts } from "../index.js";
import { clean } from "./clean.js";
import { cleanCharts } from "./clean-charts.js";
import { checkExpiredMutings } from "./check-expired-mutings.js";
import { checkExpiredMemoriets } from "./check-expired-memoriets.js";
import { resyncCharts } from "./resync-charts.js";
import { tickCharts } from "./tick-charts.js";
import { verifyLinks } from "./verify-links.js";
import { publishScheduledNoteJob } from "./publish-scheduled-note.js";
import { enqueueScheduledNote } from "./scheduled-note.js";
const processors = {
    clean,
    cleanCharts,
    checkExpiredMutings,
    checkExpiredMemoriets,
    resyncCharts,
    tickCharts,
    verifyLinks,
    publishScheduledNote: publishScheduledNoteJob
};
async function process(job) {
    const processor = processors[job.name];
    if (processor === undefined) return "skip: unknown job name";
    return await processor(job);
}
const [systemQueue_, systemInitQueue] = createQueue("system", process, {
    concurrency: Object.keys(processors).length
});
export const systemQueue = systemQueue_;
async function enqueuePendingScheduledNotes() {
    const scheduledNotes = await ScheduledNotes.find({
        where: {
            status: "scheduled"
        }
    });
    for (const scheduledNote of scheduledNotes){
        await enqueueScheduledNote(scheduledNote);
    }
}
export async function systemInit() {
    await systemInitQueue();
    for (const { name, seconds } of [
        {
            name: "clean",
            seconds: 60 * 60
        },
        {
            name: "cleanCharts",
            seconds: 60 * 60
        },
        {
            name: "checkExpiredMutings",
            seconds: 5 * 60
        },
        {
            name: "checkExpiredMemoriets",
            seconds: 60
        },
        {
            name: "resyncCharts",
            seconds: 60 * 60
        },
        {
            name: "tickCharts",
            seconds: 60
        },
        {
            name: "verifyLinks",
            seconds: 60 * 60 * 24
        }
    ]){
        await systemQueue.upsertJobScheduler(name, {
            every: seconds * 1000
        }, {
            opts: defaultJobOpts
        });
    }
    await enqueuePendingScheduledNotes();
}
