import { apiInstance } from "../apiConfig";
import {
  GetRouteSightsResponse,
  GetRouteStationsResponse,
  GetMediaResponse,
  GetRouteResponse,
  GetCarrierResponse,
  GetCityResponse,
  GetSightArticleResponse,
  GetArticleResponse,
} from "./types";

export const getRoute = async (routeId: string): Promise<GetRouteResponse> => {
  const response = await apiInstance.get(`/route/${routeId}`);
  return response.data;
};

export const getCarrier = async (
  carrierId: number,
): Promise<GetCarrierResponse> => {
  const response = await apiInstance.get(`/carrier/${carrierId}`);
  return response.data;
};

export const getCity = async (cityId: number): Promise<GetCityResponse> => {
  const response = await apiInstance.get(`/city/${cityId}`);
  return response.data;
};

export const getRouteSights = async (
  routeId: string,
  lang: string = "ru",
): Promise<GetRouteSightsResponse> => {
  const response = await apiInstance.get(
    `/route/${routeId}/sight?lang=${lang}`,
  );
  return response.data;
};

export const getRouteStations = async (
  routeId: string,
  lang: string = "ru",
): Promise<GetRouteStationsResponse> => {
  const response = await apiInstance.get(
    `/route/${routeId}/station?lang=${lang}`,
  );
  return response.data;
};

export const getArticleMedia = async (
  articleId: number,
): Promise<GetMediaResponse> => {
  const response = await apiInstance.get(`/article/${articleId}/media`);
  return response.data;
};

export const getMedia = async (): Promise<GetMediaResponse[]> => {
  const response = await apiInstance.get("/media");
  return response.data;
};

export const getArticle = async (
  articleId: number,
  lang: string = "ru",
): Promise<GetArticleResponse> => {
  const response = await apiInstance.get(`/article/${articleId}?lang=${lang}`);
  return response.data;
};

export const getSightArticlesIds = async (
  sightId: number,
): Promise<number[]> => {
  const response = await apiInstance.get(`/sight/${sightId}/article`);
  const articles = response.data;
  return articles.map((article: GetSightArticleResponse) => article.id);
};

export const getSightStations = async (
  sightId: number,
  lang: string = "ru",
): Promise<GetRouteStationsResponse> => {
  const response = await apiInstance.get(
    `/sight/${sightId}/station?lang=${lang}`,
  );
  return response.data;
};

export const getStationSights = async (
  stationId: number,
  lang: string = "ru",
): Promise<GetRouteSightsResponse> => {
  const response = await apiInstance.get(
    `/station/${stationId}/sight?lang=${lang}`,
  );
  return response.data;
};
