/**
 * Admin API - Storage Management
 */

import { api as client } from '../apiClient';

export interface StorageStats {
  totalFiles: number;
  totalSize: number;
  totalCapacity?: number;
  usedSpace?: number;
  usagePercent?: number;
  fileCount?: number;
}

export interface StorageFile {
  id: string;
  key: string;
  path: string;
  size: number;
  createdAt: number;
  userId: string;
  username?: string;
  projectName?: string;
  projectId?: string;
}

export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

export interface PaginatedResponse<T> {
  items: T[];
  page: number;
  pageSize: number;
  total: number;
  totalPages: number;
}

export const getStorageStats = async (): Promise<StorageStats> => {
  const response = await client.get('/api/admin/storage/stats');
  return response.data;
};

export const listStorageFiles = async (params: { page?: number; pageSize?: number; userId?: string; projectId?: string }): Promise<PaginatedResponse<StorageFile>> => {
  const response = await client.get('/api/admin/storage/files', { params });
  const { items, pagination } = response.data.data;
  return {
    items,
    ...pagination,
  };
};

export interface StorageRankingItem {
  userId: string;
  username: string;
  totalSize: number;
  storageUsed: number;
  fileCount: number;
  rank: number;
}

export interface StorageRankingResponse {
  items: StorageRankingItem[];
}

export const getStorageUserRanking = async (params: { page?: number; pageSize?: number; limit?: number }): Promise<StorageRankingResponse> => {
  const response = await client.get('/api/admin/storage/ranking', { params });
  return response.data.data;
};

export const cleanupOrphanFiles = async (_params?: { dryRun?: boolean }): Promise<{ deleted: number }> => {
  const response = await client.post('/api/admin/storage/cleanup');
  return response.data;
};
