import { TraceGraphNodeKind } from "./buildTraceGraphNodes";
import { TraceGraphPhase } from "./traceGraphLayout";

/**
 * Palette aligned with phase/category so kinds in different lanes do not all
 * collapse onto the same chip color (outbound vs pick vs scrap vs fail).
 */
export const kindColor = (
  kind: TraceGraphNodeKind,
):
  | "success"
  | "warning"
  | "info"
  | "secondary"
  | "default"
  | "primary"
  | "error" => {
  switch (kind) {
    case "PURCHASE":
    case "RECEIPT":
    case "MATERIAL_IN":
    case "OPEN":
    case "IN":
      return "success";
    case "QC":
    case "MATERIAL_QC":
      return "info";
    case "FAIL":
      return "error";
    case "MATERIAL_PICK":
    case "PICK_GROUP":
      return "secondary";
    case "PRODUCTION_STEP":
    case "JO_CREATED":
    case "BYPRODUCT":
      return "primary";
    case "SCRAP":
      return "warning";
    case "DEFECT":
    case "EXPIRED":
      return "error";
    case "PUTAWAY":
    case "TRANSFER":
    case "REPACK":
      return "primary";
    case "STOCK_TAKE":
      return "secondary";
    case "ADJUSTMENT":
    case "DEPLETED":
      return "default";
    case "DO_OUT":
    case "DO_GROUP":
    case "REPLENISHMENT_CREATED":
    case "OUT":
      return "warning";
    case "JO_OUT":
    case "PO_OUT":
      return "secondary";
    case "RETURN":
      return "error";
    default:
      return "default";
  }
};

export const kindLabelKey = (kind: TraceGraphNodeKind): string => {
  switch (kind) {
    case "MATERIAL_IN":
      return "nodeMaterialIn";
    case "JO_CREATED":
      return "nodeJoCreated";
    case "MATERIAL_QC":
      return "tabQc";
    case "MATERIAL_PICK":
      return "nodeMaterialPick";
    case "PRODUCTION_STEP":
      return "nodeProductionStep";
    case "BYPRODUCT":
      return "nodeByproduct";
    case "SCRAP":
      return "nodeScrap";
    case "DEFECT":
      return "nodeDefect";
    case "OPEN":
      return "nodeOpen";
    case "FAIL":
      return "nodeFail";
    case "DO_OUT":
      return "nodeDoOut";
    case "REPLENISHMENT_CREATED":
      return "nodeReplenishmentCreated";
    case "JO_OUT":
      return "nodeJoOut";
    case "PO_OUT":
      return "nodePoOut";
    case "DO_GROUP":
      return "nodeDoGroup";
    case "PICK_GROUP":
      return "nodePickGroup";
    case "RETURN":
      return "nodeReturn";
    case "REPACK":
      return "nodeRepack";
    case "PURCHASE":
      return "nodePurchase";
    case "RECEIPT":
      return "nodeReceipt";
    case "PUTAWAY":
      return "nodePutaway";
    case "IN":
      return "directionIn";
    case "OUT":
      return "directionOut";
    case "QC":
      return "tabQc";
    case "STOCK_TAKE":
      return "nodeStockTake";
    case "ADJUSTMENT":
      return "nodeAdjustment";
    case "TRANSFER":
      return "nodeTransfer";
    case "EXPIRED":
      return "nodeExpired";
    case "DEPLETED":
      return "nodeDepleted";
    default:
      return "type";
  }
};

export const phaseLabelKey = (phase: TraceGraphPhase): string => {
  switch (phase) {
    case "MATERIAL_PICK":
      return "phaseMaterialPick";
    case "PRODUCTION":
      return "phaseProduction";
    case "PURCHASE":
      return "phasePurchase";
    case "INBOUND":
      return "phaseInbound";
    case "QC":
      return "phaseQc";
    case "PUTAWAY":
      return "phasePutaway";
    case "WAREHOUSE":
      return "phaseWarehouse";
    case "OUTBOUND":
      return "phaseOutbound";
    case "STOCK_TAKE":
      return "phaseStockTake";
    default:
      return "type";
  }
};

export const phaseAccent = (phase: TraceGraphPhase): string => {
  switch (phase) {
    case "MATERIAL_PICK":
      return "#f9a825";
    case "PRODUCTION":
      return "#6a1b9a";
    case "PURCHASE":
      return "#33691e";
    case "INBOUND":
      return "#2e7d32";
    case "QC":
      return "#0288d1";
    case "PUTAWAY":
      return "#00897b";
    case "WAREHOUSE":
      return "#7b1fa2";
    case "OUTBOUND":
      return "#ed6c02";
    case "STOCK_TAKE":
      return "#5c6bc0";
    default:
      return "#757575";
  }
};

/** MUI Chip color for a phase — matches the node chips typically shown in that lane. */
export const phaseChipColor = (
  phase: TraceGraphPhase,
):
  | "success"
  | "warning"
  | "info"
  | "secondary"
  | "default"
  | "primary"
  | "error" => {
  switch (phase) {
    case "PURCHASE":
    case "INBOUND":
      return "success";
    case "QC":
      return "info";
    case "PUTAWAY":
    case "PRODUCTION":
    case "WAREHOUSE":
      return "primary";
    case "MATERIAL_PICK":
    case "STOCK_TAKE":
      return "secondary";
    case "OUTBOUND":
      return "warning";
    default:
      return "default";
  }
};

export const minimapNodeColor = (kind: TraceGraphNodeKind): string => {
  const map: Record<string, string> = {
    success: "#2e7d32",
    warning: "#ed6c02",
    info: "#0288d1",
    secondary: "#5c6bc0",
    primary: "#7b1fa2",
    default: "#9e9e9e",
    error: "#d32f2f",
  };
  return map[kindColor(kind)] ?? "#9e9e9e";
};
