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

import java.util.Map;

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.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 com.ffii.fhsmsc.modules.user.req.SaveGroupReq;
import com.ffii.fhsmsc.modules.user.service.GroupService;

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

@RestController
@RequestMapping("/group")
public class GroupController{

    private GroupService groupService;
	
    public GroupController(
		GroupService groupService
		) {
		this.groupService = groupService;
	}
    
    @PostMapping("/save")
	public IdRes saveOrUpdate(@RequestBody @Valid SaveGroupReq req) {
		return new IdRes(groupService.saveOrUpdate(req).getId());
	}

	@DeleteMapping("/{id}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable Long id) {
		groupService.delete(groupService.find(id).orElseThrow(NotFoundException::new));
	}

	@GetMapping("/{id}")
	public Map<String, Object> load(@PathVariable Long id) {
		return Map.of(
				Params.DATA, groupService.find(id).orElseThrow(NotFoundException::new),
				"authIds", groupService.listGroupAuthId(id),
				"userIds", groupService.listGroupUserId(id));
	}

	@GetMapping("/combo")
	public RecordsRes<Map<String, Object>> comboJson(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(groupService.searchForCombo(
				CriteriaArgsBuilder.withRequest(request)
						.addInteger(Params.ID)
						.addStringLike(Params.QUERY)
						.build()));
	}

	@GetMapping
	public RecordsRes<Map<String, Object>> listJson(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(groupService.search(
				CriteriaArgsBuilder.withRequest(request)
						.addInteger(Params.ID)
						.addStringLike(Params.NAME)
						.addStringLike("description")
						.addInteger("userId")
						.build()));
	}

	@GetMapping("/member")
	public RecordsRes<Map<String, Object>> listGroupUser(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(groupService.listGroupMember(
				CriteriaArgsBuilder.withRequest(request)
						.addInteger(Params.ID)
						.build()));
	}

	@GetMapping("/auth/combo")
	public RecordsRes<Map<String, Object>> authComboJson(HttpServletRequest request) throws ServletRequestBindingException {
		System.out.println(request);
		return new RecordsRes<>(groupService.listGroupAuth(
				CriteriaArgsBuilder.withRequest(request)
						.addInteger("groupId")
						.build()));
	}

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