import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { USER_ROLES } from "components/auth/constants";
import { UsersState } from "./state";
import { addUserAsync, listUsersAsync } from "./operations";
import { RoleType } from "./types";

const initialState: UsersState = {
  domain: { users: [] },
  apps: {
    addUser: {
      name: "",
      role: USER_ROLES.NONE,
      authorId: "",
      typistGroupName: [],
      email: "",
      emailVerified: true,
      autoRenew: true,
      licenseAlert: true,
      notification: true,
      encryption: true,
      licenseStatus: "",
      prompt: false,
    },
    isLoading: false,
  },
};

export const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    changeName: (state, action: PayloadAction<{ name: string }>) => {
      const { name } = action.payload;
      state.apps.addUser.name = name;
    },
    changeEmail: (state, action: PayloadAction<{ email: string }>) => {
      const { email } = action.payload;
      state.apps.addUser.email = email;
    },
    changeRole: (state, action: PayloadAction<{ role: RoleType }>) => {
      const { role } = action.payload;
      state.apps.addUser.role = role;
    },
    changeAuthorId: (
      state,
      action: PayloadAction<{ authorId: string | undefined }>
    ) => {
      const { authorId } = action.payload;
      state.apps.addUser.authorId = authorId;
    },
    changeTypistGroupId: (
      state,
      action: PayloadAction<{ typistGroupId: number | undefined }>
    ) => {
      const { typistGroupId } = action.payload;
      state.apps.addUser.typistGroupId = typistGroupId;
    },
    changeAutoRenew: (state, action: PayloadAction<{ autoRenew: boolean }>) => {
      const { autoRenew } = action.payload;
      state.apps.addUser.autoRenew = autoRenew;
    },
    changeLicenseAlert: (
      state,
      action: PayloadAction<{ licenseAlert: boolean }>
    ) => {
      const { licenseAlert } = action.payload;
      state.apps.addUser.licenseAlert = licenseAlert;
    },
    changeNotification: (
      state,
      action: PayloadAction<{ notification: boolean }>
    ) => {
      const { notification } = action.payload;
      state.apps.addUser.notification = notification;
    },
    cleanupAddUser: (state) => {
      state.apps.addUser = initialState.apps.addUser;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(listUsersAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(listUsersAsync.fulfilled, (state, action) => {
      state.domain.users = action.payload.users;
      state.apps.isLoading = false;
    });
    builder.addCase(listUsersAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(addUserAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(addUserAsync.fulfilled, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(addUserAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
  },
});

export const {
  changeName,
  changeEmail,
  changeRole,
  changeAuthorId,
  changeTypistGroupId,
  changeAutoRenew,
  changeLicenseAlert,
  changeNotification,
  cleanupAddUser,
} = userSlice.actions;

export default userSlice.reducer;
