import { apiClient } from './client';

export interface SubscriptionAddon {
  id: string;
  codename: string;
  nombre: string;
  precio: number;
  quantity: number;
  contribuyenteId: string | null;
  status: string;
  currentPeriodStart: string | null;
  currentPeriodEnd: string | null;
}

export interface AddonsResponse {
  subscription: { id: string; plan: string; status: string } | null;
  addons: SubscriptionAddon[];
}

export async function listMyAddons(contribuyenteId?: string): Promise<AddonsResponse> {
  const params = contribuyenteId ? { contribuyenteId } : undefined;
  const { data } = await apiClient.get<AddonsResponse>('/subscriptions/me/addons', { params });
  return data;
}

export async function subscribeAddon(params: {
  addonCodename: string;
  quantity?: number;
  contribuyenteId?: string | null;
}): Promise<{ addon: SubscriptionAddon; paymentUrl: string }> {
  const { data } = await apiClient.post('/subscriptions/me/addons', params);
  return data;
}

export async function cancelAddon(addonId: string): Promise<void> {
  await apiClient.delete(`/subscriptions/me/addons/${addonId}`);
}
