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

const initialState: LicenseSummaryState = {
  domain: {
    totalLicense: 0,
    allocatedLicense: 0,
    reusableLicense: 0,
    freeLicense: 0,
    expiringWithin14daysLicense: 0,
    issueRequesting: 0,
    numberOfRequesting: 0,
    shortage: 0,
    storageSize: 0,
    usedSize: 0,
    isStorageAvailable: false,
  },
  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 = action.payload;
    });
  },
});

export const { cleanupApps } = licenseSummarySlice.actions;

export default licenseSummarySlice.reducer;
