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";

// GET: Fetch the Watts Lights page config
export const GET = withErrorHandling(async () => {
  let page = await prisma.wattslightspage.findFirst();
  if (!page) {
    page = await prisma.wattslightspage.create({
      data: {
        hero: {
          title: "Watts Lights by Nitescape",
          subtitle:
            "The permanent, smart lighting system that makes your home unforgettable—every night, every season.",
          buttonText: "Get My Free Quote",
          buttonLink: "/contact",
          youtubeVideoId: "nNJq94Q2Lsw",
        },
        features: [
          {
            icon: "🎨",
            title: "Vivid Custom Colors",
            description: "Millions of color options, all at your fingertips for any occasion.",
          },
          {
            icon: "📱",
            title: "Smart App Control",
            description: "Control your lights with our intuitive app, voice, or automation.",
          },
          {
            icon: "❄️",
            title: "All-Weather Ready",
            description: "Engineered for harsh winters, with a robust 5-year warranty.",
          },
        ],
        infoSection: {
          title: "What Makes Watts Lights Different?",
          content:
            "Watts Lights are a permanent, professionally installed LED lighting system designed to blend seamlessly with your home's architecture. Unlike traditional holiday lights, they're nearly invisible by day and brilliant by night. With full app and voice control, you can set the mood for any occasion—holidays, parties, security, or just a beautiful evening at home. Choose from millions of colors and patterns, schedule your lights, and enjoy effortless curb appeal all year long. Built for Canadian weather, Watts Lights are energy efficient, low maintenance, and backed by a 5-year warranty. Discover the future of outdoor lighting with Nitescape.",
        },
        ctaSection: {
          headline: "Ready to Light Up Your Home?",
          text: "Get a free quote today and see how Watts Lights can transform your home's curb appeal, security, and year-round enjoyment.",
          buttonText: "Get My Free Quote",
          buttonLink: "/contact",
        },
        appStores: {
          appleStoreLink: "",
          googlePlayLink: "",
        },
        tutorialVideo: {
          youtubeVideoId: "",
          title: "How to Use Watts Lights App",
          description: "Learn how to control your Watts Lights system with our easy-to-use mobile app.",
        },
      },
    });
  }
  return ok(page);
});

// PUT: Update the Watts Lights page config
const HeroSchema = z.object({
  title: z.string().min(1),
  subtitle: z.string().min(1),
  buttonText: z.string().min(1),
  buttonLink: z.string().min(1),
  youtubeVideoId: z.string(),
});

const FeatureSchema = z.object({
  icon: z.string().min(1),
  title: z.string().min(1),
  description: z.string().min(1),
});

const InfoSectionSchema = z.object({
  title: z.string().min(1),
  content: z.string().min(1),
});

const CtaSectionSchema = z.object({
  headline: z.string().min(1),
  text: z.string().min(1),
  buttonText: z.string().min(1),
  buttonLink: z.string().min(1),
});

const AppStoresSchema = z.object({
  appleStoreLink: z.string(),
  googlePlayLink: z.string(),
});

const TutorialVideoSchema = z.object({
  youtubeVideoId: z.string(),
  title: z.string(),
  description: z.string(),
});

const WattsLightsPageSchema = z.object({
  hero: HeroSchema,
  features: z.array(FeatureSchema),
  infoSection: InfoSectionSchema,
  ctaSection: CtaSectionSchema,
  appStores: AppStoresSchema,
  tutorialVideo: TutorialVideoSchema,
});

export const PUT = withErrorHandling(async (request: Request) => {
  const body = await parseJson(request, WattsLightsPageSchema);
  let page = await prisma.wattslightspage.findFirst();
  if (!page) {
    page = await prisma.wattslightspage.create({ data: body });
  } else {
    page = await prisma.wattslightspage.update({
      where: { id: page.id },
      data: body,
    });
  }
  return ok(page);
});
