import { d as defineEventHandler, r as readBody, c as createError } from '../../../nitro/nitro.mjs';
import { Pool } from 'pg';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';

const pool = new Pool({
  user: "musicuser",
  host: "localhost",
  database: "musicdb",
  password: "Tjqjqhdks$321",
  port: 5432
});
const register_post = defineEventHandler(async (event) => {
  try {
    const body = await readBody(event);
    const { user_id, password, name, role_level } = body;
    if (!user_id || !password || !name || !role_level) {
      throw createError({
        statusCode: 400,
        statusMessage: "\uBAA8\uB4E0 \uD544\uB4DC\uB97C \uC785\uB825\uD574\uC8FC\uC138\uC694."
      });
    }
    if (role_level < 1 || role_level > 3) {
      throw createError({
        statusCode: 400,
        statusMessage: "\uAD8C\uD55C \uB4F1\uAE09\uC740 1~3 \uC0AC\uC774\uC5EC\uC57C \uD569\uB2C8\uB2E4."
      });
    }
    const existingUser = await pool.query(
      "SELECT user_id FROM members WHERE user_id = $1",
      [user_id]
    );
    if (existingUser.rows.length > 0) {
      throw createError({
        statusCode: 409,
        statusMessage: "\uC774\uBBF8 \uC874\uC7AC\uD558\uB294 \uC544\uC774\uB514\uC785\uB2C8\uB2E4."
      });
    }
    const result = await pool.query(
      "INSERT INTO members (user_id, password, name, role_level) VALUES ($1, $2, $3, $4) RETURNING *",
      [user_id, password, name, role_level]
    );
    const newUser = result.rows[0];
    return {
      success: true,
      message: "\uD68C\uC6D0\uAC00\uC785\uC774 \uC644\uB8CC\uB418\uC5C8\uC2B5\uB2C8\uB2E4.",
      user: {
        id: newUser.id,
        user_id: newUser.user_id,
        name: newUser.name,
        role_level: newUser.role_level
      }
    };
  } catch (error) {
    console.error("Register error:", error);
    throw error;
  }
});

export { register_post as default };
