import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth";

export async function GET() {
  const session = await getSession();
  if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const treatments = await prisma.treatment.findMany({
    where: { clinicId: session.id, isActive: true },
    orderBy: { name: "asc" },
  });

  return NextResponse.json(treatments);
}

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

  const body = await req.json();

  const treatment = await prisma.treatment.create({
    data: {
      clinicId: session.id,
      name: body.name,
      duration: body.duration || 30,
      price: body.price || null,
      prepInstructions: body.prepInstructions || null,
      aftercareInstructions: body.aftercareInstructions || null,
    },
  });

  return NextResponse.json(treatment);
}
