/** Get all scheduled actions within a date range for a calendar view. */
export async function getScheduleCalendar(from, to, db) {
    const docs = await db.document.findMany({
        where: {
            deletedAt: null,
            OR: [
                { scheduledAt: { gte: from, lte: to } },
                { scheduledUnpublishAt: { gte: from, lte: to } },
            ],
        },
        select: {
            id: true,
            collection: true,
            title: true,
            scheduledAt: true,
            scheduledUnpublishAt: true,
        },
    });
    const entries = [];
    for (const doc of docs) {
        if (doc.scheduledAt && doc.scheduledAt >= from && doc.scheduledAt <= to) {
            entries.push({
                id: `${doc.id}-publish`,
                documentId: doc.id,
                collection: doc.collection,
                title: doc.title ?? 'Untitled',
                action: 'publish',
                scheduledAt: doc.scheduledAt,
            });
        }
        if (doc.scheduledUnpublishAt &&
            doc.scheduledUnpublishAt >= from &&
            doc.scheduledUnpublishAt <= to) {
            entries.push({
                id: `${doc.id}-unpublish`,
                documentId: doc.id,
                collection: doc.collection,
                title: doc.title ?? 'Untitled',
                action: 'unpublish',
                scheduledAt: doc.scheduledUnpublishAt,
            });
        }
    }
    return entries.sort((a, b) => a.scheduledAt.getTime() - b.scheduledAt.getTime());
}
/** Process documents due for scheduled publishing. */
export async function processScheduledPublish(db) {
    const now = new Date();
    const docs = await db.document.findMany({
        where: {
            status: 'SCHEDULED',
            scheduledAt: { lte: now },
            deletedAt: null,
        },
    });
    let published = 0;
    const errors = [];
    for (const doc of docs) {
        try {
            await db.document.update({
                where: { id: doc.id },
                data: {
                    status: 'PUBLISHED',
                    publishedAt: now,
                    scheduledAt: null,
                },
            });
            published++;
        }
        catch (err) {
            errors.push({
                documentId: doc.id,
                error: err instanceof Error ? err.message : 'Unknown error',
            });
        }
    }
    return { published, errors };
}
/** Process documents due for scheduled unpublishing. */
export async function processScheduledUnpublish(db) {
    const now = new Date();
    const docs = await db.document.findMany({
        where: {
            status: 'PUBLISHED',
            scheduledUnpublishAt: { lte: now },
            deletedAt: null,
        },
    });
    let unpublished = 0;
    const errors = [];
    for (const doc of docs) {
        try {
            await db.document.update({
                where: { id: doc.id },
                data: {
                    status: 'DRAFT',
                    scheduledUnpublishAt: null,
                },
            });
            unpublished++;
        }
        catch (err) {
            errors.push({
                documentId: doc.id,
                error: err instanceof Error ? err.message : 'Unknown error',
            });
        }
    }
    return { unpublished, errors };
}
/** Cron handler that runs both publish and unpublish processing. */
export async function schedulingCronHandler(db) {
    const publishResult = await processScheduledPublish(db);
    const unpublishResult = await processScheduledUnpublish(db);
    return {
        published: publishResult.published,
        unpublished: unpublishResult.unpublished,
        errors: [...publishResult.errors, ...unpublishResult.errors],
    };
}
//# sourceMappingURL=index.js.map