import { NextRequest, NextResponse } from "next/server";
import { getTeamMembers, createTeamMember } from "@/lib/db";
import { revalidatePath } from "next/cache";
import type { RichListItem, VictoryItem } from "@/types/content";

export async function GET() {
  const members = getTeamMembers();
  return NextResponse.json(members, {
    headers: { "Cache-Control": "private, max-age=60" },
  });
}

export async function POST(request: NextRequest) {
  const data = await request.json() as {
    name: string;
    role: string;
    image: string;
    instagram?: string;
    description?: string;
    experience?: string[];
    victories?: VictoryItem[];
    education?: RichListItem[];
  };

  if (!data.name || !data.role || !data.image) {
    return NextResponse.json(
      { error: "name, role, and image are required" },
      { status: 400 }
    );
  }

  const id = createTeamMember(data);
  revalidatePath("/");

  return NextResponse.json({ id }, { status: 201 });
}
