import type { ClientStatus } from "../../entities/client/types";
import type { ServiceStatus } from "../../entities/service/types";
import type { SyncState } from "../../entities/sync/types";
import type { LauncherUserStatus } from "../../entities/user/types";
import { cn } from "../lib/cn";

type BadgeTone = "green" | "yellow" | "red" | "violet" | "muted";

const serviceLabels: Record<ServiceStatus, string> = {
  active: "Активен",
  maintenance: "Техработы",
  hidden: "Скрыт",
  disabled: "Отключён",
};

const clientLabels: Record<ClientStatus, string> = {
  active: "Активен",
  demo: "Demo",
  suspended: "Приостановлен",
  expired: "Истёк",
};

const userLabels: Record<LauncherUserStatus, string> = {
  active: "Активен",
  invited: "Приглашён",
  blocked: "Заблокирован",
};

const syncLabels: Record<SyncState, string> = {
  synced: "Синхронизировано",
  pending: "В очереди",
  error: "Ошибка",
  disabled: "Отключено",
};

export function StatusBadge({
  label,
  tone = "muted",
  className,
}: {
  label: string;
  tone?: BadgeTone;
  className?: string;
}) {
  return <span className={cn("status-badge", `status-badge--${tone}`, className)}>{label}</span>;
}

export function ServiceStatusBadge({ status }: { status: ServiceStatus }) {
  return <StatusBadge label={serviceLabels[status]} tone={statusTone(status)} />;
}

export function ClientStatusBadge({ status }: { status: ClientStatus }) {
  return <StatusBadge label={clientLabels[status]} tone={clientTone(status)} />;
}

export function UserStatusBadge({ status }: { status: LauncherUserStatus }) {
  return <StatusBadge label={userLabels[status]} tone={status === "active" ? "green" : status === "invited" ? "yellow" : "red"} />;
}

export function SyncStatusBadge({ state }: { state: SyncState }) {
  return <StatusBadge label={syncLabels[state]} tone={syncTone(state)} />;
}

function statusTone(status: ServiceStatus): BadgeTone {
  if (status === "active") return "green";
  if (status === "maintenance") return "yellow";
  if (status === "hidden") return "violet";
  return "red";
}

function clientTone(status: ClientStatus): BadgeTone {
  if (status === "active") return "green";
  if (status === "demo") return "yellow";
  return "red";
}

function syncTone(state: SyncState): BadgeTone {
  if (state === "synced") return "green";
  if (state === "pending") return "yellow";
  if (state === "error") return "red";
  return "muted";
}
