import {
  db,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { purchasePaymentTerm } from "./purchasePaymentTerm";

export const PURCHASE_PAYMENT_TERM_LINE_KINDS: [string, string, string, string] = [
  "NET_DAYS",
  "END_OF_MONTH",
  "FIXED_DAY",
  "INSTALLMENT",
];

export function createPurchasePaymentTermLineType() {
  return db
    .type("PurchasePaymentTermLine", {
      purchasePaymentTermId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: purchasePaymentTerm },
          backward: "lines",
        })
        .description("Foreign key to PurchasePaymentTerm"),
      kind: db
        .enum(PURCHASE_PAYMENT_TERM_LINE_KINDS)
        .description("Schedule line type: NET_DAYS, END_OF_MONTH, FIXED_DAY, INSTALLMENT"),
      days: db.int({ optional: true }).description("Number of days until due"),
      fixedDayOfMonth: db
        .int({ optional: true })
        .description("Fixed day of month for FIXED_DAY kind"),
      percentage: db
        .decimal({ optional: true })
        .description("Installment percentage of total amount"),
      fixedAmount: db.decimal({ optional: true }).description("Fixed installment amount"),
      note: db.string({ optional: true }).description("Optional note for this schedule line"),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const purchasePaymentTermLine = createPurchasePaymentTermLineType();
