import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { z } from "zod";

export async function GET() {
  const items = await prisma.partnerShop.findMany({ orderBy: [{ active: "desc" }, { sortOrder: "asc" }, { createdAt: "desc" }] });
  return NextResponse.json({ items });
}

const createSchema = z.object({
  region: z.string().min(1),
  name: z.string().min(1),
  address: z.string().min(1),
  imageUrl: z.string().min(1),
  sortOrder: z.coerce.number().int().optional(),
  active: z.boolean().optional(),
});

export async function POST(req: Request) {
  const body = await req.json().catch(() => ({}));
  const parsed = createSchema.safeParse(body);
  if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
  const item = await prisma.partnerShop.create({ data: parsed.data });
  return NextResponse.json({ item }, { status: 201 });
}


