import {
  getTrainingCategoryLabel,
  getTrainingStatusLabel,
  getTrainingTypeLabel,
} from "@/constants/thai-labels";

export const uiTrainingTypeValues = [
  "INTERNAL_TRAINING",
  "EXTERNAL_TRAINING",
  "ONLINE_COURSE",
  "OJT",
] as const;

export type UiTrainingType = (typeof uiTrainingTypeValues)[number];

export type LegacyTrainingType = "internal" | "external" | "online" | "onsite";

export type AnyTrainingType = UiTrainingType | LegacyTrainingType;

export const legacyTrainingTypeValues = [
  "internal",
  "external",
  "online",
  "onsite",
] as const;

export const approvalStatusValues = [
  "draft",
  "pending",
  "approved",
  "rejected",
  "needs_revision",
] as const;

export const uiTrainingTypeToLegacyMap: Record<
  UiTrainingType,
  LegacyTrainingType
> = {
  INTERNAL_TRAINING: "internal",
  EXTERNAL_TRAINING: "external",
  ONLINE_COURSE: "online",
  OJT: "onsite",
};

export const legacyTrainingTypeToUiMap: Record<
  LegacyTrainingType,
  UiTrainingType
> = {
  internal: "INTERNAL_TRAINING",
  external: "EXTERNAL_TRAINING",
  online: "ONLINE_COURSE",
  onsite: "OJT",
};

export function normalizeTrainingTypeForUi(
  type: string | null | undefined,
): UiTrainingType {
  if (!type) return "INTERNAL_TRAINING";

  if (type in uiTrainingTypeToLegacyMap) {
    return type as UiTrainingType;
  }

  if (type in legacyTrainingTypeToUiMap) {
    return legacyTrainingTypeToUiMap[type as LegacyTrainingType];
  }

  return "INTERNAL_TRAINING";
}

export function normalizeTrainingTypeForDb(
  type: UiTrainingType | LegacyTrainingType | string | null | undefined,
): LegacyTrainingType {
  if (!type) return "internal";

  if (type in uiTrainingTypeToLegacyMap) {
    return uiTrainingTypeToLegacyMap[type as UiTrainingType];
  }

  if (type in legacyTrainingTypeToUiMap) {
    return type as LegacyTrainingType;
  }

  return "internal";
}

export const trainingTypeOptions = uiTrainingTypeValues.map((value) => ({
  value,
  label: getTrainingTypeLabel(value),
}));

export const trainingCategoryOptions = [
  { value: "K", label: getTrainingCategoryLabel("K") },
  { value: "S", label: getTrainingCategoryLabel("S") },
  { value: "A", label: getTrainingCategoryLabel("A") },
];

export const trainingTypeLabelMap = Object.fromEntries(
  uiTrainingTypeValues.map((value) => [value, getTrainingTypeLabel(value)]),
) as Record<UiTrainingType, string>;

export const trainingTypeFilterOptions = trainingTypeOptions;

export const approvalStatusFilterOptions = [
  { value: "draft", label: getTrainingStatusLabel("draft") },
  { value: "pending", label: getTrainingStatusLabel("pending") },
  { value: "approved", label: getTrainingStatusLabel("approved") },
  { value: "rejected", label: getTrainingStatusLabel("rejected") },
  { value: "needs_revision", label: getTrainingStatusLabel("needs_revision") },
];

export const trainingRecordStatusLabelMap = {
  draft: getTrainingStatusLabel("draft"),
  pending: getTrainingStatusLabel("pending"),
  approved: getTrainingStatusLabel("approved"),
  rejected: getTrainingStatusLabel("rejected"),
  needs_revision: getTrainingStatusLabel("needs_revision"),
} as const;
