package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.example.demo.dto.UserDto;
import com.example.demo.models.UserPO;
import com.example.demo.repository.UserRepository;

@Service
public class UserService {
	
	private final UserRepository userRepository;
	
	public UserService(UserRepository userRepository) {
		this.userRepository = userRepository;
	}
	
	/**
	 * Get all user
	 * @return return all found user
	 */
	public List<UserDto> loadAllUsers() {
		List<UserPO> userListPO = this.userRepository.findAll();
		List<UserDto> userListDto = new ArrayList<>();
		
		userListPO.stream().forEach(userPO -> {
			userListDto.add(this.convertUserPOToUserDto(userPO));
		});
		
		return userListDto;
	}
	
	/**
	 * Get a specific user
	 * @param id The id of the project
	 * @return The found project
	 */
	public UserDto loadUser(Integer id) {
		Optional<UserPO> userPO = this.userRepository.findById(id);
		return this.convertUserPOToUserDto(userPO.get());
	}
	
	/**
	 * Update a specific user
	 * @param id The id of the user
	 * @param userPO The updated user
	 */
	public void updateUser(Integer id, UserDto userDto) {
		Optional<UserPO> userPO = userRepository.findById(id);
		userPO.get().setFirstName(userDto.getFirstName());
		userPO.get().setLastName(userDto.getLastName());
		 this.userRepository.save(userPO.get());
	}
	
	/**
	 * Delete a specific user
	 * @param id The id of the user
	 */
	public void deleteUser(Integer id) {
		userRepository.deleteById(id);
	}
	
	public UserPO createUser(UserPO user) {
		return userRepository.save(user);
	}
	
	/**
	 * Convert a UserPO object into a UserDto object
	 * @param userPO The UserPO object
	 * @return The UserDto object
	 */
	private UserDto convertUserPOToUserDto(UserPO userPO) {
			UserDto userDto = new UserDto();
			userDto.setFirstName(userPO.getFirstName());
			userDto.setId(userPO.getId());
			userDto.setLastName(userPO.getLastName());
			
		return userDto;
	}
}
