"use client"

import { Plus, X } from "lucide-react"
import { IconPicker } from "@/components/dashboard/shared/IconPicker"

export interface PackageFeatureItem {
  iconKey: string
  title: string
  description: string
}

export interface ProcedurePackageData {
  eyebrow: string
  title: string
  subtitle: string
  features: PackageFeatureItem[]
  startingPriceLabel: string
  startingPriceValue: string
  buttonText: string
  buttonLink: string
}

interface Props {
  value: ProcedurePackageData
  onChange: (value: ProcedurePackageData) => void
}

const inputClass =
  "w-full bg-card border border-border rounded-xl px-3 py-2.5 text-sm text-foreground font-sans placeholder:text-muted-foreground outline-none transition-colors focus:ring-2 focus:ring-[#2BB673]/30 focus:border-[#2BB673]"

const inputMuted =
  "w-full bg-muted bg-white border border-border rounded-xl px-3 py-2 text-sm text-foreground font-sans placeholder:text-muted-foreground outline-none transition-colors focus:ring-2 focus:ring-[#2BB673]/30 focus:border-[#2BB673]"

export function ProcedurePackage({ value, onChange }: Props) {
  function set<K extends keyof ProcedurePackageData>(key: K, val: ProcedurePackageData[K]) {
    onChange({ ...value, [key]: val })
  }

  function addFeature() {
    set("features", [...value.features, { iconKey: "", title: "", description: "" }])
  }
  function removeFeature(i: number) {
    set("features", value.features.filter((_, idx) => idx !== i))
  }
  function updateFeature(i: number, field: keyof PackageFeatureItem, val: string) {
    set("features", value.features.map((f, idx) => idx === i ? { ...f, [field]: val } : f))
  }

  return (
    <div className="flex flex-col gap-5">
      {/* Section Headings */}
      <div className="flex flex-col gap-3 rounded-xl border border-border bg-muted/30 p-4">
        <div className="flex items-center gap-2 pb-2 border-b border-border">
          <span className="w-1 h-4 rounded-full shrink-0" style={{ background: "var(--brand-green)" }} aria-hidden="true" />
          <p className="text-[11px] font-bold uppercase tracking-wider text-muted-foreground font-sans">Section Headings</p>
        </div>
        <div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Eyebrow</label><input type="text" value={value.eyebrow} onChange={(e) => set("eyebrow", e.target.value)} placeholder="e.g. All Inclusive" className={inputClass} /></div>
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Title</label><input type="text" value={value.title} onChange={(e) => set("title", e.target.value)} placeholder="e.g. What's Included" className={inputClass} /></div>
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Subtitle</label><input type="text" value={value.subtitle} onChange={(e) => set("subtitle", e.target.value)} placeholder="e.g. Everything you need" className={inputClass} /></div>
        </div>
      </div>

      {/* Features */}
      <div className="flex flex-col gap-3">
        <div className="flex items-center justify-between">
          <label className="text-xs font-bold text-foreground font-sans uppercase tracking-wider">Package Features</label>
          <button
            type="button"
            onClick={addFeature}
            className="flex cursor-pointer items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-semibold font-sans transition-all hover:brightness-105"
            style={{ background: "rgba(43,182,115,0.10)", color: "var(--brand-green)" }}
          >
            <Plus className="w-3.5 h-3.5" aria-hidden="true" />
            Add Feature
          </button>
        </div>
        {value.features.length === 0 ? (
          <div className="flex flex-col items-center justify-center gap-2 py-8 rounded-xl border border-dashed" style={{ borderColor: "var(--border)" }}>
            <p className="text-xs text-muted-foreground font-sans">No package features added yet.</p>
            <button type="button" onClick={addFeature} className="text-xs cursor-pointer font-semibold font-sans" style={{ color: "var(--brand-green)" }}>Add first feature</button>
          </div>
        ) : (
          <div className="flex flex-col gap-4">
            {value.features.map((feature, i) => (
              <div key={i} className="rounded-2xl border border-border bg-card overflow-hidden">
                <div className="flex items-center justify-between px-4 py-2.5 border-b border-border" style={{ background: "rgba(43,182,115,0.02)" }}>
                  <span className="text-xs font-bold font-sans px-2 py-0.5 rounded-md" style={{ background: "rgba(43,182,115,0.10)", color: "var(--brand-green)" }}>
                    Feature {i + 1}
                  </span>
                  <button type="button" onClick={() => removeFeature(i)} className="p-1.5 cursor-pointer rounded-lg text-muted-foreground hover:text-red-500 hover:bg-red-50 transition-colors" aria-label={`Remove feature ${i + 1}`}>
                    <X className="w-3.5 h-3.5" aria-hidden="true" />
                  </button>
                </div>
                <div className="p-4 grid grid-cols-1 sm:grid-cols-3 gap-3">
                  <div className="flex flex-col gap-1">
                    <label className="text-[10px] font-bold text-muted-foreground font-sans uppercase tracking-wider">Icon</label>
                    <IconPicker value={feature.iconKey} onChange={(key) => updateFeature(i, "iconKey", key)} />
                  </div>
                  <div className="flex flex-col gap-1">
                    <label className="text-[10px] font-bold text-muted-foreground font-sans uppercase tracking-wider">Title</label>
                    <input type="text" value={feature.title} onChange={(e) => updateFeature(i, "title", e.target.value)} placeholder="e.g. Airport Transfer" className={inputMuted} />
                  </div>
                  <div className="flex flex-col gap-1">
                    <label className="text-[10px] font-bold text-muted-foreground font-sans uppercase tracking-wider">Description</label>
                    <input type="text" value={feature.description} onChange={(e) => updateFeature(i, "description", e.target.value)} placeholder="Brief description..." className={inputMuted} />
                  </div>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>

      {/* Starting Price + Button */}
      <div className="flex flex-col gap-3 rounded-xl border border-border bg-muted/30 p-4">
        <div className="flex items-center gap-2 pb-2 border-b border-border">
          <span className="w-1 h-4 rounded-full shrink-0" style={{ background: "var(--brand-navy)" }} aria-hidden="true" />
          <p className="text-[11px] font-bold uppercase tracking-wider text-muted-foreground font-sans">Pricing & CTA</p>
        </div>
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Starting Price Label</label><input type="text" value={value.startingPriceLabel} onChange={(e) => set("startingPriceLabel", e.target.value)} placeholder="e.g. Starting From" className={inputClass} /></div>
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Starting Price Value</label><input type="text" value={value.startingPriceValue} onChange={(e) => set("startingPriceValue", e.target.value)} placeholder="e.g. $1,500" className={inputClass} /></div>
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Button Text</label><input type="text" value={value.buttonText} onChange={(e) => set("buttonText", e.target.value)} placeholder="e.g. Get a Free Quote" className={inputClass} /></div>
          <div className="flex flex-col gap-1.5"><label className="text-xs font-semibold text-foreground font-sans">Button Link</label><input type="text" value={value.buttonLink} onChange={(e) => set("buttonLink", e.target.value)} placeholder="e.g. /contact" className={inputClass} /></div>
        </div>
      </div>
    </div>
  )
}
