package com.example.demo.controller;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dto.UserDto;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepo;
import com.example.demo.service.UserService;

@RestController
@RequestMapping("/api/users")
public class UserController {

	private UserService userService;
	private UserRepo userRepo;

	public UserController(UserService userService, UserRepo userRepo) {
		super();
		this.userService = userService;
		this.userRepo = userRepo;
	}
	

	// build create user REST API
	@PostMapping
	public ResponseEntity<User> saveUser(@RequestBody User user) {
		return new ResponseEntity<User>(userService.saveUser(user), HttpStatus.CREATED);
	}

	// build get all users REST API
	@GetMapping
	public List<UserDto> getAllUsers() {
		return userService.getAllUsers();
	}
	
	// build get user by name REST API
	@GetMapping("/search")
	public ResponseEntity<List<UserDto>> getUsersByName(@RequestParam String name) {
	    List<UserDto> users = userService.getUsersByName(name);
	    return new ResponseEntity<>(users, HttpStatus.OK);
	}

	// build get current user REST API
	@GetMapping("/me")
	public UserDto getLoggedInUser(Principal principal) {
	    if (principal == null) return null;
	    String username = principal.getName();
	    User user = (userRepo.findByUsername(username))
	    		.orElse(null);
	    return userService.convertToDto(user);
	}

	// build get user by id REST API
	// http://localhost:8080/api/users/(id number goes here)
	@GetMapping("{id}")
	public ResponseEntity<UserDto> getUserById(@PathVariable("id") Integer userId) {
		return new ResponseEntity<UserDto>(userService.getUserById(userId), HttpStatus.OK);
	}

	// build create favorite REST API
	@PostMapping("/{userId}/favorites/{recipeId}")
	public ResponseEntity<UserDto> saveFavorite(@PathVariable Integer userId, @PathVariable Integer recipeId) {

		UserDto updatedUser = userService.saveFavorite(userId, recipeId);

		return new ResponseEntity<>(updatedUser, HttpStatus.OK);
	}

	// build update user REST API
	// http://localhost:8080/api/users/(id number goes here)
	@PutMapping("{id}")
	public ResponseEntity<UserDto> updateUser(@PathVariable("id") Integer userId, @RequestBody User user) {
		return new ResponseEntity<UserDto>(userService.updateUser(user, userId), HttpStatus.OK);
	}

	// build delete user REST API
	@DeleteMapping("{id}")
	public ResponseEntity<String> deleteUser(@PathVariable("id") Integer userId) {
		userService.deleteUser(userId);
		return new ResponseEntity<String>("User deleted succesfully!", HttpStatus.OK);
	}

	// build delete favorite REST API
	@DeleteMapping("/{userId}/favorites/{recipeId}")
	public ResponseEntity<String> deleteFavorite(@PathVariable Integer userId, @PathVariable Integer recipeId) {

		userService.deleteFavorite(userId, recipeId);
		return new ResponseEntity<String>("Favorite deleted succesfully!", HttpStatus.OK);
	}

}
