/**
 * 系统信息相关 API — 委托给 Transport 层
 * 本地模式走 Wails IPC，远程模式走 HTTP REST API
 */

import type { File } from './types'
import { connectionManager } from './connection-manager'

/**
 * 转换后端文件数据格式（蛇形 → 驼峰）
 */
function transformFile(file: any): File {
  return { ...file, isDir: file.is_dir, modified_time: file.mod_time }
}

function transformFileList(files: any[]): File[] {
  return files.map(transformFile)
}

const t = () => connectionManager.getTransport()

export async function getSystemInfo() { return t().getFileInfo('/') }

export async function getCPUInfo() {
  if (connectionManager.isRemote()) return {}
  try { return await (window.go?.main?.App?.GetCPUInfo?.()) ?? {} } catch { return {} }
}

export async function getMemoryInfo() {
  if (connectionManager.isRemote()) return {}
  try { return await (window.go?.main?.App?.GetMemoryInfo?.()) ?? {} } catch { return {} }
}

export async function getDiskInfo() {
  if (connectionManager.isRemote()) return {}
  try { return await (window.go?.main?.App?.GetDiskInfo?.()) ?? {} } catch { return {} }
}

export async function listDir(path: string): Promise<File[]> {
  return transformFileList(await t().listDir(path))
}

export async function readFile(path: string): Promise<string> {
  return t().readFile(path)
}

export async function writeFile(path: string, content: string): Promise<void> {
  await t().writeFile(path, String(content))
}

export async function saveBase64File(path: string, base64Content: string): Promise<void> {
  if (!base64Content) throw new Error('无效的 base64 内容')
  await t().saveBase64File(path, base64Content)
}

export async function deletePath(path: string): Promise<any> {
  return t().deletePath(path)
}

export async function createDir(parentPath: string, dirname: string): Promise<any> {
  return t().createDir(parentPath, dirname)
}

export async function createFile(dirPath: string, filename: string): Promise<any> {
  return t().createFile(dirPath, filename)
}

export async function renamePath(oldPath: string, newPath: string): Promise<any> {
  return t().renamePath(oldPath, String(newPath))
}

export async function getEnvVars(): Promise<Record<string, string>> {
  try { return await (window.go?.main?.App?.GetEnvVars?.()) ?? {} } catch { return {} }
}

export async function listZipContents(zipPath: string): Promise<File[]> {
  return transformFileList(await t().listZipContents(zipPath))
}

export async function extractFileFromZip(zipPath: string, filePath: string): Promise<string> {
  return t().extractFileFromZip(zipPath, filePath)
}

export async function extractFileFromZipToTemp(zipPath: string, filePath: string): Promise<string> {
  return t().extractFileFromZipToTemp(zipPath, filePath)
}

export async function getZipFileInfo(zipPath: string, filePath: string): Promise<File> {
  return transformFile(await t().getZipFileInfo(zipPath, filePath))
}

export async function openPath(path: string): Promise<void> {
  await t().openPath(path)
}

export async function getFileServerURL(): Promise<string> {
  return t().getFileServerURL()
}

export async function resolveShortcut(lnkPath: string): Promise<any> {
  return t().resolveShortcut(lnkPath)
}

export async function detectFileTypeByContent(path: string) {
  return t().detectFileTypeByContent(path)
}

export async function getCommonPaths() {
  return t().getCommonPaths()
}
