import { format, isValid, parse } from 'date-fns';
import { createParser, parseAsArrayOf, parseAsInteger } from 'nuqs';

import { NAVBAR_HEIGHT } from 'src/utils/sidebar';

import { type CreateKudosFormValues } from './components/KudosCreateDrawer/types';
import { type KudosRankingFilterValues } from './types';

export const MAX_CATEGORY_CHIP_NAME = 30;
export const ACKNOWLEDGEDS_MAX_LENGTH = 30;
export const USERS_AVATARS_LIMIT = 7;
export const ACKNOWLEDGEMENT_MESSAGE_MAX_LENGTH = 4000;
export const MAX_CATEGORY_NAME_LENGTH = 70;
export const MAX_VISIBLE_CATEGORY_CHIPS = 4;
export const MIN_ACTIVE_CATEGORIES_TO_SHOW_CHIPS = 2;
export const kudosFeedCategoryIdParser = parseAsInteger.withOptions({
  clearOnDefault: true,
});
export const RANKING_MAX_HEIGHT = 450;
export const RANKING_PAGE_LIMIT = 20;
export const RANKING_ROW_HEIGHT_ESTIMATE = 64;
export const ACKNOWLEDGEMENT_ROW_HEIGHT_ESTIMATE = 790;
export const CONTAINER_VERTICAL_PADDING = 32;
export const FEED_HEIGHT = `calc(100vh - ${NAVBAR_HEIGHT}px - ${CONTAINER_VERTICAL_PADDING}px)`;

const parseAsKudosDate = createParser<Date>({
  parse: (queryValue: string) => {
    const date = parse(queryValue, 'yyyy-MM-dd', new Date());
    return isValid(date) ? date : null;
  },
  serialize: (value: Date) => format(value, 'yyyy-MM-dd'),
});

export const KUDOS_FILTERS_STATE = {
  startDate: parseAsKudosDate,
  endDate: parseAsKudosDate,
  categoryIds: parseAsArrayOf(parseAsInteger),
  segmentationItemIds: parseAsArrayOf(parseAsInteger),
};

export const EMPTY_FILTER_VALUES: KudosRankingFilterValues = {
  startDate: null,
  endDate: null,
  categories: {},
  segmentationItemIds: {},
};

export const DEFAULT_KUDOS_VALUES: CreateKudosFormValues = {
  acknowledgeds: [],
  categoryId: null,
  body: '',
  attachments: [],
};
