import {
  db,
  type TailorAnyDBField,
  type TailorAnyDBType,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { currency as currencyStub, item as itemStub, unit as unitStub } from "../lib/_db_deps";
import { salesOrder } from "./salesOrder";
import { salesPriceRule } from "./salesPriceRule";

export interface CreateSalesOrderLineTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
  salesOrderType?: TailorAnyDBType;
  itemType?: TailorAnyDBType;
  currencyType?: TailorAnyDBType;
  unitType?: TailorAnyDBType;
  salesPriceRuleType?: TailorAnyDBType;
}

export function createSalesOrderLineType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateSalesOrderLineTypeParams<F>,
) {
  return db
    .type("SalesOrderLine", {
      salesOrderId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.salesOrderType ?? salesOrder },
          backward: "lines",
        })
        .description("Foreign key to SalesOrder"),
      itemId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.itemType ?? itemStub },
          backward: "salesOrderLines",
        })
        .description("Foreign key to Item"),
      description: db.string({ optional: true }).description("Optional line description"),
      quantity: db.float().description("Ordered quantity"),
      unitPrice: db.float().description("Unit price"),
      currencyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.currencyType ?? currencyStub },
          backward: "salesOrderLines",
        })
        .description("Foreign key to Currency"),
      unitId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: params.unitType ?? unitStub },
          backward: "salesOrderLines",
        })
        .description("Optional unit"),
      matchedPriceRuleId: db
        .uuid({ optional: true })
        .relation({
          type: "n-1",
          toward: { type: params.salesPriceRuleType ?? salesPriceRule },
          backward: "salesOrderLines",
        })
        .description("Optional matched SalesPriceRule"),
      billingPolicy: db.string({ optional: true }).description("ORDER or SHIPMENT"),
      requestedShipDate: db.date({ optional: true }).description("Requested ship date"),
      requiresPhysicalFulfillment: db.bool().description("Whether physical shipment is required"),
      priceSnapshot: db.string({ optional: true }).description("Serialized price snapshot"),
      shippedQuantity: db.float().description("Cumulative shipped quantity"),
      invoicedQuantity: db.float().description("Cumulative invoiced quantity"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const salesOrderLine = createSalesOrderLineType({});
