package com.ffii.fhsmsc.modules.diet_records.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.diet_records.req.diet_recordsReq;
import com.ffii.fhsmsc.modules.diet_records.service.diet_recordsService;

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

@RestController
@RequestMapping("/diet_records")
public class diet_recordsController {
    private static final Logger logger = LoggerFactory.getLogger(diet_recordsController.class);
    private diet_recordsService diet_recordsService;

    public diet_recordsController(
		diet_recordsService diet_recordsService
		) {
		this.diet_recordsService = diet_recordsService;
	}

    @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)
                .addInteger("user_id")
                .addDate("start_date")  // Add start date parameter
                .addDate("end_date")    // Add end date parameter
                .build();
        logger.info("Built args: {}", args);
        return new RecordsRes<>(diet_recordsService.search(args));
	}
	
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid diet_recordsReq req) {
		return new IdRes(diet_recordsService.saveOrUpdate(req).getId());
	}
}
