import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { AccountState } from "./state";
import {
  updateAccountInfoAsync,
  getAccountRelationsAsync,
  deleteAccountAsync,
} from "./operations";

const initialState: AccountState = {
  domain: {
    getAccountInfo: {
      account: {
        accountId: 0,
        companyName: "",
        tier: 0,
        country: "",
        delegationPermission: false,
      },
    },
    dealers: [],
    users: [],
  },

  apps: {
    updateAccountInfo: {
      parentAccountId: undefined,
      delegationPermission: false,
      primaryAdminUserId: 0,
      secondryAdminUserId: undefined,
    },
    isLoading: false,
  },
};

export const accountSlice = createSlice({
  name: "account",
  initialState,
  reducers: {
    changeDealer: (
      state,
      action: PayloadAction<{ parentAccountId: number | undefined }>
    ) => {
      const { parentAccountId } = action.payload;
      state.apps.updateAccountInfo.parentAccountId = parentAccountId;
    },
    changeDealerPermission: (
      state,
      action: PayloadAction<{ delegationPermission: boolean }>
    ) => {
      const { delegationPermission } = action.payload;
      state.apps.updateAccountInfo.delegationPermission = delegationPermission;
    },
    changePrimaryAdministrator: (
      state,
      action: PayloadAction<{ primaryAdminUserId: number }>
    ) => {
      const { primaryAdminUserId } = action.payload;
      state.apps.updateAccountInfo.primaryAdminUserId = primaryAdminUserId;
    },
    changeSecondryAdministrator: (
      state,
      action: PayloadAction<{ secondryAdminUserId: number | undefined }>
    ) => {
      const { secondryAdminUserId } = action.payload;
      state.apps.updateAccountInfo.secondryAdminUserId = secondryAdminUserId;
    },
    cleanupApps: (state) => {
      state.domain = initialState.domain;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getAccountRelationsAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(getAccountRelationsAsync.fulfilled, (state, action) => {
      state.domain.getAccountInfo = action.payload.accountInfo;
      state.domain.dealers = action.payload.dealers.dealers;
      state.domain.users = action.payload.users.users;
      state.apps.updateAccountInfo.parentAccountId =
        action.payload.accountInfo.account.parentAccountId;
      state.apps.updateAccountInfo.delegationPermission =
        action.payload.accountInfo.account.delegationPermission;
      if (action.payload.accountInfo.account.primaryAdminUserId)
        state.apps.updateAccountInfo.primaryAdminUserId =
          action.payload.accountInfo.account.primaryAdminUserId;
      state.apps.updateAccountInfo.secondryAdminUserId =
        action.payload.accountInfo.account.secondryAdminUserId;
      state.apps.isLoading = false;
    });
    builder.addCase(getAccountRelationsAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(updateAccountInfoAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(updateAccountInfoAsync.fulfilled, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(updateAccountInfoAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(deleteAccountAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(deleteAccountAsync.fulfilled, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(deleteAccountAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
  },
});
export const {
  changeDealer,
  changeDealerPermission,
  changePrimaryAdministrator,
  changeSecondryAdministrator,
  cleanupApps,
} = accountSlice.actions;
export default accountSlice.reducer;
