export type BackendName = 'x11' | 'wayland';
export type CoordinateSpace = 'desktop' | 'display';
export type CoordinateUnit = 'physical-pixel' | 'logical-pixel';
export type PointerButton = 'left' | 'middle' | 'right';

export interface Rect {
  readonly x: number;
  readonly y: number;
  readonly width: number;
  readonly height: number;
}

export interface DisplayInfo extends Rect {
  readonly id: string;
  readonly name: string;
  readonly primary: boolean;
  readonly pixelWidth: number;
  readonly pixelHeight: number;
  readonly scale: number;
}

export interface ComputerInfo {
  readonly backend: BackendName;
  readonly sessionType: string;
  readonly coordinateUnit: CoordinateUnit;
  readonly coordinateSpaces: readonly CoordinateSpace[];
  readonly virtualBounds: Rect;
  readonly displays: readonly DisplayInfo[];
  readonly capabilities: {
    readonly observe: boolean;
    readonly pointer: boolean;
    readonly keyboard: boolean;
    readonly text: boolean;
  };
}

export type CoordinatePoint =
  | { readonly space: 'desktop'; readonly x: number; readonly y: number }
  | { readonly space: 'display'; readonly displayId: string; readonly x: number; readonly y: number };

export type CoordinateRegion =
  | { readonly space: 'desktop'; readonly x: number; readonly y: number; readonly width: number; readonly height: number }
  | { readonly space: 'display'; readonly displayId: string; readonly x: number; readonly y: number; readonly width: number; readonly height: number };

export type ObserveTarget =
  | { readonly kind: 'desktop' }
  | { readonly kind: 'display'; readonly displayId: string }
  | { readonly kind: 'region'; readonly region: CoordinateRegion };

export interface ObserveRequest {
  readonly target: ObserveTarget;
  readonly includePointer: boolean;
}

export interface Observation {
  readonly backend: BackendName;
  readonly mimeType: 'image/png';
  readonly data: Buffer;
  readonly bytes: number;
  readonly capturedAt: string;
  readonly region: Rect;
  readonly pixelWidth: number;
  readonly pixelHeight: number;
}

export type ComputerAction =
  | { readonly type: 'move'; readonly point: CoordinatePoint }
  | { readonly type: 'click'; readonly button: PointerButton; readonly point?: CoordinatePoint }
  | { readonly type: 'doubleClick'; readonly button: PointerButton; readonly point?: CoordinatePoint; readonly intervalMs: number }
  | { readonly type: 'scroll'; readonly deltaX: number; readonly deltaY: number; readonly point?: CoordinatePoint }
  | { readonly type: 'drag'; readonly button: PointerButton; readonly from: CoordinatePoint; readonly to: CoordinatePoint }
  | { readonly type: 'mouseDown'; readonly button: PointerButton; readonly point?: CoordinatePoint }
  | { readonly type: 'mouseUp'; readonly button: PointerButton; readonly point?: CoordinatePoint }
  | { readonly type: 'key'; readonly keys: readonly string[] }
  | { readonly type: 'text'; readonly text: string; readonly delayMs: number };

export interface ActionResult {
  readonly type: ComputerAction['type'];
  readonly executed: true;
  readonly executedAt: string;
}
