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

import { CALENDAR_FILTER_VALUE, TABS_CONFIG, VIDEO_METRICS_TABS } from '../../constants';
import { WIDGET_DISPLAY_STATE_NO_DATA } from '@/modules/analytics/common/constants';

const initialState = {
  // filters
  hub: null,
  hubOptions: null,
  watch: null,
  watchOptions: null,
  watchChannel: null,
  watchChannelOptions: null,
  origin: 'ALL',
  calendar: CALENDAR_FILTER_VALUE.last30days,
  calendarCustom: {
    dateStart: null,
    dateEnd: null,
    comparison: {
      dateStart: null,
      dateEnd: null,
    },
    interval: 'DAYS',
  },
  includeArchived: false,
  activeTabId: TABS_CONFIG.VIDEOS.id,
  total: 0,
  topSearches: [],
  tableItems: [],
  search: null,
  searchOptions: [],
  sortBy: 'NO_OF_VIEWS',
  sortOrder: 'DESC',
  metrics: {
    total: null,
    comparison: null,
  },
  metricViews: [],
  metricAttentionSpan: [],
  metricSearches: [],
  selectedItemId: null,
  activeMetric: VIDEO_METRICS_TABS[0].key,
  itemMetrics: [],

  // displayState,
  displayStateMainMetrics: WIDGET_DISPLAY_STATE_NO_DATA,
  displayStateTopSearches: WIDGET_DISPLAY_STATE_NO_DATA,
  displayStateTopVideos: WIDGET_DISPLAY_STATE_NO_DATA,

  // exports loading
  isExportCsvLoading: false,
  isExportPdfLoading: false,
};

export const slice = createSlice({
  name: '@@analyticsVideoContentPerformance/ANALYTICS_VIDEO_CONTENT_PERFORMANCE',
  initialState,

  reducers: {
    setFieldAction: (state, action) => {
      Object.entries(action.payload).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    getHubOptionsAction: (state) => state,
    getWatchOptionsAction: (state) => state,
    getTableItemsAction: (state) => state,
    getPerformanceTopSearchesAction: (state) => state,
    getSearchOptionsAction: (state) => state,
    getPerformanceVideosTotalAction: (state) => state,
    getPerformanceVideosTotalMetricAction: (state) => state,
    getItemMetricsAction: (state) => state,
    exportToCsvAction: (state) => state,
    exportToPdfAction: (state) => state,
  },
});

export const {
  setFieldAction,
  getHubOptionsAction,
  getWatchOptionsAction,
  getTableItemsAction,
  getPerformanceTopSearchesAction,
  getSearchOptionsAction,
  getPerformanceVideosTotalAction,
  getPerformanceVideosTotalMetricAction,
  getItemMetricsAction,
  exportToCsvAction,
  exportToPdfAction,
} = slice.actions;

export default slice.reducer;
