import { http } from './index';

// ==================== 类型定义 ====================

export type LibraryType = 'PICTURE_BOOK' | 'MATERIAL' | 'TEMPLATE';
export type FileType = 'IMAGE' | 'PDF' | 'VIDEO' | 'AUDIO' | 'PPT' | 'OTHER';

export interface ResourceLibrary {
  id: number;
  name: string;
  libraryType: LibraryType;
  description?: string;
  coverImage?: string;
  createdBy?: number;
  status?: string;
  sortOrder?: number;
  itemCount?: number;
  createdAt?: string;
  updatedAt?: string;
}

export interface ResourceItem {
  id: number;
  libraryId: number;
  title: string;
  description?: string;
  fileType: FileType;
  filePath: string;
  fileSize?: number;
  tags?: string[];
  sortOrder?: number;
  createdAt?: string;
  updatedAt?: string;
  library?: {
    id: number;
    name: string;
    libraryType: LibraryType;
  };
}

export interface CreateLibraryDto {
  name: string;
  libraryType: LibraryType;
  description?: string;
  coverImage?: string;
}

export interface UpdateLibraryDto {
  name?: string;
  description?: string;
  coverImage?: string;
  sortOrder?: number;
}

export interface CreateResourceItemDto {
  libraryId: number;
  title: string;
  description?: string;
  fileType: FileType;
  filePath: string;
  fileSize?: number;
  tags?: string[];
}

export interface UpdateResourceItemDto {
  title?: string;
  description?: string;
  tags?: string[];
  sortOrder?: number;
}

export interface ResourceStats {
  totalLibraries: number;
  totalItems: number;
  itemsByType: Record<FileType, number>;
  itemsByLibraryType: Record<LibraryType, number>;
}

// ==================== 资源库管理 ====================

export const getLibraries = (params?: {
  pageNum?: number;
  pageSize?: number;
  libraryType?: LibraryType;
  keyword?: string;
}) =>
  http.get<{ list: ResourceLibrary[]; total: number; pageNum: number; pageSize: number; pages: number }>(
    '/v1/admin/resources/libraries',
    { params }
  );

export const getLibrary = (id: number) =>
  http.get<ResourceLibrary & { items: ResourceItem[] }>(`/v1/admin/resources/libraries/${id}`);

export const createLibrary = (data: CreateLibraryDto) =>
  http.post<ResourceLibrary>('/v1/admin/resources/libraries', data);

export const updateLibrary = (id: number, data: UpdateLibraryDto) =>
  http.put<ResourceLibrary>(`/v1/admin/resources/libraries/${id}`, data);

export const deleteLibrary = (id: number) =>
  http.delete(`/v1/admin/resources/libraries/${id}`);

// ==================== 资源项目管理 ====================

export const getResourceItems = (params?: {
  pageNum?: number;
  pageSize?: number;
  libraryId?: number;
  fileType?: FileType;
  keyword?: string;
}) =>
  http.get<{ list: ResourceItem[]; total: number; pageNum: number; pageSize: number; pages: number }>(
    '/v1/admin/resources/items',
    { params }
  );

export const getResourceItem = (id: number) =>
  http.get<ResourceItem>(`/v1/admin/resources/items/${id}`);

export const createResourceItem = (data: CreateResourceItemDto) =>
  http.post<ResourceItem>('/v1/admin/resources/items', data);

export const updateResourceItem = (id: number, data: UpdateResourceItemDto) =>
  http.put<ResourceItem>(`/v1/admin/resources/items/${id}`, data);

export const deleteResourceItem = (id: number) =>
  http.delete(`/v1/admin/resources/items/${id}`);

export const batchDeleteResourceItems = (ids: number[]) =>
  http.post<{ message: string }>('/v1/admin/resources/items/batch-delete', ids);

// ==================== 统计数据 ====================

export const getResourceStats = () =>
  http.get<ResourceStats>('/v1/admin/resources/stats');
