import {
  db,
  type TailorAnyDBType,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { company as companyStub } from "../lib/_db_deps";

export interface CreateAuditPolicyTypeParams {
  companyType?: TailorAnyDBType;
}

export function createAuditPolicyType(params: CreateAuditPolicyTypeParams) {
  return db
    .type("AuditPolicy", {
      entityName: db.string().description("Target entity name for audit tracking"),
      companyId: db
        .uuid({ optional: true })
        .relation({
          type: "keyOnly",
          toward: { type: params.companyType ?? companyStub },
          backward: "auditPolicies",
        })
        .description("Foreign key to Company; null for global entities"),
      operationType: db.string().description("Operation type: CREATE, UPDATE, or DELETE"),
      status: db.string().description("Policy lifecycle status: DRAFT, ACTIVE, INACTIVE"),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const auditPolicy = createAuditPolicyType({});
