import type { ReactNode } from "react";
import { cn } from "@/pages/app/components/ui";

export function StatCard({
  icon,
  label,
  value,
  unit,
  tone = "plain",
}: {
  icon: string;
  label: string;
  value: string | number;
  unit?: string;
  tone?: "plain" | "mint" | "yellow";
}) {
  const tones = {
    plain: "bg-background-100 border-background-200",
    mint: "bg-accent-50 border-accent-200",
    yellow: "bg-secondary-50 border-secondary-200",
  } as const;
  const iconTone = {
    plain: "bg-background-200 text-foreground-800",
    mint: "bg-accent-100 text-accent-800",
    yellow: "bg-secondary-100 text-secondary-900",
  } as const;
  return (
    <div className={cn("rounded-[18px] border p-3.5", tones[tone])}>
      <div className={cn("w-9 h-9 rounded-[11px] flex items-center justify-center", iconTone[tone])}>
        <i className={cn(icon, "text-[18px] leading-none")} />
      </div>
      <p className="text-[11px] text-foreground-600 mt-2.5">{label}</p>
      <p className="text-[17px] font-bold text-foreground-950 mt-0.5 leading-tight">
        {typeof value === "number" ? value.toLocaleString("en-US") : value}
        {unit && <span className="text-[11px] font-semibold mr-1">{unit}</span>}
      </p>
    </div>
  );
}

export function Field({
  label,
  children,
  hint,
  required,
}: {
  label: string;
  children: ReactNode;
  hint?: string;
  required?: boolean;
}) {
  return (
    <div>
      <label className="block text-[12.5px] font-semibold text-foreground-900 mb-1.5">
        {label}
        {required && <span className="text-danger-500 mr-1">*</span>}
      </label>
      {children}
      {hint && <p className="text-[11px] text-foreground-500 mt-1.5">{hint}</p>}
    </div>
  );
}

const inputCls =
  "w-full h-[48px] px-4 rounded-[13px] bg-background-100 border border-background-200 text-[13.5px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 transition-colors font-body";

export function TextInput({
  value,
  onChange,
  placeholder,
  type = "text",
  prefix,
  dir,
}: {
  value: string;
  onChange: (v: string) => void;
  placeholder?: string;
  type?: string;
  prefix?: string;
  dir?: "rtl" | "ltr";
}) {
  return (
    <div className="relative flex items-center">
      {prefix && (
        <span className="absolute right-4 text-[13.5px] font-semibold text-foreground-700 pointer-events-none" dir="ltr">
          {prefix}
        </span>
      )}
      <input
        type={type}
        value={value}
        dir={dir}
        onChange={(e) => onChange(e.target.value)}
        placeholder={placeholder}
        className={cn(inputCls, prefix && "pr-[62px]")}
      />
    </div>
  );
}

export function TextArea({
  value,
  onChange,
  placeholder,
  max = 500,
  rows = 4,
}: {
  value: string;
  onChange: (v: string) => void;
  placeholder?: string;
  max?: number;
  rows?: number;
}) {
  return (
    <div>
      <textarea
        value={value}
        onChange={(e) => onChange(e.target.value.slice(0, max))}
        placeholder={placeholder}
        rows={rows}
        className="w-full px-4 py-3 rounded-[13px] bg-background-100 border border-background-200 text-[13.5px] text-foreground-950 placeholder:text-foreground-500 outline-none focus:border-foreground-400 transition-colors font-body resize-none leading-relaxed"
      />
      <p className="text-[10.5px] text-foreground-500 text-left mt-1" dir="ltr">
        {value.length}/{max}
      </p>
    </div>
  );
}

export function Select({
  value,
  onChange,
  options,
  placeholder,
}: {
  value: string;
  onChange: (v: string) => void;
  options: string[];
  placeholder?: string;
}) {
  return (
    <div className="relative">
      <select
        value={value}
        onChange={(e) => onChange(e.target.value)}
        className={cn(inputCls, "appearance-none pl-10 cursor-pointer", !value && "text-foreground-500")}
      >
        {placeholder && <option value="">{placeholder}</option>}
        {options.map((o) => (
          <option key={o} value={o}>
            {o}
          </option>
        ))}
      </select>
      <i className="ri-arrow-down-s-line text-[19px] text-foreground-500 absolute left-3.5 top-1/2 -translate-y-1/2 pointer-events-none leading-none" />
    </div>
  );
}

export function Toggle({
  checked,
  onChange,
  label,
  desc,
}: {
  checked: boolean;
  onChange: (v: boolean) => void;
  label?: string;
  desc?: string;
}) {
  return (
    <div className="flex items-center justify-between gap-3">
      {(label || desc) && (
        <div className="min-w-0">
          {label && <p className="text-[13.5px] font-medium text-foreground-950">{label}</p>}
          {desc && <p className="text-[11.5px] text-foreground-600 mt-0.5">{desc}</p>}
        </div>
      )}
      <button
        type="button"
        onClick={() => onChange(!checked)}
        className={cn(
          "relative w-[48px] h-[28px] rounded-full shrink-0 transition-colors cursor-pointer",
          checked ? "bg-primary-500" : "bg-background-300",
        )}
      >
        <span
          className={cn(
            "absolute top-1 w-[20px] h-[20px] rounded-full bg-background-50 transition-all",
            checked ? "right-1" : "right-[26px]",
          )}
        />
      </button>
    </div>
  );
}

export function UploadBox({
  label,
  hint,
  icon = "ri-upload-cloud-2-line",
  filled,
  onToggle,
}: {
  label: string;
  hint?: string;
  icon?: string;
  filled?: boolean;
  onToggle?: () => void;
}) {
  return (
    <button
      type="button"
      onClick={onToggle}
      className={cn(
        "w-full rounded-[15px] border border-dashed p-4 flex items-center gap-3 cursor-pointer transition-colors text-right",
        filled
          ? "border-accent-300 bg-accent-50"
          : "border-background-300 bg-background-100 hover:border-foreground-400",
      )}
    >
      <div
        className={cn(
          "w-11 h-11 rounded-[13px] flex items-center justify-center shrink-0",
          filled ? "bg-accent-100 text-accent-800" : "bg-background-200 text-foreground-700",
        )}
      >
        <i className={cn(filled ? "ri-check-line" : icon, "text-[20px] leading-none")} />
      </div>
      <div className="min-w-0 flex-1">
        <p className="text-[13px] font-semibold text-foreground-950">{label}</p>
        {hint && <p className="text-[11px] text-foreground-600 mt-0.5">{hint}</p>}
      </div>
      <i className="ri-add-line text-[18px] text-foreground-500 leading-none" />
    </button>
  );
}

export function StepIndicator({ current, total }: { current: number; total: number }) {
  return (
    <div className="flex items-center gap-2">
      <span className="text-[12.5px] font-semibold text-foreground-900">
        الخطوة {current} من {total}
      </span>
      <div className="flex-1 flex gap-1.5">
        {Array.from({ length: total }).map((_, i) => (
          <span
            key={i}
            className={cn(
              "h-1.5 rounded-full flex-1 transition-colors",
              i < current ? "bg-accent-500" : "bg-background-300",
            )}
          />
        ))}
      </div>
    </div>
  );
}

export function KvRow({
  label,
  value,
  strong,
  tone,
}: {
  label: string;
  value: string;
  strong?: boolean;
  tone?: "danger" | "success";
}) {
  return (
    <div className="flex items-center justify-between gap-3">
      <span className="text-[12.5px] text-foreground-600">{label}</span>
      <span
        className={cn(
          "text-[13px]",
          strong ? "font-bold text-foreground-950" : "font-medium text-foreground-900",
          tone === "danger" && "text-danger-600",
          tone === "success" && "text-accent-700",
        )}
      >
        {value}
      </span>
    </div>
  );
}

export function ConfirmModal({
  open,
  title,
  desc,
  confirmLabel = "تأكيد",
  cancelLabel = "إلغاء",
  danger,
  onConfirm,
  onCancel,
}: {
  open: boolean;
  title: string;
  desc?: string;
  confirmLabel?: string;
  cancelLabel?: string;
  danger?: boolean;
  onConfirm: () => void;
  onCancel: () => void;
}) {
  if (!open) return null;
  return (
    <div className="absolute inset-0 z-[60] flex items-center justify-center px-6">
      <div onClick={onCancel} className="absolute inset-0 bg-foreground-950/45" />
      <div className="relative w-full max-w-[320px] rounded-[22px] bg-background-50 p-5 animate-fade-in">
        <h3 className="text-[16px] font-bold text-foreground-950 text-center">{title}</h3>
        {desc && <p className="text-[12.5px] text-foreground-600 text-center mt-2 leading-relaxed">{desc}</p>}
        <div className="mt-5 space-y-2.5">
          <button
            type="button"
            onClick={onConfirm}
            className={cn(
              "w-full h-[48px] rounded-[13px] text-[14px] font-semibold text-background-50 cursor-pointer",
              danger ? "bg-danger-500 hover:bg-danger-600" : "bg-primary-500 hover:bg-primary-600",
            )}
          >
            {confirmLabel}
          </button>
          <button
            type="button"
            onClick={onCancel}
            className="w-full h-[48px] rounded-[13px] text-[14px] font-semibold text-foreground-900 border border-background-300 hover:bg-background-200 cursor-pointer"
          >
            {cancelLabel}
          </button>
        </div>
      </div>
    </div>
  );
}

export function Sheet({
  open,
  onClose,
  children,
}: {
  open: boolean;
  onClose: () => void;
  children: ReactNode;
}) {
  if (!open) return null;
  return (
    <div className="absolute inset-0 z-[60] flex flex-col justify-end">
      <div onClick={onClose} className="absolute inset-0 bg-foreground-950/45" />
      <div className="relative w-full rounded-t-[24px] bg-background-50 p-5 animate-sheet-up max-h-[85%] overflow-y-auto no-scrollbar">
        <div className="w-10 h-1.5 rounded-full bg-background-300 mx-auto mb-4" />
        {children}
      </div>
    </div>
  );
}

export function SuccessBlock({
  icon = "ri-checkbox-circle-fill",
  title,
  desc,
  tone = "mint",
  children,
}: {
  icon?: string;
  title: string;
  desc?: string;
  tone?: "mint" | "danger";
  children?: ReactNode;
}) {
  return (
    <div className="flex flex-col items-center text-center py-10 px-6">
      <div
        className={cn(
          "w-20 h-20 rounded-full flex items-center justify-center mb-5",
          tone === "mint" ? "bg-accent-100" : "bg-danger-100",
        )}
      >
        <i
          className={cn(
            icon,
            "text-[44px] leading-none",
            tone === "mint" ? "text-accent-600" : "text-danger-500",
          )}
        />
      </div>
      <h2 className="text-[20px] font-bold text-foreground-950">{title}</h2>
      {desc && <p className="text-[13px] text-foreground-600 mt-2 max-w-[260px] leading-relaxed">{desc}</p>}
      {children}
    </div>
  );
}