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

export interface CreateManufacturingCostLineTypeParams {
  costSummaryType: TailorAnyDBType;
}

export function createManufacturingCostLineType(params: CreateManufacturingCostLineTypeParams) {
  return db
    .type("ManufacturingCostLine", {
      costSummaryId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: params.costSummaryType },
          backward: "costLines",
        })
        .description("Foreign key to ManufacturingCostSummary"),
      costType: db.string().description("Cost type: e.g. MATERIAL, LABOR, MACHINE, OVERHEAD"),
      plannedAmount: db.float().description("Planned cost amount"),
      actualAmount: db.float().description("Actual cost amount"),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const manufacturingCostLine = createManufacturingCostLineType({
  costSummaryType: manufacturingCostSummary,
});
