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

export interface PostGetCountriesReturn extends ResponseType {
  data: { id: number; country: string; flag: string }[];
}

export interface PostGetFixersReturn extends ResponseType {
  data: {
    id: number;
    month: number;
    year: number;
    contact: string;
    name: string;
    email: string;
    phone: string;
    web: string;
    comment: string;
    added_by_uid: number;
    added_by_name: string;
    can_rate: 0 | 1;
    can_edit: 0 | 1;
    ratings: Rating[];
  }[];
}

type Rating = {
  rate: string;
  name: string;
  comment: string;
};

export interface PostSaveRating {
  token: string;
  fixer_id: number;
  rating1: number;
  rating2: number;
  rating3: number;
  comment: string;
}

export interface PostAddFixer {
  token: string;
  month: number;
  year: number;
  un_ids: number[];
  name: string;
  anonymous: 0 | 1;
  email: string;
  phone: string;
  website: string;
  comment: string;
}

export interface PostEditFixer {
  token: string;
  fixer_id: number;
  month: number;
  year: number;
  un_ids: number[];
  name: string;
  anonymous: 0 | 1;
  email: string;
  phone: string;
  website: string;
  comment: string;
}

export const fixersApi = {
  getCountries: (token: string) =>
    request.postForm<PostGetCountriesReturn>(API.GET_FIXERS_COUNTRIES, { token }),
  getAllCountries: (token: string) =>
    request.postForm<PostGetCountriesReturn>(API.GET_ALL_FIXERS_COUNTRIES, { token }),
  getFixers: (token: string, un_id: number) =>
    request.postForm<PostGetFixersReturn>(API.GET_FIXERS, { token, un_id }),
  saveRating: (data: PostSaveRating) => request.postForm<ResponseType>(API.SAVE_RATING, data),
  addFixer: (data: PostAddFixer) => request.postForm<ResponseType>(API.ADD_FIXER, data),
  editFixer: (data: PostEditFixer) => request.postForm<ResponseType>(API.EDIT_FIXER, data)
};
