import { createSlice } from "@reduxjs/toolkit";
import { LicenseSummaryState } from "./state";
import { getCompanyNameAsync, getLicenseSummaryAsync } from "./operations";

const initialState: LicenseSummaryState = {
  domain: {
    licenseSummaryInfo: {
      totalLicense: 0,
      allocatedLicense: 0,
      reusableLicense: 0,
      freeLicense: 0,
      expiringWithin14daysLicense: 0,
      issueRequesting: 0,
      numberOfRequesting: 0,
      shortage: 0,
      storageSize: 0,
      usedSize: 0,
      isStorageAvailable: false,
    },
    accountInfo: {
      companyName: "",
    },
  },
  apps: {
    isLoading: false,
  },
};

export const licenseSummarySlice = createSlice({
  name: "licenseSummary",
  initialState,
  reducers: {
    cleanupApps: (state) => {
      state.domain = initialState.domain;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getLicenseSummaryAsync.fulfilled, (state, action) => {
      state.domain.licenseSummaryInfo = action.payload;
    });
    builder.addCase(getCompanyNameAsync.fulfilled, (state, action) => {
      state.domain.accountInfo.companyName = action.payload.companyName;
    });
  },
});

export const { cleanupApps } = licenseSummarySlice.actions;

export default licenseSummarySlice.reducer;
