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

export function createThreeWayMatchEventType() {
  return db
    .type("ThreeWayMatchEvent", {
      purchaseBillLineId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: purchaseBillLine },
          backward: "threeWayMatchEvents",
        })
        .description("Foreign key to PurchaseBillLine"),
      goodsReceiptLineId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: goodsReceiptLine },
          backward: "threeWayMatchEvents",
        })
        .description("Foreign key to GoodsReceiptLine"),
      matchedQuantity: db.decimal().description("Matched quantity for this event"),
      matchedAt: db.date().description("Timestamp when this match event occurred"),
      matchedBy: db
        .string({ optional: true })
        .description("User or system that performed the match"),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const threeWayMatchEvent = createThreeWayMatchEventType();
