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

import { AD_FORMAT_CONFIG, CALENDAR_FILTER_VALUE, CARDS_FILTERS_ALL } from '../../constants';

const initialState = {
  calendar: CALENDAR_FILTER_VALUE.thisMonth,
  calendarCustom: {
    dateStart: null,
    dateEnd: null,
  },
  activeCardFilter: {
    ...CARDS_FILTERS_ALL[0],
    ...CARDS_FILTERS_ALL[0].getCardConfig({ adFormat: AD_FORMAT_CONFIG.all.value }),
  },
  adFormat: AD_FORMAT_CONFIG.all.value,
  demandSources: [],
  demandSourcesOptions: [],
  players: [],
  playersOptions: [],
  domains: [],
  domainsOptions: [],
  countries: [],
  countriesOptions: [],
  devices: [],
  totals: {
    total: {
      externalRevenues: null,
      platformFee: null,
      adImpressions: null,
      viewability: null,
      fillRate: null,
      adRequests: null,
      cpm: null,
      rpm: null,
      playerLoads: null,
      netRevenue: null,
      ctr: null,
      adClicks: null,
    },
    comparisonPct: {
      externalRevenuesPct: null,
      platformFeePct: null,
      adImpressionsPct: null,
      viewabilityPct: null,
      fillRatePct: null,
      adRequestsPct: null,
      cpmPct: null,
      rpmPct: null,
      playerLoadsPct: null,
      netRevenuePct: null,
    },
    devices: [],
    countries: [],
    hideComparisonNumbers: false,
  },
  chartData: [],
  showFees: false,
  countriesFullList: [],
  isExportPdfLoading: false,
  isTotalsLoading: false,
  isChartDataLoading: false,
};

export const slice = createSlice({
  name: '@@analyticsMonetizationDashboard/ANALYTICS_MONETIZATION_DASHBOARD',
  initialState,

  reducers: {
    setFieldAction: (state, action) => {
      Object.entries(action.payload).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    setDefaultStateAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        state[key] = initialState[key];
      });
    },
    exportToPDFAction: (state) => state,
    exportToCSVAction: (state) => state,
    getCountriesFullListAction: (state) => state,
    getCountriesAction: (state) => state,
    getDemandSourcesAction: (state) => state,
    getDomainsAction: (state) => state,
    getPlayersAction: (state) => state,
    getTotalsAction: (state) => state,
    getChartDataAction: (state) => state,
  },
});

export const {
  setFieldAction,
  setDefaultStateAction,
  exportToPDFAction,
  exportToCSVAction,
  getCountriesFullListAction,
  getCountriesAction,
  getDemandSourcesAction,
  getDomainsAction,
  getPlayersAction,
  getTotalsAction,
  getChartDataAction,
} = slice.actions;

export default slice.reducer;
