import { apiClient } from '../api/api-client';
import type { CommunicationRepository } from '@/domain/repositories/communication.repository';
import type { Contact } from '@/domain/entities/contact';
import type { EscalationPolicy } from '@/domain/entities/escalation-policy';

export const apiCommunicationRepository: CommunicationRepository = {
  async listContacts(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    return apiClient<Contact[]>(`/api/v1/comm/contacts${query}`);
  },

  async getContactById(id) {
    return apiClient<Contact>(`/api/v1/comm/contacts/${id}`);
  },

  async createContact(contact) {
    return apiClient<Contact>('/api/v1/comm/contacts', {
      method: 'POST',
      body: contact,
    });
  },

  async updateContact(id, data) {
    return apiClient<Contact>(`/api/v1/comm/contacts/${id}`, {
      method: 'PUT',
      body: data,
    });
  },

  async removeContact(id) {
    await apiClient(`/api/v1/comm/contacts/${id}`, {
      method: 'DELETE',
    });
  },

  async listEscalationPolicies(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    return apiClient<EscalationPolicy[]>(`/api/v1/comm/escalation-policies${query}`);
  },

  async getEscalationPolicyById(id) {
    return apiClient<EscalationPolicy>(`/api/v1/comm/escalation-policies/${id}`);
  },

  async createEscalationPolicy(policy) {
    return apiClient<EscalationPolicy>('/api/v1/comm/escalation-policies', {
      method: 'POST',
      body: policy,
    });
  },

  async updateEscalationPolicy(id, data) {
    return apiClient<EscalationPolicy>(`/api/v1/comm/escalation-policies/${id}`, {
      method: 'PUT',
      body: data,
    });
  },

  async removeEscalationPolicy(id) {
    await apiClient(`/api/v1/comm/escalation-policies/${id}`, {
      method: 'DELETE',
    });
  },

  async sendTestMessage(channelType, contactId) {
    await apiClient('/api/v1/comm/test-message', {
      method: 'POST',
      body: { channelType, contactId },
    });
  },
};
