/**
 * Articles resource – Real HTTP implementation.
 * Endpoints: GET/POST /api/articles, GET/PUT /api/articles/{id},
 *   POST /api/articles/{id}/activate|deactivate,
 *   POST /api/articles/{id}/sales-units, DELETE /api/articles/{id}/sales-units/{suId},
 *   PUT /api/articles/{id}/sales-units/{suId}/price,
 *   POST /api/articles/{id}/suppliers, DELETE /api/articles/{id}/suppliers/{supplierId}
 *
 * DELETE endpoints for sales-units and suppliers return 204 No Content → re-fetch.
 */

import type { AxiosInstance } from 'axios';
import type {
  ArticleDTO,
  SalesUnitDTO,
  CreateArticleRequest,
  UpdateArticleRequest,
  AddSalesUnitRequest,
  UpdateSalesUnitPriceRequest,
} from '@effigenix/types';

export type Unit = 'PIECE_FIXED' | 'KG' | 'HUNDRED_GRAM' | 'PIECE_VARIABLE';
export type PriceModel = 'FIXED' | 'WEIGHT_BASED';
export type ArticleStatus = 'ACTIVE' | 'INACTIVE';

export const UNIT_LABELS: Record<Unit, string> = {
  PIECE_FIXED: 'Stück (fix)',
  KG: 'Kilogramm',
  HUNDRED_GRAM: '100g',
  PIECE_VARIABLE: 'Stück (variabel)',
};

export const PRICE_MODEL_LABELS: Record<PriceModel, string> = {
  FIXED: 'Festpreis',
  WEIGHT_BASED: 'Gewichtsbasiert',
};

export type {
  ArticleDTO,
  SalesUnitDTO,
  CreateArticleRequest,
  UpdateArticleRequest,
  AddSalesUnitRequest,
  UpdateSalesUnitPriceRequest,
};

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

export function createArticlesResource(client: AxiosInstance) {
  return {
    async list(): Promise<ArticleDTO[]> {
      const res = await client.get<ArticleDTO[]>('/api/articles');
      return res.data;
    },

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

    async create(request: CreateArticleRequest): Promise<ArticleDTO> {
      const res = await client.post<ArticleDTO>('/api/articles', request);
      return res.data;
    },

    async update(id: string, request: UpdateArticleRequest): Promise<ArticleDTO> {
      const res = await client.put<ArticleDTO>(`/api/articles/${id}`, request);
      return res.data;
    },

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

    async deactivate(id: string): Promise<ArticleDTO> {
      const res = await client.post<ArticleDTO>(`/api/articles/${id}/deactivate`);
      return res.data;
    },

    async addSalesUnit(id: string, request: AddSalesUnitRequest): Promise<ArticleDTO> {
      const res = await client.post<ArticleDTO>(`/api/articles/${id}/sales-units`, request);
      return res.data;
    },

    // Returns 204 No Content → re-fetch article
    async removeSalesUnit(articleId: string, salesUnitId: string): Promise<ArticleDTO> {
      await client.delete(`/api/articles/${articleId}/sales-units/${salesUnitId}`);
      const res = await client.get<ArticleDTO>(`/api/articles/${articleId}`);
      return res.data;
    },

    async updateSalesUnitPrice(
      articleId: string,
      salesUnitId: string,
      request: UpdateSalesUnitPriceRequest,
    ): Promise<ArticleDTO> {
      const res = await client.put<ArticleDTO>(
        `/api/articles/${articleId}/sales-units/${salesUnitId}/price`,
        request,
      );
      return res.data;
    },

    async assignSupplier(articleId: string, supplierId: string): Promise<ArticleDTO> {
      const res = await client.post<ArticleDTO>(`/api/articles/${articleId}/suppliers`, {
        supplierId,
      });
      return res.data;
    },

    // Returns 204 No Content → re-fetch article
    async removeSupplier(articleId: string, supplierId: string): Promise<ArticleDTO> {
      await client.delete(`/api/articles/${articleId}/suppliers/${supplierId}`);
      const res = await client.get<ArticleDTO>(`/api/articles/${articleId}`);
      return res.data;
    },
  };
}

export type ArticlesResource = ReturnType<typeof createArticlesResource>;
