import { defaultGqlPermission, defaultPermission } from "@tailor-platform/erp-kit/core";
import { db } from "@tailor-platform/sdk";
import { role } from "./role";
import { user } from "./user";

export function createUserRoleType() {
  return db
    .type("UserRole", {
      userId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: user },
          backward: "userRoles",
        })
        .description("Foreign key to User"),
      roleId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: role },
          backward: "userRoles",
        })
        .description("Foreign key to Role"),
      ...db.fields.timestamps(),
    })
    .indexes({
      fields: ["userId", "roleId"],
      unique: true,
      name: "user_role_unique_idx",
    })
    .permission(defaultPermission)
    .gqlPermission(defaultGqlPermission);
}

export const userRole = createUserRoleType();
