"use client"

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

export interface DepartmentItem {
  icon: string
  title: string
  description: string
}

export interface HospitalDepartmentsData {
  eyebrow: string
  title: string
  subtitle: string
  departments: DepartmentItem[]
}

interface Props {
  value: HospitalDepartmentsData
  onChange: (value: HospitalDepartmentsData) => 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 bg-white 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 HospitalDepartmentsSection({ value, onChange }: Props) {
  function set<K extends keyof HospitalDepartmentsData>(key: K, val: HospitalDepartmentsData[K]) {
    onChange({ ...value, [key]: val })
  }

  function updateDepartment(idx: number, field: keyof DepartmentItem, val: string) {
    const updated = value.departments.map((d, i) => (i === idx ? { ...d, [field]: val } : d))
    set("departments", updated)
  }

  function addDepartment() {
    set("departments", [...value.departments, { icon: "", title: "", description: "" }])
  }

  function removeDepartment(idx: number) {
    set("departments", value.departments.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. Our Departments" 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. Specialized Medical Departments" 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. Expert care across all specialties" className={inputClass} />
          </div>
        </div>
      </div>

      {/* Departments list */}
      <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">
            Departments ({value.departments.length})
          </label>
          <button
            type="button"
            onClick={addDepartment}
            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 Department
          </button>
        </div>
        {value.departments.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 departments added yet.</p>
            <button
              type="button"
              onClick={addDepartment}
              className="text-xs cursor-pointer font-semibold font-sans"
              style={{ color: "var(--brand-green)" }}
            >
              Add your first department
            </button>
          </div>
        ) : (
          <div className="flex flex-col gap-3">
            {value.departments.map((dept, 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)" }}
                  >
                    Department {idx + 1}
                  </span>
                  <button
                    type="button"
                    onClick={() => removeDepartment(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 department"
                  >
                    <Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
                  </button>
                </div>
                <div className="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={dept.icon} onChange={(k) => updateDepartment(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">Title</label>
                    <input
                      type="text"
                      value={dept.title}
                      onChange={(e) => updateDepartment(idx, "title", e.target.value)}
                      placeholder="e.g. Cardiology"
                      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={dept.description}
                      onChange={(e) => updateDepartment(idx, "description", e.target.value)}
                      placeholder="Brief description..."
                      className={listFieldClass}
                    />
                  </div>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  )
}
