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

import {
  type Goal,
  type GoalCycle,
  type GoalCycleRangeFields,
  type GoalCycleStats,
  type GoalCyclesConfiguration,
  type GoalHistory,
  type GoalPayload,
  type GoalProgress,
  type GoalTeamUser,
  type IndividualGoal,
} from './types';

export const getGoals = (
  goalCycleId: number | undefined = undefined,
  queryParams?: PaginationParamsWithQ,
) =>
  api.get<Pagination<Goal[]>>('goals', {
    params: {
      order: queryParams?.order,
      orderBy: queryParams?.orderBy,
      q: queryParams?.q,
      page: queryParams ? queryParams.page + 1 : 1,
      limit: queryParams?.limit,
      goalCycleId:
        goalCycleId === -2 || goalCycleId === -1 ? undefined : goalCycleId,
      withoutGoalCycle: goalCycleId === -1,
    },
  });

export const getTeamGoals = (id: number, queryParams: PaginationParamsWithQ) =>
  api.get<Pagination<GoalTeamUser[]>>(`goal-cycles/${id}/goals-with-stats`, {
    params: {
      page: queryParams.page + 1,
      limit: queryParams.limit,
      ...(queryParams.q ? { q: queryParams.q } : {}),
    },
  });

export const getGoalCycles = (
  queryParams?: PaginationParamsWithQ,
  withGoalCount = false,
) =>
  api.get<Pagination<GoalCycle[]>>('goal-cycles', {
    params: {
      page: queryParams ? queryParams.page + 1 : 1,
      limit: queryParams?.limit,
      withGoalCount,
    },
  });

export const getGoalCycle = (goalCycleId: number) =>
  api.get<GoalCycle>(`goal-cycles/${goalCycleId}`);

export const createGoal = (body: GoalPayload, publish = false) =>
  api.post(`goals/multiple-owners?publish=${publish}`, body);

export const updateGoal = (goalId: number, body: GoalPayload) =>
  api.put(`goals/${goalId}`, body);

export const getGoal = (goalId: number) => api.get<Goal>(`goals/${goalId}`);

export const getGoalHistory = (
  goalId: number,
  queryParams?: PaginationParamsWithQ,
) =>
  api.get<Pagination<GoalHistory[]>>(`goals/${goalId}/progressHistory`, {
    params: {
      page: queryParams ? queryParams.page + 1 : 1,
      limit: queryParams?.limit,
    },
  });

export const updateGoalProgress = (goalId: number, body: GoalProgress) =>
  api.post(`goals/${goalId}/progress`, body);

export const deleteGoal = (goalId: number) => api.delete(`goals/${goalId}`);

export const getGoalWeights = (goalId: number, userId: number) =>
  api.get<{
    goalWeights: IndividualGoal[];
    weightedProgress: number;
  }>(`goal-cycles/${goalId}/owner/${userId}/weights-with-weighted-progress`);

export const updateGoalWeights = (
  goalId: number,
  userId: number,
  goalsWeight: { goalId: number; weight: number }[],
  publish = false,
) =>
  api.patch(
    `goal-cycles/${goalId}/owner/${userId}/weights-with-weighted-progress?publish=${publish}`,
    { goalsWeight },
  );

export const getGoalCycleStats = (goalCycleId: number) =>
  api.get<GoalCycleStats>(`goal-cycles/${goalCycleId}/stats`);

export const getGoalCycleRangeFields = (formulaChainId: number) =>
  api.get<GoalCycleRangeFields>(`goal-formulas/${formulaChainId}/range-fields`);

export const getGoalCyclesConfiguration = () =>
  api.get<GoalCyclesConfiguration>('goal-cycles/configuration');

export const getMyGoals = (
  goalCycleId: number,
  queryParams: PaginationParamsWithQ,
) =>
  api.get<Pagination<Goal[]> & { weightedProgress: number }>('goals/my-goals', {
    params: {
      goalCycleId,
      page: queryParams.page + 1,
      limit: queryParams.limit,
      ...(queryParams.q ? { q: queryParams.q } : {}),
    },
  });

export const getGoalFormulas = (queryParams?: PaginationParamsWithQ) =>
  api.get('goal-formulas', {
    params: queryParams ? formatPagination(queryParams) : {},
  });

export const getGoalFormulaChain = (id: number) =>
  api.get(`goal-formulas/${id}`);
