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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.Authentication;

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

import jakarta.validation.Valid;

@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> saveRecipe(@Valid @RequestBody Recipe recipe, Authentication authentication) {
		RecipeDto recipeDto = recipeService.convertToDto(recipe);
		String currentUsername = authentication.getName();
		return new ResponseEntity<>(recipeService.saveRecipe(recipeDto, currentUsername), 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 get recipe by name REST API
	@GetMapping("/search")
	public ResponseEntity<List<RecipeDto>> searchRecipes(
			@RequestParam(required = false) String name,  	  // by not adding a name all recipes will appear basically
			@RequestParam(required = false) List<String> tags, // since users can choose no tags this isnt required
			@RequestParam(required = false) List<Integer> prices,
			@RequestParam(required = false) List<Integer> cookTime,
			@RequestParam(required = false) List<Integer> prepTime
			) {
		
		
		
	    List<RecipeDto> recipes = recipeService.getRecipes(name, tags, prices, cookTime, prepTime);
	    return new ResponseEntity<>(recipes, HttpStatus.OK);
	}

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

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

}
