// frontend/src/utils/inventoryEnumHelpers.ts
/**
 * Utilities for working with inventory enums and translations
 */

import { useTranslation } from 'react-i18next';
import type { SelectOption } from '../components/ui/Select';
import {
  ProductType,
  ProductionStage,
  UnitOfMeasure,
  IngredientCategory,
  ProductCategory,
  StockMovementType,
  type EnumOption
} from '../api/types/inventory';

/**
 * Generic function to convert enum to select options with i18n translations
 */
export function enumToSelectOptions<T extends Record<string, string | number>>(
  enumObject: T,
  translationKey: string,
  t: (key: string) => string,
  options?: {
    includeDescription?: boolean;
    descriptionKey?: string;
    sortAlphabetically?: boolean;
  }
): SelectOption[] {
  const selectOptions = Object.entries(enumObject).map(([, value]) => ({
    value,
    label: t(`${translationKey}.${value}`),
    ...(options?.includeDescription && options?.descriptionKey && {
      description: t(`${options.descriptionKey}.${value}`)
    })
  }));

  if (options?.sortAlphabetically) {
    selectOptions.sort((a, b) => a.label.localeCompare(b.label));
  }

  return selectOptions;
}

/**
 * Hook for inventory enum utilities
 */
export function useInventoryEnums() {
  const { t } = useTranslation('inventory');

  return {
    // Product Type
    getProductTypeOptions: (): SelectOption[] =>
      enumToSelectOptions(ProductType, 'enums.product_type', t, {
        includeDescription: true,
        descriptionKey: 'descriptions'
      }),

    getProductTypeLabel: (type: ProductType): string =>
      t(`enums.product_type.${type}`),

    // Production Stage
    getProductionStageOptions: (): SelectOption[] =>
      enumToSelectOptions(ProductionStage, 'enums.production_stage', t, {
        includeDescription: true,
        descriptionKey: 'descriptions'
      }),

    getProductionStageLabel: (stage: ProductionStage): string =>
      t(`enums.production_stage.${stage}`),

    // Unit of Measure
    getUnitOfMeasureOptions: (): SelectOption[] =>
      enumToSelectOptions(UnitOfMeasure, 'enums.unit_of_measure', t, {
        includeDescription: true,
        descriptionKey: 'descriptions'
      }),

    getUnitOfMeasureLabel: (unit: UnitOfMeasure): string =>
      t(`enums.unit_of_measure.${unit}`),

    // Ingredient Category
    getIngredientCategoryOptions: (): SelectOption[] =>
      enumToSelectOptions(IngredientCategory, 'enums.ingredient_category', t, {
        includeDescription: true,
        descriptionKey: 'descriptions',
        sortAlphabetically: true
      }),

    getIngredientCategoryLabel: (category: IngredientCategory): string =>
      t(`enums.ingredient_category.${category}`),

    // Product Category
    getProductCategoryOptions: (): SelectOption[] =>
      enumToSelectOptions(ProductCategory, 'enums.product_category', t, {
        sortAlphabetically: true
      }),

    getProductCategoryLabel: (category: ProductCategory): string =>
      t(`enums.product_category.${category}`),

    // Stock Movement Type
    getStockMovementTypeOptions: (): SelectOption[] =>
      enumToSelectOptions(StockMovementType, 'enums.stock_movement_type', t, {
        includeDescription: true,
        descriptionKey: 'descriptions'
      }),

    getStockMovementTypeLabel: (type: StockMovementType): string =>
      t(`enums.stock_movement_type.${type}`),

    // Field Labels
    getFieldLabel: (field: string): string =>
      t(`labels.${field}`),

    getFieldDescription: (field: string): string =>
      t(`descriptions.${field}`)
  };
}

/**
 * Utility to get enum value from select option value
 */
export function getEnumFromValue<T>(
  enumObject: Record<string, T>,
  value: string | number
): T | undefined {
  return Object.values(enumObject).find(enumValue => enumValue === value);
}

/**
 * Utility to validate enum value
 */
export function isValidEnumValue<T>(
  enumObject: Record<string, T>,
  value: unknown
): value is T {
  return Object.values(enumObject).includes(value as T);
}