// server/api/config/klinik-ruang.post.ts
import { getConfigDb, parseConfigRow } from '~/server/utils/configDb';

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  const { kodeKlinik, namaKlinik, ruangList = [] } = body;

  if (!kodeKlinik || !namaKlinik) {
    throw createError({ statusCode: 400, message: 'kodeKlinik dan namaKlinik wajib diisi' });
  }

  const db = getConfigDb();
  const result = db.prepare(
    `INSERT INTO config_klinik_ruang (kodeKlinik, namaKlinik, ruangList) VALUES (?, ?, ?)`
  ).run(kodeKlinik, namaKlinik, JSON.stringify(ruangList));

  const newRow = db.prepare('SELECT * FROM config_klinik_ruang WHERE id = ?').get(result.lastInsertRowid);
  return { success: true, data: parseConfigRow(newRow), message: `Klinik Ruang ${namaKlinik} berhasil ditambahkan` };
});
