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

export interface CreateManufacturingCostSettlementRecordTypeParams {
  costSummaryType: TailorAnyDBType;
}

export function createManufacturingCostSettlementRecordType(
  params: CreateManufacturingCostSettlementRecordTypeParams,
) {
  return db
    .type("ManufacturingCostSettlementRecord", {
      costSummaryId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.costSummaryType },
          backward: "settlementRecords",
        })
        .description("Foreign key to ManufacturingCostSummary"),
      settlementDate: db.datetime().description("Date of settlement"),
      settlementReference: db.string().description("Reference identifier for the settlement"),
      acknowledgedDate: db
        .datetime({ optional: true })
        .description("Date when settlement was acknowledged"),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const manufacturingCostSettlementRecord = createManufacturingCostSettlementRecordType({
  costSummaryType: manufacturingCostSummary,
});
