import { db, type TailorAnyDBType } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
import { company as companyStub } from "../lib/_db_deps";
import { auditPolicyLifecycle } from "./auditPolicy.lifecycle.generated";

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.enum(auditPolicyLifecycle.states).description("Lifecycle status"),
      ...db.fields.timestamps(),
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const auditPolicy = createAuditPolicyType({});
