import qs from 'qs';

import api from 'src/config/api';
import {
  CelebrationType,
  UpcomingCelebrationsResponse,
  CelebrationListResponse,
  CelebrationsConfig,
} from 'src/types/upcomingCelebrations';

type UpcomingCelebrationsParams = {
  celebrationType: CelebrationType;
  limit?: number;
};

const getSegmentParams = (
  celebrationType: CelebrationType,
): Partial<{
  segmentType: CelebrationType;
  celebrationType: CelebrationType;
}> => {
  if (celebrationType === CelebrationType.BIRTHDAY_SEGMENTED) {
    const actualCelebrationType = CelebrationType.BIRTHDAY;
    return {
      segmentType: actualCelebrationType,
      celebrationType: actualCelebrationType,
    };
  }
};

export const getUpcomingCelebrations = (params: UpcomingCelebrationsParams) =>
  api.get<UpcomingCelebrationsResponse>('celebrations/upcoming-dates', {
    params: {
      ...params,
      limit: params.limit,
      celebrationType: params.celebrationType || undefined,
      ...getSegmentParams(params.celebrationType),
    },
    paramsSerializer: serializerParams => qs.stringify(serializerParams),
  });

export const getCelebrationsByDate = (
  celebrationDate: string,
  celebrationType: CelebrationType,
  page: number,
  limit: number = 10,
) =>
  api.get<CelebrationListResponse>('celebrations/users', {
    params: {
      celebrationDate,
      page,
      limit,
      celebrationType: celebrationType || undefined,
      ...getSegmentParams(celebrationType),
    },
    paramsSerializer: params => qs.stringify(params),
  });

export const getCelebrationsConfig = () =>
  api.get<CelebrationsConfig>('celebrations/config');
