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

import {
  FEE_BUSINESS_MODEL_IMPRESSIONS_CPM,
  REDUX_FIELD_NAME,
  ROWS_PER_PAGE_DEFAULT,
  TAB_DETAILS,
  TABLE_REDUX_FIELD_NAME,
  TABLE_SORT_BY,
} from '../../constants';
import { SORT_DESC } from '@/modules/@common/constants/sort';

import { validationScheme } from '../../helpers/validationScheme';
import createFormSlice from '@/modules/@common/Form/redux/slices';
import createTableSlice from '@/modules/@common/Table/redux/slices';

const formSlice = createFormSlice(REDUX_FIELD_NAME, validationScheme);

export const { validateFields, validateSingleField } = formSlice;

const tableSlice = createTableSlice(TABLE_REDUX_FIELD_NAME, {
  page: 1,
  pageSize: ROWS_PER_PAGE_DEFAULT,
  sortBy: TABLE_SORT_BY,
  sortOrder: SORT_DESC,
});

const initialState = {
  salesforceInstanceUrl: '',
  salesforceId: '',
  name: '',
  salesManager: '',
  accountManager: '',
  accountManagerEmail: '',
  type: '',
  publisherDemand: false,
  adServingFeeBusinessModel: FEE_BUSINESS_MODEL_IMPRESSIONS_CPM,
  adServingFeeDisplay: null,
  adServingFeeVideo: null,
  avatarUrl: '',
  avatar: {
    base64Url: '',
    mimeType: '',
  },
  customLogoUrl: '',
  customLoadingMessage: '',
  customLoginPageUrl: '',
  subdomain: '',
  publisherRevShare: null,
  expenses: null,
  usersLimit: 100,
  videoDuplicatesBy: 'Title',
  activeTabId: TAB_DETAILS,

  featuresUi: [],
  featuresData: null,
  featuresDependsFieldData: null,

  dashboardsGeneral: [],
  dashboardsCustom: [],

  isSalesforceDataLoading: false,

  ...formSlice.state,
  // table
  ...tableSlice.state,
};

export const slice = createSlice({
  name: '@@ACCOUNTS/EDITOR',
  initialState,
  reducers: {
    setAction: (state, action) => {
      Object.entries(action.payload).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    setInitialAction: (state) => {
      Object.entries(initialState).forEach(([key, value]) => {
        state[key] = value;
      });
    },
    setActiveTabIdAction: (state, action) => {
      state.activeTabId = action.payload;
    },
    getSalesforceDataAction: (state) => state,
    getItemAction: (state) => state,
    createItemAction: (state) => state,
    updateItemAction: (state) => state,
    updateContentOwner: (state) => state,
    deleteAllVideos: (state) => state,

    setScrollToFieldNameAction: formSlice.actions.setScrollToFieldAction,
    setErrorByPropAction: formSlice.actions.updateValidationSchemeAction,
    removeErrorByPropAction: formSlice.actions.removeErrorByFieldNameAction,

    getDataAction: tableSlice.actions.getTableDataAction,
    setTableAction: tableSlice.actions.setTableAction,
  },
});

export const {
  setAction,
  setInitialAction,
  setActiveTabIdAction,
  getSalesforceDataAction,
  getItemAction,
  createItemAction,
  updateItemAction,
  updateContentOwner,
  deleteAllVideos,
  setScrollToFieldNameAction,
  setErrorByPropAction,
  removeErrorByPropAction,

  getDataAction,
  setTableAction,
} = slice.actions;

export default slice.reducer;
