"use client"

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

export interface AboutStat {
  icon: string
  label: string
  value: string
  description: string
}

export interface HospitalAboutData {
  eyebrow: string
  title: string
  description: string
  aboutStats: AboutStat[]
}

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

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

const listFieldClass =
  "w-full bg-muted 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 HospitalAboutSection({ value, onChange }: Props) {
  function set<K extends keyof HospitalAboutData>(key: K, val: HospitalAboutData[K]) {
    onChange({ ...value, [key]: val })
  }

  function updateStat(idx: number, field: keyof AboutStat, val: string) {
    const updated = value.aboutStats.map((s, i) => (i === idx ? { ...s, [field]: val } : s))
    set("aboutStats", updated)
  }

  function addStat() {
    set("aboutStats", [...value.aboutStats, { icon: "", label: "", value: "", description: "" }])
  }

  function removeStat(idx: number) {
    set("aboutStats", value.aboutStats.filter((_, i) => i !== idx))
  }

  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. About Us" 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. A Legacy of Excellence" className={inputClass} />
          </div>
          <div className="flex flex-col gap-1.5">
            <label className="text-xs font-semibold text-foreground font-sans">Description</label>
            <input type="text" value={value.description} onChange={(e) => set("description", e.target.value)} placeholder="Brief description..." className={inputClass} />
          </div>
        </div>
      </div>

      {/* About Stats */}
      <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">
            About Stats ({value.aboutStats.length})
          </label>
          <button
            type="button"
            onClick={addStat}
            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 active:scale-[0.98]"
            style={{ background: "rgba(43,182,115,0.10)", color: "var(--brand-green)" }}
          >
            <Plus className="w-3.5 h-3.5" aria-hidden="true" />
            Add Stat
          </button>
        </div>
        {value.aboutStats.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 about stats added yet.</p>
            <button
              type="button"
              onClick={addStat}
              className="text-xs cursor-pointer font-semibold font-sans"
              style={{ color: "var(--brand-green)" }}
            >
              Add your first stat
            </button>
          </div>
        ) : (
          <div className="flex flex-col gap-3">
            {value.aboutStats.map((stat, idx) => (
              <div key={idx} className="flex flex-col gap-2 p-4 rounded-xl border border-border bg-card">
                <div className="flex items-center justify-between gap-2">
                  <span
                    className="text-xs font-bold font-sans px-2 py-0.5 rounded-md"
                    style={{ background: "rgba(29,45,104,0.08)", color: "var(--brand-navy)" }}
                  >
                    Stat {idx + 1}
                  </span>
                  <button
                    type="button"
                    onClick={() => removeStat(idx)}
                    className="p-1.5 cursor-pointer rounded-lg text-muted-foreground hover:text-red-500 hover:bg-red-50 transition-colors shrink-0"
                    aria-label="Remove stat"
                  >
                    <Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
                  </button>
                </div>
                <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 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={stat.icon} onChange={(k) => updateStat(idx, "icon", k)} />
                  </div>
                  <div className="flex flex-col gap-1">
                    <label className="text-[10px] font-bold text-muted-foreground font-sans uppercase tracking-wider">Label</label>
                    <input
                      type="text"
                      value={stat.label}
                      onChange={(e) => updateStat(idx, "label", e.target.value)}
                      placeholder="e.g. Years of Experience"
                      className={listFieldClass}
                    />
                  </div>
                  <div className="flex flex-col gap-1">
                    <label className="text-[10px] font-bold text-muted-foreground font-sans uppercase tracking-wider">Value</label>
                    <input
                      type="text"
                      value={stat.value}
                      onChange={(e) => updateStat(idx, "value", e.target.value)}
                      placeholder="e.g. 30+"
                      className={listFieldClass}
                    />
                  </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={stat.description}
                      onChange={(e) => updateStat(idx, "description", e.target.value)}
                      placeholder="e.g. Serving patients since 1994"
                      className={listFieldClass}
                    />
                  </div>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  )
}
