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

export interface PostGetTripsYearsReturn extends ResponseType {
  data: string[];
}

export interface PostGetTripsForYearReturn extends ResponseType {
  trips: {
    id: number;
    date_from: string;
    date_to: string;
    description: string;
    regions: {
      region_name: string;
      flag1: string;
      flag2: string | null;
      status: 0 | 1;
      id: number;
    }[];
    dates_missing: 0 | 1;
  }[];
  statistics: {
    dates_missing: 0 | 1;
    countries: {
      description: string;
      list: {
        id: number;
        alt_id: number | null;
        country: string;
        days_spent: number;
        flag: string | null;
        regions: {
          id: number;
          region: string;
          days_spent: number;
        }[];
      }[];
    };
    general: [string];
    regions: {
      description: string;
      list: {
        id: number;
        days_spent: number;
        flag: string | null;
        flag1: string | null;
        flag2: string | null;
        region: string;
      }[];
    };
  } | null;
}

export interface PostGetTripsForRegionReturn extends ResponseType {
  trips: {
    id: number;
    date_from: string;
    date_to: string;
    description: string;
    regions: {
      region_name: string;
      flag1: string;
      flag2: string | null;
      status: 0 | 1;
      id: number;
    }[];
    dates_missing: 0 | 1;
  }[];
}

export interface RegionData {
  id: number;
  quality: number;
  // status: 0 | 1;
  hidden: boolean;
  year_from: number | null;
  year_to: number | null;
  month_from?: number | null;
  month_to?: number | null;
  day_from?: number | null;
  day_to?: number | null;
}

export interface PostSetNewTrip {
  token: string;
  date_from?: string | null;
  date_to?: string | null;
  description: string;
  regions: RegionData[];
  year?: number | null;
}

export interface PostSetNewTripReturn extends ResponseType {
  trip_id: number;
}

export interface PostUpdateTrip {
  token: string;
  trip_id: number;
  date_from?: string | null;
  date_to?: string | null;
  description: string;
  regions: RegionData[];
  year?: number | null;
}

export interface PostGetTripReturn extends ResponseType {
  trip: {
    id: number;
    date_from: string;
    date_to: string;
    description: string;
    regions: {
      id: number;
      region: number;
      region_name: string;
      flag1: string;
      flag2: string | null;
      quality: number;
      // status: 0 | 1;
      hidden: boolean;
      can_be_hidden: boolean;
      year_from: number;
      year_to: number;
      month_from: number;
      month_to: number;
      day_from: number | null;
      day_to: number | null;
    }[];
  };
}

export interface PostGetRegionsForTripsReturn extends ResponseType {
  regions: {
    id: number;
    region_name: string;
    flag1: string;
    flag2: string | null;
    hidden: boolean;
  }[];
}

export interface PostGetRegionsThatHaveTripsReturn extends ResponseType {
  data: {
    id: number;
    name: string;
    flag1: string;
    flag2: string | null;
  }[];
}

export const tripsApi = {
  getTripsYears: (token: string) =>
    request.postForm<PostGetTripsYearsReturn>(API.GET_TRIPS_YEARS, { token }),
  getTripsForYear: (token: string, year: string) =>
    request.postForm<PostGetTripsForYearReturn>(API.GET_TRIPS_FOR_YEAR, { token, year }),
  getTripsForRegion: (token: string, region: number) =>
    request.postForm<PostGetTripsForRegionReturn>(API.GET_TRIPS_FOR_REGION, { token, region }),
  setNewTrip: (data: PostSetNewTrip) =>
    request.postForm<PostSetNewTripReturn>(API.SET_NEW_TRIP, data),
  updateTrip: (data: PostUpdateTrip) => request.postForm<ResponseType>(API.UPDATE_TRIP, data),
  deleteTrip: (token: string, trip_id: number) =>
    request.postForm<ResponseType>(API.DELETE_TRIP, { token, trip_id }),
  getTrip: (token: string, trip_id: number) =>
    request.postForm<PostGetTripReturn>(API.GET_TRIP, { token, trip_id }),
  getRegionsForTrips: () => request.get<PostGetRegionsForTripsReturn>(API.GET_REGIONS_FOR_TRIPS),
  getRegionsThatHaveTrips: () =>
    request.get<PostGetRegionsThatHaveTripsReturn>(API.GET_REGIONS_THAT_HAVE_TRIPS)
};
