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

export interface PostGetSlowReturn extends ResponseType {
  slow: {
    country_id: number;
    country: string;
    flag: string;
    mega: number[];
    visited: 0 | 1;
    slow11: 0 | 1;
    slow31: 0 | 1;
    slow101: 0 | 1;
    yes: number;
  }[];
  megaregions: {
    id: number;
    name: string;
  }[];
}

export interface PostSetSlow {
  token: string;
  id: number;
  v: boolean;
  s11: boolean;
  s31: boolean;
  s101: boolean;
}

export interface PostGetCountryDataReturn extends ResponseType {
  data: {
    visited: boolean;
    slow11: 0 | 1;
    slow31: 0 | 1;
    slow101: 0 | 1;
    name: string;
    flag: string;
    users_from_country_count: number;
    users_who_visited_country_count: number;
    photos: {
      title: string;
      id: number;
      user_id: number;
      first_name: string;
      last_name: string;
      avatar: string | null;
    }[];
    users_who_visited_country: string[];
    users_from_country: string[];
    series: {
      series_id: number;
      series_name: string;
      icon: string;
      items: {
        id: number;
        name: string;
        description: string | null;
        new: 0 | 1;
        visited: 0 | 1;
        double_point: 0 | 1;
        visited_double: 0 | 1;
        link: string;
      }[];
    }[];
  };
}

export interface PostGetUsersFromCountryDataReturn extends ResponseType {
  data: {
    max_pages: number;
    users: User[];
  };
}

export interface PostGetUsersWhoVisitedCountryDataReturn extends ResponseType {
  data: {
    max_pages: number;
    countries: {
      [key: string]: {
        country: string;
        flag: string;
      };
    };
    users: User[];
  };
}

export interface PostGetUserDataReturn extends ResponseType {
  data: {
    bbox: any;
    center: [number, number];
    visited: boolean;
    slow11: 0 | 1;
    slow31: 0 | 1;
    slow101: 0 | 1;
  };
}

export interface PostGetListCountriesReturn extends ResponseType {
  data: {
    id: number;
    name: string;
    bbox: any;
    flag: string;
  }[];
}

export const countriesApi = {
  getSlow: (token: string) => request.postForm<PostGetSlowReturn>(API.GET_SLOW, { token }),
  setSlow: (data: PostSetSlow) => request.postForm<ResponseType>(API.SET_SLOW, data),
  getCountryData: (id: number, token?: string) =>
    request.postForm<PostGetCountryDataReturn>(API.GET_COUNTRY_SCREEN_DATA, { id, token }),
  getUsersFromCountry: (id: number, page: number, sort?: string, age?: number) =>
    request.postForm<PostGetUsersFromCountryDataReturn>(API.GET_USERS_FROM_COUNTRY, {
      id,
      page,
      sort,
      age
    }),
  getUsersWhoVisitedCountry: (
    id: number,
    page: number,
    sort?: string,
    age?: number,
    country?: string
  ) =>
    request.postForm<PostGetUsersWhoVisitedCountryDataReturn>(API.GET_USERS_WHO_VISITED_COUNTRY, {
      id,
      page,
      sort,
      age,
      country
    }),
  getCountryUserData: (id: number, token: string) =>
    request.postForm<PostGetUserDataReturn>(API.GET_COUNTRY_USER_DATA, { id, token }),
  getListCountries: () => request.postForm<PostGetListCountriesReturn>(API.GET_LIST_COUNTRIES)
};
