package com.example.demo.service.Impl;

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

import org.springframework.stereotype.Service;

import com.example.demo.dto.ImageDto;
import com.example.demo.dto.RecipeDto;
import com.example.demo.dto.RecipeIngredientDto;
import com.example.demo.dto.StepDto;
import com.example.demo.dto.TagDto;
import com.example.demo.dto.UserDto;
import com.example.demo.entity.Recipe;
import com.example.demo.entity.User;
import com.example.demo.exception.NotFoundException;
import com.example.demo.repository.RecipeRepo;
import com.example.demo.repository.UserRepo;
import com.example.demo.service.UserService;

import jakarta.transaction.Transactional;

@Service
public class UserServiceImpl implements UserService {

	private UserRepo userRepository;
	private RecipeRepo recipeRepository;

	public UserServiceImpl(UserRepo userRepository, RecipeRepo recipeRepository) {
		super();
		this.userRepository = userRepository;
		this.recipeRepository = recipeRepository;
	}

	public UserDto convertToDto(User user) {
		return new UserDto(user.getId(), user.getUsername(), user.getEmail());
	}

	@Override
	public User saveUser(User user) {
		return userRepository.save(user);
	}

	@Override
	public List<UserDto> getAllUsers() {

		List<UserDto> list = new ArrayList<>();
		for (User user : userRepository.findAll()) {
			UserDto userDto = convertToDto(user);
			list.add(userDto);
		}

		return list;
	}

	@Override
	public UserDto getUserById(Integer Id) {

		return convertToDto(userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id)));
	}

	@Override
	@Transactional
	public UserDto saveFavorite(Integer userId, Integer recipeId) {
		User existingUser = userRepository.findById(userId)
				.orElseThrow(() -> new NotFoundException("User", "id", userId));

		Recipe existingRecipe = recipeRepository.findById(recipeId)
				.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));

		existingUser.getFavRecipes().add(existingRecipe);
		userRepository.save(existingUser);

		return convertToDto(existingUser);
	}

	@Override
	public UserDto updateUser(User user, Integer Id) {

		User existingUser = userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));

		existingUser.setUsername(user.getUsername());
		existingUser.setEmail(user.getEmail());

		userRepository.save(existingUser);

		return convertToDto(existingUser);
	}

	@Override
	public void deleteUser(Integer Id) {
		userRepository.findById(Id).orElseThrow(() -> new NotFoundException("User", "id", Id));
		userRepository.deleteById(Id);
	}

	@Override
	@Transactional
	public void deleteFavorite(Integer userId, Integer recipeId) {
		User existingUser = userRepository.findById(userId)
				.orElseThrow(() -> new NotFoundException("User", "id", userId));

		Recipe existingRecipe = recipeRepository.findById(recipeId)
				.orElseThrow(() -> new NotFoundException("Recipe", "id", recipeId));
		userRepository.save(existingUser);

		existingUser.getFavRecipes().remove(existingRecipe);

	}

}
