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

export interface CreateRoleTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
  additionalStatuses?: string[];
}

const BASE_STATUSES = ["ACTIVE", "INACTIVE"] as const;

export function createRoleType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateRoleTypeParams<F>,
) {
  const statuses = [...BASE_STATUSES, ...(params.additionalStatuses ?? [])] as [
    string,
    ...string[],
  ];

  return db
    .type("Role", {
      name: db.string().unique().description("Role name (unique identifier)"),
      description: db
        .string({ optional: true })
        .description("Optional description of what this role represents"),
      status: db.enum(statuses).description("Role status (ACTIVE or INACTIVE)"),
      permissions: db
        .string({ array: true, optional: true })
        .description("Array of permission keys in module:scope:command format"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const role = createRoleType({});
