import { ReactNode, InputHTMLAttributes, TextareaHTMLAttributes, SelectHTMLAttributes } from "react";

export function SectionCard({
  title,
  description,
  children,
  actions,
}: {
  title: string;
  description?: string;
  children: ReactNode;
  actions?: ReactNode;
}) {
  return (
    <div className="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
      <div className="flex items-start justify-between gap-4 p-5 md:p-6 border-b border-slate-100">
        <div>
          <h3 className="text-lg md:text-xl font-bold text-slate-900">{title}</h3>
          {description && <p className="text-sm text-slate-500 mt-1">{description}</p>}
        </div>
        {actions && <div className="shrink-0">{actions}</div>}
      </div>
      <div className="p-5 md:p-6 space-y-5">{children}</div>
    </div>
  );
}

export function PageHeader({
  title,
  description,
  icon,
}: {
  title: string;
  description?: string;
  icon?: ReactNode;
}) {
  return (
    <div className="mb-6 flex items-center gap-4">
      {icon && (
        <div className="w-12 h-12 rounded-2xl bg-[#0b6e3e] text-white flex items-center justify-center shadow">
          {icon}
        </div>
      )}
      <div>
        <h1 className="text-2xl md:text-3xl font-extrabold text-slate-900">{title}</h1>
        {description && <p className="text-slate-500 text-sm md:text-base mt-1">{description}</p>}
      </div>
    </div>
  );
}

export function Field({
  label,
  hint,
  children,
  required,
}: {
  label: string;
  hint?: string;
  children: ReactNode;
  required?: boolean;
}) {
  return (
    <label className="block">
      <div className="flex items-center justify-between mb-1.5">
        <span className="text-sm font-semibold text-slate-800">
          {label} {required && <span className="text-red-500">*</span>}
        </span>
        {hint && <span className="text-[11px] text-slate-400">{hint}</span>}
      </div>
      {children}
    </label>
  );
}

export function TextInput(props: InputHTMLAttributes<HTMLInputElement>) {
  return (
    <input
      {...props}
      className={`w-full px-3 py-2.5 rounded-lg border border-slate-300 bg-white text-slate-900 text-sm focus:outline-none focus:ring-2 focus:ring-[#0b6e3e] focus:border-transparent transition ${props.className ?? ""}`}
    />
  );
}

export function TextArea(props: TextareaHTMLAttributes<HTMLTextAreaElement>) {
  return (
    <textarea
      {...props}
      className={`w-full px-3 py-2.5 rounded-lg border border-slate-300 bg-white text-slate-900 text-sm focus:outline-none focus:ring-2 focus:ring-[#0b6e3e] focus:border-transparent transition ${props.className ?? ""}`}
    />
  );
}

export function Select(props: SelectHTMLAttributes<HTMLSelectElement>) {
  return (
    <select
      {...props}
      className={`w-full px-3 py-2.5 rounded-lg border border-slate-300 bg-white text-slate-900 text-sm focus:outline-none focus:ring-2 focus:ring-[#0b6e3e] focus:border-transparent transition ${props.className ?? ""}`}
    />
  );
}

export function Toggle({
  checked,
  onChange,
  label,
  description,
}: {
  checked: boolean;
  onChange: (v: boolean) => void;
  label: string;
  description?: string;
}) {
  return (
    <div className="flex items-start justify-between gap-4 py-3 border-b border-slate-100 last:border-0">
      <div className="min-w-0">
        <div className="text-sm font-semibold text-slate-800">{label}</div>
        {description && <div className="text-xs text-slate-500 mt-0.5">{description}</div>}
      </div>
      <button
        type="button"
        onClick={() => onChange(!checked)}
        role="switch"
        aria-checked={checked}
        className={`relative w-11 h-6 rounded-full transition shrink-0 ${checked ? "bg-[#0b6e3e]" : "bg-slate-300"}`}
      >
        <span
          className={`absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white shadow transition-transform ${checked ? "translate-x-5" : ""}`}
        />
      </button>
    </div>
  );
}

export function ColorPicker({
  value,
  onChange,
  label,
}: {
  value: string;
  onChange: (v: string) => void;
  label: string;
}) {
  return (
    <div>
      <div className="text-sm font-semibold text-slate-800 mb-1.5">{label}</div>
      <div className="flex items-center gap-2">
        <input
          type="color"
          value={value}
          onChange={(e) => onChange(e.target.value)}
          className="w-12 h-10 rounded-lg border border-slate-300 cursor-pointer bg-white"
        />
        <input
          type="text"
          value={value}
          onChange={(e) => onChange(e.target.value)}
          className="flex-1 px-3 py-2 rounded-lg border border-slate-300 bg-white text-slate-900 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-[#0b6e3e]"
        />
      </div>
    </div>
  );
}

export function SaveBar({
  onSave,
  onReset,
  saving,
}: {
  onSave: () => void;
  onReset?: () => void;
  saving?: boolean;
}) {
  return (
    <div className="sticky bottom-0 -mx-4 md:mx-0 mt-6 bg-white/95 backdrop-blur border-t md:border md:rounded-2xl border-slate-200 shadow-lg px-4 md:px-6 py-4 flex items-center justify-between gap-3 z-10">
      <div className="text-xs text-slate-500 hidden sm:block">
        Unsaved changes are kept locally until you save.
      </div>
      <div className="flex gap-2 ml-auto">
        {onReset && (
          <button
            onClick={onReset}
            className="px-4 py-2 rounded-lg border border-slate-300 text-slate-700 text-sm font-semibold hover:bg-slate-50 transition"
          >
            Reset
          </button>
        )}
        <button
          onClick={onSave}
          disabled={saving}
          className="px-5 py-2 rounded-lg bg-[#0b6e3e] text-white text-sm font-bold hover:bg-[#0a8048] transition disabled:opacity-60"
        >
          {saving ? "Saving…" : "Save Changes"}
        </button>
      </div>
    </div>
  );
}

export function Badge({
  children,
  tone = "slate",
}: {
  children: ReactNode;
  tone?: "slate" | "green" | "yellow" | "red" | "blue";
}) {
  const tones: Record<string, string> = {
    slate: "bg-slate-100 text-slate-700 border-slate-200",
    green: "bg-emerald-50 text-emerald-700 border-emerald-200",
    yellow: "bg-amber-50 text-amber-700 border-amber-200",
    red: "bg-red-50 text-red-700 border-red-200",
    blue: "bg-blue-50 text-blue-700 border-blue-200",
  };
  return (
    <span className={`inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[11px] font-semibold border ${tones[tone]}`}>
      {children}
    </span>
  );
}

export function useLocalState<T>(key: string, initial: T): [T, (v: T) => void, () => void] {
  // Small helper to persist CMS state locally (mock backend)
  const read = (): T => {
    try {
      const raw = localStorage.getItem(key);
      return raw ? (JSON.parse(raw) as T) : initial;
    } catch {
      return initial;
    }
  };
  const state = read();
  const set = (v: T) => {
    localStorage.setItem(key, JSON.stringify(v));
  };
  const reset = () => {
    localStorage.removeItem(key);
  };
  return [state, set, reset];
}