import { db, type TailorAnyDBType } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
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(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

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