/**
 * Suppliers resource – Real HTTP implementation.
 * Endpoints: GET/POST /api/suppliers, GET/PUT /api/suppliers/{id},
 *   POST /api/suppliers/{id}/activate|deactivate,
 *   POST /api/suppliers/{id}/rating,
 *   POST /api/suppliers/{id}/certificates,
 *   DELETE /api/suppliers/{id}/certificates (with body)
 *
 * DELETE /api/suppliers/{id}/certificates returns 204 No Content → re-fetch.
 */

import type { AxiosInstance } from 'axios';
import type {
  SupplierDTO,
  AddressDTO,
  ContactInfoDTO,
  PaymentTermsDTO,
  QualityCertificateDTO,
  SupplierRatingDTO,
  CreateSupplierRequest,
  UpdateSupplierRequest,
  RateSupplierRequest,
  AddCertificateRequest,
  RemoveCertificateRequest,
} from '@effigenix/types';

export type SupplierStatus = 'ACTIVE' | 'INACTIVE';

export type {
  SupplierDTO,
  AddressDTO,
  ContactInfoDTO,
  PaymentTermsDTO,
  QualityCertificateDTO,
  SupplierRatingDTO,
  CreateSupplierRequest,
  UpdateSupplierRequest,
  RateSupplierRequest,
  AddCertificateRequest,
  RemoveCertificateRequest,
};

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

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

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

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

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

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

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

    async rate(id: string, request: RateSupplierRequest): Promise<SupplierDTO> {
      const res = await client.post<SupplierDTO>(`/api/suppliers/${id}/rating`, request);
      return res.data;
    },

    async addCertificate(id: string, request: AddCertificateRequest): Promise<SupplierDTO> {
      const res = await client.post<SupplierDTO>(`/api/suppliers/${id}/certificates`, request);
      return res.data;
    },

    // Returns 204 No Content → re-fetch supplier
    async removeCertificate(id: string, request: RemoveCertificateRequest): Promise<SupplierDTO> {
      await client.delete(`/api/suppliers/${id}/certificates`, { data: request });
      const res = await client.get<SupplierDTO>(`/api/suppliers/${id}`);
      return res.data;
    },
  };
}

export type SuppliersResource = ReturnType<typeof createSuppliersResource>;
