import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { apiGetAccountFormData } from '@/services/AccountServices';

export const getForm = createAsyncThunk('accountDetailForm/data/getForm', async (data) => {
  const response = await apiGetAccountFormData(data);
  return response.data;
});

const initialState = {
  formData: {
    CompanyRegistrationInfo: {
      //company info
      businessActivityType: '',
      commercialNameArabic: '',
      commercialNameEnglish: '',
      companyRegistrationNo: '',
      companyRegistrationCertificate: '',
      //address info
      countryCode: 'OMN',
      address: '',
      governorate: '',
      companyTelephoneNo: '',
      dialCode: '+968',
    },
    CompanyAddress: {},
    UserInformation: {
      idDocumentType: '',
      idDocumentNo: '',
      fullName: '',
      authorizedManagerNationality: '',
      email: '',
      authorizedManagerMobileNo: '',
      dialCode: '+968',
      frontsideIDCard: '',
      backsideIDCard: '',
    },
    VerificationOTP: {
      verificationCode: '',
    },
    NewPassword: {
      password: '',
      confirmPassword: '',
    },
  },
  stepStatus: {
    0: { status: 'pending' },
    1: { status: 'pending' },
    2: { status: 'pending' },
    3: { status: 'pending' },
    4: { status: 'pending' },
  },
};
const dataSlice = createSlice({
  name: 'accountDetailForm/data',
  initialState: initialState,
  reducers: {
    setFormData: (state, action) => {
      state.formData = { ...state.formData, ...action.payload };
    },
    setStepStatus: (state, action) => {
      state.stepStatus = { ...state.stepStatus, ...action.payload };
    },
    clearData: (state) => {
      state.formData = initialState.formData;
      state.stepStatus = initialState.stepStatus;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(getForm.fulfilled, (state, action) => {
      state.formData = action.payload.formData;
      state.stepStatus = action.payload.formStatus;
    });
  },
});

export const { setFormData, setStepStatus, clearData } = dataSlice.actions;

export default dataSlice.reducer;
