import { apiClient } from './client';

export interface ClientInvitation {
  id: string;
  email: string;
  invitedBy: string;
  nombreDespacho: string | null;
  rfc: string | null;
  status: string;
  token: string;
  sentAt: string;
  expiresAt: string;
  acceptedAt: string | null;
  createdAt: string;
}

export async function createInvitation(data: {
  email: string;
  nombreDespacho?: string;
  rfc?: string;
}): Promise<{ message: string; invitation: ClientInvitation }> {
  const res = await apiClient.post('/invitations/client', data);
  return res.data;
}

export async function validateInvitationToken(token: string): Promise<{
  email: string;
  nombreDespacho: string | null;
  rfc: string | null;
  expiresAt: string;
}> {
  const res = await apiClient.get(`/invitations/client/validate/${token}`);
  return res.data;
}

export async function registerFromInvitation(
  token: string,
  data: {
    nombre: string;
    password: string;
    nombreDespacho: string;
    rfc: string;
    verticalProfile: 'CONTABLE' | 'JURIDICO' | 'ARQUITECTURA';
    codigoPostal?: string;
  }
): Promise<{
  accessToken: string;
  refreshToken: string;
  user: any;
}> {
  const res = await apiClient.post(`/invitations/client/register/${token}`, data);
  return res.data;
}

export async function resendInvitation(id: string): Promise<{ message: string }> {
  const res = await apiClient.post(`/invitations/client/${id}/resend`);
  return res.data;
}

export async function getClientInvitations(): Promise<ClientInvitation[]> {
  const res = await apiClient.get('/invitations/client');
  return res.data;
}
