/**
 * Form 16 – SAP simulation for Credit Note and Debit Note generation.
 * When real SAP APIs are integrated, replace calls to this service with actual SAP API calls
 * and keep the same request/response shapes below.
 */

/** Dealer details sent to SAP for credit note generation */
export interface SapCreditNoteDealerDetails {
  dealerCode: string;
  dealerName?: string | null;
  dealerEmail?: string | null;
  dealerContact?: string | null;
  /** Optional: address or other identifiers */
  [key: string]: string | number | null | undefined;
}

/** Simulated SAP response for credit note generation (JSON response from SAP) */
export interface SapCreditNoteResponse {
  success: boolean;
  creditNoteNumber: string;
  sapDocumentNumber: string;
  amount: number;
  issueDate: string;
  financialYear?: string;
  quarter?: string;
  status: string;
  message?: string;
}

/** Dealer info sent to SAP for debit note generation */
export interface SapDebitNoteDealerInfo {
  dealerCode: string;
  dealerName?: string | null;
  dealerEmail?: string | null;
  dealerContact?: string | null;
  [key: string]: string | number | null | undefined;
}

/** Simulated SAP response for debit note generation (JSON response from SAP) */
export interface SapDebitNoteResponse {
  success: boolean;
  debitNoteNumber: string;
  sapDocumentNumber: string;
  amount: number;
  issueDate: string;
  status: string;
  creditNoteNumber: string;
  message?: string;
}

/**
 * Simulate SAP credit note generation.
 * Input: dealer details + amount. Output: credit note JSON (as from SAP).
 * Replace this with real SAP API call when integrating.
 */
export function simulateCreditNoteFromSap(
  dealerDetails: SapCreditNoteDealerDetails,
  amount: number
): SapCreditNoteResponse {
  const now = new Date();
  const ts = now.getTime().toString(36).toUpperCase();
  const code = (dealerDetails.dealerCode || 'XX').replace(/\s/g, '').slice(0, 8);
  const creditNoteNumber = `CN-SAP-${now.getFullYear()}-${code}-${ts}`;
  const sapDocumentNumber = `SAP-CN-${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}-${ts.slice(-6)}`;

  return {
    success: true,
    creditNoteNumber,
    sapDocumentNumber,
    amount: Number(amount),
    issueDate: now.toISOString().split('T')[0],
    status: 'issued',
    message: 'Simulated SAP credit note (replace with real SAP integration)',
  };
}

/**
 * Simulate SAP debit note generation.
 * Input: dealer code, dealer info, credit note number, amount. Output: debit note JSON (as from SAP).
 * Replace this with real SAP API call when integrating.
 */
export function simulateDebitNoteFromSap(params: {
  dealerCode: string;
  dealerInfo: SapDebitNoteDealerInfo;
  creditNoteNumber: string;
  amount: number;
}): SapDebitNoteResponse {
  const { dealerCode, dealerInfo, creditNoteNumber, amount } = params;
  const now = new Date();
  const ts = now.getTime().toString(36).toUpperCase();
  const code = (dealerCode || 'XX').replace(/\s/g, '').slice(0, 8);
  const debitNoteNumber = `DN-SAP-${now.getFullYear()}-${code}-${ts}`;
  const sapDocumentNumber = `SAP-DN-${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}-${ts.slice(-6)}`;

  return {
    success: true,
    debitNoteNumber,
    sapDocumentNumber,
    amount: Number(amount),
    issueDate: now.toISOString().split('T')[0],
    status: 'issued',
    creditNoteNumber: String(creditNoteNumber),
    message: 'Simulated SAP debit note (replace with real SAP integration)',
  };
}
