import { Freight } from "@edr/types";

const { TrainScheduleStatus } = Freight;

export function isArrived(
  status?: Freight.TrainScheduleStatus | null,
): boolean {
  return status === TrainScheduleStatus.Arrived;
}

export function isDispatched(
  status?: Freight.TrainScheduleStatus | null,
): boolean {
  return status === TrainScheduleStatus.Dispatched;
}

/** Human label for the schedule status, from the rider's point of view. */
export function shipmentStatusLabel(
  status?: Freight.TrainScheduleStatus | null,
  currentSequenceNo = -1,
): string {
  switch (status) {
    case TrainScheduleStatus.Arrived:
      return "Arrived";
    case TrainScheduleStatus.Dispatched:
      return currentSequenceNo <= 0 ? "Departed" : "In transit";
    case TrainScheduleStatus.Scheduled:
      return "Scheduled";
    case TrainScheduleStatus.Cancelled:
      return "Cancelled";
    case TrainScheduleStatus.Draft:
      return "Preparing";
    default:
      return "Not dispatched";
  }
}

/**
 * 0–100 progress across the corridor, derived from how many stations the train
 * has reached. Arrived → 100. Not departed → 0.
 */
export function corridorProgress(
  stationCount: number,
  currentSequenceNo: number,
  arrived: boolean,
): number {
  if (arrived) return 100;
  if (stationCount <= 1 || currentSequenceNo < 0) return 0;
  const lastSeq = stationCount - 1;
  return Math.round((Math.min(currentSequenceNo, lastSeq) / lastSeq) * 100);
}

/** Caption for a checkpoint kind. */
export function checkpointKindLabel(kind: Freight.TrainCheckpointKind): string {
  switch (kind) {
    case Freight.TrainCheckpointKind.Departed:
      return "Departed";
    case Freight.TrainCheckpointKind.Arrived:
      return "Arrived";
    case Freight.TrainCheckpointKind.Passed:
    default:
      return "Passed";
  }
}
