// Server -> Client messages
export interface ScreenMessage {
  type: 'screen';
  surface: string;
  content: string;
  lines: string[];
  scrollback?: boolean;
  cursor?: { row: number; col: number };
}

export interface ScreenDiffMessage {
  type: 'screen-diff';
  surface: string;
  patches: DiffPatch[];
}

export interface DiffPatch {
  startLine: number;
  deleteCount: number;
  insertLines: string[];
}

export interface WorkspacesMessage {
  type: 'workspaces';
  workspaces: WorkspaceInfo[];
}

export interface WorkspaceInfo {
  id: string;
  ref: string;
  name?: string;
  panes: PaneInfo[];
}

export interface PaneInfo {
  id: string;
  ref: string;
  surfaces: SurfaceInfo[];
}

export interface SurfaceInfo {
  id: string;
  ref: string;
  type: string;
  title?: string;
}

export interface AuthResultMessage {
  type: 'auth-ok' | 'auth-fail';
  message?: string;
}

export interface ErrorMessage {
  type: 'error';
  message: string;
}

export interface BrowserScreenshotMessage {
  type: 'browser-screenshot';
  surface: string;
  imageData: string; // base64
  mime: string;      // e.g. 'image/jpeg'
}

export interface PingMessage {
  type: 'ping';
}

export type ServerMessage =
  | ScreenMessage
  | ScreenDiffMessage
  | WorkspacesMessage
  | AuthResultMessage
  | ErrorMessage
  | BrowserScreenshotMessage
  | PingMessage;

// Client -> Server messages
export interface AuthMessage {
  type: 'auth';
  token: string;
}

export interface SubscribeMessage {
  type: 'subscribe';
  workspace: string;
  surface: string;
}

export interface UnsubscribeMessage {
  type: 'unsubscribe';
  surface: string;
}

export interface SendTextMessage {
  type: 'send-text';
  workspace: string;
  surface: string;
  text: string;
  // When true, the server presses Enter AFTER send_text completes (awaited
  // ordering), so the paste reliably submits without a client-side timing race.
  submit?: boolean;
}

export interface SendKeyMessage {
  type: 'send-key';
  workspace: string;
  surface: string;
  key: string;
}

export interface ListWorkspacesMessage {
  type: 'list-workspaces';
}

export interface ListSurfacesMessage {
  type: 'list-surfaces';
  workspace: string;
}

export interface ScrollRequestMessage {
  type: 'scroll-request';
  workspace: string;
  surface: string;
  lines: number;
}

export interface PongMessage {
  type: 'pong';
}

export type ClientMessage =
  | AuthMessage
  | SubscribeMessage
  | UnsubscribeMessage
  | SendTextMessage
  | SendKeyMessage
  | ListWorkspacesMessage
  | ListSurfacesMessage
  | ScrollRequestMessage
  | PongMessage;
