"use client";

import Image from "next/image";
import { Button } from "@/components/ui/button";
import { useState } from "react";
import { Menu, X, Home, Box, Briefcase, Newspaper } from "lucide-react";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import { Textarea } from "../ui/textarea";
import { RadioGroup, RadioGroupItem } from "../ui/radio-group";
import type { CmsHeroContent } from "@/types/cms-landing";

function isExternalUrl(url: string) {
  return /^https?:\/\//i.test(url);
}

export default function Header({ hero }: { hero?: CmsHeroContent | null }) {
  const [open, setOpen] = useState(false);
  const [contactOpen, setContactOpen] = useState(false);

  // Only main title is required in CMS; optional fields stay empty (no placeholder dashes or fake CTAs).
  const fallbackHeadline = "Beyond Expectations to Build Reputation.";
  const title = hero?.primary_title?.trim()
    ? hero.primary_title.trim()
    : !hero
      ? fallbackHeadline
      : "";
  const subtitle = hero?.secondary_title?.trim();
  const lead = hero?.description?.trim();
  const primaryCta = hero?.primary_cta?.trim();
  const secondaryCta = hero?.secondary_cta_text?.trim();
  const heroImg = hero?.images?.[0]?.image_url?.trim();

  return (
    <>
      <header className="relative w-full bg-white overflow-hidden">
        {/* SIDEBAR */}
        <aside
          className={`fixed right-0 top-0 z-50 h-full w-[280px] bg-[#966314] text-white transition-transform duration-300 ${
            open ? "translate-x-0" : "translate-x-full"
          }`}
        >
          <div className="flex items-center justify-between border-b border-white/20 p-6">
            <div className="flex rounded-full bg-white text-sm font-semibold text-[#966314]">
              <button className="rounded-full bg-white px-3 py-1">ID</button>
              <button className="px-3 py-1">EN</button>
            </div>

            <button onClick={() => setOpen(false)}>
              <X />
            </button>
          </div>

          <nav className="flex flex-col gap-6 p-6 text-sm font-medium">
            <MenuItem icon={<Home size={18} />} label="Home" />
            <MenuItem icon={<Box size={18} />} label="Product" />
            <MenuItem icon={<Briefcase size={18} />} label="Services" />
            <MenuItem
              icon={<Newspaper size={18} />}
              label="News and Services"
            />
          </nav>
        </aside>

        {/* HERO */}
        <div className="container mx-auto flex min-h-[90vh] items-center px-6">
          <div className="flex-1 space-y-6">
            <h1 className="text-4xl font-extrabold leading-tight whitespace-pre-line md:text-6xl">
              {title}
            </h1>
            {subtitle ? (
              <p className="text-lg text-gray-600 md:text-xl">{subtitle}</p>
            ) : null}
            {lead ? (
              <p className="max-w-xl text-sm leading-relaxed text-gray-600 md:text-base whitespace-pre-line">
                {lead}
              </p>
            ) : null}

            {(primaryCta || secondaryCta) ? (
              <div className="flex flex-wrap gap-3">
                {primaryCta ? (
                  <Button
                    size="lg"
                    onClick={() => setContactOpen(true)}
                    className="rounded-full bg-[#966314] px-8 py-6 text-base hover:bg-[#7c520f]"
                  >
                    {primaryCta}
                  </Button>
                ) : null}
                {secondaryCta ? (
                  <Button
                    size="lg"
                    variant="outline"
                    className="rounded-full border-[#966314] px-8 py-6 text-base text-[#966314]"
                    type="button"
                  >
                    {secondaryCta}
                  </Button>
                ) : null}
              </div>
            ) : null}
          </div>

          <div className="relative hidden max-h-[min(90vh,520px)] flex-1 justify-end md:flex">
            {heroImg && isExternalUrl(heroImg) ? (
              // eslint-disable-next-line @next/next/no-img-element
              <img
                src={heroImg}
                alt=""
                width={520}
                height={520}
                className="max-h-[520px] w-auto object-contain"
              />
            ) : (
              <Image
                src={heroImg || "/image/img1.png"}
                alt="Illustration"
                width={520}
                height={520}
                className="object-contain"
              />
            )}
          </div>
        </div>
      </header>

      {/* CONTACT MODAL */}
      {contactOpen && <ContactDialog onClose={() => setContactOpen(false)} />}
    </>
  );
}

function MenuItem({ icon, label }: { icon: React.ReactNode; label: string }) {
  return (
    <div className="flex cursor-pointer items-center justify-between border-b border-white/20 pb-3">
      <span>{label}</span>
      {icon}
    </div>
  );
}

function ContactDialog({ onClose }: { onClose: () => void }) {
  const [contactMethod, setContactMethod] = useState("office");

  return (
    <div className="fixed inset-0 z-[999] bg-black/40 backdrop-blur-sm flex items-end md:items-center justify-center">
      {/* CONTAINER */}
      <div
        className="
        w-full 
        h-[90vh] md:h-auto 
        md:max-w-2xl 
        bg-white 
        rounded-t-3xl md:rounded-2xl 
        p-5 md:p-8 
        shadow-2xl 
        relative 
        overflow-y-auto
      "
      >
        {/* Close Button */}
        <button
          onClick={onClose}
          className="absolute right-4 top-4 md:right-6 md:top-6 text-gray-500 hover:text-black"
        >
          <X size={20} />
        </button>

        {/* Header */}
        <h2 className="text-xl md:text-2xl font-bold text-[#966314] mb-2">
          Contact Us
        </h2>
        <p className="text-sm md:text-base text-gray-500 mb-6">
          Select a contact method and fill in your personal information. We will
          get back to you shortly.
        </p>

        {/* Contact Method */}
        <div className="space-y-3 md:space-y-4 mb-6">
          <RadioGroup
            value={contactMethod}
            onValueChange={setContactMethod}
            className="space-y-3"
          >
            {/* Option 1 */}
            <div
              className={`flex items-start space-x-3 rounded-xl p-4 cursor-pointer border transition ${
                contactMethod === "office" ? "border-[#966314]" : "border-muted"
              }`}
            >
              <RadioGroupItem value="office" id="office" className="mt-1" />
              <div className="space-y-1">
                <Label htmlFor="office" className="font-medium cursor-pointer">
                  Office Presentation
                </Label>
                <p className="text-sm text-muted-foreground">
                  Our team will come to your office for a presentation.
                </p>
              </div>
            </div>

            {/* Option 2 */}
            <div
              className={`flex items-start space-x-3 rounded-xl p-4 cursor-pointer border transition ${
                contactMethod === "hais" ? "border-[#966314]" : "border-muted"
              }`}
            >
              <RadioGroupItem value="hais" id="hais" className="mt-1" />
              <div className="space-y-1">
                <Label htmlFor="hais" className="font-medium cursor-pointer">
                  Via HAIs
                </Label>
                <p className="text-sm text-muted-foreground">
                  Online consultation through HAIs platform.
                </p>
              </div>
            </div>
          </RadioGroup>
        </div>

        {/* Form */}
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          <div className="space-y-2">
            <Label htmlFor="fullName">
              Full Name <span className="text-red-500">*</span>
            </Label>
            <Input id="fullName" placeholder="Enter full name" />
          </div>

          <div className="space-y-2">
            <Label htmlFor="email">
              Email <span className="text-red-500">*</span>
            </Label>
            <Input id="email" type="email" placeholder="email@example.com" />
          </div>

          <div className="space-y-2">
            <Label htmlFor="phone">
              Phone Number <span className="text-red-500">*</span>
            </Label>
            <Input id="phone" placeholder="08xx xxxx xxxx" />
          </div>

          <div className="space-y-2">
            <Label htmlFor="company">
              Company Name <span className="text-red-500">*</span>
            </Label>
            <Input id="company" placeholder="PT. Example Company" />
          </div>
        </div>

        <div className="space-y-2 mt-4">
          <Label htmlFor="message">Message / Requirement</Label>
          <Textarea
            id="message"
            placeholder="Describe your needs or questions..."
            className="min-h-[120px]"
          />
        </div>

        {/* Buttons */}
        <div className="mt-6 flex flex-col-reverse md:flex-row gap-3 md:justify-end">
          <button
            onClick={onClose}
            className="w-full md:w-auto rounded-xl border px-6 py-3 hover:bg-gray-100"
          >
            Cancel
          </button>

          <button className="w-full md:w-auto rounded-xl bg-[#966314] px-6 py-3 text-white hover:bg-[#7c520f]">
            Send Request
          </button>
        </div>
      </div>
    </div>
  );
}
