import apiClient from "@/lib/axiosClient";
import { API_ENDPOINTS } from "@/lib/apiEndpoints";
import Cookies from "js-cookie";
export interface RawFullProfile {
  id: number;

  channel_id: string;
  channel_name: string;

  full_name: string;
  email: string;

  country_code: string;
  mobile_number: string;
  country_name: string;

  type: number;

  image_storage_type: number;
  image: string;

  cover_img_storage_type: number;
  cover_img: string;

  description: string;

  device_type: number;
  device_token: string;

  website: string;
  facebook_url: string;
  instagram_url: string;
  twitter_url: string;

  wallet_balance: number;
  wallet_earning: number;

  is_account_verify: number;

  bank_name: string;
  bank_code: string;
  bank_address: string;

  ifsc_no: string;
  account_no: string;

  front_id_proof_storage_type: number;
  front_id_proof: string;

  back_id_proof_storage_type: number;
  back_id_proof: string;

  address: string;
  city: string;
  state: string;
  country: string;

  pincode: number;

  user_penal_status: number;

  reference_code: string;

  push_notification_status: number;
  send_mail_status: number;

  status: number;

  created_at: string;
  updated_at: string;

  is_buy: number;
  is_block: number;

  total_content: number;
  total_subscriber: number;

  is_subscribe: number;

  package_name: string;
  package_price: string;
  package_image: string;

  ads_free: string;
  download_content: string;
  background_play: string;
}

export interface RawChannelContent {
  id: number;
  content_type: number;
  channel_id: string;
  category_id: number;
  title: string;
  description: string;
  landscape_img: string;
  portrait_img: string;
  content: string;
  content_duration: number;
  content_upload_type: string;
  is_rent: number;
  rent_price: number;
  total_view: number;
  total_like: number;
  total_dislike: number;
  is_like: number;
  is_comment: number;
  total_comment: number;
  status: number;
  created_at: string;
  channel_name: string;
  channel_image: string;
  category_name: string;
  language_name: string;
  total_subscriber: number;
  is_buy: number;
}

export interface RawFeedContent {
  id: number;
  channel_id: string;
  description: string;
  is_like: number;
  is_comment: number;
  total_like: number;
  total_comment: number;
  status: number;
  created_at: string;
  feed_content: {
    id: number;
    feed_id: number;
    content_type: number;
    image: string;
    video: string;
  }[];
  hastegs: { id: number; name: string }[];
  user_id: number;
  channel_name: string;
  full_name: string;
  profile_img: string;
  is_subscriber: number;
}

export interface RawRentContent {
  id: number;
  content_type: number;
  title: string;
  description?: string;
  landscape_img: string;
  portrait_img: string;
  content: string;
  content_upload_type: string;
  content_duration: number;
  is_rent: number;
  rent_price: number;
  total_view: number;
  total_like: number;
  total_dislike: number;
  is_user_like_dislike?: number;
  is_comment?: number;
  created_at: string;
  user_id?: number;
  channel_id?: string;
  channel_name: string;
  channel_image: string;
  category_name: string;
  language_name: string;
  is_subscribe?: number;
  is_rent_buy: number;
  is_buy: number;
}

interface PaginatedResponse<T> {
  status: number;
  message: string;
  result: T[];
  total_rows: number;
  total_page: number;
  current_page: number;
  more_page: boolean;
}

export interface UpdateProfilePayload {
  channel_name?: string;
  full_name?: string;
  email?: string;
  password?: string;
  country_code?: string;
  country_name?: string;
  description?: string;
  device_type?: string;
  website?: string;
  facebook_url?: string;
  instagram_url?: string;
  twitter_url?: string;
  is_account_verify?: string;
  bank_name?: string;
  bank_code?: string;
  bank_address?: string;
  ifsc_no?: string;
  account_no?: string;
  address?: string;
  city?: string;
  state?: string;
  country?: string;
  pincode?: string;
  user_penal_status?: string;
  push_notification_status?: string;
  send_mail_status?: string;
  image?: File | null;
  cover_img?: File | null;
  front_id_proof?: File | null;
  back_id_proof?: File | null;
}

export const profileService = {
  getProfile: async (): Promise<{
    status: number;
    result: RawFullProfile[];
  }> => {

    const res = await apiClient.post(
      API_ENDPOINTS.AUTH.PROFILE,
      {
        to_user_id: Cookies.get("user_id"),
      }
    );

    return res.data;
  },
  // apiClient.post(API_ENDPOINTS.AUTH.PROFILE, { to_user_id: Cookies.get("user_id") }).then((r) => r.data),

  getOtherProfile: (toUserId: string): Promise<{ status: number; result: RawFullProfile[] }> =>
    apiClient.post(API_ENDPOINTS.AUTH.PROFILE, { to_user_id: toUserId }).then((r) => r.data),

  updateProfile: (
    payload: UpdateProfilePayload
  ): Promise<{ status: number; message: string }> => {

    const form = new FormData();

    // Add user_id
    form.append("user_id", Cookies.get("user_id") || "");

    (Object.keys(payload) as (keyof UpdateProfilePayload)[]).forEach(
      (key) => {

        const val = payload[key];

        if (val === undefined || val === null) return;

        if (val instanceof File) {
          form.append(key, val);
        } else {
          form.append(key, String(val));
        }
      }
    );

    return apiClient
      .post(API_ENDPOINTS.UPDATE_PROFILE, form, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((r) => r.data);
  },

  getContentByChannel: (
    channelId: string,
    contentType: number,
    pageNo = 1,
  ): Promise<PaginatedResponse<RawChannelContent>> =>
    apiClient
      .post(API_ENDPOINTS.GET_CONTENT_BY_CHANNEL, {
        channel_id: channelId,
        content_type: contentType,
        page_no: pageNo,
      })
      .then((r) => r.data),

  getChannelFeed: (
    channelId: string,
    pageNo = 1,
  ): Promise<PaginatedResponse<RawFeedContent>> =>
    apiClient
      .post(API_ENDPOINTS.GET_CHANNEL_FEED, {
        channel_id: channelId,
        page_no: pageNo,
      })
      .then((r) => r.data),

  getUserRentContent: (pageNo = 1): Promise<PaginatedResponse<RawRentContent>> =>
    apiClient
      .post(API_ENDPOINTS.GET_USER_RENT_CONTENT, { page_no: pageNo })
      .then((r) => r.data),
};
