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;
    }[];
  }[];
}

export interface RegionData {
  id: number;
  quality: number;
  status: 0 | 1;
  hidden: boolean;
}

export interface PostSetNewTrip {
  token: string;
  date_from: string;
  date_to: string;
  description: string;
  regions: RegionData[];
}

export interface PostSetNewTripReturn extends ResponseType {
  trip_id: number;
}

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

export interface PostGetTripReturn extends ResponseType {
  trip: {
    id: number;
    date_from: string;
    date_to: string;
    description: string;
    regions: {
      id: number;
      region_name: string;
      flag1: string;
      flag2: string | null;
      quality: number;
      status: 0 | 1;
      hidden: boolean;
      can_be_hidden: boolean;
    }[];
  };
}

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

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 }),
  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)
};
