import {
  collectPermissionScopes,
  userManagementPermissions,
  itemManagementPermissions,
  productManagementPermissions,
} from "@tailor-platform/erp-kit/app";
import { createResolver, t } from "@tailor-platform/sdk";

const ALL_SCOPES = collectPermissionScopes(
  userManagementPermissions,
  itemManagementPermissions,
  productManagementPermissions,
);

const AUTO_ASSIGNED_SCOPES = new Set(["user-management:profile"]);

const HIDDEN_SCOPES = new Set(["item-management:taxonomy"]);

export default createResolver({
  name: "availablePermissions",
  operation: "query",
  input: {},
  body: () => {
    return {
      scopes: ALL_SCOPES.filter((s) => !HIDDEN_SCOPES.has(s)).map((s) => ({
        scope: s,
        autoAssigned: AUTO_ASSIGNED_SCOPES.has(s),
      })),
    };
  },
  output: t
    .object({
      scopes: t
        .object(
          {
            scope: t.string().description("Scope key (module:scope)"),
            autoAssigned: t.bool().description("Whether this scope is auto-assigned to all roles"),
          },
          { array: true },
        )
        .description("Available permission scopes"),
    })
    .description("Available permission scopes"),
});
