import { ref, type Ref } from "vue";

export interface FileEntry {
  name: string;
  path: string;
  size: number;
  isDir: boolean;
  permissions: string;
  modTime: string;
}

export interface UseSftpReturn {
  currentPath: Ref<string>;
  entries: Ref<FileEntry[]>;
  isLoading: Ref<boolean>;
  followTerminal: Ref<boolean>;
  navigateTo: (path: string) => Promise<void>;
  goUp: () => Promise<void>;
  refresh: () => Promise<void>;
}

/** Mock directory listings used until Wails SFTP bindings are connected. */
const mockDirectories: Record<string, FileEntry[]> = {
  "/home/user": [
    { name: "docs", path: "/home/user/docs", size: 0, isDir: true, permissions: "drwxr-xr-x", modTime: "2026-03-17" },
    { name: "projects", path: "/home/user/projects", size: 0, isDir: true, permissions: "drwxr-xr-x", modTime: "2026-03-16" },
    { name: ".ssh", path: "/home/user/.ssh", size: 0, isDir: true, permissions: "drwx------", modTime: "2026-03-10" },
    { name: ".bashrc", path: "/home/user/.bashrc", size: 3771, isDir: false, permissions: "-rw-r--r--", modTime: "2026-03-15" },
    { name: "deploy.sh", path: "/home/user/deploy.sh", size: 1024, isDir: false, permissions: "-rwxr-xr-x", modTime: "2026-03-16" },
    { name: ".profile", path: "/home/user/.profile", size: 807, isDir: false, permissions: "-rw-r--r--", modTime: "2026-03-10" },
  ],
  "/home/user/docs": [
    { name: "readme.md", path: "/home/user/docs/readme.md", size: 2048, isDir: false, permissions: "-rw-r--r--", modTime: "2026-03-17" },
    { name: "notes.txt", path: "/home/user/docs/notes.txt", size: 512, isDir: false, permissions: "-rw-r--r--", modTime: "2026-03-14" },
  ],
  "/home/user/projects": [
    { name: "app", path: "/home/user/projects/app", size: 0, isDir: true, permissions: "drwxr-xr-x", modTime: "2026-03-16" },
    { name: "Makefile", path: "/home/user/projects/Makefile", size: 256, isDir: false, permissions: "-rw-r--r--", modTime: "2026-03-16" },
  ],
  "/home/user/.ssh": [
    { name: "authorized_keys", path: "/home/user/.ssh/authorized_keys", size: 743, isDir: false, permissions: "-rw-------", modTime: "2026-03-10" },
    { name: "config", path: "/home/user/.ssh/config", size: 128, isDir: false, permissions: "-rw-------", modTime: "2026-03-10" },
  ],
};

/**
 * Composable that manages SFTP file browsing state.
 *
 * Uses mock data until Wails SFTPService bindings are connected.
 */
export function useSftp(_sessionId: string): UseSftpReturn {
  const currentPath = ref("/home/user");
  const entries = ref<FileEntry[]>([]);
  const isLoading = ref(false);
  const followTerminal = ref(false);

  async function listDirectory(path: string): Promise<FileEntry[]> {
    // TODO: Replace with Wails binding call — SFTPService.List(sessionId, path)
    // Simulate network delay
    await new Promise((resolve) => setTimeout(resolve, 150));
    return mockDirectories[path] ?? [];
  }

  async function navigateTo(path: string): Promise<void> {
    isLoading.value = true;
    try {
      currentPath.value = path;
      entries.value = await listDirectory(path);
    } finally {
      isLoading.value = false;
    }
  }

  async function goUp(): Promise<void> {
    const parts = currentPath.value.split("/").filter(Boolean);
    if (parts.length <= 1) {
      await navigateTo("/");
      return;
    }
    parts.pop();
    await navigateTo("/" + parts.join("/"));
  }

  async function refresh(): Promise<void> {
    await navigateTo(currentPath.value);
  }

  // Load initial directory
  navigateTo(currentPath.value);

  return {
    currentPath,
    entries,
    isLoading,
    followTerminal,
    navigateTo,
    goUp,
    refresh,
  };
}
