import { requestClient } from '#/api/request';

// 存储配置接口
export interface StorageConfig {
  driver: 'local' | 'oss' | 'cos' | 'qiniu' | 'upyun' | 's3';
  // 本地存储配置
  local_path?: string;
  local_domain?: string;
  // 阿里云OSS配置
  oss_access_key_id?: string;
  oss_access_key_secret?: string;
  oss_bucket?: string;
  oss_region?: string;
  oss_domain?: string;
  oss_is_private?: boolean;
  // 腾讯云COS配置
  cos_secret_id?: string;
  cos_secret_key?: string;
  cos_bucket?: string;
  cos_region?: string;
  // 七牛云配置
  qiniu_access_key?: string;
  qiniu_secret_key?: string;
  qiniu_bucket?: string;
  qiniu_domain?: string;
  // 又拍云配置
  upyun_username?: string;
  upyun_password?: string;
  upyun_bucket?: string;
  upyun_domain?: string;
  // AWS S3配置
  s3_access_key_id?: string;
  s3_secret_access_key?: string;
  s3_bucket?: string;
  s3_region?: string;
  s3_endpoint?: string;
  // 上传限制
  max_size: number;
  allowed_types: string[];
  // 图片处理
  thumbnail_enabled: boolean;
  thumbnail_width?: number;
  thumbnail_height?: number;
  // 其他设置
  enabled: boolean;
  is_default: boolean;
}

// 存储统计接口
export interface StorageStats {
  total_files: number;
  total_size: number;
  used_space: string;
  available_space: string;
  files_by_type: Record<string, number>;
  upload_trend: Array<{
    date: string;
    count: number;
    size: number;
  }>;
}

// 文件信息接口
export interface FileInfo {
  id: string;
  name: string;
  path: string;
  url: string;
  size: number;
  type: string;
  mime_type: string;
  driver: string;
  created_at: string;
  updated_at: string;
}

// 上传结果接口
export interface UploadResult {
  success: boolean;
  url: string;
  path: string;
  size: number;
  type: string;
  name: string;
}

/**
 * 获取存储配置
 */
export function getStorageConfigApi() {
  return requestClient.get<StorageConfig>('/admin/settings/storage');
}

/**
 * 更新存储配置
 */
export function updateStorageConfigApi(data: StorageConfig) {
  return requestClient.put('/admin/settings/storage', data);
}

/**
 * 测试存储配置
 */
export function testStorageApi(formData: FormData) {
  return requestClient.post<UploadResult>('/admin/settings/storage/test', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 重置存储配置
 */
export function resetStorageConfigApi() {
  return requestClient.post('/admin/settings/storage/reset');
}

/**
 * 获取存储统计信息
 */
export function getStorageStatsApi() {
  return requestClient.get<StorageStats>('/admin/settings/storage/stats');
}

/**
 * 获取文件列表
 */
export function getFileListApi(params?: {
  page?: number;
  limit?: number;
  type?: string;
  driver?: string;
  keyword?: string;
}) {
  return requestClient.get<{
    list: FileInfo[];
    total: number;
    page: number;
    limit: number;
  }>('/admin/files', { params });
}

/**
 * 删除文件
 */
export function deleteFileApi(id: string) {
  return requestClient.delete(`/admin/files/${id}`);
}

/**
 * 批量删除文件
 */
export function batchDeleteFilesApi(ids: string[]) {
  return requestClient.delete('/admin/files/batch', { data: { ids } });
}

/**
 * 上传文件
 */
export function uploadFileApi(formData: FormData) {
  return requestClient.post<UploadResult>('/admin/files/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 获取文件详情
 */
export function getFileDetailApi(id: string) {
  return requestClient.get<FileInfo>(`/admin/files/${id}`);
}

/**
 * 清理无效文件
 */
export function cleanInvalidFilesApi() {
  return requestClient.post('/admin/files/clean');
}

/**
 * 同步文件信息
 */
export function syncFilesApi() {
  return requestClient.post('/admin/files/sync');
}

/**
 * 验证存储配置
 */
export function validateStorageConfigApi(data: Partial<StorageConfig>) {
  return requestClient.post<{ valid: boolean; message?: string }>(
    '/admin/settings/storage/validate',
    data
  );
}

/**
 * 获取存储驱动模板配置
 */
export function getStorageDriverTemplateApi(driver: string) {
  return requestClient.get<Partial<StorageConfig>>(
    `/admin/settings/storage/template/${driver}`
  );
}