import {
  entities,
  permission_tokens,
  permission_token_rights,
  entity_sets,
  identities,
} from "drizzle/schema";
import { v7 } from "uuid";
import { PgTransaction } from "drizzle-orm/pg-core";
import { NodePgDatabase } from "drizzle-orm/node-postgres";
import { Json } from "supabase/database.types";

export async function createIdentity(
  db: NodePgDatabase,
  data?: { email?: string; atp_did?: string },
) {
  return db.transaction(async (tx) => {
    // Create a new entity set
    let [entity_set] = await tx.insert(entity_sets).values({}).returning();
    // Create a root-entity
    let [entity] = await tx
      .insert(entities)
      // And add it to that permission set
      .values({ set: entity_set.id, id: v7() })
      .returning();
    //Create a new permission token
    let [permissionToken] = await tx
      .insert(permission_tokens)
      .values({ root_entity: entity.id })
      .returning();
    //and give it all the permission on that entity set
    let [rights] = await tx
      .insert(permission_token_rights)
      .values({
        token: permissionToken.id,
        entity_set: entity_set.id,
        read: true,
        write: true,
        create_token: true,
        change_entity_set: true,
      })
      .returning();
    let [identity] = await tx
      .insert(identities)
      .values({ home_page: permissionToken.id, ...data })
      .returning();
    return identity as Omit<typeof identity, "interface_state"> & {
      interface_state: Json;
    };
  });
}
