import React from "react";
import styles from "@/styles/modules/ui.module.css";

export type StatusTone =
  | "success"
  | "warning"
  | "info"
  | "danger"
  | "neutral"
  | "flagged";

type StatusMeta = { tone: StatusTone; label: string };

const TONE_CLASS: Record<StatusTone, string> = {
  success: styles.tone_success,
  warning: styles.tone_warning,
  info: styles.tone_info,
  danger: styles.tone_danger,
  neutral: styles.tone_neutral,
  flagged: styles.tone_flagged,
};

const STATUS_MAP: Record<string, StatusMeta> = {
  // Assignments
  completed: { tone: "success", label: "Completed" },
  pending: { tone: "warning", label: "Pending" },
  upcoming: { tone: "info", label: "Upcoming" },
  "in-progress": { tone: "warning", label: "In Progress" },
  cancelled: { tone: "danger", label: "Cancelled" },
  // Bids
  accepted: { tone: "success", label: "Accepted" },
  rejected: { tone: "danger", label: "Rejected" },
  // Payments
  COMPLETED: { tone: "info", label: "Completed" },
  CREATED: { tone: "neutral", label: "Created" },
  APPROVED: { tone: "success", label: "Approved" },
  DISBURSEMENT_REQUIRED: { tone: "warning", label: "Disbursement Required" },
  EXPIRED: { tone: "neutral", label: "Expired" },
  CANCELLED: { tone: "danger", label: "Cancelled" },
  // Promo (display values)
  unused: { tone: "info", label: "Unused" },
  applied: { tone: "success", label: "Applied" },
  Used: { tone: "success", label: "Used" },
  Expired: { tone: "neutral", label: "Expired" },
  active: { tone: "success", label: "Active" },
  inactive: { tone: "neutral", label: "Inactive" },
  // User roles
  guest: { tone: "neutral", label: "Guest" },
  client: { tone: "info", label: "Client" },
  assistant: { tone: "success", label: "Assistant" },
  waiter: { tone: "success", label: "Assistant" },
  both: { tone: "flagged", label: "Both" },
  admin: { tone: "warning", label: "Admin" },
  // Booleans
  true: { tone: "success", label: "True" },
  false: { tone: "danger", label: "False" },
  // Flag
  flagged: { tone: "flagged", label: "Flagged" },
};

function resolveStatus(status?: string | boolean | null): StatusMeta | null {
  if (status === true) return STATUS_MAP.true;
  if (status === false) return STATUS_MAP.false;
  if (status == null || status === "") return null;

  const raw = String(status);
  if (STATUS_MAP[raw]) return STATUS_MAP[raw];

  const lower = raw.toLowerCase();
  if (STATUS_MAP[lower]) return STATUS_MAP[lower];

  return {
    tone: "neutral",
    label: raw.replace(/[_-]/g, " "),
  };
}

export interface StatusBadgeProps {
  status?: string | boolean | null;
  label?: string;
  tone?: StatusTone;
  className?: string;
}

const StatusBadge: React.FC<StatusBadgeProps> = ({
  status,
  label,
  tone,
  className,
}) => {
  const mapped = resolveStatus(status);
  const resolvedTone = tone ?? mapped?.tone ?? "neutral";
  const resolvedLabel = label ?? mapped?.label ?? "—";

  return (
    <span
      className={[styles.statusBadge, TONE_CLASS[resolvedTone], className]
        .filter(Boolean)
        .join(" ")}
    >
      {resolvedLabel}
    </span>
  );
};

export default StatusBadge;
