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

export function createApprovalPolicyStepType() {
  return db
    .type("ApprovalPolicyStep", {
      approvalPolicyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: approvalPolicy },
          backward: "steps",
        })
        .description("Foreign key to ApprovalPolicy"),
      stepOrder: db.int().description("1-based sequential position within the policy"),
      name: db.string().description("Human-readable step label"),
      minimumApprovals: db.int().description("Quorum threshold (1 .. count(assignees))"),
      ...db.fields.timestamps(),
    })
    .indexes({
      fields: ["approvalPolicyId", "stepOrder"],
      unique: true,
      name: "approval_policy_step_order_unique_idx",
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const approvalPolicyStep = createApprovalPolicyStepType();
