import { EPlacementKeys, ResponseGetSuccess } from '@/api/resources/Paywall'
import { createSlice, createSelector } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

type TAdditionalParameters = {
    isMustUpdate: {
        [key in EPlacementKeys]: boolean
    };
}

type TPaywalls = {
    [key in EPlacementKeys]: ResponseGetSuccess | null;
} & TAdditionalParameters

type IPayloadUpdatePaywall = {
    [key in EPlacementKeys]: ResponseGetSuccess
}

const initialState: TPaywalls = {
    "aura.placement.v1.mike": null,
    "aura.placement.main": null,
    "aura.placement.redesign.main": null,
    "aura.placement.email.marketing": null,
    "aura.placement.secret.discount": null,
    "aura.placement.palmistry.main": null,
    "aura.placement.palmistry.redesign": null,
    "aura.placement.chat": null,
    "aura.placement.email.palmistry": null,
    "aura.placement.email.palmistry.discount": null,
    "aura.placement.email.compatibility.discount": null,
    "aura.placement.palmistry.secret.discount": null,
    "aura.placement.compatibility.v2": null,
    "aura.placement.compatibility.v2.secret.discount": null,
    "aura.placement.compatibility.v3": null,
    "aura.placement.compatibility.v3.secret.discount": null,
    "aura.placement.compatibility.v4": null,
    "aura.placement.compatibility.v4.secret.discount": null,
    "aura.placement.payment": null,
    isMustUpdate: {
        "aura.placement.v1.mike": true,
        "aura.placement.main": true,
        "aura.placement.redesign.main": true,
        "aura.placement.email.marketing": true,
        "aura.placement.secret.discount": true,
        "aura.placement.palmistry.main": true,
        "aura.placement.palmistry.redesign": true,
        "aura.placement.chat": true,
        "aura.placement.email.palmistry": true,
        "aura.placement.email.palmistry.discount": true,
        "aura.placement.email.compatibility.discount": true,
        "aura.placement.palmistry.secret.discount": true,
        "aura.placement.compatibility.v2": true,
        "aura.placement.compatibility.v2.secret.discount": true,
        "aura.placement.compatibility.v3": true,
        "aura.placement.compatibility.v3.secret.discount": true,
        "aura.placement.compatibility.v4": true,
        "aura.placement.compatibility.v4.secret.discount": true,
        "aura.placement.payment": true,
    },
}

const paywallsSlice = createSlice({
    name: 'paywalls',
    initialState,
    reducers: {
        updatePaywall(state, action: PayloadAction<Partial<IPayloadUpdatePaywall>>) {
            return { ...state, ...action.payload }
        },
        updateIsMustUpdate(state, action: PayloadAction<Partial<TAdditionalParameters['isMustUpdate']>>) {
            return { ...state, isMustUpdate: { ...state.isMustUpdate, ...action.payload } }
        },
        resetIsMustUpdate(state) {
            return { ...state, isMustUpdate: initialState.isMustUpdate }
        }
    },
    extraReducers: (builder) => builder.addCase('reset', () => initialState),
})

export const { actions } = paywallsSlice
export const selectPaywalls = createSelector(
    (state: { paywalls: TPaywalls }) => state.paywalls,
    (paywalls) => paywalls
)
export const selectPaywallsIsMustUpdate = createSelector(
    (state: { paywalls: TPaywalls }) => state.paywalls.isMustUpdate,
    (paywalls) => paywalls
)
export default paywallsSlice.reducer
