/**
 * QuarkDriver — 夸克网盘统一驱动（向后兼容壳子）
 *
 * 所有方法委托到子模块（纯函数，不持有状态）。
 * 新代码应直接导入子模块函数。
 *
 * 模块结构：
 *   quark-api.ts       — HTTP 封装、headers、params、共享工具函数
 *   quark-auth.ts      — Cookie 验证
 *   quark-storage.ts   — 转存流水线、目录管理、递归统计
 *   quark-share.ts     — 分享链接解析、转存任务、创建分享链接
 *   quark-rename.ts    — 防和谐重命名（文件名/目录名）
 *   quark-cleanup.ts   — 容量信息、空间清理
 *   quark-ad-cleanup.ts — 广告清理
 *
 * This file (quark.driver.ts) is kept for backward compatibility.
 * All sub-module exports are re-exported from here.
 */
import { validate, fetchNickname } from './quark-auth';
import {
  saveFromShare,
  createDir,
  findOrCreateDir,
  countRecursive,
} from './quark-storage';
import {
  acquireStoken,
  createShareLink,
  getDetailAt,
  getShareFiles,
  renameFile,
  saveFiles,
  waitForTask,
} from './quark-share';
import {
  calculateUsedSpace,
  cleanupBySpaceThreshold,
  cleanupOldDateFolders,
  emptyTrash,
  getStorageInfo,
  getStorageInfoQuick,
  trashFiles,
} from './quark-cleanup';
import { listRootDir } from './quark-api';

// Re-export all sub-module functions
export * from './quark-api';
export * from './quark-auth';
export * from './quark-storage';
export * from './quark-share';
export * from './quark-rename';
export * from './quark-cleanup';
export * from './quark-ad-cleanup';
export { validate };

export interface QuarkConfig {
  cookie: string;
  nickname?: string;
}

export interface StorageInfo {
  used: string;
  total: string;
  usedBytes: number;
  totalBytes: number;
}

interface ShareFile {
  fid: string;
  file_name: string;
  share_fid_token: string;
  dir: boolean;
  size?: number;
}

/**
 * QuarkDriver — 向后兼容的驱动类。
 * 所有方法委托到纯函数模块。
 */
export class QuarkDriver {
  private config: QuarkConfig;

  constructor(config: QuarkConfig) {
    this.config = config;
  }

  get cookie(): string {
    return this.config.cookie;
  }

  // ==================== Auth ====================
  async validate(): Promise<boolean> {
    return validate(this.cookie);
  }

  async fetchNickname(): Promise<string | null> {
    return fetchNickname(this.cookie);
  }

  // ==================== Storage (Save from Share) ====================
  async saveFromShare(shareUrl: string, sourceTitle?: string): Promise<any> {
    return saveFromShare(this.cookie, this.config.nickname || '', shareUrl, sourceTitle || '');
  }

  async createDir(dirName: string): Promise<any> {
    return createDir(this.cookie, dirName);
  }

  async findOrCreateDir(dirName: string): Promise<any> {
    return findOrCreateDir(this.cookie, dirName);
  }

  async countRecursive(pdirFid: string): Promise<any> {
    return countRecursive(this.cookie, pdirFid);
  }

  // ==================== Share ====================
  async createShareLink(fileId: string): Promise<any> {
    return createShareLink(this.cookie, fileId);
  }

  async renameFile(fid: string, newName: string): Promise<any> {
    return renameFile(this.cookie, fid, newName);
  }

  // ==================== Storage Info ====================
  async getStorageInfoQuick(fallbackTotal?: number): Promise<any> {
    return getStorageInfoQuick(this.cookie, fallbackTotal);
  }

  async getStorageInfo(onBackgroundComplete?: (info: any) => void): Promise<any> {
    return getStorageInfo(this.cookie, onBackgroundComplete);
  }

  async calculateUsedSpace(): Promise<any> {
    return calculateUsedSpace(this.cookie);
  }

  // ==================== Cleanup ====================
  async listRootDir(): Promise<any> {
    return listRootDir(this.cookie);
  }

  async trashFiles(fids: string[]): Promise<any> {
    return trashFiles(this.cookie, fids);
  }

  async emptyTrash(): Promise<boolean> {
    return emptyTrash(this.cookie);
  }

  async cleanupOldDateFolders(days: number, whitelistDirs?: string[]): Promise<any> {
    return cleanupOldDateFolders(this.cookie, days, whitelistDirs);
  }

  async cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number, whitelistDirs?: string[]): Promise<any> {
    return cleanupBySpaceThreshold(this.cookie, thresholdPercent, deletePercent, whitelistDirs);
  }
}
