import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core";
import { db, type TailorAnyDBType } from "@tailor-platform/sdk";
import { auditPolicyLifecycle } from "./auditPolicy.lifecycle.generated";

export interface CreateAuditPolicyTypeParams {
  companyType?: TailorAnyDBType;
}

export function createAuditPolicyType(params: CreateAuditPolicyTypeParams) {
  const companyId = db
    .uuid({ optional: true })
    .description("Foreign key to Company; null for global entities");
  return db
    .type("AuditPolicy", {
      entityName: db.string().description("Target entity name for audit tracking"),
      companyId: params.companyType
        ? companyId.relation({
            type: "keyOnly",
            toward: { type: params.companyType },
            backward: "auditPolicies",
          })
        : companyId,
      operationType: db.string().description("Operation type: CREATE, UPDATE, or DELETE"),
      status: db.enum(auditPolicyLifecycle.states).description("Lifecycle status"),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const auditPolicy = createAuditPolicyType({});
