import { request } from '../../../utils';
import { API } from '../../../types';
import { ResponseType } from '../response-type';
import { User } from '@api/regions';

type FriendRequest = {
  avatar: string | null;
  first_name: string;
  flag: string | null;
  id: number;
  last_name: string;
  status: number;
  user_id: number;
};

type UserData = {
  max_pages: number;
  countries: {
    [key: string]: {
      country: string;
      flag: string;
    };
  };
  users: User & { friend_db_id: number; friend_status: number }[];
};

export interface PostGetFriendsDataReturn extends ResponseType {
  data: UserData;
}

export interface PostGetFriendsSettingsDataReturn extends ResponseType {
  friends: UserData;
  sent: UserData;
  received: UserData;
}

export interface PostGetFriendsNotificationReturn extends ResponseType {
  active: boolean;
  count: number;
}

export const friendsApi = {
  getFriends: (uid: number, page: number, sort?: string, age?: number, country?: string) =>
    request.postForm<PostGetFriendsDataReturn>(API.GET_FRIENDS, {
      uid,
      page,
      sort,
      age,
      country
    }),
  sendFriendRequest: (token: string, uid: number) =>
    request.postForm<ResponseType>(API.SEND_FRIEND_REQUEST, {
      token,
      uid
    }),
  loadFriendsSettings: (
    token: string,
    type: string,
    page: number,
    sort?: string,
    age?: number,
    country?: string
  ) =>
    request.postForm<PostGetFriendsSettingsDataReturn>(API.LOAD_FRIENDS_SETTINGS, {
      token,
      type,
      page,
      sort,
      age,
      country
    }),
  updateFriendStatus: (token: string, id: number, status: number) =>
    request.postForm<ResponseType>(API.UPDATE_FRIEND_STATUS, {
      token,
      id,
      status
    }),
  hideShowRequest: (token: string, id: number, show: 0 | 1) =>
    request.postForm<ResponseType>(API.HIDE_SHOW_REQUEST, {
      token,
      id,
      show
    }),
  getNotification: (token: string) =>
    request.postForm<PostGetFriendsNotificationReturn>(API.GET_FRIENDS_NOTIFICATION, {
      token
    })
};
