import { StatusBadge } from "@/components/ui/badge";
import type {
  SettlementStatus,
  StoreClientStatus,
  StoreUserRole,
  StoreUserStatus,
  WebhookDeliveryStatus,
} from "@/types/store";

type Variant =
  | "default"
  | "primary"
  | "secondary"
  | "outline"
  | "success"
  | "warning"
  | "destructive"
  | "accent"
  | "info";

/**
 * `suspended` is a warning rather than a failure: a pause is meant to end, and drawing it like a
 * rejection would make somebody treat a temporary hold as a closed door.
 */
const STORE_STATUS_VARIANT: Record<StoreClientStatus, Variant> = {
  pending: "warning",
  approved: "success",
  rejected: "destructive",
  suspended: "secondary",
};

export function StoreStatusBadge({
  status,
  label,
}: {
  status: StoreClientStatus;
  label: string;
}) {
  return <StatusBadge variant={STORE_STATUS_VARIANT[status]}>{label}</StatusBadge>;
}

const STORE_USER_STATUS_VARIANT: Record<StoreUserStatus, Variant> = {
  pending: "warning",
  active: "success",
  suspended: "secondary",
};

export function StoreUserStatusBadge({
  status,
  label,
}: {
  status: StoreUserStatus;
  label: string;
}) {
  return <StatusBadge variant={STORE_USER_STATUS_VARIANT[status]}>{label}</StatusBadge>;
}

export function StoreUserRoleBadge({ role, label }: { role: StoreUserRole; label: string }) {
  return <StatusBadge variant={role === "owner" ? "primary" : "outline"}>{label}</StatusBadge>;
}

/**
 * `dropped` is the only one drawn as a failure.
 *
 * `pending` is a warning rather than neutral on purpose: a queue of unsent callbacks is the one
 * failure in this feature with no symptom anywhere else — orders keep flowing, every screen looks
 * healthy, and the store hears nothing.
 */
const WEBHOOK_STATUS_VARIANT: Record<WebhookDeliveryStatus, Variant> = {
  pending: "warning",
  sent: "success",
  failed: "accent",
  dropped: "destructive",
};

export function WebhookStatusBadge({
  status,
  label,
}: {
  status: WebhookDeliveryStatus;
  label: string;
}) {
  return <StatusBadge variant={WEBHOOK_STATUS_VARIANT[status]}>{label}</StatusBadge>;
}

/** A draft is ours and unfinished, so it reads as neutral rather than as a real payment. */
const SETTLEMENT_STATUS_VARIANT: Record<SettlementStatus, Variant> = {
  draft: "outline",
  settled: "success",
  cancelled: "secondary",
};

export function SettlementStatusBadge({
  status,
  label,
}: {
  status: SettlementStatus;
  label: string;
}) {
  return <StatusBadge variant={SETTLEMENT_STATUS_VARIANT[status]}>{label}</StatusBadge>;
}
