/** Production Orders resource – Production BC. */

import type { AxiosInstance } from 'axios';
import type { ProductionOrderDTO, CreateProductionOrderRequest, StartProductionOrderRequest } from '@effigenix/types';

export type Priority = 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';

export const PRIORITY_LABELS: Record<Priority, string> = {
  LOW: 'Niedrig',
  NORMAL: 'Normal',
  HIGH: 'Hoch',
  URGENT: 'Dringend',
};

export type ProductionOrderStatus = 'CREATED' | 'RELEASED' | 'IN_PRODUCTION' | 'COMPLETED' | 'CANCELLED';

export const PRODUCTION_ORDER_STATUS_LABELS: Record<ProductionOrderStatus, string> = {
  CREATED: 'Erstellt',
  RELEASED: 'Freigegeben',
  IN_PRODUCTION: 'In Produktion',
  COMPLETED: 'Abgeschlossen',
  CANCELLED: 'Storniert',
};

export type { ProductionOrderDTO, CreateProductionOrderRequest, StartProductionOrderRequest };

const BASE = '/api/production/production-orders';

export function createProductionOrdersResource(client: AxiosInstance) {
  return {
    async list(): Promise<ProductionOrderDTO[]> {
      const res = await client.get<ProductionOrderDTO[]>(BASE);
      return res.data;
    },

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

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

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

    async start(id: string, request: StartProductionOrderRequest): Promise<ProductionOrderDTO> {
      const res = await client.post<ProductionOrderDTO>(`${BASE}/${id}/start`, request);
      return res.data;
    },
  };
}

export type ProductionOrdersResource = ReturnType<typeof createProductionOrdersResource>;
