package com.ffii.fhsmsc.modules.food_nutrients.web;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ffii.core.response.IdRes;
import com.ffii.core.response.RecordsRes;
import com.ffii.core.utils.CriteriaArgsBuilder;
import com.ffii.fhsmsc.modules.food_nutrients.req.food_nutrientsReq;
import com.ffii.fhsmsc.modules.food_nutrients.service.food_nutrientsService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/food_nutrients")
public class food_nutrientsController {
    private static final Logger logger = LoggerFactory.getLogger(food_nutrientsController.class);
    private food_nutrientsService food_nutrientsService;

    public food_nutrientsController(
		food_nutrientsService food_nutrientsService
		) {
		this.food_nutrientsService = food_nutrientsService;
	}

    @GetMapping("/list")
	public RecordsRes<Map<String, Object>> listJson(HttpServletRequest request) throws ServletRequestBindingException {
        logger.info("Received list request with parameters: {}", request.getParameterMap());
        Map<String, Object> args = CriteriaArgsBuilder.withRequest(request)
                .addString("food_name")
                .addBoolean("embedding_text")  // Add embedding_text parameter
                .addBoolean("embedding_vector")
                .addInteger("id")
                .build();
        logger.info("Built args: {}", args);
        return new RecordsRes<>(food_nutrientsService.search(args));
	}
	
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid food_nutrientsReq req) {
		return new IdRes(food_nutrientsService.saveOrUpdate(req).getId());
	}

  
}
