import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";

export async function GET() {
  const clients = await prisma.client.findMany({
    orderBy: { createdAt: "desc" },
  });
  return NextResponse.json(clients);
}

export async function POST(request: Request) {
  const session = await getServerSession(authOptions);
  if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const body = await request.json();
  const client = await prisma.client.create({
    data: {
      name: body.name,
      logo: body.logo,
      websiteUrl: body.websiteUrl,
      isActive: body.isActive ?? true,
    },
  });
  return NextResponse.json(client, { status: 201 });
}
