/**
 * 目录浏览 API
 */
import axios from 'axios'
import type { DirectoryItem, FilePair } from '@/stores'

const API_BASE = 'http://localhost:8000/api/directories'

export interface BrowseResponse {
  current_path: string
  parent_path: string | null
  items: DirectoryItem[]
}

export interface ScanResponse {
  total: number
  pairs: FilePair[]
  labeled_count: number
}

/**
 * 浏览目录
 */
export async function browseDirectory(
  path: string = '~',
  showHidden: boolean = false
): Promise<BrowseResponse> {
  const response = await axios.get(`${API_BASE}/browse`, {
    params: {
      path,
      show_hidden: showHidden
    }
  })
  return response.data
}

/**
 * 扫描目录下的图片+JSON文件对
 */
export async function scanDirectory(
  imageDir: string,
  jsonDir: string,
  imagePattern?: string,
  jsonPattern?: string,
  outputDir?: string
): Promise<ScanResponse> {
  const response = await axios.post(`${API_BASE}/scan`, {
    image_dir: imageDir,
    json_dir: jsonDir,
    image_pattern: imagePattern,
    json_pattern: jsonPattern,
    output_dir: outputDir
  })
  return response.data
}

/**
 * 获取用户主目录
 */
export async function getHomeDirectory(): Promise<string> {
  const response = await axios.get(`${API_BASE}/home`)
  return response.data.path
}
