/** Stocks resource – Inventory BC. */

import type { AxiosInstance } from 'axios';
import type {
  StockDTO,
  StockBatchDTO,
  AddStockBatchRequest,
  CreateStockRequest,
  CreateStockResponse,
  UpdateStockRequest,
  RemoveStockBatchRequest,
  BlockStockBatchRequest,
  ReservationDTO,
  ReserveStockRequest,
} from '@effigenix/types';

export type BatchType = 'PURCHASED' | 'PRODUCED';

export const BATCH_TYPE_LABELS: Record<BatchType, string> = {
  PURCHASED: 'Eingekauft',
  PRODUCED: 'Produziert',
};

export type StockBatchStatus = 'AVAILABLE' | 'EXPIRING_SOON' | 'EXPIRED' | 'BLOCKED';

export const STOCK_BATCH_STATUS_LABELS: Record<StockBatchStatus, string> = {
  AVAILABLE: 'Verfügbar',
  EXPIRING_SOON: 'Bald ablaufend',
  EXPIRED: 'Abgelaufen',
  BLOCKED: 'Gesperrt',
};

export type ReferenceType = 'PRODUCTION_ORDER' | 'SALES_ORDER' | 'TRANSFER';

export const REFERENCE_TYPE_LABELS: Record<ReferenceType, string> = {
  PRODUCTION_ORDER: 'Produktionsauftrag',
  SALES_ORDER: 'Kundenauftrag',
  TRANSFER: 'Umlagerung',
};

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

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

export type {
  StockDTO,
  StockBatchDTO,
  AddStockBatchRequest,
  CreateStockRequest,
  CreateStockResponse,
  UpdateStockRequest,
  RemoveStockBatchRequest,
  BlockStockBatchRequest,
  ReservationDTO,
  ReserveStockRequest,
};

export interface StockFilter {
  storageLocationId?: string;
  articleId?: string;
}

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

const BASE = '/api/inventory/stocks';

export function createStocksResource(client: AxiosInstance) {
  return {
    async list(filter?: StockFilter): Promise<StockDTO[]> {
      const params: Record<string, string> = {};
      if (filter?.storageLocationId) params.storageLocationId = filter.storageLocationId;
      if (filter?.articleId) params.articleId = filter.articleId;
      const res = await client.get<StockDTO[]>(BASE, { params });
      return res.data;
    },

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

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

    async update(id: string, request: UpdateStockRequest): Promise<StockDTO> {
      const res = await client.put<StockDTO>(`${BASE}/${id}`, request);
      return res.data;
    },

    async addBatch(stockId: string, request: AddStockBatchRequest): Promise<StockBatchDTO> {
      const res = await client.post<StockBatchDTO>(`${BASE}/${stockId}/batches`, request);
      return res.data;
    },

    async removeBatch(stockId: string, batchId: string, request: RemoveStockBatchRequest): Promise<void> {
      await client.post(`${BASE}/${stockId}/batches/${batchId}/remove`, request);
    },

    async blockBatch(stockId: string, batchId: string, request: BlockStockBatchRequest): Promise<void> {
      await client.post(`${BASE}/${stockId}/batches/${batchId}/block`, request);
    },

    async unblockBatch(stockId: string, batchId: string): Promise<void> {
      await client.post(`${BASE}/${stockId}/batches/${batchId}/unblock`);
    },

    async reserveStock(stockId: string, request: ReserveStockRequest): Promise<ReservationDTO> {
      const res = await client.post<ReservationDTO>(`${BASE}/${stockId}/reservations`, request);
      return res.data;
    },

    async releaseReservation(stockId: string, reservationId: string): Promise<void> {
      await client.delete(`${BASE}/${stockId}/reservations/${reservationId}`);
    },

    async confirmReservation(stockId: string, reservationId: string): Promise<void> {
      await client.post(`${BASE}/${stockId}/reservations/${reservationId}/confirm`);
    },
  };
}

export type StocksResource = ReturnType<typeof createStocksResource>;
