import {
  db,
  type TailorAnyDBField,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";

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

export function createPermissionType<const F extends Record<string, TailorAnyDBField>>(
  params: CreatePermissionTypeParams<F>,
) {
  return db
    .type("Permission", {
      key: db
        .string()
        .unique()
        .description("Permission key in resource:action format (e.g., orders:read)"),
      description: db
        .string({ optional: true })
        .description("Optional description of what this permission grants"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const permission = createPermissionType({});
