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

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
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.master.req.UpdateAppreciationCategoryReq;
import com.ffii.lioner.modules.master.service.AppreciationCategoryService;
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("/appreciationCategory")
public class AppreciationCategoryController{

    private final Log logger = LogFactory.getLog(getClass());
    private AppreciationCategoryService appreciationCategoryService;
	
    public AppreciationCategoryController(
		AppreciationCategoryService appreciationCategoryService
		) {
		this.appreciationCategoryService = appreciationCategoryService;
	}
    
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid UpdateAppreciationCategoryReq req) {
		return new IdRes(appreciationCategoryService.saveOrUpdate(req).getId());
	}

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

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

    @GetMapping
	public RecordsRes<Map<String, Object>> getChannelList(HttpServletRequest request) throws ServletRequestBindingException {
		System.out.println(request);
		return new RecordsRes<>(appreciationCategoryService.list(
				CriteriaArgsBuilder.withRequest(request)
						.addInteger("id")
                        .addStringLike("name")
						.build()));
	}
	
	@GetMapping("/combo")
	public RecordsRes<Map<String, Object>> getChannelCombo(HttpServletRequest request) throws ServletRequestBindingException {
		System.out.println(request);
		return new RecordsRes<>(appreciationCategoryService.listDepartmentCombo(
				CriteriaArgsBuilder.withRequest(request)
						.build()));
	}

	@GetMapping("/checkDuplicate")
    public Map<String, Boolean> checkDuplicate(@RequestParam String name, @RequestParam Long id) {
        boolean isNameTaken = appreciationCategoryService.isNameTaken(name,id);
        return Map.of(
            "isTaken", isNameTaken
        );
    }
}


