import { createSlice } from '@reduxjs/toolkit';

import {
  AD_FORMAT_FILTER_NAME,
  AD_FORMAT_FILTER_OPTIONS,
  CALENDAR_FILTER_VALUE,
  COUNTRY_FILTER_NAME,
  DEMAND_SOURCE_FILTER_NAME,
  DEVICE_FILTER_NAME,
  DEVICE_FILTER_OPTIONS,
  DIMENSIONS_LIST,
  EMBED_CODE_VARIANT_FILTER_NAME,
  EMBED_CODE_VARIANT_FILTER_OPTIONS,
  HUB_FILTER_NAME,
  PLAYER_DOMAIN_FILTER_NAME,
  PLAYER_ID_FILTER_NAME,
  PLAYER_NAME_FILTER_NAME,
  PLAYER_TYPE_FILTER_NAME,
} from '../../constants';

const SETUP_PAGE_INITIAL_STATE = {
  name: 'New Custom Report',
  period: CALENDAR_FILTER_VALUE.monthToDate.value,
  periodCustomStartDate: null,
  periodCustomEndDate: null,
  scheduleType: null,
  scheduleDay: null,
  scheduleTime: null,
  recipients: [],
  dimensionsList: DIMENSIONS_LIST,
  checkedMetrics: [],
  checkedDimensions: [],
  isReadOnly: false,
  isDownloading: false,
  // filters
  [`${DEMAND_SOURCE_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${DEMAND_SOURCE_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${DEMAND_SOURCE_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${AD_FORMAT_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${AD_FORMAT_FILTER_NAME}_FILTER_OPTIONS`]: AD_FORMAT_FILTER_OPTIONS,
  [`${AD_FORMAT_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${HUB_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${HUB_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${HUB_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${PLAYER_NAME_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${PLAYER_NAME_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${PLAYER_NAME_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${PLAYER_ID_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${PLAYER_ID_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${PLAYER_ID_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${PLAYER_TYPE_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${PLAYER_TYPE_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${PLAYER_TYPE_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${COUNTRY_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${COUNTRY_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${COUNTRY_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${DEVICE_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${DEVICE_FILTER_NAME}_FILTER_OPTIONS`]: DEVICE_FILTER_OPTIONS,
  [`${DEVICE_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${PLAYER_DOMAIN_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${PLAYER_DOMAIN_FILTER_NAME}_FILTER_OPTIONS`]: [],
  [`${PLAYER_DOMAIN_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',

  [`${EMBED_CODE_VARIANT_FILTER_NAME}_FILTER_VALUES`]: [],
  [`${EMBED_CODE_VARIANT_FILTER_NAME}_FILTER_OPTIONS`]: EMBED_CODE_VARIANT_FILTER_OPTIONS,
  [`${EMBED_CODE_VARIANT_FILTER_NAME}_FILTER_ACTION`]: 'INCLUDE',
};

const initialState = {
  isLoading: false,
  searchText: '',
  page: 1,
  pageSize: 15,
  sortBy: 'createdAt',
  sortOrder: 'DESC',
  records: [],
  recordsTotal: 0,
  downloadReportTryRequestCounter: 0,
  // setup page
  ...SETUP_PAGE_INITIAL_STATE,
};

export const slice = createSlice({
  name: '@@customReportsNew/CUSTOM_REPORTS_NEW',
  initialState,

  reducers: {
    setFieldAction: (state, action) => {
      Object.entries(action.payload).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    setDefaultSetupPageAction: (state) => {
      Object.keys(SETUP_PAGE_INITIAL_STATE).forEach((key) => {
        state[key] = SETUP_PAGE_INITIAL_STATE[key];
      });
    },

    createCustomReportAction: (state) => state,
    updateCustomReportAction: (state) => state,
    getCustomReportByIdAction: (state) => state,
    deleteCustomReportAction: (state) => state,
    getCustomReportsAction: (state) => state,
    getDemandSourcesAction: (state) => state,
    getHubsAction: (state) => state,
    getCountriesAction: (state) => state,
    getUserPlayersAction: (state) => state,
    getUserDomainsAction: (state) => state,
    runDownloadReportAction: (state) => state,
    checkDownloadReportAction: (state) => state,
  },
});

export const {
  setFieldAction,
  setDefaultSetupPageAction,
  createCustomReportAction,
  updateCustomReportAction,
  getCustomReportByIdAction,
  deleteCustomReportAction,
  getCustomReportsAction,
  getDemandSourcesAction,
  getHubsAction,
  getCountriesAction,
  getUserPlayersAction,
  getUserDomainsAction,
  runDownloadReportAction,
  checkDownloadReportAction,
} = slice.actions;

export default slice.reducer;
