import { db, type TailorAnyDBField } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
import { approvalPolicyLifecycle } from "./approvalPolicy.lifecycle.generated";

export interface CreateApprovalPolicyTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
}

export function createApprovalPolicyType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateApprovalPolicyTypeParams<F>,
) {
  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"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const approvalPolicy = createApprovalPolicyType({});
