import apiClient from "@/lib/axiosClient";
import { API_ENDPOINTS } from "@/lib/apiEndpoints";

export interface RawPlaylist {
  id: number;
  content_type: number;
  channel_id: string;
  title: string;
  portrait_img: string;
  playlist_type: number;
  created_at: string;
  playlist_image?: string[];
}

export interface Playlist {
  id: string;
  title: string;
  channelId: string;
  playlistType: number;
  images: string[];
  coverImg: string;
}

export interface PlaylistContentItem {
  id: string;
  title: string;
  thumbnail: string;
  contentType: number;
  duration: string;
  channelName: string;
}

export const playlistService = {
  getPlaylistContent: async (playlistId: string, contentType: number): Promise<PlaylistContentItem[]> => {
    const res = await apiClient.post(API_ENDPOINTS.PLAYLIST.GET_CONTENT, {
      playlist_id:  playlistId,
      content_type: contentType,
    });
    return (res.data?.result ?? []).map((r: {
      id: number; title: string; landscape_img: string; portrait_img: string;
      content_type: number; content_duration: number; channel_name: string;
    }) => ({
      id:          String(r.id),
      title:       r.title,
      thumbnail:   r.landscape_img || r.portrait_img,
      contentType: r.content_type,
      duration:    r.content_duration > 0 ? `${Math.floor(r.content_duration / 60)}:${String(r.content_duration % 60).padStart(2, "0")}` : "",
      channelName: r.channel_name,
    }));
  },

  getMyPlaylists: async (channelId: string): Promise<Playlist[]> => {
    const res = await apiClient.post(API_ENDPOINTS.PLAYLIST.GET, {
      channel_id:   channelId,
      content_type: 5, // 5 = playlist
      page_no:      1,
    });
    const raw: RawPlaylist[] = res.data?.result ?? [];
    return raw.map((p) => ({
      id:          String(p.id),
      title:       p.title,
      channelId:   p.channel_id,
      playlistType: p.playlist_type,
      images:      p.playlist_image ?? [],
      coverImg:    p.playlist_image?.[0] ?? p.portrait_img ?? "",
    }));
  },

  addOrRemove: (params: {
    channelId: string;
    playlistId: string;
    contentType: number;
    contentId: string;
    episodeId?: string;
    type: 1 | 2; // 1 = add, 2 = remove
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.PLAYLIST.ADD_REMOVE, {
        channel_id:   params.channelId,
        playlist_id:  params.playlistId,
        content_type: params.contentType,
        content_id:   params.contentId,
        episode_id:   params.episodeId ?? "0",
        type:         params.type,
      })
      .then((r) => r.data),

  create: (params: {
    channelId: string;
    playlistType: number;
    title: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.PLAYLIST.CREATE, {
        channel_id:    params.channelId,
        playlist_type: params.playlistType,
        title:         params.title,
      })
      .then((r) => r.data),

  bulkAdd: (params: {
    channelId: string;
    playlistId: string;
    contentType: number;
    contentId: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.PLAYLIST.BULK_ADD, {
        channel_id:   params.channelId,
        playlist_id:  params.playlistId,
        content_type: params.contentType,
        content_id:   params.contentId,
      })
      .then((r) => r.data),

  deletePlaylist: (playlistId: string): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.PLAYLIST.DELETE, { content_id: playlistId })
      .then((r) => r.data),

  editPlaylist: (params: {
    playlistId: string;
    title: string;
    playlistType: number;
    description?: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.PLAYLIST.EDIT, {
        content_id:    params.playlistId,
        title:         params.title,
        playlist_type: params.playlistType,
        description:   params.description ?? "",
      })
      .then((r) => r.data),
};
