package com.example.demo.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;

import com.example.demo.dto.RecipeDto;
import com.example.demo.entity.Recipe;
import com.example.demo.entity.User;
import com.example.demo.service.RecipeService;

@RestController
@RequestMapping("/api/recipes")
public class RecipeController {

	private RecipeService recipeService;

	public RecipeController(RecipeService recipeService) {
		super();
		this.recipeService = recipeService;
	}

	// build create recipe REST API
	@PostMapping
	public ResponseEntity<RecipeDto> saveUser(@RequestBody Recipe recipe) {
		RecipeDto recipeDto = recipeService.convertToDto(recipe);
		return new ResponseEntity<RecipeDto>(recipeService.saveRecipe(recipeDto), HttpStatus.CREATED);
	}

	// build get all recipes REST API
	@GetMapping
	public List<RecipeDto> getAllRecipes() {
		return recipeService.getAllRecipes();
	}

	// build get recipe by id REST API
	// http://localhost:8080/api/recipes/(id number goes here)
	@GetMapping("{id}")
	public ResponseEntity<RecipeDto> getRecipeById(@PathVariable("id") Integer recipeId) {
		return new ResponseEntity<RecipeDto>(recipeService.getRecipeById(recipeId), HttpStatus.OK);
	}

	// build update recipe REST API
	// http://localhost:8080/api/recipes/(id number goes here)
	@PutMapping("{id}")
	public ResponseEntity<RecipeDto> updateUser(@PathVariable("id") Integer recipeId, @RequestBody Recipe recipe) {
		RecipeDto recipeDto = recipeService.convertToDto(recipe);
		return new ResponseEntity<RecipeDto>(recipeService.updateRecipe(recipeDto, recipeId), HttpStatus.OK);
	}

	// build delete recipe REST API
	// http://localhost:8080/api/recipes/(id number goes here)
	@DeleteMapping("{id}")
	public ResponseEntity<String> deleteUser(@PathVariable("id") Integer recipeId) {
		recipeService.deleteRecipe(recipeId);
		return new ResponseEntity<String>("Recipe deleted succesfully!", HttpStatus.OK);
	}

}
