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

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

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

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

  return db
    .type("User", {
      email: db.string().unique().description("User email address (unique identifier)"),
      name: db.string().description("User display name"),
      status: db.enum(statuses).description("User status"),
      permissions: db
        .string({ array: true, optional: true })
        .description("Flattened permission keys from all assigned roles"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const user = createUserType({});
