import { createSlice } from '@reduxjs/toolkit';

import { getHostGroups, getHostManagement, updateHostManagement } from './thunks';

export interface HostIdentifierOption {
  scope: string;
  name: string;
  label: string;
  example: string | null;
}

export interface HostGroup {
  id: string;
  name: string;
}

export interface InactivityCleanup {
  enabled: boolean;
  days: number;
  completelyClear: boolean;
}

export interface DeletedHostsCleanup {
  enabled: boolean;
  days: number;
}

export interface DuplicateCleanup {
  enabled: boolean;
  completelyClear: boolean;
}

export interface GroupCleanup {
  enabled: boolean;
  groupId: number | null;
  completelyClear: boolean;
}

export interface HostManagementForm {
  identifier: string;
  inactiveHostsCleanup: InactivityCleanup;
  deletedHostsCleanup: DeletedHostsCleanup;
  duplicateHostnameHostsCleanup: DuplicateCleanup;
  duplicateIpHostsCleanup: DuplicateCleanup;
  groupHostsCleanup: GroupCleanup;
}

export interface HostManagementState {
  identifier: {
    current: string;
    options: HostIdentifierOption[];
  };
  inactiveHostsCleanup: InactivityCleanup;
  deletedHostsCleanup: DeletedHostsCleanup;
  duplicateHostnameHostsCleanup: DuplicateCleanup;
  duplicateIpHostsCleanup: DuplicateCleanup;
  groupHostsCleanup: GroupCleanup;
  groups: HostGroup[];
  loading: boolean;
  isSaving: boolean;
  isDirty: boolean;
  error: string | null;
}

const initialState: HostManagementState = {
  identifier: { current: '', options: [] },
  inactiveHostsCleanup: { enabled: false, days: 30, completelyClear: false },
  deletedHostsCleanup: { enabled: false, days: 90 },
  duplicateHostnameHostsCleanup: { enabled: false, completelyClear: false },
  duplicateIpHostsCleanup: { enabled: false, completelyClear: false },
  groupHostsCleanup: { enabled: false, groupId: null, completelyClear: false },
  groups: [],
  loading: false,
  isSaving: false,
  isDirty: false,
  error: null,
};

const hostManagementSlice = createSlice({
  name: 'hostManagement',
  initialState,
  reducers: {
    setIdentifier(state, { payload }: { payload: string }) {
      state.identifier.current = payload;
      state.isDirty = true;
    },
    setInactiveHostsCleanup(state, { payload }: { payload: InactivityCleanup }) {
      state.inactiveHostsCleanup = payload;
      state.isDirty = true;
    },
    setDeletedHostsCleanup(state, { payload }: { payload: DeletedHostsCleanup }) {
      state.deletedHostsCleanup = payload;
      state.isDirty = true;
    },
    setDuplicateHostnameHostsCleanup(state, { payload }: { payload: DuplicateCleanup }) {
      state.duplicateHostnameHostsCleanup = payload;
      state.isDirty = true;
    },
    setDuplicateIpHostsCleanup(state, { payload }: { payload: DuplicateCleanup }) {
      state.duplicateIpHostsCleanup = payload;
      state.isDirty = true;
    },
    setGroupHostsCleanup(state, { payload }: { payload: GroupCleanup }) {
      state.groupHostsCleanup = payload;
      state.isDirty = true;
    },
  },
  extraReducers: builder => {
    builder
      .addCase(getHostManagement.pending, state => {
        state.loading = true;
        state.error = null;
      })
      .addCase(getHostManagement.fulfilled, (state, { payload }) => {
        state.loading = false;
        state.identifier = payload.identifier;
        state.inactiveHostsCleanup = payload.inactiveHostsCleanup;
        state.deletedHostsCleanup = payload.deletedHostsCleanup;
        state.duplicateHostnameHostsCleanup = payload.duplicateHostnameHostsCleanup;
        state.duplicateIpHostsCleanup = payload.duplicateIpHostsCleanup;
        state.groupHostsCleanup = payload.groupHostsCleanup;
        state.isDirty = false;
      })
      .addCase(getHostManagement.rejected, (state, action) => {
        state.loading = false;
        state.error = action.error.message || 'Failed to fetch host management settings.';
      });

    builder.addCase(getHostGroups.fulfilled, (state, { payload }) => {
      state.groups = payload;
    });

    builder
      .addCase(updateHostManagement.pending, state => {
        state.isSaving = true;
        state.error = null;
      })
      .addCase(updateHostManagement.fulfilled, (state, { payload }) => {
        state.isSaving = false;
        state.identifier.current = payload.identifier;
        state.inactiveHostsCleanup = payload.inactiveHostsCleanup;
        state.deletedHostsCleanup = payload.deletedHostsCleanup;
        state.duplicateHostnameHostsCleanup = payload.duplicateHostnameHostsCleanup;
        state.duplicateIpHostsCleanup = payload.duplicateIpHostsCleanup;
        state.groupHostsCleanup = payload.groupHostsCleanup;
        state.isDirty = false;
      })
      .addCase(updateHostManagement.rejected, (state, action) => {
        state.isSaving = false;
        state.error = action.error.message || 'Failed to update host management settings.';
      });
  },
});

export const {
  setIdentifier,
  setInactiveHostsCleanup,
  setDeletedHostsCleanup,
  setDuplicateHostnameHostsCleanup,
  setDuplicateIpHostsCleanup,
  setGroupHostsCleanup,
} = hostManagementSlice.actions;

export const selectIsSaving = (state: { hostManagement: HostManagementState }) => state.hostManagement.isSaving;
export const selectIsDirty = (state: { hostManagement: HostManagementState }) => state.hostManagement.isDirty;

export default hostManagementSlice.reducer;
