import { createSlice } from "@reduxjs/toolkit"
import { EStatuses } from "../../enum/EStatuses"
import IUser from "../../interfaces/IUser"
import { createAppAsyncThunk } from "../createAppAsyncThunk"
import IUserCreateDto from "../../interfaces/IUserCreateDto"
import { usersApi } from "../../api/usersApi"
import IUsersState from "./IUserState"

const namespace: string = 'users'

export const loginUser = createAppAsyncThunk(
    `${namespace}/loginUser`,
    async (user: IUserCreateDto) => {
        return usersApi.loginUser(user)
    }
)

export const createUser = createAppAsyncThunk(
    `${namespace}/createUser`,
    async (user: IUserCreateDto) => {
        return await usersApi.createUser(user)
    }
)

export const checkToken = createAppAsyncThunk(
    `${namespace}/checkToken`,
    async () => {
        return usersApi.checkToken()
    }
)

const initialState: IUsersState = {
    user: {} as IUser,
    isAuth: false,
    loadingUser: false,
    messageUser: ''
}

export const usersSlice = createSlice({
    name: namespace,
    initialState: initialState,
    reducers: {
        setToInitState(state){
            state.isAuth = false
            state.user = {} as IUser
            state.loadingUser = false
            state.messageUser = ''
        },
        hideMessage(state){
            state.messageUser = ''
        }
    },
    extraReducers: (builder) => {
        builder
        .addCase(loginUser.pending, (state) => {
            state.loadingUser = true
        })
        .addCase(loginUser.rejected, (state) => {
            state.loadingUser = false
        })
        .addCase(loginUser.fulfilled, (state, action) => {
            state.loadingUser = false
            const user = action.payload.result
            state.user = user
            if (action.payload.status === EStatuses.FAILURE){
                state.messageUser = action.payload.message
            }
            if (user){
                localStorage.setItem('token', user.token)
                state.isAuth = true
            }
        })
        .addCase(createUser.pending, (state) => {
            state.loadingUser = true
        })
        .addCase(createUser.rejected, (state) => {
            state.loadingUser = false
        })
        .addCase(createUser.fulfilled, (state, action) => {
            state.loadingUser = false
            const user = action.payload.result
            state.user = user
            if (action.payload.status === EStatuses.FAILURE){
                state.messageUser = action.payload.message
            }
            if (user){
                localStorage.setItem('token', user.token)
                state.isAuth = true
            }
        })
        .addCase(checkToken.pending, (state) => {
            state.loadingUser = true
        })
        .addCase(checkToken.rejected, (state) => {
            state.loadingUser = false
        })
        .addCase(checkToken.fulfilled, (state, action) => {
            state.loadingUser = false
            const user = action.payload.result
            state.user = user
            if (user) {
                state.isAuth = true
            } else {
                state.isAuth = false
            }
        })
    }
})

export const {setToInitState, hideMessage} = usersSlice.actions