import { getPairingTokenFromUrl, setPairingTokenOnUrl } from "./pairingUrl";

const DEFAULT_HOSTED_APP_URL = "https://app.trifecta.belweave.ai";
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);

export interface HostedPairingRequest {
  readonly host: string;
  readonly token: string;
  readonly label: string;
}

export type HostedAppChannel = "latest" | "nightly";

function configuredHostedAppUrl(): string {
  return import.meta.env.VITE_HOSTED_APP_URL?.trim() || DEFAULT_HOSTED_APP_URL;
}

function configuredBackendUrl(): string {
  return import.meta.env.VITE_HTTP_URL?.trim() || import.meta.env.VITE_WS_URL?.trim() || "";
}

function configuredHostedAppChannel(): HostedAppChannel | null {
  const channel = import.meta.env.VITE_HOSTED_APP_CHANNEL?.trim().toLowerCase();
  return channel === "latest" || channel === "nightly" ? channel : null;
}

function originFromUrl(value: string): string | null {
  try {
    return new URL(value).origin;
  } catch {
    return null;
  }
}

function isLoopbackHostname(hostname: string): boolean {
  return LOOPBACK_HOSTNAMES.has(
    hostname
      .trim()
      .toLowerCase()
      .replace(/^\[(.*)\]$/, "$1"),
  );
}

function isHostedDomain(url: URL, hostedUrl: string): boolean {
  try {
    const hosted = new URL(hostedUrl);

    // Exact match (for custom preview / configured URLs)
    if (url.hostname === hosted.hostname) {
      return true;
    }

    // Canonical trifecta.belweave.* domains: allow any TLD and channel subdomains
    const canonicalPattern = /^([a-z0-9-]+\.)?app\.trifecta\.belweave\.[a-z]+$/i;
    if (canonicalPattern.test(url.hostname) && canonicalPattern.test(hosted.hostname)) {
      return true;
    }

    // Subdomain match for custom domains (e.g. preview.example.com vs app.example.com)
    return url.hostname.endsWith("." + hosted.hostname);
  } catch {
    return false;
  }
}

function isBrowserOnlyHostedOrigin(url: URL): boolean {
  if (typeof window === "undefined") {
    return false;
  }

  if (url.origin !== window.location.origin) {
    return false;
  }

  if (window.desktopBridge) {
    return false;
  }

  if (url.protocol !== "https:") {
    return false;
  }

  if (isLoopbackHostname(url.hostname)) {
    return false;
  }

  return true;
}

export function isHostedStaticApp(url: URL = new URL(window.location.href)): boolean {
  if (configuredBackendUrl()) {
    return false;
  }

  if (configuredHostedAppChannel()) {
    return true;
  }

  return isHostedDomain(url, configuredHostedAppUrl()) || isBrowserOnlyHostedOrigin(url);
}

export function readHostedPairingRequest(url: URL = new URL(window.location.href)) {
  const host = url.searchParams.get("host")?.trim() ?? "";
  const token = getPairingTokenFromUrl(url)?.trim() ?? "";
  const label = url.searchParams.get("label")?.trim() ?? "";

  if (!host || !token) {
    return null;
  }

  return {
    host,
    token,
    label,
  } satisfies HostedPairingRequest;
}

export function hasHostedPairingRequest(url: URL = new URL(window.location.href)): boolean {
  return readHostedPairingRequest(url) !== null;
}

export function buildHostedPairingUrl(input: {
  readonly host: string;
  readonly token: string;
  readonly label?: string | null;
}): string {
  const url = new URL("/pair", configuredHostedAppUrl());
  url.searchParams.set("host", input.host);

  const label = input.label?.trim();
  if (label) {
    url.searchParams.set("label", label);
  }

  return setPairingTokenOnUrl(url, input.token).toString();
}

export function buildHostedChannelSelectionUrl(input: {
  readonly channel: HostedAppChannel;
}): string {
  const url = new URL("/__belweave/channel", configuredHostedAppUrl());
  url.searchParams.set("channel", input.channel);
  return url.toString();
}
