import type { ControlUiBootstrapProfileHint } from "../../../src/gateway/control-ui-contract.js";
import type { EventLogEntry } from "../api/event-log.ts";
import type { GatewayBrowserClient, GatewayEventListener, GatewayHelloOk } from "../api/gateway.ts";
import type { AuthenticatedUser } from "./user-profile.ts";

export type ApplicationGatewayPhase =
  | "stopped"
  | "connecting"
  | "connected"
  | "reconnecting"
  | "offline";

export type ApplicationGatewaySnapshot = {
  client: GatewayBrowserClient | null;
  phase: ApplicationGatewayPhase;
  offlineStable: boolean;
  hello: GatewayHelloOk | null;
  canvasPluginSurfaceUrl: string | null;
  assistantAgentId: string | null;
  sessionKey: string;
  lastError: string | null;
  lastErrorCode: string | null;
  /** Identity projected from this browser connection's own presence entry. */
  selfUser?: AuthenticatedUser | null;
};

export type ApplicationGatewayConnection = {
  gatewayUrl: string;
  token: string;
  bootstrapToken: string;
  bootstrapProfile?: ControlUiBootstrapProfileHint;
  password: string;
};

export type ApplicationGatewayConnectOptions = Partial<ApplicationGatewayConnection> & {
  sessionKey?: string;
};

export type ApplicationGateway = {
  readonly snapshot: ApplicationGatewaySnapshot;
  readonly connection: ApplicationGatewayConnection;
  readonly eventLog: readonly EventLogEntry[];
  connect: (connection?: ApplicationGatewayConnectOptions) => void;
  setSessionKey: (sessionKey: string) => void;
  start: () => void;
  stop: () => void;
  subscribe: (listener: (snapshot: ApplicationGatewaySnapshot) => void) => () => void;
  subscribeEventLog: (listener: (events: readonly EventLogEntry[]) => void) => () => void;
  subscribeEvents: (listener: GatewayEventListener) => () => void;
  updateSelfUser?: (patch: Partial<Omit<AuthenticatedUser, "id">>) => void;
};
