import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getApiUser } from "@/lib/apiAuth";
export async function GET(req: NextRequest) {
  const countries = await db.country.findMany({
    include: { group: true },
    orderBy: { name: "asc" },
  });
  return NextResponse.json(countries);
}

export async function POST(req: NextRequest) {
  const apiUser = await getApiUser(req);
  if (!apiUser || apiUser.role !== "ADMIN")
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const body = await req.json();
  const country = await db.country.create({ data: body });
  return NextResponse.json(country, { status: 201 });
}
