/* ─────────────────────────────────────────────────────────────────────────────
   Shared mapper — converts raw API items to ContentItem.
   Imported by historyService, likedContentService, watchLaterService.
───────────────────────────────────────────────────────────────────────────── */

import { formatDuration, formatViews, formatRelativeDate, formatlike } from "@/utils/format";
import { RawContentItem, ContentItem } from "@/types/content";
import { CT } from "@/config/contentTypes";

export function mapContentItem(raw: RawContentItem): ContentItem {
  const progressPct =
    raw.content_duration > 0 && raw.stop_time
      ? Math.min(Math.round((raw.stop_time / raw.content_duration) * 100), 100)
      : 0;

  return {
    id: String(raw.id),
    contentType: raw.content_type,
    title: raw.title,
    description: raw.description || "",
    thumbnail: raw.landscape_img,
    portraitImg: raw.portrait_img,
    videoUrl: raw.content,
    duration: raw.content_duration > 0 ? formatDuration(raw.content_duration) : "",
    durationMs: raw.content_duration,
    views: formatViews(raw.total_view),
    likes: formatlike(raw.total_like),
    dislikes: formatlike(raw.total_dislike),
    uploadedAt: formatRelativeDate(raw.created_at),
    channelName: raw.channel_name,
    channelImage: raw.channel_image,
    channelId: raw.channel_id ?? "",
    channelUserId: String(raw.user_id),
    category: raw.category_name,
    language: raw.language_name,
    isDownload: raw.is_download === 1,
    isPremium: raw.is_rent === 1,
    stopTime: raw.stop_time ?? 0,
    progressPct,
    totalComments: raw.total_comment,
    totalSubscribers: formatlike(raw.total_subscriber),
    isSubscribed: raw.is_subscribe === 1,
    isLive: false,
  };
}
