/** Recipes resource – Production BC. */

import type { AxiosInstance } from 'axios';
import type {
  RecipeDTO,
  RecipeSummaryDTO,
  IngredientDTO,
  ProductionStepDTO,
  CreateRecipeRequest,
  AddRecipeIngredientRequest,
  AddProductionStepRequest,
} from '@effigenix/types';

export type RecipeType = 'RAW_MATERIAL' | 'INTERMEDIATE' | 'FINISHED_PRODUCT';
export type RecipeStatus = 'DRAFT' | 'ACTIVE' | 'ARCHIVED';

export const RECIPE_TYPE_LABELS: Record<RecipeType, string> = {
  RAW_MATERIAL: 'Rohstoff',
  INTERMEDIATE: 'Halbfabrikat',
  FINISHED_PRODUCT: 'Fertigprodukt',
};

export type UoM = 'KILOGRAM' | 'GRAM' | 'LITER' | 'MILLILITER' | 'PIECE' | 'METER';
export const UOM_VALUES: UoM[] = ['KILOGRAM', 'GRAM', 'LITER', 'MILLILITER', 'PIECE', 'METER'];
export const UOM_LABELS: Record<UoM, string> = {
  KILOGRAM: 'Kilogramm (kg)',
  GRAM: 'Gramm (g)',
  LITER: 'Liter (L)',
  MILLILITER: 'Milliliter (mL)',
  PIECE: 'Stück (pc)',
  METER: 'Meter (m)',
};

export type {
  RecipeDTO,
  RecipeSummaryDTO,
  IngredientDTO,
  ProductionStepDTO,
  CreateRecipeRequest,
  AddRecipeIngredientRequest,
  AddProductionStepRequest,
};

// ── Resource factory ─────────────────────────────────────────────────────────

const BASE = '/api/recipes';

export function createRecipesResource(client: AxiosInstance) {
  return {
    async list(status?: RecipeStatus): Promise<RecipeSummaryDTO[]> {
      const params: Record<string, string> = {};
      if (status) params['status'] = status;
      const res = await client.get<RecipeSummaryDTO[]>(BASE, { params });
      return res.data;
    },

    async getById(id: string): Promise<RecipeDTO> {
      const res = await client.get<RecipeDTO>(`${BASE}/${id}`);
      return res.data;
    },

    async create(request: CreateRecipeRequest): Promise<RecipeDTO> {
      const res = await client.post<RecipeDTO>(BASE, request);
      return res.data;
    },

    async addIngredient(id: string, request: AddRecipeIngredientRequest): Promise<RecipeDTO> {
      const res = await client.post<RecipeDTO>(`${BASE}/${id}/ingredients`, request);
      return res.data;
    },

    async removeIngredient(recipeId: string, ingredientId: string): Promise<void> {
      await client.delete(`${BASE}/${recipeId}/ingredients/${ingredientId}`);
    },

    async addProductionStep(id: string, request: AddProductionStepRequest): Promise<RecipeDTO> {
      const res = await client.post<RecipeDTO>(`${BASE}/${id}/steps`, request);
      return res.data;
    },

    async removeProductionStep(id: string, stepNumber: number): Promise<void> {
      await client.delete(`${BASE}/${id}/steps/${stepNumber}`);
    },

    async activateRecipe(id: string): Promise<RecipeDTO> {
      const res = await client.post<RecipeDTO>(`${BASE}/${id}/activate`);
      return res.data;
    },

    async archiveRecipe(id: string): Promise<RecipeDTO> {
      const res = await client.post<RecipeDTO>(`${BASE}/${id}/archive`);
      return res.data;
    },
  };
}

export type RecipesResource = ReturnType<typeof createRecipesResource>;
