import { http } from "./index";
import { readingApi } from "./client";

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

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;
  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?: {
  page?: number;
  pageSize?: number;
  libraryType?: LibraryType;
  keyword?: string;
}) =>
  readingApi.getLibraries(params as any).then((res) => {
    const list = (res.data as any as ResourceLibrary[]) || [];
    const page = params?.page ?? 1;
    const pageSize = params?.pageSize ?? (list.length || 10);
    return {
      items: list,
      total: list.length,
      page,
      pageSize,
    };
  });

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

export const createLibrary = (data: CreateLibraryDto) =>
  readingApi
    .createLibrary(data as any)
    .then((res) => res.data as any as ResourceLibrary);

export const updateLibrary = (id: number, data: UpdateLibraryDto) =>
  readingApi
    .updateLibrary(id, data as any)
    .then((res) => res.data as any as ResourceLibrary);

export const deleteLibrary = (id: number) =>
  readingApi.deleteLibrary(id).then(() => undefined);

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

export const getResourceItems = (params?: {
  page?: number;
  pageSize?: number;
  libraryId?: number;
  fileType?: FileType;
  keyword?: string;
}) =>
  readingApi.getItems(params as any).then((res) => {
    const page = res.data as any;
    return {
      items: (page?.items as ResourceItem[]) ?? [],
      total: page?.total ?? 0,
      page: page?.page ?? params?.page ?? 1,
      pageSize: page?.pageSize ?? params?.pageSize ?? 10,
    };
  });

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

export const createResourceItem = (data: CreateResourceItemDto) =>
  readingApi
    .createItem(data as any)
    .then((res) => res.data as any as ResourceItem);

export const updateResourceItem = (id: number, data: UpdateResourceItemDto) =>
  readingApi
    .updateItem(id, data as any)
    .then((res) => res.data as any as ResourceItem);

export const deleteResourceItem = (id: number) =>
  readingApi.deleteItem(id).then(() => undefined);

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

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

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