import { PORT_NONE } from "@/api/config/servicePort";
import http from "@/api";
import httpStore from "@/api/indexStore";
import httpApi from "@/api/indexApi";

/**
 * 动态管理相关接口
 */

// 获取动态列表（旧接口）
export const getDynamicList = (params: {
  type: string; // recommend 推荐, follow 关注
  page: number;
  pageSize: number;
}) => {
  return http.get(PORT_NONE + `/dynamic/getDynamicList`, params);
};

// 获取用户动态列表（新接口）
// 使用 httpStore，baseURL 为 /api/alienStore
// 最终请求路径：/api/alienStore/userDynamics/getUserDynamics
export const getUserDynamics = (params: {
  type: number; // 2 表示动态类型
  isfollowed: number; // 0 推荐, 1 关注
  myself: number; // 0 他人, 1 自己
  page: number;
  size: number;
  phoneId: string; // 店铺ID
}) => {
  return httpStore.get(PORT_NONE + `/userDynamics/getUserDynamics`, params);
};

// 获取他人动态列表
export const getUserDynamicsList = (params: {
  myselfPhoneId: string; // 当前登录用户的phoneId
  phoneId: string; // 目标用户的phoneId
  type: number; // 2 表示动态类型
  page: number;
  size: number;
}) => {
  return httpStore.get(PORT_NONE + `/userDynamics/getUserDynamicsList`, params);
};

// 获取用户动态详情
export const getUserDynamicDetail = (params: {
  dynamicId: number | string; // 动态ID
  phoneId: string; // 店铺ID
}) => {
  return httpStore.get(PORT_NONE + `/userDynamics/getUserDynamicDetail`, params);
};

// 发布动态（旧接口）
export const publishDynamic = (params: { title: string; content: string; imageUrl: string }) => {
  return http.post(PORT_NONE + `/dynamic/publishDynamic`, params);
};

// 发布/更新动态（新接口）
export const addOrUpdateDynamic = (params: {
  address?: string; // 经纬度
  addressName?: string; // 地址名称
  addressProvince?: string; // 省市
  context: string; // 正文内容
  createId: number | string; // 创建者ID
  draft: number; // 0表示发布，1表示草稿
  imagePath: string; // 图片/视频路径（多个用逗号分隔）
  phoneId: string; // 店铺ID
  title: string; // 标题
  type: string; // "2" 表示动态类型
  id?: number | string; // 动态ID（更新时需要）
}) => {
  return httpStore.post(PORT_NONE + `/userDynamics/addOrUpdate`, params);
};

// 删除动态
export const deleteDynamic = (params: { id: number }) => {
  return http.post(PORT_NONE + `/dynamic/deleteDynamic`, params);
};

// 点赞动态
export const likeDynamic = (params: { id: number }) => {
  return http.post(PORT_NONE + `/dynamic/likeDynamic`, params);
};

// 取消点赞
export const unlikeDynamic = (params: { id: number }) => {
  return http.post(PORT_NONE + `/dynamic/unlikeDynamic`, params);
};

// 获取动态详情
export const getDynamicDetail = (params: { id: number }) => {
  return http.get(PORT_NONE + `/dynamic/getDynamicDetail`, params);
};

// 上传图片
export const uploadDynamicImage = (params: FormData) => {
  return http.post(PORT_NONE + `/dynamic/uploadImage`, params);
};

// 保存草稿
export const saveDraft = (params: {
  title: string;
  content: string;
  images: string[];
  location?: string;
  locationId?: string;
}) => {
  return http.post(PORT_NONE + `/dynamic/saveDraft`, params);
};

// 获取草稿列表
export const getDraftList = (params: { page: number; pageSize: number }) => {
  return http.get(PORT_NONE + `/dynamic/getDraftList`, params);
};

// 删除草稿
export const deleteDraft = (params: { id: number }) => {
  return http.post(PORT_NONE + `/dynamic/deleteDraft`, params);
};

// 获取位置列表
export const getLocationList = (params: { keyword?: string }) => {
  return http.get(PORT_NONE + `/dynamic/getLocationList`, params);
};

// 获取我的动态列表
export const getMyDynamicList = (params: { page: number; pageSize: number }) => {
  return http.get(PORT_NONE + `/dynamic/getMyDynamicList`, params);
};

// 获取我赞过的列表
export const getMyLikedList = (params: { page: number; pageSize: number }) => {
  return http.get(PORT_NONE + `/dynamic/getMyLikedList`, params);
};

// 获取用户信息
export const getUserInfo = () => {
  return http.get(PORT_NONE + `/dynamic/getUserInfo`);
};

// 获取关系列表（好友/关注/粉丝）
export const getRelationList = (params: { type: "friend" | "follow" | "fans" }) => {
  return http.get(PORT_NONE + `/dynamic/getRelationList`, params);
};

// 关注用户（旧接口）
export const followUser = (params: { userId: number }) => {
  return http.post(PORT_NONE + `/dynamic/followUser`, params);
};

// 取消关注用户（旧接口）
export const unfollowUser = (params: { userId: number }) => {
  return http.post(PORT_NONE + `/dynamic/unfollowUser`, params);
};

// 关注/取消关注用户（新接口）
export const toggleFollowUser = (params: {
  followedId: string | number; // 被关注用户phoneId
  fansId: string; // 当前用户phoneId
  fansType: number; // 2表示关注
}) => {
  return httpStore.post(PORT_NONE + `/user/addFans`, params);
};

// 取消关注用户
export const cancelFollowed = (params: {
  followedId: string; // 被关注用户phoneId
  fansId: string; // 当前用户phoneId
}) => {
  return httpStore.post(PORT_NONE + `/user/cancelFollewed`, params);
};

// 点赞动态（新接口）- 表单方式提交
export const likeDynamicNew = (params: {
  userId: string; // 当前用户phoneId
  huifuId: number; // 动态ID
  type: number; // 类型：2表示点赞
}) => {
  const formData = new FormData();
  formData.append("userId", params.userId);
  formData.append("huifuId", String(params.huifuId));
  formData.append("type", String(params.type));
  return httpStore.post(PORT_NONE + `/comment/like`, formData);
};

// 取消点赞动态（新接口）- 表单方式提交
export const unlikeDynamicNew = (params: {
  userId: string; // 当前用户phoneId
  huifuId: number; // 动态ID
  type: number; // 类型：2表示点赞
}) => {
  const formData = new FormData();
  formData.append("userId", params.userId);
  formData.append("huifuId", String(params.huifuId));
  formData.append("type", String(params.type));
  return httpStore.post(PORT_NONE + `/comment/cancelLike`, formData);
};

// 列表点赞动态 - 表单方式提交
export const likeDynamicList = (params: {
  dynamicId: number; // 动态ID
  phoneId: string; // 当前用户phoneId
}) => {
  const formData = new FormData();
  formData.append("dynamicId", String(params.dynamicId));
  formData.append("phoneId", params.phoneId);
  return httpStore.post(PORT_NONE + `/userDynamics/like`, formData);
};

// 举报动态
export const reportDynamic = (params: { dynamicId: number; reason: string; description: string; images: string[] }) => {
  return http.post(PORT_NONE + `/dynamic/reportDynamic`, params);
};

// 举报动态（新接口）
export const reportUserViolation = (params: {
  dynamicsId: number | string;
  otherReasonContent: string;
  reportContextType: string;
  reportEvidenceImg: string;
  reportedUserId: string | number;
  reportedUserType: number;
  reportingUserId: string | number;
  reportingUserType: number;
  violationType: number;
}) => {
  return httpStore.post(PORT_NONE + `/user-violation/reporting`, params);
};

// 拉黑用户
export const blockUser = (params: {
  blockerType: number; // 拉黑者类型：1表示商家
  blockedType: number; // 被拉黑者类型：1表示商家
  blockerId: number | string; // 拉黑者ID
  blockedId: number | string; // 被拉黑者ID
}) => {
  return httpApi.post(`alienStore/life-blacklist/blackList`, params, { loading: false });
};

// 根据手机号获取用户ID
export const getUserByPhone = (params: {
  phone: string; // 手机号
}) => {
  return httpStore.get(PORT_NONE + `/store/user/getUserByPhone`, params);
};

// 获取黑名单列表
export const getBlackList = (params: {
  userType: number; // 用户类型：1表示商家
  userId: number | string; // 用户ID（store_user表的ID）
  type: number; // 类型：0表示查询所有
}) => {
  return httpApi.get(`alienStore/life-blacklist/blackListByUserId`, params, { loading: false });
  // return httpStore.get(  `/life-blacklist/blackListByUserId`, params);
};

// 取消拉黑
export const unblockUser = (params: {
  id: number | string; // 拉黑记录ID
  blockedId: number | string; // 被拉黑方ID
  blockedType: number; // 被拉黑方类型：1商户，2用户
  blockerId: number | string; // 拉黑方ID
  blockerType: number; // 拉黑方类型：1商户，2用户
}) => {
  return httpApi.post(`alienStore/life-blacklist/cancelBlacklist`, params, { loading: false });
  // return httpStore.post(PORT_NONE + `/life-blacklist/cancelBlacklist`, params);
};

// 获取评论列表
export const getCommentsList = (params: { dynamicId: number; sortType: string }) => {
  return http.get(PORT_NONE + `/dynamic/getCommentsList`, params);
};

// 提交评论
export const submitComment = (params: { dynamicId: number; content: string; parentId?: number }) => {
  return http.post(PORT_NONE + `/dynamic/submitComment`, params);
};

// 点赞评论
export const likeComment = (params: { commentId: number }) => {
  return http.post(PORT_NONE + `/dynamic/likeComment`, params);
};

// 取消点赞评论
export const unlikeComment = (params: { commentId: number }) => {
  return http.post(PORT_NONE + `/dynamic/unlikeComment`, params);
};

//评论列表
export const commentList = (params: { commentId: number }) => {
  return http.post(PORT_NONE + `/storeComment/getList`, params);
};
