import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { User, AiSettings } from "../../types/user";

interface ProfileState {
  user: User | null;
  aiSettings: AiSettings | null;
  aiEnabled: boolean;
  loading: boolean;
}

const initialState: ProfileState = {
  user: null,
  aiSettings: null,
  aiEnabled: false,
  loading: false,
};

const profileSlice = createSlice({
  name: "profile",
  initialState,
  reducers: {
    setUser: (state, action: PayloadAction<User>) => {
      state.user = action.payload;
    },
    setAiSettings: (state, action: PayloadAction<AiSettings>) => {
      state.aiSettings = action.payload;
      state.aiEnabled = action.payload.ai_enabled === 1;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
  },
});

export const { setUser, setAiSettings, setLoading } = profileSlice.actions;
export default profileSlice.reducer;
