// This file contains Enterprise regions licensed under LICENSE_ENTERPRISE.
import {
  ARCHESTRA_MCP_SERVER_NAME,
  type ArchestraMcpIdentityOptions,
  type ArchestraToolShortName,
  getArchestraMcpCatalogName,
  getArchestraMcpServerName,
  getArchestraToolFullName,
  getArchestraToolPrefix,
  getArchestraToolShortName,
  POLICY_EVALUATED_ARCHESTRA_TOOL_SHORT_NAMES,
} from "@archestra/shared";
import config from "@/config";
import type { Organization } from "@/types";

type ArchestraBrandingState = {
  appName: string | null;
  iconLogo: string | null;
};

class ArchestraMcpBranding {
  get identity(): ArchestraMcpIdentityOptions {
    return {
      appName: this.state.appName,
      fullWhiteLabeling: config.enterpriseFeatures.fullWhiteLabeling,
    };
  }

  get catalogName(): string {
    return getArchestraMcpCatalogName(this.identity);
  }

  get serverName(): string {
    return getArchestraMcpServerName(this.identity);
  }

  get toolPrefix(): string {
    return getArchestraToolPrefix(this.identity);
  }

  // SPDX-SnippetBegin
  // SPDX-SnippetCopyrightText: 2026 Archestra Inc.
  // SPDX-License-Identifier: LicenseRef-Archestra-Enterprise
  get iconLogo(): string | null {
    return config.enterpriseFeatures.fullWhiteLabeling
      ? this.state.iconLogo
      : null;
  }
  // SPDX-SnippetEnd

  get allowedServerNames(): string[] {
    return Array.from(
      new Set([
        ARCHESTRA_MCP_SERVER_NAME,
        getArchestraMcpServerName(this.identity),
      ]),
    );
  }

  syncFromOrganization(
    organization: Pick<Organization, "appName" | "iconLogo"> | null,
  ): void {
    this.state = {
      appName: organization?.appName ?? null,
      iconLogo: organization?.iconLogo ?? null,
    };
  }

  getToolName(shortName: ArchestraToolShortName): string {
    return getArchestraToolFullName(shortName, this.identity);
  }

  getToolShortName(toolName: string): ArchestraToolShortName | null {
    return getArchestraToolShortName(toolName, {
      ...this.identity,
      includeDefaultPrefix: true,
    });
  }

  isToolName(toolName: string): boolean {
    return this.getToolShortName(toolName) !== null;
  }

  /**
   * True when the tool is a built-in that bypasses tool invocation and
   * trusted data policies. Most built-ins do; the ones in
   * {@link POLICY_EVALUATED_ARCHESTRA_TOOL_SHORT_NAMES} (e.g.
   * `query_knowledge_sources`, whose results can carry prompt injection from
   * knowledge-base content) are evaluated like external tools instead.
   */
  isPolicyBypassedToolName(toolName: string): boolean {
    const shortName = this.getToolShortName(toolName);
    return (
      shortName !== null &&
      !POLICY_EVALUATED_ARCHESTRA_TOOL_SHORT_NAMES.has(shortName)
    );
  }

  private state: ArchestraBrandingState = {
    appName: null,
    iconLogo: null,
  };
}

export const archestraMcpBranding = new ArchestraMcpBranding();
