"use client";

import { motion, useReducedMotion } from "framer-motion";

type CodePart = {
  color: string;
  text: string;
};

type CodeLine =
  | { color: string; text: string }
  | { parts: readonly CodePart[] };

const lines: CodeLine[] = [
  { color: "text-[#4ADE80]", text: "// Create a conversation from your app" },
  {
    parts: [
      { color: "text-[#60A5FA]", text: "fetch" },
      { color: "text-[#E8EEFF]", text: "(" },
      {
        color: "text-[#4ADE80]",
        text: "'https://api.chathiro.com/v1/conversations'",
      },
      { color: "text-[#E8EEFF]", text: ", {" },
    ],
  },
  {
    parts: [
      { color: "text-[#C4B5FD]", text: "  method" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#4ADE80]", text: "'POST'" },
      { color: "text-[#E8EEFF]", text: "," },
    ],
  },
  {
    parts: [
      { color: "text-[#C4B5FD]", text: "  headers" },
      { color: "text-[#E8EEFF]", text: ": {" },
    ],
  },
  {
    parts: [
      { color: "text-[#4ADE80]", text: "    'Authorization'" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#4ADE80]", text: "'Bearer YOUR_API_KEY'" },
      { color: "text-[#E8EEFF]", text: "," },
    ],
  },
  {
    parts: [
      { color: "text-[#4ADE80]", text: "    'Content-Type'" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#4ADE80]", text: "'application/json'" },
    ],
  },
  { color: "text-[#E8EEFF]", text: "  }," },
  {
    parts: [
      { color: "text-[#C4B5FD]", text: "  body" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#60A5FA]", text: "JSON" },
      { color: "text-[#E8EEFF]", text: "." },
      { color: "text-[#60A5FA]", text: "stringify" },
      { color: "text-[#E8EEFF]", text: "({" },
    ],
  },
  {
    parts: [
      { color: "text-[#C4B5FD]", text: "    customer" },
      { color: "text-[#E8EEFF]", text: ": { " },
      { color: "text-[#C4B5FD]", text: "email" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#FB923C]", text: "'alex@northstar.io'" },
      { color: "text-[#E8EEFF]", text: " }," },
    ],
  },
  {
    parts: [
      { color: "text-[#C4B5FD]", text: "    message" },
      { color: "text-[#E8EEFF]", text: ": " },
      { color: "text-[#FB923C]", text: "'I need help with my plan.'" },
    ],
  },
  { color: "text-[#E8EEFF]", text: "  })" },
  { color: "text-[#E8EEFF]", text: "});" },
];

const lineContent = (line: CodeLine) =>
  "parts" in line ? line.parts.map((part) => part.text).join("") : line.text;

const CodePreview = () => {
  const reduceMotion = useReducedMotion();

  return (
    <motion.div
      className="relative w-full"
      initial={reduceMotion ? false : { opacity: 0, y: 20 }}
      whileInView={{ opacity: 1, y: 0 }}
      viewport={{ once: true, amount: 0.3 }}
      transition={{ duration: 0.65, ease: [0.22, 1, 0.36, 1] }}
    >
      <div className="relative overflow-hidden rounded-2xl border border-white/10 bg-[#0A1228] sm:rounded-[20px]">
        <div className="flex items-center justify-between px-4 py-3 sm:px-5 sm:py-3.5">
          <div className="flex items-center gap-1.5">
            <span className="size-2 rounded-full bg-[#4B556F] sm:size-2.5" />
            <span className="size-2 rounded-full bg-[#4B556F] sm:size-2.5" />
            <span className="size-2 rounded-full bg-[#4B556F] sm:size-2.5" />
          </div>
          <p className="font-mono text-[11px] tracking-wide text-[#9AA3B8] sm:text-xs">
            POST /v1/conversations
          </p>
        </div>

        <pre className="overflow-x-auto px-5 pb-6 text-[12px] leading-[1.7] sm:px-6 sm:pb-7 sm:text-[14px] sm:leading-[1.75]">
          <code className="font-mono">
            {lines.map((line, index) => (
              <motion.span
                key={`${lineContent(line)}-${index}`}
                className="block whitespace-pre"
                initial={reduceMotion ? false : { opacity: 0, y: 6 }}
                whileInView={{ opacity: 1, y: 0 }}
                viewport={{ once: true }}
                transition={{
                  delay: reduceMotion ? 0 : 0.2 + index * 0.055,
                  duration: 0.35,
                  ease: [0.22, 1, 0.36, 1],
                }}
              >
                {"parts" in line
                  ? line.parts.map((part) => (
                      <span
                        key={`${part.text}-${part.color}`}
                        className={part.color}
                      >
                        {part.text}
                      </span>
                    ))
                  : (
                      <span className={line.color}>{line.text}</span>
                    )}
              </motion.span>
            ))}
          </code>
        </pre>
      </div>
    </motion.div>
  );
};

export default CodePreview;
