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.UpdateCategoryReq;
import com.ffii.lioner.modules.master.service.CategoryService;
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("/category")
public class CategoryController{

    private final Log logger = LogFactory.getLog(getClass());
    private CategoryService categoryService;
	
    public CategoryController(
		CategoryService categoryService
		) {
		this.categoryService = categoryService;
	}
    
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid UpdateCategoryReq req) {
		return new IdRes(categoryService.saveOrUpdate(req).getId());
	}

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

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

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

	@GetMapping("/combo")
	public RecordsRes<Map<String, Object>> listCategoryCombo(HttpServletRequest request) throws ServletRequestBindingException {
		System.out.println(request);
		return new RecordsRes<>(categoryService.listCategoryCombo(
				CriteriaArgsBuilder.withRequest(request)
						.build()));
	}
	
	@GetMapping("/checkDuplicate")
    public Map<String, Boolean> checkDuplicate(@RequestParam String name, @RequestParam Long categoryId) {
        boolean isNameTaken = categoryService.isNameTaken(name,categoryId);
        return Map.of(
            "isTaken", isNameTaken
        );
    }

}


