import { d as defineEventHandler, c as createError, g as getHeader, a as getSchemaManager, b as getDatabaseManager } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const publishScheduled_post = defineEventHandler(async (event) => {
  var _a;
  try {
    const expectedSecret = process.env.CRON_SECRET;
    if (!expectedSecret || expectedSecret.trim() === "") {
      throw createError({
        statusCode: 503,
        statusMessage: "Service Unavailable",
        message: "CRON_SECRET is not configured on the server"
      });
    }
    const authHeader = getHeader(event, "x-cron-secret");
    if (authHeader !== expectedSecret) {
      throw createError({
        statusCode: 401,
        statusMessage: "Unauthorized",
        message: "Invalid cron secret"
      });
    }
    const schemaManager = getSchemaManager();
    const db = getDatabaseManager();
    const dialect = db.getDialect();
    const schemas = schemaManager.getAllSchemas();
    const results = {};
    let totalPublished = 0;
    const nowExpression = dialect === "sqlite" ? "datetime('now')" : "NOW()";
    for (const schema of schemas) {
      if (!((_a = schema.options) == null ? void 0 : _a.draftAndPublish)) {
        continue;
      }
      const contentType = schema.info.singularName;
      const tableName = schema.info.pluralName.toLowerCase().replace(/-/g, "_");
      try {
        const sql = `
          SELECT id
          FROM ${tableName}
          WHERE published_at IS NOT NULL
          AND published_at <= ${nowExpression}
        `;
        const records = await db.query(sql);
        if (records.length > 0) {
          results[contentType] = records.length;
          totalPublished += records.length;
          console.log(`[Scheduled Publish] Found ${records.length} ${contentType}(s) to publish`);
        }
      } catch (error) {
        console.error(`[Scheduled Publish] Error processing ${contentType}:`, error);
      }
    }
    return {
      success: true,
      message: `Processed scheduled publications`,
      published: totalPublished,
      details: results,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    };
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error("[POST /api/_cron/publish-scheduled] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to process scheduled publications",
      message: error instanceof Error ? error.message : "Unknown error"
    });
  }
});

export { publishScheduled_post as default };
//# sourceMappingURL=publish-scheduled.post.mjs.map
