import { create } from 'zustand'
import { ActivateUser, CreatePosition, DeactivateUser, DemoteUser, GetUserTable, PromoteUser, SetUserPosition } from '../client/controllers'
import { UserResponse } from '../client/response'
type UserData = {
	tableUsers: UserResponse[]
}

type UsersActions = {
	sync: (page: number) => Promise<UserResponse[]>
	setUserPosition: (userId: number, positionName: string) => Promise<void>
	promoteUser: (userId: number) => Promise<void>
	demoteUser: (userId: number) => Promise<void>
	deactivateUser: (userId: number) => Promise<void>
	activateUser: (userId: number) => Promise<void>
	createPosition: (positionName: string) => Promise<void>
}

export const useUserStore = create<UserData & UsersActions>((set) => ({
	sync: async (page: number): Promise<UserResponse[]> => {
		const users_in_page = await GetUserTable(page)
		set((state) => ({
			... state,
			tableUsers: users_in_page,
		}))
		return users_in_page
	},
	tableUsers: [] as UserResponse[],
	setUserPosition: async (userId, positionName): Promise<void> => {
		await SetUserPosition(userId, positionName)
		
		
		set((state) => {
			const match_user = state.tableUsers.filter((u) => u.Id === userId)[0]
			match_user.JobPosition = positionName
			return {
				... state,
				tableUsers: [...state.tableUsers, match_user]
			}
		})
	},
	promoteUser: async (userId: number): Promise<void> => {
		await PromoteUser(userId)
		set((state) => {
			const users = state.tableUsers.filter((u) => u.Id === userId)
			if(users.length > 0)
			{
				const user = users[0]
				const tableUsersWithoutUser = state.tableUsers.filter((u) => u.Id !== userId)
				user.Admin = true
				return {
					...state,
					tableUsers: [...tableUsersWithoutUser, user]
				}
			}
			return state
		})
	},
	demoteUser: async (userId: number): Promise<void> => {
		await DemoteUser(userId)
		set((state) => {
			const users = state.tableUsers.filter((u) => u.Id === userId)
			if(users.length > 0)
			{
				const user = users[0]
				const tableUsersWithoutUser = state.tableUsers.filter((u) => u.Id !== userId)
				user.Admin = false
				return {
					...state,
					tableUsers: [...tableUsersWithoutUser, user]
				}
			}
			return state
		})
	},
	deactivateUser: async (userId: number): Promise<void> => {
		await DeactivateUser(userId)

		set((state) => {
			const users = state.tableUsers.filter((u) => u.Id === userId)
			if(users.length > 0)
			{
				const user = users[0]
				const tableUsersWithoutUser = state.tableUsers.filter((u) => u.Id !== userId)
				user.Active = false
				return {
					...state,
					tableUsers: [...tableUsersWithoutUser, user]
				}
			}
			return state
		})
	},
	activateUser: async (userId: number): Promise<void> => {
		await ActivateUser(userId)
		set((state) => {
			const users = state.tableUsers.filter((u) => u.Id === userId)
			if(users.length > 0)
			{
				const user = users[0]
				const tableUsersWithoutUser = state.tableUsers.filter((u) => u.Id !== userId)
				user.Active = true
				return {
					...state,
					tableUsers: [...tableUsersWithoutUser, user]
				}
			}
			return state
		})
	},
	createPosition: async (positionName: string): Promise<void> => {
		await CreatePosition(positionName)
	}
}))
