import { d as defineEventHandler, $ as getAuthRequestContext, q as readBody, c as createError, aB as isValidUsername, aC as isValidEmail, dH as isValidPassword, G as createContentManager, dA as hashPassword, du as generateJWT, dI as emitAuthRegister, b2 as setResponseStatus } 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 register_post = defineEventHandler(async (event) => {
  const reqCtx = getAuthRequestContext(event);
  try {
    const body = await readBody(event);
    if (!body || typeof body !== "object") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Request body must be a valid JSON object"
      });
    }
    const { username, email, password } = body;
    if (!username || !email || !password) {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Username, email, and password are required"
      });
    }
    if (!isValidUsername(username)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Username must be 3-50 characters long and contain only letters, numbers, and underscores"
      });
    }
    if (!isValidEmail(email)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Invalid email format"
      });
    }
    if (!isValidPassword(password)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Password must be at least 6 characters long"
      });
    }
    const userManager = createContentManager("user");
    const existingUsername = await userManager.findMany({
      filters: { username },
      limit: 1
    });
    if (existingUsername.data.length > 0) {
      throw createError({
        statusCode: 400,
        statusMessage: "Conflict",
        message: "Username is already taken"
      });
    }
    const existingEmail = await userManager.findMany({
      filters: { email },
      limit: 1
    });
    if (existingEmail.data.length > 0) {
      throw createError({
        statusCode: 400,
        statusMessage: "Conflict",
        message: "Email is already registered"
      });
    }
    const hashedPassword = await hashPassword(password);
    const roleManager = createContentManager("role");
    const authenticatedRoles = await roleManager.findMany({
      filters: { name: "authenticated" },
      limit: 1
    });
    if (authenticatedRoles.data.length === 0) {
      throw createError({
        statusCode: 500,
        statusMessage: "Configuration Error",
        message: 'Default "authenticated" role not found. Please run database sync.'
      });
    }
    const authenticatedRole = authenticatedRoles.data[0];
    const user = await userManager.create({
      username,
      email,
      password: hashedPassword,
      confirmed: false,
      blocked: false,
      role: authenticatedRole.id
    });
    const jwtPayload = {
      id: user.id,
      username: user.username,
      email: user.email,
      role: "authenticated"
    };
    const token = generateJWT(jwtPayload);
    const { password: _, ...userWithoutPassword } = user;
    await emitAuthRegister(jwtPayload.id, jwtPayload.email, jwtPayload.username, reqCtx);
    setResponseStatus(event, 201);
    return {
      success: true,
      message: "User registered successfully",
      data: {
        user: userWithoutPassword,
        token
      }
    };
  } catch (error) {
    if (error instanceof Error && error.message.includes("Schema") && error.message.includes("not found")) {
      throw createError({
        statusCode: 500,
        statusMessage: "Configuration Error",
        message: "User schema is not configured. Please run database sync."
      });
    }
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    console.error("[POST /api/auth/register] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Registration Failed",
      message: error instanceof Error ? error.message : "Unknown error occurred"
    });
  }
});

export { register_post as default };
//# sourceMappingURL=register.post.mjs.map
