import { PrismaClient } from '../../generated/prisma/client';

const prisma = new PrismaClient();

export async function GET() {
  const notices = await prisma.noticeBoard.findMany({
    where: {
      isDeleted: false,
    },
    select: {
      id: true,
      title: true,
      pubDate: true,
      tag: true,
      content: true,
      // 필요한 다른 필드들도 추가
    },
  });
  return Response.json(notices);
}


export async function POST(request: Request) {
  const { title, content, tag } = await request.json();
  const newNotice = await prisma.noticeBoard.create({
    data: {
      title,
      content,
      tag,
      pubDate: new Date(),
    },
  });
  return Response.json(newNotice, { status: 201 });
}

export async function PUT(request: Request) {
  const { id, ...updateData } = await request.json();
  const updatedNotice = await prisma.noticeBoard.update({
    where: { id },
    data: updateData,
  });
  return Response.json(updatedNotice);
}

export async function DELETE(request: Request) {
  const url = new URL(request.url);
  const id = url.searchParams.get('id');
  
  if (!id) {
    return new Response('ID is required', { status: 400 });
  }

  const deletedNotice = await prisma.noticeBoard.update({
    where: { id },
    data: { isDeleted: true },
  });

  return new Response(JSON.stringify(deletedNotice), { status: 200 });
}