import type { CSSProperties } from "react";

import type { BuiltTrainStatus } from "@/services/trainBuilder.service";

/** Badge color per built-train lifecycle status (Mantine palette keys). */
export const trainStatusColor = (status: BuiltTrainStatus | string): string => {
  switch (status) {
    case "AVAILABLE":
      return "edr-green";
    case "SCHEDULED":
      return "blue";
    case "IN_SERVICE":
      return "teal";
    case "UNDER_MAINTENANCE":
      return "yellow";
    case "OUT_OF_SERVICE":
      return "red";
    case "DEACTIVATED":
      return "gray";
    default:
      return "gray";
  }
};

export const trainStatusLabel = (status: BuiltTrainStatus | string): string =>
  String(status)
    .toLowerCase()
    .replace(/_/g, " ")
    .replace(/^\w/, (c) => c.toUpperCase());

/** Statuses that block a deactivated train from reactivating (mirrors the API gate). */
export const UNFIT_LOCOMOTIVE_STATUSES = new Set(["MAINTENANCE", "OUT_OF_SERVICE", "UNAVAILABLE"]);

/** Badge color per locomotive status (Mantine palette keys). */
export const locomotiveStatusColor = (status: string): string => {
  switch (status) {
    case "AVAILABLE":
    case "IMPORT_READY":
    case "EXPORT_READY":
      return "edr-green";
    case "ASSIGNED":
      return "blue";
    case "MAINTENANCE":
      return "yellow";
    case "OUT_OF_SERVICE":
    case "UNAVAILABLE":
      return "red";
    default:
      return "gray";
  }
};

export const locomotiveStatusLabel = (status: string): string =>
  String(status)
    .toLowerCase()
    .replace(/_/g, " ")
    .replace(/^\w/, (c) => c.toUpperCase());

/** Badge color per trade direction (Mantine palette keys). */
export const directionColor = (direction?: string | null): string =>
  direction === "IMPORT" ? "blue" : direction === "EXPORT" ? "orange" : "gray";

/** Row background tint for a train whose active schedule runs in `direction`. */
export const directionRowStyle = (
  direction?: string | null,
): CSSProperties | undefined =>
  direction === "IMPORT"
    ? { backgroundColor: "var(--mantine-color-blue-0)" }
    : direction === "EXPORT"
      ? { backgroundColor: "var(--mantine-color-orange-0)" }
      : undefined;
