import apiClient from "@/lib/axiosClient";
import { API_ENDPOINTS } from "@/lib/apiEndpoints";
import { formatViews, formatDuration, formatlike } from "@/utils/format";

/* ── Raw API types ─────────────────────────────────────────────────────────── */

export interface RawMusicItem {
  id: number;
  content_type: number;
  title: string;
  portrait_img: string;
  landscape_img: string;
  content: string;
  content_upload_type: string;
  content_duration: number;
  channel_name: string;
  channel_image: string;
  category_name: string;
  language_name: string;
  total_view: number;
  total_like: number;
  total_comment: number;
  is_like: number;
  is_comment: number;
  is_download: number;
  is_rent: number;
  rent_price: number;
  total_episode?: number;
  episode_array?: { id: number; name: string; portrait_img: string; description: string }[];
  /* playlist-specific */
  playlist_image?: string[];
  playlist_type?: number;
  /* language-specific (screen_layout: "language") */
  name?: string;
  image?: string;
}

export interface RawMusicSection {
  id: number;
  title: string;
  short_title: string;
  screen_layout: string;
  no_of_content: number;
  view_all: number;
  sort_order: number;
  data: RawMusicItem[];
}

export interface MusicSectionResponse {
  status: number;
  message: string;
  result: RawMusicSection[];
  total_rows: number;
  total_page: number;
  current_page: number;
  more_page: boolean;
}

/* ── Mapped types ──────────────────────────────────────────────────────────── */

export interface MusicItem {
  id: string;
  contentType: number;
  title: string;
  thumbnail: string;
  landscapeImg: string;
  audioUrl: string;
  channelName: string;
  channelImage: string;
  duration: string;
  durationMs: number;
  views: string;
  likes: string;
  category: string;
  language: string;
  isDownload: boolean;
  isRent: boolean;
  rentPrice: number;
  /* optional — for content actions (watch later, playlist, report) */
  channelId?: string;
  channelUserId?: string;
  /* playlist */
  playlistImages?: string[];
  /* podcast */
  totalEpisodes?: number;
  /* language section */
  langName?: string;
}

export interface MusicSection {
  id: string;
  title: string;
  shortTitle: string;
  screenLayout: string;
  viewAll: boolean;
  items: MusicItem[];
}

/* ── Mapper ────────────────────────────────────────────────────────────────── */

function mapItem(raw: RawMusicItem): MusicItem {
  return {
    id: String(raw.id),
    contentType: raw.content_type,
    title: raw.title || raw.name || "",
    thumbnail: raw.portrait_img,
    landscapeImg: raw.landscape_img,
    audioUrl: raw.content || "",
    channelName: raw.channel_name || "",
    channelImage: raw.channel_image || "",
    duration: raw.content_duration ? formatDuration(raw.content_duration) : "",
    durationMs: raw.content_duration || 0,
    views: formatViews(raw.total_view || 0),
    likes: formatlike(raw.total_like || 0),
    category: raw.category_name || "",
    language: raw.language_name || "",
    isDownload: !!raw.is_download,
    isRent: !!raw.is_rent,
    rentPrice: raw.rent_price || 0,
    playlistImages: raw.playlist_image,
    totalEpisodes: raw.total_episode,
    langName: raw.name,
  };
}

export interface MusicSectionDetailResponse {
  status: number;
  message: string;
  result: RawMusicItem[];
  total_rows: number;
  total_page: number;
  current_page: number;
  more_page: boolean;
}

export const musicSectionService = {
  getSections: (contentType: number): Promise<MusicSectionResponse> =>
    apiClient
      .post(API_ENDPOINTS.GET_MUSIC_SECTION, {
        content_type: contentType,
        is_home_screen: 2,
        page_no: 1,
      })
      .then((r) => r.data),

  getSectionDetail: (sectionId: string, pageNo: number): Promise<MusicSectionDetailResponse> =>
    apiClient
      .post(API_ENDPOINTS.MUSIC.DETAIL, { section_id: sectionId, page_no: pageNo })
      .then((r) => r.data),

  mapSection: (raw: RawMusicSection): MusicSection => ({
    id: String(raw.id),
    title: raw.title,
    shortTitle: raw.short_title,
    screenLayout: raw.screen_layout,
    viewAll: !!raw.view_all,
    items: raw.data.map(mapItem),
  }),

  mapItem,
};
