import apiClient from '../apiClient';
import type { SuscriptorDto } from '../../models/dtos/Suscripciones/SuscriptorDto';
import type { CreateSuscriptorDto } from '../../models/dtos/Suscripciones/CreateSuscriptorDto';
import type { UpdateSuscriptorDto } from '../../models/dtos/Suscripciones/UpdateSuscriptorDto';

const API_URL = '/suscriptores';

const getAllSuscriptores = async (nombre?: string, nroDoc?: string, soloActivos: boolean = true): Promise<SuscriptorDto[]> => {
  const params = new URLSearchParams();
  if (nombre) params.append('nombre', nombre);
  if (nroDoc) params.append('nroDoc', nroDoc);
  params.append('soloActivos', String(soloActivos));

  const response = await apiClient.get<SuscriptorDto[]>(`${API_URL}?${params.toString()}`);
  return response.data;
};

const getSuscriptorById = async (id: number): Promise<SuscriptorDto> => {
  const response = await apiClient.get<SuscriptorDto>(`${API_URL}/${id}`);
  return response.data;
};

const createSuscriptor = async (data: CreateSuscriptorDto): Promise<SuscriptorDto> => {
  const response = await apiClient.post<SuscriptorDto>(API_URL, data);
  return response.data;
};

const updateSuscriptor = async (id: number, data: UpdateSuscriptorDto): Promise<void> => {
  await apiClient.put(`${API_URL}/${id}`, data);
};

const deactivateSuscriptor = async (id: number): Promise<void> => {
  await apiClient.delete(`${API_URL}/${id}`);
};

const activateSuscriptor = async (id: number): Promise<void> => {
  await apiClient.post(`${API_URL}/${id}/activar`);
};

export default {
  getAllSuscriptores,
  getSuscriptorById,
  createSuscriptor,
  updateSuscriptor,
  deactivateSuscriptor,
  activateSuscriptor,
};