import type { CodeEditorRef } from "@/features/editor/components/code-editor";
import type { FileEntry } from "./app";

export interface WorkspaceFolder {
  path: string;
  name: string;
  isPrimary?: boolean;
}

export interface FsState {
  files: FileEntry[];
  rootFolderPath?: string;
  workspaceFolders: WorkspaceFolder[];
  filesVersion: number;
  isFileTreeLoading: boolean;
  isSwitchingProject: boolean;

  // Cache for project files
  projectFilesCache?: {
    path: string;
    files: FileEntry[];
    timestamp: number;
  };
}

export interface FsActions {
  // Folder operations
  handleOpenFolder: () => Promise<boolean>;
  handleOpenFolderByPath: (path: string) => Promise<boolean>;
  addFolderToWorkspace: (path?: string) => Promise<boolean>;
  removeFolderFromWorkspace: (path: string) => Promise<boolean>;
  handleOpenRemoteProject: (connectionId: string, connectionName: string) => Promise<boolean>;
  closeFolder: () => Promise<boolean>;
  resetWorkspace: () => Promise<void>;
  switchToProject: (projectId: string) => Promise<boolean>;
  closeProject: (projectId: string) => Promise<boolean>;
  // File operations
  handleFileSelect: (
    path: string,
    isDir: boolean,
    line?: number,
    column?: number,
    codeEditorRef?: React.RefObject<CodeEditorRef | null>,
    isPreview?: boolean,
  ) => Promise<void>;
  handleFileOpen: (path: string, isDir: boolean) => Promise<void>;
  toggleFolder: (path: string) => Promise<void>;
  revealPathInTree: (targetPath: string) => Promise<void>;
  handleCreateNewFile: () => Promise<void>;
  handleCreateNewFileInDirectory: (
    dirPath: string,
    fileName?: string,
  ) => Promise<string | undefined>;
  handleCreateNewFolder: () => Promise<void>;
  handleCreateNewFolderInDirectory: (
    dirPath: string,
    folderName?: string,
  ) => Promise<string | undefined>;
  handleDeletePath: (targetPath: string, isDirectory: boolean) => Promise<void>;
  refreshDirectory: (directoryPath: string) => Promise<void>;
  handleCollapseAllFolders: () => Promise<void>;
  handleFileMove: (oldPath: string, newPath: string) => Promise<void>;
  handleRevealInFolder: (path: string) => Promise<void>;
  handleDuplicatePath: (path: string) => Promise<void>;
  handleRenamePath: (path: string, newName?: string) => Promise<void>;

  // Search operations
  getAllProjectFiles: () => Promise<FileEntry[]>;

  // Background preload
  preloadSubtree: (rootPath: string, maxDepth?: number, maxDirs?: number) => Promise<void>;

  // CRUD operations
  createFile: (directoryPath: string, fileName: string) => Promise<string>;
  createDirectory: (parentPath: string, folderName: string) => Promise<string>;
  deleteFile: (path: string) => Promise<void>;

  // Setter methods
  setFiles: (newFiles: FileEntry[]) => void;
  setIsSwitchingProject: (value: boolean) => void;

  // Session restoration
  restoreSession: (projectPath: string, skipBufferPath?: string) => Promise<void>;
  persistActiveProjectSession: () => void;
}
