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

import { SORT_DESC } from '@/modules/@common/constants/sort';
import { RECENT, TRENDING } from '@/modules/editorial/constants/video';

import { SORTERS } from '../../helpers';

const initialState = {
  page: 1,
  pageSize: 10,
  recordsTotal: 0,
  watches: [],
  watchId: null,
  channelId: null,
  playlistsEntities: {},
  videosEntities: {},
  embedCode: '',
  publishers: [],
  publisher: null,
  search: '',
  copiedChannelId: null,
  copiedChannelDesign: null,
  status: null,
  mrssOnly: false,
  verticalOnly: false,
  sortBy: SORTERS[1].value,
  sortOrder: SORT_DESC,
  aiPlaylist: {
    [RECENT]: null,
    [TRENDING]: null,
  },
  aiFilters: {
    [RECENT]: [],
    [TRENDING]: [],
  },
};

export const slice = createSlice({
  name: '@@EDITORIAL/SIDEBAR/WATCHES',
  initialState,
  reducers: {
    getWatchesAction: (state) => state,
    getNextWatchesAction: (state) => state,
    deleteWatchAction: (state) => state,
    deleteChannelAction: (state) => state,
    getPlaylistByIdAction: (state) => state,
    updatePlaylistAction: (state) => state,
    getPlaylistVideosAction: (state) => state,
    getEmbedCodeAction: (state) => state,
    updateChannelAction: (state) => state,
    getPublishersAction: (state) => state,
    copyWatchChannelAction: (state) => state,
    setWatchesAction: (state, action) => {
      state.watches = action.payload;
    },
    setNextWatchesAction: (state, action) => {
      state.watches = [...state.watches, ...action.payload];
    },
    setWatchesPaginationInfoAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    openChannelAction: (state, action) => {
      state.channelId = action.payload.channelId;
      state.watchId = action.payload.watchId;
    },
    setPlaylistEntityAction: (state, action) => {
      state.playlistsEntities = {
        ...state.playlists,
        ...action.payload,
      };
    },
    setPlaylistVideosEntitiesAction: (state, action) => {
      state.videosEntities = {
        ...state.videosEntities,
        ...action.payload,
      };
    },
    setPlaylistVideosEntityAttributeAction: (state, action) => {
      const { uid, attribute } = action.payload;

      state.videosEntities = Object.entries(state.videosEntities).reduce((acc, [key, entity]) => {
        acc[key] = entity.uid === uid ? { ...entity, ...attribute } : entity;
        return acc;
      }, {});
    },
    setEmbedCodeAction: (state, action) => {
      state.embedCode = action.payload;
    },
    setPublishersAction: (state, action) => {
      state.publishers = action.payload;
    },
    setPublisherAction: (state, action) => {
      state.publisher = action.payload;
    },
    setSearchAction: (state, action) => {
      state.search = action.payload;
    },
    setStatusAction: (state, action) => {
      state.status = action.payload;
    },
    setMrssOnlyAction: (state, action) => {
      state.mrssOnly = action.payload;
    },
    setVerticalOnlyAction: (state, action) => {
      state.verticalOnly = action.payload;
    },
    setSortByAction: (state, action) => {
      state.sortBy = action.payload;
    },
    setSortOrderAction: (state, action) => {
      state.sortOrder = action.payload;
    },
    copyChannelAction: (state, action) => {
      state.copiedChannelId = action.payload?.id ?? null;
      state.copiedChannelDesign = action.payload?.design ?? null;
    },
    getAiPlaylistAction: (state) => state,
    setAiPlaylistAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state.aiPlaylist[key] = action.payload[key];
      });
    },
    createAiPlaylistAction: (state) => state,
    updateAiPlaylistAction: (state) => state,
    deleteAiPlaylistAction: (state) => state,
    clearAiPlaylistAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state.aiPlaylist[key] = action.payload[key];
      });
    },
    getAiFiltersAction: (state) => state,
    setAiFiltersAction: (state, action) => {
      state.aiFilters = {
        ...state.aiFilters,
        ...action.payload,
      };
    },
    updateAiFiltersAction: (state) => state,
    clearAiFiltersAction: (state, action) => {
      state.aiFilters = {
        ...state.aiFilters,
        ...action.payload,
      };
    },
  },
});

export const {
  copyChannelAction,
  copyWatchChannelAction,
  deleteChannelAction,
  deleteWatchAction,
  getEmbedCodeAction,
  getNextWatchesAction,
  getPlaylistByIdAction,
  getPlaylistVideosAction,
  getPublishersAction,
  getWatchesAction,
  openChannelAction,
  setEmbedCodeAction,
  setNextWatchesAction,
  setPlaylistEntityAction,
  setPlaylistVideosEntitiesAction,
  setPublisherAction,
  setPublishersAction,
  setSearchAction,
  setStatusAction,
  setMrssOnlyAction,
  setVerticalOnlyAction,
  setSortByAction,
  setSortOrderAction,
  setWatchesAction,
  setWatchesPaginationInfoAction,
  updateChannelAction,
  updatePlaylistAction,
  getAiPlaylistAction,
  setAiPlaylistAction,
  createAiPlaylistAction,
  updateAiPlaylistAction,
  deleteAiPlaylistAction,
  clearAiPlaylistAction,
  getAiFiltersAction,
  setAiFiltersAction,
  updateAiFiltersAction,
  clearAiFiltersAction,
  setPlaylistVideosEntityAttributeAction,
} = slice.actions;

export default slice.reducer;
