// src/messages/ide.ts
// Copyright (C) 2026 Rob Colbert <rob.colbert@openplatform.us>
// Licensed under the Apache License, Version 2.0

import { IChatSession } from "../interfaces/chat-session.ts";
import { IDroneRegistration } from "../interfaces/drone-registration.ts";
import { IProject } from "../interfaces/project.ts";
import { GadgetId } from "../lib/gadget-id.ts";

/*
 * requestSessionLock
 */

export type RequestSessionLockCallback = (
  success: boolean,
  chatSessionId: string,
) => void;

export type RequestSessionLockMessage = (
  registration: IDroneRegistration,
  project: IProject,
  chatSession: IChatSession,
  cb: RequestSessionLockCallback,
) => void;

/*
 * requestWorkspaceMode
 */

export enum WorkspaceMode {
  Idle = "idle",
  Syncing = "syncing",
  User = "user",
  Agent = "agent",
}

export type RequestWorkspaceModeCallback = (
  success: boolean,
  mode: WorkspaceMode,
  reason?: string,
) => void;

export type RequestWorkspaceModeMessage = (
  registration: IDroneRegistration,
  project: IProject,
  chatSession: IChatSession,
  mode: WorkspaceMode,
  cb: RequestWorkspaceModeCallback,
) => void;

/*
 * submitPrompt
 */

export interface SubmitPromptCallbackData {
  turnId?: GadgetId;
  message?: string;
}

export type SubmitPromptCallback = (
  success: boolean,
  data: SubmitPromptCallbackData,
) => void;

export type SubmitPromptMessage = (
  prompt: string,
  cb: SubmitPromptCallback,
) => void;

/*
 * releaseSessionLock
 */

export type ReleaseSessionLockCallback = (success: boolean) => void;

export type ReleaseSessionLockMessage = (
  registration: IDroneRegistration,
  project: IProject,
  chatSession: IChatSession,
  cb: ReleaseSessionLockCallback,
) => void;

/*
 * abortWorkOrder
 */

export type AbortWorkOrderCallback = (
  success: boolean,
  message?: string,
) => void;

export type AbortWorkOrderMessage = (cb: AbortWorkOrderCallback) => void;

/*
 * sessionHeartbeat
 */

export type SessionHeartbeatCallback = (ack: boolean) => void;

export type SessionHeartbeatMessage = (cb: SessionHeartbeatCallback) => void;

/*
 * fileTreeRequest / fileTreeResponse
 */

export interface FileTreeEntry {
  name: string;           // File/directory name
  path: string;           // Full relative path from project root
  type: 'file' | 'directory' | 'symlink';
  size?: number;          // File size in bytes (directories: undefined)
  modified?: string;      // ISO 8601 timestamp
  isHidden?: boolean;     // Starts with '.'
}

export interface FileTreeRequestArgs {
  path: string;           // Relative path from project root (e.g., "src/components")
  recursive?: boolean;    // false for lazy loading (only immediate children)
  maxDepth?: number;      // Optional depth limit (default: 1 for lazy load)
}

export type FileTreeRequestCallback = (
  success: boolean,
  data: { entries?: FileTreeEntry[]; error?: string },
) => void;

export type FileTreeRequestMessage = (
  args: FileTreeRequestArgs,
  cb: FileTreeRequestCallback,
) => void;

export type FileTreeResponseMessage = (
  path: string,
  entries: FileTreeEntry[],
  error?: string,
) => void;

/*
 * fileReadRequest / fileReadResponse
 */

export type FileReadRequestCallback = (
  success: boolean,
  data: { content?: string; error?: string },
) => void;

export type FileReadRequestMessage = (
  args: { path: string },
  cb: FileReadRequestCallback,
) => void;

export type FileReadResponseMessage = (
  path: string,
  content: string | null,
  error?: string,
) => void;

/*
 * fileWriteRequest / fileWriteResponse
 */

export type FileWriteRequestCallback = (
  success: boolean,
  data: { success?: boolean; error?: string },
) => void;

export type FileWriteRequestMessage = (
  args: { path: string; content: string },
  cb: FileWriteRequestCallback,
) => void;

export type FileWriteResponseMessage = (
  path: string,
  success: boolean,
  error?: string,
) => void;
