"use client"

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

export interface ContactInfoItem {
  label: string
  value: string
  icon: string
}

export interface ContactInfoSectionData {
  items: ContactInfoItem[]
}

interface Props {
  value: ContactInfoSectionData
  onChange: (value: ContactInfoSectionData) => void
  disabled?: boolean
  validation?: {
    items?: boolean
    itemErrors?: Array<{ label?: boolean; value?: boolean; icon?: boolean }>
  }
}

function fieldClass(invalid = false) {
  return `w-full bg-card 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 ${
    invalid
      ? "border-red-400 focus:ring-red-200/70 focus:border-red-500"
      : "border-border focus:ring-[#2BB673]/30 focus:border-[#2BB673]"
  }`
}

function Label({ children }: { children: React.ReactNode }) {
  return (
    <label className="text-xs font-bold text-foreground font-sans uppercase tracking-wider">
      {children}
    </label>
  )
}

export function ContactInfoSection({ value, onChange, disabled = false, validation }: Props) {
  function addItem() {
    if (disabled) return
    onChange({ items: [...value.items, { label: "", value: "", icon: "" }] })
  }

  function removeItem(index: number) {
    onChange({ items: value.items.filter((_, i) => i !== index) })
  }

  function updateItem(index: number, field: keyof ContactInfoItem, val: string) {
    onChange({
      items: value.items.map((item, i) => (i === index ? { ...item, [field]: val } : item)),
    })
  }

  return (
    <div
      className={`flex flex-col gap-3 rounded-xl ${
        validation?.items && value.items.length === 0 ? "border border-red-300 p-3 bg-red-50/40" : ""
      }`}
    >
      <div className="flex items-center justify-between">
        <Label>Contact Info Items</Label>
        <button
          type="button"
          onClick={addItem}
          disabled={disabled}
          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 Item
        </button>
      </div>

      {value.items.length === 0 ? (
        <div className="flex flex-col items-center justify-center gap-2 py-8 rounded-xl border border-dashed border-border">
          <p className="text-xs text-muted-foreground font-sans">No contact info items yet.</p>
          <button
            type="button"
            onClick={addItem}
            disabled={disabled}
            className="text-xs cursor-pointer font-semibold font-sans"
            style={{ color: "var(--brand-green)" }}
          >
            Add your first item
          </button>
        </div>
      ) : (
        <div className="flex flex-col gap-3">
          {value.items.map((item, index) => (
            <div
              key={index}
              className="flex flex-col gap-3 p-4 rounded-xl border border-border bg-muted/40"
            >
              <div className="flex items-center justify-between">
                <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)" }}
                >
                  Item {index + 1}
                </span>
                <button
                  type="button"
                  onClick={() => removeItem(index)}
                  disabled={disabled}
                  className="p-1.5 cursor-pointer rounded-lg text-muted-foreground hover:text-red-500 hover:bg-red-50 transition-colors"
                  aria-label={`Remove item ${index + 1}`}
                >
                  <Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
                </button>
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div className="flex flex-col gap-1.5">
                  <label className="text-xs font-semibold text-muted-foreground font-sans">Label</label>
                  <input
                    type="text"
                    value={item.label}
                    disabled={disabled}
                    onChange={(e) => updateItem(index, "label", e.target.value)}
                    placeholder="e.g. Phone"
                    className={fieldClass(!!validation?.itemErrors?.[index]?.label)}
                  />
                </div>
                <div className="flex flex-col gap-1.5">
                  <label className="text-xs font-semibold text-muted-foreground font-sans">Value</label>
                  <input
                    type="text"
                    value={item.value}
                    disabled={disabled}
                    onChange={(e) => updateItem(index, "value", e.target.value)}
                    placeholder="e.g. +90 212 000 0000"
                    className={fieldClass(!!validation?.itemErrors?.[index]?.value)}
                  />
                </div>
              </div>

              <div className="flex flex-col gap-1.5">
                <label className="text-xs font-semibold text-muted-foreground font-sans">Icon</label>
                <div className={disabled ? "pointer-events-none opacity-60" : ""}>
                  <IconPicker
                    value={item.icon}
                    onChange={(key) => updateItem(index, "icon", key)}
                    invalid={!!validation?.itemErrors?.[index]?.icon}
                  />
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  )
}
