import { z } from "zod";
import prisma from "@/server/db/prisma";
import { parseJson } from "@/server/http/parse";
import { ok } from "@/server/http/response";
import { withErrorHandling } from "@/server/http/withErrorHandling";

export const GET = withErrorHandling(async () => {
  const wattsSection = await prisma.wattssection.findFirst();

  if (!wattsSection) {
    return ok({
      title: "Smart Permanent Lighting Solutions",
      subtitle:
        "Transform your home with state-of-the-art LED lighting that combines beauty, functionality, and smart control",
      features: [
        {
          title: "Smart Control",
          description:
            "Control your lights from anywhere with the Watts 2.0 app. Create custom animations, set schedules, and choose from 1.6M colors.",
          icon: "🎨",
        },
        {
          title: "Permanent Installation",
          description:
            "No more climbing ladders! Our professional installation creates a seamless, permanent lighting solution that enhances your home's value.",
          icon: "🏠",
        },
        {
          title: "Year-Round Beauty",
          description:
            "Enjoy warm white lighting for everyday ambiance and vibrant colors for special occasions. Perfect for both architectural enhancement and holiday celebrations.",
          icon: "✨",
        },
      ],
    });
  }

  return ok(wattsSection);
});

const WattsSectionSchema = z.object({
  title: z.string().min(1),
  subtitle: z.string().min(1),
  features: z.array(z.any()),
});

export const POST = withErrorHandling(async (request: Request) => {
  const data = await parseJson(request, WattsSectionSchema);
  const existing = await prisma.wattssection.findFirst();

  if (existing) {
    const updated = await prisma.wattssection.update({
      where: { id: existing.id },
      data: { title: data.title, subtitle: data.subtitle, features: data.features },
    });
    return ok(updated);
  }

  const created = await prisma.wattssection.create({
    data: { title: data.title, subtitle: data.subtitle, features: data.features },
  });
  return ok(created);
});
