// Documeso API client utilities
interface DocumesoConfig {
  apiUrl: string;
  apiKey: string;
}

interface DocumesoRecipient {
  id: number;
  documentId: number;
  email: string;
  name: string;
  role: 'SIGNER' | 'APPROVER' | 'VIEWER';
  signingOrder: number;
  token: string;
  signedAt: string | null;
  readStatus: 'NOT_OPENED' | 'OPENED';
  signingStatus: 'NOT_SIGNED' | 'SIGNED';
  sendStatus: 'NOT_SENT' | 'SENT';
  signingUrl: string;
}

interface DocumesoDocument {
  id: number;
  externalId: string;
  userId: number;
  teamId: number;
  title: string;
  status: 'DRAFT' | 'PENDING' | 'COMPLETED' | 'CANCELLED';
  documentDataId: string;
  createdAt: string;
  updatedAt: string;
  completedAt: string | null;
  recipients: DocumesoRecipient[];
}

interface DocumesoListResponse {
  documents: DocumesoDocument[];
  total: number;
  page: number;
  perPage: number;
}

// Get Documeso configuration
const getDocumesoConfig = (): DocumesoConfig => {
  return {
    apiUrl: 'https://signatures.portnimara.dev/api/v1',
    apiKey: 'Bearer api_malptg62zqyb0wrp'
  };
};

// Fetch a single document by ID
export const getDocumesoDocument = async (documentId: number): Promise<DocumesoDocument> => {
  const config = getDocumesoConfig();
  
  try {
    const response = await $fetch<DocumesoDocument>(`${config.apiUrl}/documents/${documentId}`, {
      headers: {
        'Authorization': config.apiKey,
        'Content-Type': 'application/json'
      }
    });
    
    return response;
  } catch (error) {
    console.error('Failed to fetch Documeso document:', error);
    throw error;
  }
};

// Search documents by external ID (e.g., 'loi-94')
export const searchDocumesoDocuments = async (externalId?: string): Promise<DocumesoDocument[]> => {
  const config = getDocumesoConfig();
  
  try {
    const response = await $fetch<DocumesoListResponse>(`${config.apiUrl}/documents`, {
      headers: {
        'Authorization': config.apiKey,
        'Content-Type': 'application/json'
      },
      params: {
        perPage: 100
      }
    });
    
    // If externalId is provided, filter by it
    if (externalId) {
      return response.documents.filter(doc => doc.externalId === externalId);
    }
    
    return response.documents;
  } catch (error) {
    console.error('Failed to search Documeso documents:', error);
    throw error;
  }
};

// Get document by external ID (e.g., 'loi-94')
export const getDocumesoDocumentByExternalId = async (externalId: string): Promise<DocumesoDocument | null> => {
  const documents = await searchDocumesoDocuments(externalId);
  return documents.length > 0 ? documents[0] : null;
};

// Check signature status for a document
export const checkDocumentSignatureStatus = async (documentId: number): Promise<{
  documentStatus: string;
  unsignedRecipients: DocumesoRecipient[];
  signedRecipients: DocumesoRecipient[];
  clientSigned: boolean;
  allSigned: boolean;
}> => {
  const document = await getDocumesoDocument(documentId);
  
  const unsignedRecipients = document.recipients.filter(r => r.signingStatus === 'NOT_SIGNED');
  const signedRecipients = document.recipients.filter(r => r.signingStatus === 'SIGNED');
  
  // Check if client (signingOrder = 1) has signed
  const clientRecipient = document.recipients.find(r => r.signingOrder === 1);
  const clientSigned = clientRecipient ? clientRecipient.signingStatus === 'SIGNED' : false;
  
  const allSigned = unsignedRecipients.length === 0;
  
  return {
    documentStatus: document.status,
    unsignedRecipients,
    signedRecipients,
    clientSigned,
    allSigned
  };
};

// Get recipients who need to sign (excluding client)
export const getRecipientsToRemind = async (documentId: number): Promise<DocumesoRecipient[]> => {
  const status = await checkDocumentSignatureStatus(documentId);
  
  // Only remind if client has signed
  if (!status.clientSigned) {
    return [];
  }
  
  // Return unsigned recipients with signingOrder > 1
  return status.unsignedRecipients.filter(r => r.signingOrder > 1);
};

// Format recipient name for emails
export const formatRecipientName = (recipient: DocumesoRecipient): string => {
  const firstName = recipient.name.split(' ')[0];
  return firstName;
};

// Get signing URL for a recipient
export const getSigningUrl = (recipient: DocumesoRecipient): string => {
  return recipient.signingUrl;
};
