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

export function createApprovalPolicyType() {
  return db
    .type("ApprovalPolicy", {
      purpose: db
        .string()
        .description("Opaque business intent tag (e.g. PRODUCT_ACTIVATION, PO_CONFIRMATION)"),
      name: db.string().description("Human-readable policy name, unique with purpose under ACTIVE"),
      description: db.string({ optional: true }).description("Optional administrator description"),
      status: db
        .enum(approvalPolicyLifecycle.states)
        .description("Lifecycle status: DRAFT, ACTIVE, or INACTIVE (terminal)"),
      activatedAt: db
        .datetime({ optional: true })
        .description("Set when status transitions to ACTIVE; null in DRAFT"),
      deactivatedAt: db
        .datetime({ optional: true })
        .description("Set when status transitions to INACTIVE; null otherwise"),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const approvalPolicy = createApprovalPolicyType();
