package com.ffii.lioner.modules.lioner.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.ServletRequestBindingException;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.ffii.lioner.modules.lioner.req.UpdateSearchCriteriaTemplateReq;
import com.ffii.lioner.modules.lioner.service.SearchCriteriaTemplateService;
import com.ffii.core.exception.NotFoundException;
import com.ffii.core.response.IdRes;
import com.ffii.core.response.RecordsRes;
import com.ffii.core.utils.CriteriaArgsBuilder;
import com.ffii.core.utils.Params;

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

@RestController
@RequestMapping("/searchTemplate")
public class SearchCriteriaTemplateController{

    private final Log logger = LogFactory.getLog(getClass());
    private SearchCriteriaTemplateService searchCriteriaTemplateService;
	
    public SearchCriteriaTemplateController(
		SearchCriteriaTemplateService searchCriteriaTemplateService
		) {
		this.searchCriteriaTemplateService = searchCriteriaTemplateService;
	}
    
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid UpdateSearchCriteriaTemplateReq req) {
		return new IdRes(searchCriteriaTemplateService.saveOrUpdate(req));
	}

	@DeleteMapping("/{id}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable Long id) {
		searchCriteriaTemplateService.markDeleteWithAuditLog(searchCriteriaTemplateService.find(id).get());
	}

	@GetMapping
	public RecordsRes<Map<String, Object>> list(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(searchCriteriaTemplateService.search(
				CriteriaArgsBuilder.withRequest(request)
						.addStringLike("module")
						.addStringLike("templateName")
						.addInteger("userId")
						.build()));
	}

	@GetMapping("/combo")
	public RecordsRes<Map<String, Object>> combo(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(searchCriteriaTemplateService.combo(
				CriteriaArgsBuilder.withRequest(request)
						.addString("module")
						.addInteger("userId")
						.build()));
	}

	@GetMapping("/{id}")
	public Map<String, Object> get(@PathVariable Long id) {
		return Map.of(
				Params.DATA, searchCriteriaTemplateService.find(id).orElseThrow(NotFoundException::new)
			);
	}

    @GetMapping("/checkTemplateName")
    public Map<String, Boolean> checkTemplateName(@RequestParam String name, @RequestParam Long userId,
										 @RequestParam(defaultValue = "-1") Long templateId, @RequestParam String module) {
        boolean isTemplateNameTaken = searchCriteriaTemplateService.isTemplateNameTaken(name,userId,templateId,module);
        return Map.of(
            "isTaken", isTemplateNameTaken
        );
    }
}



