"use client"

import { Plus, Trash2 } from "lucide-react"

const SOCIAL_PLATFORMS = [
  "Facebook",
  "Instagram",
  "WhatsApp",
  "Twitter / X",
  "YouTube",
  "LinkedIn",
  "TikTok",
  "Snapchat",
  "Pinterest",
  "Telegram",
]

export interface SocialLink {
  platform: string
  url: string
}

export interface SocialMediaSectionData {
  title: string
  socialLinks: SocialLink[]
}

interface Props {
  value: SocialMediaSectionData
  onChange: (value: SocialMediaSectionData) => void
  disabled?: boolean
  validation?: {
    title?: boolean
    socialLinks?: boolean
    socialLinkErrors?: Array<{ platform?: boolean; url?: 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 SocialMediaSection({ value, onChange, disabled = false, validation }: Props) {
  function set<K extends keyof SocialMediaSectionData>(key: K, val: SocialMediaSectionData[K]) {
    onChange({ ...value, [key]: val })
  }

  function addLink() {
    if (disabled) return
    set("socialLinks", [...value.socialLinks, { platform: "", url: "" }])
  }

  function removeLink(index: number) {
    set("socialLinks", value.socialLinks.filter((_, i) => i !== index))
  }

  function updateLink(index: number, field: keyof SocialLink, val: string) {
    set(
      "socialLinks",
      value.socialLinks.map((link, i) => (i === index ? { ...link, [field]: val } : link))
    )
  }

  return (
    <div className="flex flex-col gap-5">
      {/* Title */}
      <div className="flex flex-col gap-1.5">
        <Label>Title</Label>
        <input
          type="text"
          value={value.title}
          disabled={disabled}
          onChange={(e) => set("title", e.target.value)}
          placeholder="e.g. Follow Us"
          className={fieldClass(!!validation?.title)}
        />
      </div>

      {/* Social Links */}
      <div
        className={`flex flex-col gap-3 rounded-xl ${
          validation?.socialLinks && value.socialLinks.length === 0 ? "border border-red-300 p-3 bg-red-50/40" : ""
        }`}
      >
        <div className="flex items-center justify-between">
          <Label>Social Links</Label>
          <button
            type="button"
            onClick={addLink}
            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 Link
          </button>
        </div>

        {value.socialLinks.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 social links yet.</p>
            <button
              type="button"
              onClick={addLink}
              disabled={disabled}
              className="text-xs cursor-pointer font-semibold font-sans"
              style={{ color: "var(--brand-green)" }}
            >
              Add your first link
            </button>
          </div>
        ) : (
          <div className="flex flex-col gap-3">
            {value.socialLinks.map((link, index) => (
              <div
                key={index}
                className="flex items-center gap-3 p-4 rounded-xl border border-border bg-muted/40"
              >
                <span
                  className="text-xs font-bold font-sans px-2 py-0.5 rounded-md shrink-0"
                  style={{ background: "rgba(29,45,104,0.08)", color: "var(--brand-navy)" }}
                >
                  {index + 1}
                </span>

                <div className="flex-1 grid grid-cols-2 gap-3">
                  {/* Platform select */}
                  <div className="flex flex-col gap-1.5">
                    <label className="text-xs font-semibold text-muted-foreground font-sans">Platform</label>
                    <select
                      value={link.platform}
                      disabled={disabled}
                      onChange={(e) => updateLink(index, "platform", e.target.value)}
                      className={fieldClass(!!validation?.socialLinkErrors?.[index]?.platform)}
                    >
                      <option value="">Select platform...</option>
                      {SOCIAL_PLATFORMS.map((p) => (
                        <option key={p} value={p}>
                          {p}
                        </option>
                      ))}
                    </select>
                  </div>

                  {/* URL */}
                  <div className="flex flex-col gap-1.5">
                    <label className="text-xs font-semibold text-muted-foreground font-sans">URL</label>
                    <input
                      type="url"
                      value={link.url}
                      disabled={disabled}
                      onChange={(e) => updateLink(index, "url", e.target.value)}
                      placeholder="https://..."
                      className={fieldClass(!!validation?.socialLinkErrors?.[index]?.url)}
                    />
                  </div>
                </div>

                <button
                  type="button"
                  onClick={() => removeLink(index)}
                  disabled={disabled}
                  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 link ${index + 1}`}
                >
                  <Trash2 className="w-3.5 h-3.5" aria-hidden="true" />
                </button>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  )
}
