import { ACCOUNT_PAYABLE_RECOGNIZED, type AccountingEventKind } from "./eventKinds";

export type AccountPayableDocumentType = "INVOICE" | "DEBIT_MEMO" | "CREDIT_MEMO";

export type AccountPayableRecognizedV1Payload = {
  source: {
    documentType: "ACCOUNT_PAYABLE_DOCUMENT";
    documentId: string;
    documentNumber: string;
  };
  documentType: AccountPayableDocumentType;
  supplierId: string;
  documentDate: Date;
  accountingDate: Date;
  dueSchedule: AccountPayableEventPayloadDueScheduleLine[];
  currencyId: string;
  currencyCode: string;
  totalAmount: string;
  payableAccountId: string;
  lines: AccountPayableEventPayloadLine[];
  distributions: AccountPayableEventPayloadDistribution[];
};

export type AccountPayableEventPayloadLine = {
  lineId: string;
  description: string | null;
  quantity: string | null;
  unitPrice: string | null;
  amount: string;
};

export type AccountPayableEventPayloadDueScheduleLine = {
  dueDate: Date;
  amount: string;
};

export type AccountPayableEventPayloadDistribution = {
  distributionId: string;
  lineId: string;
  accountId: string;
  amount: string;
};

type AccountingEventPayloadRegistry = {
  [ACCOUNT_PAYABLE_RECOGNIZED]: {
    "1": AccountPayableRecognizedV1Payload;
  };
};

export type StandardAccountingEvent = {
  [K in AccountingEventKind]: {
    [V in keyof AccountingEventPayloadRegistry[K] & string]: {
      companyId: string;
      eventKind: K;
      schemaVersion: V;
      payload: AccountingEventPayloadRegistry[K][V] & object;
    };
  }[keyof AccountingEventPayloadRegistry[K] & string];
}[AccountingEventKind];
