import qs from 'qs';

import type * as PxTypes from '@material-hu/components/composed-components/peopleExperience/types';

import api from 'src/config/api';
import { type Pagination } from 'src/types/services';
import { type PaginatedResponse } from 'src/utils/pagination';

import {
  type BackupAnswerParams,
  type CollaboratorSurveryResData,
  type CollaboratorSurveysReqParams,
  type DimensionsParams,
  type EnpsChart,
  type EnpsChartParams,
  type EnpsScoresParams,
  type EnpsScoresResponse,
  type FavDimensionIndexStatsParams,
  type FavDimensionIndexStatsResponse,
  type FavDimensionStatsParams,
  type FavDimensionStatsResponse,
  type FavListIndexParams,
  type FavListIndexStatsResponse,
  type FavQuestionsStatsParams,
  type FavQuestionsStatsResponse,
  type FilterOptionResponse,
  type GetQuestionsParams,
  type GetQuestionsResponse,
  type HeatmapReqParams,
  type HeatmapScoresReqParams,
  type ManagerParticipationParams,
  type ManagerParticipationResponse,
  type ManagerSurvey,
  type ManagerSurveysParams,
  type ParticipationResponse,
  type QuestionAnswersParams,
  type QuestionAnswersResponse,
  type QuestionStatsParams,
  type QuestionStatsResponse,
  type RespondSurveyParams,
  type SegmentationGroup,
  type SegmentationGroupResponse,
  type SegmentationGroupsParams,
  type SegmentationItemsParams,
  type SegmentType,
  type SurveyResponse,
  type Topic,
} from './types';

export const getSurveys = ({ q, ...reqParams }: CollaboratorSurveysReqParams) =>
  api.get<Pagination<CollaboratorSurveryResData[]>>(
    '/people-experience/work-environment-survey',
    {
      params: {
        ...reqParams,
        q: q || undefined,
      },
    },
  );

export const respondSurvey = ({ surveyId, ...params }: RespondSurveyParams) =>
  api.post(`people-experience/work-environment-survey/${surveyId}`, params);

export const getSurveyDetail = (surveyId: ManagerSurvey['id']) =>
  api.get<SurveyResponse>(
    `people-experience/work-environment-survey/survey/${surveyId}`,
  );

export const getManagerSurveyDetail = (surveyId: ManagerSurvey['id']) =>
  api.get<ManagerSurvey>(
    `people-experience/work-environment-survey/managers/surveys/${surveyId}`,
  );

export const getManagerSurveys = ({ q, ...params }: ManagerSurveysParams) =>
  api.get<Pagination<ManagerSurvey[]>>(
    'people-experience/work-environment-survey/managers/surveys',
    {
      params: {
        ...params,
        q: q || undefined,
      },
    },
  );

export const getParticipationStats = (surveyId: ManagerSurvey['id']) =>
  api.get<ParticipationResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/participation`,
  );

export const getManagerParticipation = ({
  surveyId,
  q,
  ...other
}: ManagerParticipationParams) =>
  api.get<ManagerParticipationResponse>(
    `people-experience/work-environment-survey/managers/surveys/${surveyId}/participation/dimension`,
    {
      params: {
        ...other,
        q: q || undefined,
      },
    },
  );

export const backupAnswer = ({ surveyId, ...params }: BackupAnswerParams) =>
  api.post(
    `people-experience/work-environment-survey/${surveyId}/save`,
    params,
  );

// Heatmap
export const getHeatmapTotals = ({ surveyId, ...params }: HeatmapReqParams) => {
  return api.get<PxTypes.HeatmapTotalResData>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/heatmap/total`,
    {
      params,
    },
  );
};

export const getHeatmapScores = ({
  surveyId,
  dataSource,
  ...params
}: HeatmapScoresReqParams) => {
  return api.get<PxTypes.HeatmapScoresResData>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/heatmap/${dataSource}`,
    { params },
  );
};

export const getEnpsScores = ({ surveyId, ...params }: EnpsScoresParams) =>
  api.get<EnpsScoresResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/nps-total-score`,
    { params },
  );

export const getEnpsScoresDifference = (
  surveyId: ManagerSurvey['id'],
  otherSurveyId: number,
) =>
  api.get<EnpsScoresResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/nps-total-score/difference`,
    {
      params: {
        otherSurveyId,
      },
    },
  );

export const getQuestionStats = ({
  formId,
  questionId,
  ...params
}: QuestionStatsParams) =>
  api.get<QuestionStatsResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${formId}/analytics/stats-by-question-id/${questionId}`,
    { params },
  );

export const getQuestionAnswers = ({
  formId,
  questionId,
  ...params
}: QuestionAnswersParams) =>
  api.get<Pagination<QuestionAnswersResponse[]> & { passedThreshold: boolean }>(
    `/people-experience/work-environment-survey/managers/surveys/${formId}/analytics/${questionId}/answers`,
    { params, paramsSerializer: params => qs.stringify(params) },
  );

export const getDimensions = ({ q, ...params }: DimensionsParams) =>
  api.get<PaginatedResponse<Topic>>(
    'people-experience/work-environment-survey/topics',
    {
      params: {
        q: q || undefined,
        ...params,
      },
    },
  );

export const getEnpsChart = ({
  surveyId,
  dimension,
  parentId,
  ...params
}: EnpsChartParams) =>
  api.get<EnpsChart>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/nps-segments`,
    {
      params: {
        ...params,
        dimension: dimension,
        parentId: parentId || undefined,
      },
    },
  );

export const getEnpsComparedChart = (
  surveyId: ManagerSurvey['id'],
  dimension: SegmentType,
  otherSurveyId: number,
  parentId?: number,
) =>
  api.get<EnpsChart>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/nps-segments/difference`,
    {
      params: {
        dimension: dimension,
        parentId: parentId || undefined,
        otherSurveyId,
      },
    },
  );

export const getFavListIndexStats = ({
  surveyId,
  ...params
}: FavListIndexParams) =>
  api.get<FavListIndexStatsResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/favorability-score`,
    { params },
  );

export const getFavDimensionIndexStats = ({
  surveyId,
  dimensionId,
  ...params
}: FavDimensionIndexStatsParams) =>
  api.get<FavDimensionIndexStatsResponse>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/favorability/topics/${dimensionId}`,
    { params },
  );

export const getFavQuestionsStats = ({
  surveyId,
  ...params
}: FavQuestionsStatsParams) =>
  api
    .get<PaginatedResponse<FavQuestionsStatsResponse>>(
      `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/favorability/questions`,
      {
        params: { ...params, page: params.page ? params.page + 1 : undefined },
      },
    )
    .then(response => response.data);

export const getFavDimensionStats = ({
  surveyId,
  ...params
}: FavDimensionStatsParams) =>
  api
    .get<PaginatedResponse<FavDimensionStatsResponse>>(
      `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/favorability/topics`,
      {
        params: { ...params, page: params.page ? params.page + 1 : undefined },
      },
    )
    .then(response => response.data);

export const getSegmentationGroups = ({
  surveyId,
  page,
  ...other
}: SegmentationGroupsParams) =>
  api.get<PaginatedResponse<SegmentationGroupResponse>>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/filters/groups`,
    {
      params: {
        page: page + 1,
        ...other,
      },
    },
  );

export const getSegmentDropdownItems = async ({
  surveyId,
  ...params
}: SegmentationGroupsParams) => {
  const response = await api.get<PaginatedResponse<SegmentationGroup>>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/filters/groups`,
    {
      params,
    },
  );

  return response.data.items;
};

export const getSegmentationGroupsData = async ({
  surveyId,
  ...params
}: SegmentationGroupsParams) => {
  const response = await api.get<PaginatedResponse<SegmentationGroupResponse>>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/filters/groups`,
    {
      params,
    },
  );
  return response.data;
};

export const getSegmentationItems = ({
  surveyId,
  searchText,
  type,
  ...other
}: SegmentationItemsParams) =>
  api.get<PaginatedResponse<FilterOptionResponse>>(
    `/people-experience/work-environment-survey/managers/surveys/${surveyId}/analytics/filters/${type}/items`,
    {
      params: {
        ...other,
        q: searchText || undefined,
      },
    },
  );

export const getQuestions = ({
  q,
  topicIds,
  page,
  ...other
}: GetQuestionsParams) =>
  api.get<PaginatedResponse<GetQuestionsResponse>>(
    '/people-experience/work-environment-survey/questions',
    {
      params: {
        q: q || undefined,
        topicIds: topicIds ? topicIds.join(',') : undefined,
        page: page ? page + 1 : undefined,
        ...other,
      },
    },
  );
