import apiClient from "@/lib/axiosClient";
import { API_ENDPOINTS } from "@/lib/apiEndpoints";
import { RawContentItem, ContentApiResponse } from "@/types/content";
import { mapContentItem } from "./contentService";

export type { ContentItem as HistoryItem } from "@/types/content";

export const historyService = {
  getHistory: (contentType: number, pageNo: number): Promise<ContentApiResponse> =>
    apiClient
      .post(API_ENDPOINTS.GET_CONTENT_TO_HISTORY, {
        content_type: contentType,
        page_no: pageNo,
      })
      .then((r) => r.data),

  addToHistory: (params: {
    contentType: number;
    contentId: string;
    episodeId?: string;
    stopTime?: number;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.ADD_CONTENT_TO_HISTORY, {
        content_type: params.contentType,
        content_id:   params.contentId,
        episode_id:   params.episodeId ?? "0",
        stop_time:    params.stopTime  ?? 0,
      })
      .then((r) => r.data),

  removeFromHistory: (params: {
    contentType: number;
    contentId: string;
    episodeId?: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.REMOVE_CONTENT_FROM_HISTORY, {
        content_type: params.contentType,
        content_id:   params.contentId,
        episode_id:   params.episodeId ?? "0",
      })
      .then((r) => r.data),

  mapItem: (raw: RawContentItem) => mapContentItem(raw),
};
