import "server-only";
import { AUTH_URL } from "@/lib/constants";
import { env } from "@/lib/env";

const DEFAULT_WEBAPP_URL = "http://localhost:3000";
const AUTH_BASE_PATH = "/api/auth";
const MCP_RESOURCE_PATH = "/api/mcp";
const MCP_PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource/api/mcp";

// A reverse scan, not `replace(/\/+$/, "")`: that pattern is greedy and unanchored at the start, so
// on a run of N slashes the engine retries from every offset — O(N^2). Only ever fed configured
// values here (WEBAPP_URL / BETTER_AUTH_URL / NEXTAUTH_URL), so this is not the reachable case that
// better-auth-path-label.ts fixes — it is the same shape, kept off the codebase for good.
const trimTrailingSlash = (value: string): string => {
  let end = value.length;
  while (end > 0 && value[end - 1] === "/") end--;
  return value.slice(0, end);
};

const normalizeConfiguredUrl = (value: string | undefined, fallback = DEFAULT_WEBAPP_URL): URL => {
  const configured = value?.trim() || fallback;
  const url = new URL(configured);
  url.hash = "";
  url.search = "";
  url.pathname = trimTrailingSlash(url.pathname);
  return url;
};

const appendPath = (base: URL, path: string): string => {
  const basePath = trimTrailingSlash(base.pathname);
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
  const alreadyEndsWithPath = basePath === normalizedPath || basePath.endsWith(normalizedPath);
  const pathname = alreadyEndsWithPath ? basePath : `${basePath}${normalizedPath}`;

  return `${base.origin}${pathname}`;
};

const getWebAppBaseUrl = (): URL => normalizeConfiguredUrl(env.WEBAPP_URL, DEFAULT_WEBAPP_URL);

export const getAuthIssuerUrl = (): string => {
  const authBaseUrl = normalizeConfiguredUrl(AUTH_URL ?? env.WEBAPP_URL);
  return appendPath(authBaseUrl, AUTH_BASE_PATH);
};

/**
 * Returns the server-side endpoint used only to fetch Better Auth's signing keys.
 *
 * Token issuer validation, OAuth discovery, redirects, cookies, and audiences continue to use the public
 * Auth/WEBAPP URLs. Deployments whose pods cannot resolve or hairpin through that public origin can point this
 * fetch at an internal service without changing the externally visible OAuth contract.
 */
export const getMcpOAuthJwksUrl = (): string => env.MCP_OAUTH_JWKS_URL ?? `${getAuthIssuerUrl()}/jwks`;

export const getMcpResourceUrl = (): string => appendPath(getWebAppBaseUrl(), MCP_RESOURCE_PATH);

export const getMcpProtectedResourceMetadataUrl = (): string =>
  appendPath(getWebAppBaseUrl(), MCP_PROTECTED_RESOURCE_METADATA_PATH);

export const getMcpOrigin = (): string => new URL(getMcpResourceUrl()).origin;

/**
 * The authorization server's own UserInfo endpoint, which is a legitimate second value in an access
 * token's `aud`.
 *
 * The oauth-provider treats UserInfo as an implicit resource: when `openid` is in scope it appends
 * this identifier to the audience alongside the resource the client actually requested. It is the
 * only audience besides the MCP resource URL that a Formbricks-issued MCP token may carry, so the
 * resource server allow-lists exactly these two and rejects anything else (see
 * `hasAcceptedMcpAudience` in modules/mcp/auth.ts).
 *
 * Built off the issuer for the same reason `jwksUrl` is: Better Auth mounts its OAuth endpoints
 * under the auth base path, so the issuer is the prefix the plugin itself uses.
 *
 * The assumption is that this equals Better Auth's own `ctx.context.baseURL`, which is what it stamps
 * into the audience. It holds for both shapes an operator is actually told to configure:
 *
 * - a bare origin — upstream's `withPath` appends `/api/auth`, exactly as `appendPath` does here;
 * - a subpath already ending in `/api/auth` (`https://host/custom-path/api/auth`, which is the literal
 *   value `docs/self-hosting/configuration/custom-subpath.mdx` prescribes) — `withPath` returns it
 *   unchanged because `checkHasPath` is true, and `appendPath` returns it unchanged because its
 *   `basePath.endsWith(normalizedPath)` branch fires.
 *
 * The one shape where they diverge is a configured URL carrying a path that does NOT end in
 * `/api/auth`: `withPath` leaves any non-empty path alone, while `appendPath` would append. Note this
 * is narrower than it used to say here — "any subpath breaks it" is wrong, and the documented subpath
 * is precisely the case that works. Subpath deployments cannot complete a login at all today (ENG-606),
 * so it is still not a live gap.
 *
 * This matters beyond the audience now: `ssoLegacyRedirectUri` in better-auth-providers.ts builds the
 * pinned SSO callback URL from `getAuthIssuerUrl()` (ENG-2343). Because that URL is pinned explicitly,
 * Better Auth sends it on both the authorization and token legs regardless of its own `baseURL`, so a
 * divergence here cannot desynchronise the handshake — it would only mean the URL names a host the
 * operator did not intend, which is a configuration error rather than a protocol one.
 */
export const getOAuthUserInfoUrl = (): string => `${getAuthIssuerUrl()}/oauth2/userinfo`;

/**
 * The MCP resource scope families, each one a `<family>:read` / `<family>:write` pair. Every list of
 * resource scopes below, the API-key scope mapping (`getMcpScopes`) and the `list_workspaces` gate derive
 * from this one array, so a new family is one entry here plus what this array cannot do for it: its
 * permissions in the MCP permission catalogue (`MCP_PERMISSIONS`, their `scopeFamily`) and its two repair
 * migrations.
 *
 * ENG-2862: responses got their own pair rather than riding on `surveys:*`, which would silently widen
 * every token an integrator already holds to include respondent PII — the survey document is
 * configuration, a response is someone's answers. ENG-6581: dashboards got theirs on the same grounds,
 * since an app that may read records must not thereby gain the right to edit dashboards.
 *
 * **A family is added here only together with two data migrations.** Instances that have already
 * upgraded keep whatever `oauthResource.allowedScopes` they were seeded with, `resolveResourcePolicy`
 * intersects the new scopes away, and `resourceSeedMode: "insertOnly"` never repairs the row — so
 * `/authorize` answers `invalid_scope` with nothing logged. One migration grants the pair to existing
 * resource rows (listed in `REPAIR_MIGRATIONS` in `mcp-oauth-resource-seed.test.ts`). The other grants
 * it to each registered client's `scopes`: the oauth-provider validates `/authorize` as a subset of the
 * scopes a client REGISTERED with, and every spec-following MCP client asks for what the
 * protected-resource metadata advertises, so without it every integration registered before the family
 * existed gets `invalid_scope` as its connection re-authorizes. Responses took both steps (ENG-2862,
 * 20260915120000; ENG-3470, 20261005130000), dashboards too (ENG-6581, 20261022120000 and
 * 20261022120001), and the read-only `audience` family (ENG-7151, 20261112130000 and 20261112130001).
 *
 * Never by editing the 20260812110001 seed migration. That one has already run everywhere, so widening
 * its literal repairs nothing and only makes it claim something it never did.
 */
export const MCP_SCOPE_FAMILIES = [
  "surveys",
  "workflows",
  "feedbackRecords",
  "responses",
  "dashboards",
] as const;

/**
 * Families with a `<family>:read` scope and no write scope, because every tool they open only reads.
 * Added by the same rules and the same two migrations as `MCP_SCOPE_FAMILIES`, with only the read scope
 * in them. ENG-7151: `audience` (contacts, contact attributes, segments, action classes), which holds
 * personal data, so it is its own family rather than riding on another one's consent.
 */
export const MCP_READ_ONLY_SCOPE_FAMILIES = ["audience"] as const;

export type TMcpScopeFamily =
  | (typeof MCP_SCOPE_FAMILIES)[number]
  | (typeof MCP_READ_ONLY_SCOPE_FAMILIES)[number];
export type TMcpReadScope = `${TMcpScopeFamily}:read`;
export type TMcpWriteScope = `${(typeof MCP_SCOPE_FAMILIES)[number]}:write`;

type TNonEmpty<T> = readonly [T, ...T[]];

// Non-empty by construction (the family list is a non-empty literal), which is what lets a scope gate
// that refuses `[]` take these lists directly.
export const MCP_READ_SCOPES = [...MCP_SCOPE_FAMILIES, ...MCP_READ_ONLY_SCOPE_FAMILIES].map(
  (family) => `${family}:read` as const
) as unknown as TNonEmpty<TMcpReadScope>;

export const MCP_WRITE_SCOPES = MCP_SCOPE_FAMILIES.map(
  (family) => `${family}:write` as const
) as unknown as TNonEmpty<TMcpWriteScope>;

/**
 * The resource scopes MCP clients are told about: the read/write pairs in family order, then the
 * read-only families' read scopes.
 */
export const MCP_RESOURCE_SCOPES: readonly (TMcpReadScope | TMcpWriteScope)[] = [
  ...MCP_SCOPE_FAMILIES.flatMap((family) => [`${family}:read`, `${family}:write`] as const),
  ...MCP_READ_ONLY_SCOPE_FAMILIES.map((family) => `${family}:read` as const),
];

export const MCP_OAUTH_SCOPES = [
  "openid",
  "profile",
  "email",
  "offline_access",
  ...MCP_RESOURCE_SCOPES,
] as const;

// Scopes advertised in the RFC 9728 protected-resource metadata. MCP clients derive their
// Dynamic Client Registration + authorize scopes from this list (NOT from the AS metadata),
// and the oauth-provider plugin validates authorize requests against the client's REGISTERED
// scopes — so offline_access (required for refresh tokens) must be advertised here or DCR
// clients can never be granted it. `satisfies` enforces that everything advertised is grantable.
export const MCP_PROTECTED_RESOURCE_SCOPES = [
  ...MCP_RESOURCE_SCOPES,
  "offline_access",
] as const satisfies readonly (typeof MCP_OAUTH_SCOPES)[number][];

/**
 * The `scope` advertised in the 401 `WWW-Authenticate` challenge from the MCP endpoint.
 *
 * A client that hits the 401 *before* fetching the protected-resource metadata uses this string as
 * its Dynamic Client Registration `scope`, then authorizes with the scopes the metadata advertises.
 * The oauth-provider validates an authorize request as a *subset* of the client's registered scopes,
 * so the invariant is that this list must **cover** the metadata list. A narrower challenge means the
 * client registers narrow, authorizes wide, and is rejected with `invalid_scope` — failing its first
 * connect and succeeding only on retry, once the metadata is cached (ENG-2175).
 *
 * Derived from the same array so the two are identical, which satisfies the invariant by construction
 * and leaves nothing to drift.
 */
export const MCP_CHALLENGE_SCOPE = MCP_PROTECTED_RESOURCE_SCOPES.join(" ");
