import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core";
import { db } from "@tailor-platform/sdk";
import { routing } from "./routing";
import { workCenter } from "./workCenter";

export function createRoutingOperationType() {
  return db
    .type("RoutingOperation", {
      routingId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: routing },
          backward: "routingOperations",
        })
        .description("Foreign key to Routing"),
      sequenceNumber: db.int().description("Step order within the routing"),
      operationDescription: db
        .string({ optional: true })
        .description("Human-readable description of the operation"),
      workCenterId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: workCenter },
          backward: "routingOperations",
        })
        .description("Foreign key to WorkCenter"),
      standardSetupTime: db.float().description("Standard setup time (>= 0)"),
      standardRunTime: db.float().description("Standard run time per unit (>= 0)"),
      operatorInstructions: db
        .string({ optional: true })
        .description("Free-text instructions for the operator"),
      ...db.fields.timestamps(),
    })
    .indexes({
      fields: ["routingId", "sequenceNumber"],
      unique: true,
      name: "routing_operation_routing_seq_idx",
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const routingOperation = createRoutingOperationType();
