import apiClient from "@/lib/axiosClient";
import { API_ENDPOINTS } from "@/lib/apiEndpoints";
import Cookies from "js-cookie";
/* ── Watch Later ─────────────────────────────────────────────────────────── */

/* ── Content Management ──────────────────────────────────────────────────── */

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

  getSubscriberList: async (): Promise<SubscriberItem[]> => {
    const res = await apiClient.post(API_ENDPOINTS.CONTENT_MGMT.GET_SUBSCRIBER_LIST, {
      user_id: Cookies.get("user_id"),

    });
    return (res.data?.result ?? []).map((r: {
      id: number; channel_name: string; full_name: string;
      image: string; total_subscriber: number;
    }) => ({
      id: String(r.id),
      name: r.channel_name || r.full_name || "User",
      avatar: r.image ?? "",
      subCount: r.total_subscriber ?? 0,
    }));
  },

  blockChannel: (params: {
    blockUserId: string;
    blockChannelId: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.CONTENT_MGMT.BLOCK, {
        block_user_id: params.blockUserId,
        block_channel_id: params.blockChannelId,
      })
      .then((r) => r.data),
};

export interface SubscriberItem {
  id: string;
  name: string;
  avatar: string;
  subCount: number;
}

export const watchLaterService = {
  toggle: (params: {
    contentType: number;
    contentId: string;
    episodeId?: string;
    type: 1 | 2; // 1 = add, 2 = remove
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.WATCH_LATER, {
        content_type: params.contentType,
        content_id: params.contentId,
        episode_id: params.episodeId ?? "0",
        type: params.type,
      })
      .then((r) => r.data),
};

/* ── Report ──────────────────────────────────────────────────────────────── */

export interface ReportReason {
  id: number;
  reason: string;
}

export const reportService = {
  getReasons: async (): Promise<ReportReason[]> => {
    const res = await apiClient.post(API_ENDPOINTS.REPORT.GET_REASONS);
    return (res.data?.result ?? []).filter((r: ReportReason & { status: number }) => r.status === 1);
  },

  submit: (params: {
    reportUserId: string;
    contentId: string;
    contentType: number;
    message: string;
  }): Promise<{ status: number; message: string }> =>
    apiClient
      .post(API_ENDPOINTS.REPORT.SUBMIT, {
        report_user_id: params.reportUserId,
        content_id: params.contentId,
        content_type: params.contentType,
        message: params.message,
      })
      .then((r) => r.data),
};
