import { db, type TailorAnyDBField } from "@tailor-platform/sdk";
import { defaultGqlPermission, defaultPermission } from "../../../shared/modelPermissions";
import { userLifecycle } from "./user.lifecycle.generated";

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

export function createUserType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateUserTypeParams<F>,
) {
  return db
    .type("User", {
      email: db.string().unique().description("User email address (unique identifier)"),
      name: db.string().description("User display name"),
      status: db.enum(userLifecycle.states).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(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const user = createUserType({});
