import api from 'src/config/api';

import { PAGE_SIZE } from './constants';
import {
  GetMyTeamJourneyParams,
  Journey,
  JourneysParams,
  JourneyStep,
  JourneyStepsData,
  JourneySummary,
  JourneyTask,
  JourneyTaskDetail,
  JourneyTasksParams,
  MarkJourneyTaskAsDoneParams,
} from './types';

const BASE_URL = '/employee-lifecycle/user';

export const getJourneys = (params?: JourneysParams) =>
  api.get<Journey[]>(`${BASE_URL}/journeys`, { params });

export const getJourneySteps = (journeyId: number) =>
  api.get<JourneyStepsData>(`${BASE_URL}/journeys/${journeyId}/steps`);

export const getJourneyStep = (journeyId: number, stepId: number) =>
  api.get<JourneyStep>(`${BASE_URL}/journeys/${journeyId}/steps/${stepId}`);

export const getJourneyStepTasks = (params: JourneyTasksParams) =>
  api.get<JourneyTask[]>(`${BASE_URL}/journeys/tasks`, { params });

export const getJourneyTask = (taskId: number) =>
  api.get<JourneyTaskDetail>(`${BASE_URL}/journeys/tasks/${taskId}`);

export const markJourneyTaskAsDone = ({
  taskId,
  data,
}: MarkJourneyTaskAsDoneParams) =>
  api.post<null>(`${BASE_URL}/journeys/tasks/${taskId}/complete`, data);

export const reopenJourneyTask = async (taskId: number) =>
  api.post<null>(`${BASE_URL}/journeys/tasks/${taskId}/reopen`);

export const startSurveyTask = (taskId: number) =>
  api.post<null>(`${BASE_URL}/journeys/tasks/${taskId}/start-survey`);

export const getMyTeamJourneys = async (params?: GetMyTeamJourneyParams) => {
  const response = await api.get<JourneySummary[]>(
    `${BASE_URL}/subordinates/journeys`,
    { params },
  );

  return {
    ...response,
    data: response.data,
    metadata: {
      offset: params?.offset || 0,
      limit: params?.limit || PAGE_SIZE,
    },
  };
};

export const getOnboardingsNotifications = () =>
  api.get<number>(`${BASE_URL}/onboardings-badge`);

export const getTasksNotifications = () =>
  api.get<number>(`${BASE_URL}/tasks-badge`);
