import { d as defineEventHandler, r as requireAuth, bT as requirePermission, q as readBody, c as createError, bN as dbQueryOne, G as createContentManager, eC as isUniqueConstraintError } from '../../nitro/nitro.mjs';
import { p as parseNullablePositiveInt } from '../../_/ids.mjs';
import { g as getAuditActorFromEvent } from '../../_/audit-log-actor.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 index_post = defineEventHandler(async (event) => {
  try {
    await requireAuth(event);
    await requirePermission(
      event.context.user,
      "media",
      "create",
      event.context.adminUser,
      event.context.apiToken
    );
    const body = await readBody(event);
    if (!body.name || typeof body.name !== "string" || body.name.trim() === "") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Folder name is required"
      });
    }
    const name = body.name.trim();
    const parsedParentId = parseNullablePositiveInt(body.parent);
    const parentId = parsedParentId.value;
    if (!parsedParentId.valid) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Invalid parent ID"
      });
    }
    if (parentId !== null) {
      const parentFolder = await dbQueryOne(`SELECT id FROM media_folders WHERE id = ?`, [
        parentId
      ]);
      if (!parentFolder) {
        throw createError({
          statusCode: 404,
          statusMessage: "Not Found",
          message: "Parent folder not found"
        });
      }
    }
    const existingQuery = parentId !== null ? `SELECT id FROM media_folders WHERE name = ? AND parent_id = ?` : `SELECT id FROM media_folders WHERE name = ? AND parent_id IS NULL`;
    const existingParams = parentId !== null ? [name, parentId] : [name];
    const existing = await dbQueryOne(existingQuery, existingParams);
    if (existing) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: `Folder "${name}" already exists in this location`
      });
    }
    const folderManager = createContentManager("media-folder");
    const data = { name };
    if (parentId !== null) {
      data.parent = parentId;
    }
    const folder = await folderManager.create(data, {
      actor: getAuditActorFromEvent(event)
    });
    return {
      success: true,
      data: folder
    };
  } catch (error) {
    const err = error;
    if (err.statusCode) {
      throw error;
    }
    if (isUniqueConstraintError(error)) {
      throw createError({
        statusCode: 409,
        statusMessage: "Conflict",
        message: "A folder with this name already exists in the target location"
      });
    }
    console.error("Create media folder error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Internal Server Error",
      message: err.message || "Failed to create media folder"
    });
  }
});

export { index_post as default };
//# sourceMappingURL=index.post.mjs.map
