import { api } from "@/lib/api-client";
import type { MediaItem, MediaMovement, LibraryCard, MediaCreateRequest } from "@/types/library";
import type { PaginatedResponse, PaginationParams, ApiSuccessResponse } from "@/types";

export const libraryService = {
  async listMedia(params?: PaginationParams): Promise<PaginatedResponse<MediaItem>> {
    return api.get<PaginatedResponse<MediaItem>>("/library/media", params as Record<string, string | number | boolean | undefined>);
  },

  async createMedia(data: MediaCreateRequest): Promise<MediaItem> {
    return api.post<MediaItem>("/library/media", data);
  },

  async listMovements(params?: PaginationParams): Promise<PaginatedResponse<MediaMovement>> {
    return api.get<PaginatedResponse<MediaMovement>>("/library/movements", params as Record<string, string | number | boolean | undefined>);
  },

  async createMovement(data: Partial<Omit<MediaMovement, "id" | "media_name" | "student_name" | "faculty_name">>): Promise<MediaMovement> {
    return api.post<MediaMovement>("/library/movements", data);
  },

  async updateMovement(id: number, data: Partial<Omit<MediaMovement, "id" | "media_name" | "student_name" | "faculty_name">>): Promise<MediaMovement> {
    return api.patch<MediaMovement>(`/library/movements/${id}`, data);
  },

  async deleteMedia(id: number): Promise<ApiSuccessResponse> {
    return api.delete<ApiSuccessResponse>(`/library/media/${id}`);
  },

  async returnMovement(id: number): Promise<MediaMovement> {
    return api.post<MediaMovement>(`/library/movements/${id}/return`);
  },

  async listCards(params?: PaginationParams): Promise<PaginatedResponse<LibraryCard>> {
    return api.get<PaginatedResponse<LibraryCard>>("/library/cards", params as Record<string, string | number | boolean | undefined>);
  },

  async createCard(data: { student_id: number }): Promise<LibraryCard> {
    return api.post<LibraryCard>("/library/cards", data);
  },
};
