import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { PartnerState } from "./state";
import { createPartnerAccountAsync, getPartnerInfoAsync } from "./operations";
import { LIMIT_PARTNER_VIEW_NUM } from "./constants";

const initialState: PartnerState = {
  domain: {
    getPartnersInfo: {
      total: 0,
      partners: [],
    },
  },
  apps: {
    addPartner: {
      companyName: "",
      country: "",
      adminName: "",
      email: "",
    },
    limit: LIMIT_PARTNER_VIEW_NUM,
    offset: 0,
    isLoading: false,
  },
};

export const partnerSlice = createSlice({
  name: "partner",
  initialState,
  reducers: {
    changeCompany: (state, action: PayloadAction<{ company: string }>) => {
      const { company } = action.payload;
      state.apps.addPartner.companyName = company;
    },
    changeCountry: (state, action: PayloadAction<{ country: string }>) => {
      const { country } = action.payload;
      state.apps.addPartner.country = country;
    },
    changeAdminName: (state, action: PayloadAction<{ adminName: string }>) => {
      const { adminName } = action.payload;
      state.apps.addPartner.adminName = adminName;
    },
    changeEmail: (state, action: PayloadAction<{ email: string }>) => {
      const { email } = action.payload;
      state.apps.addPartner.email = email;
    },
    cleanupAddPartner: (state) => {
      state.apps.addPartner = initialState.apps.addPartner;
    },
    cleanupApps: (state) => {
      state.domain = initialState.domain;
    },
    savePageInfo: (
      state,
      action: PayloadAction<{
        limit: number;
        offset: number;
      }>
    ) => {
      const { limit, offset } = action.payload;
      state.apps.limit = limit;
      state.apps.offset = offset;
    },
    changeDelegateAccount: (
      state,
      action: PayloadAction<{
        delegatedAccountId?: number;
        delegatedCompanyName?: string;
      }>
    ) => {
      const { delegatedAccountId, delegatedCompanyName } = action.payload;
      state.apps.delegatedAccountId = delegatedAccountId;
      state.apps.delegatedCompanyName = delegatedCompanyName;
    },
    cleanupDelegateAccount: (state) => {
      state.apps.delegatedAccountId = undefined;
      state.apps.delegatedCompanyName = undefined;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(createPartnerAccountAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(createPartnerAccountAsync.fulfilled, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(createPartnerAccountAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
    builder.addCase(getPartnerInfoAsync.pending, (state) => {
      state.apps.isLoading = true;
    });
    builder.addCase(getPartnerInfoAsync.fulfilled, (state, action) => {
      state.domain.getPartnersInfo.total = action.payload.total;
      state.domain.getPartnersInfo.partners = action.payload.partners;
      state.apps.isLoading = false;
    });
    builder.addCase(getPartnerInfoAsync.rejected, (state) => {
      state.apps.isLoading = false;
    });
  },
});
export const {
  changeEmail,
  changeAdminName,
  changeCompany,
  changeCountry,
  cleanupAddPartner,
  savePageInfo,
  changeDelegateAccount,
  cleanupDelegateAccount,
} = partnerSlice.actions;
export default partnerSlice.reducer;
