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

export interface PostGetMegaReturn extends ResponseType {
  data: {
    id: number;
    name: string;
  }[];
}

export interface PostGetRegionsQeReturn extends ResponseType {
  data: {
    out_regs: {
      id: number;
      flag_1: string;
      flag_2: string;
      region_name: string;
      essential: 0 | 1;
      quality: number;
      year: number;
      last: number;
      visits: number;
    }[];
    out_tcc: {
      id: number;
      flag: string;
      flag2: string;
      name: string;
      visited: number;
    }[];
    out_mtp: {
      mtp_id: number;
      flag: string;
      flag2: string;
      name: string;
      visited: number;
    }[];
    out_dare: {
      id: number;
      name: string;
      flag1: string;
      flag2: string | null;
    }[];
  };
}

export interface PostSetNmRegion {
  token: string;
  region: number;
  first: number;
  last: number;
  visits: number;
  quality: number;
}

export interface PostSetTCCRegion {
  token: string;
  region: number;
  visits: 0 | 1;
}

export interface PostGetVisitsReturn extends ResponseType {
  data: {
    completed: 0 | 1;
    date_from: string;
    date_to: string;
    day_from: number | null;
    day_to: number | null;
    hidden: 0 | 1;
    id: number;
    month_from: number | null;
    month_to: number | null;
    quality: number;
    year_from: number;
    year_to: number;
  }[];
}

export interface PostGetRegionsReturn extends ResponseType {
  data: {
    dare: {
      flag1: string;
      flag2: string | null;
      id: number;
      name: string;
      visited: 0 | 1;
    }[];
    regions: {
      best_visit_quality: number | null;
      first_visited_in_year: number | null;
      flag1: string;
      flag2: string | null;
      id: number;
      last_visited_in_year: number | null;
      name: string;
      no_of_visits: number | null;
    }[];
    tcc: {
      flag1: string;
      flag2: string | null;
      id: number;
      name: string;
      visited: 0 | 1;
    }[];
  };
}

export interface PostAddVisit {
  token: string;
  region: number;
  quality: number;
  year_from: number;
  month_from: number;
  day_from: number;
  year_to: number;
  month_to: number;
  day_to: number;
  completed: 0 | 1;
  hidden: 0 | 1;
}

export interface PostUpdateVisit {
  token: string;
  region: number;
  id: number;
  quality: number;
  year_from: number;
  month_from: number;
  day_from: number;
  year_to: number;
  month_to: number;
  day_to: number;
  completed: 0 | 1;
  hidden: 0 | 1;
}

export interface PostDeleteVisit {
  token: string;
  id: number;
}

export interface PostUnvisit {
  token: string;
  region: number;
}

export interface PostGetSingleRegionReturn {
  not_visited?: 0 | 1;
  region?: {
    best_visit_quality: number;
    first_visited_in_year: number | null;
    id: number;
    last_visited_in_year: number | null;
    no_of_visits: number;
  };
}

export const regionsApi = {
  getMegaregions: (token: string) =>
    request.postForm<PostGetMegaReturn>(API.GET_MEGAREGIONS, { token }),
  getRegionsQe: (token: string, megaregion?: number, country?: number) =>
    request.postForm<PostGetRegionsQeReturn>(API.GET_REGIONS_QE, { megaregion, country, token }),
  setNmRegion: (data: PostSetNmRegion) => request.postForm<ResponseType>(API.SET_NM_REGION, data),
  setTCCRegion: (data: PostSetTCCRegion) =>
    request.postForm<ResponseType>(API.SET_TCC_REGION, data),
  getVisits: (token: string, region: number) =>
    request.postForm<PostGetVisitsReturn>(API.GET_VISITS_TO_REGION, { token, region }),
  getRegions: (
    token: string,
    megaregion: number | 'all' | undefined,
    country: number | 'all' | undefined
  ) => request.postForm<PostGetRegionsReturn>(API.GET_REGIONS, { megaregion, country, token }),
  addVisit: (data: PostAddVisit) => request.postForm<ResponseType>(API.ADD_VISIT, data),
  updateVisit: (data: PostUpdateVisit) => request.postForm<ResponseType>(API.UPDATE_VISIT, data),
  deleteVisit: (data: PostDeleteVisit) => request.postForm<ResponseType>(API.DELETE_VISIT, data),
  getSingleRegion: (data: PostDeleteVisit) =>
    request.postForm<PostGetSingleRegionReturn>(API.GET_SINGLE_REGION, data),
  setNotVisited: (data: PostUnvisit) => request.postForm<ResponseType>(API.SET_NOT_VISITED, data)
};
