import type { PaymentEntity, PaymentCreationData, PaymentUpdateData } from '../entities/Payment';
import type { PaymentPort } from '../ports/PaymentPort';
import { PaymentError } from '../services/PaymentService';
import { httpClient } from '../../../lib/http-client';

/**
 * HTTP Adapter implementation for Payment domain
 */
export class PaymentAdapter implements PaymentPort {
  private readonly endpoint = '/payment';

  async getAll(): Promise<PaymentEntity[]> {
    try {
      const response = await httpClient.get<PaymentEntity[]>(this.endpoint);
      return response.data;
    } catch (error) {
      throw new PaymentError('REQUEST_ERROR', `Failed to fetch payment list`);
    }
  }

  async getById(id: string): Promise<PaymentEntity> {
    try {
      const response = await httpClient.get<PaymentEntity>(`${this.endpoint}/${id}`);
      return response.data;
    } catch (error) {
      throw new PaymentError('REQUEST_ERROR', `Failed to fetch payment with id ${id}`);
    }
  }

  async create(data: PaymentCreationData): Promise<PaymentEntity> {
    try {
      const response = await httpClient.post<PaymentEntity>(this.endpoint, data);
      return response.data;
    } catch (error) {
      throw new PaymentError('REQUEST_ERROR', `Failed to create payment`);
    }
  }

  async update(id: string, data: PaymentUpdateData): Promise<PaymentEntity> {
    try {
      const response = await httpClient.put<PaymentEntity>(`${this.endpoint}/${id}`, data);
      return response.data;
    } catch (error) {
      throw new PaymentError('REQUEST_ERROR', `Failed to update payment with id ${id}`);
    }
  }

  async delete(id: string): Promise<void> {
    try {
      await httpClient.delete(`${this.endpoint}/${id}`);
    } catch (error) {
      throw new PaymentError('REQUEST_ERROR', `Failed to delete payment with id ${id}`);
    }
  }
}