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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.CrossOrigin;
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.RestController;

import com.ffii.core.response.RecordsRes;
import com.ffii.core.utils.CriteriaArgsBuilder;
import com.ffii.core.utils.Params;
import com.ffii.lioner.modules.lioner.client.req.UpdateClientReq;
import com.ffii.lioner.modules.lioner.pdf.req.UpdateConsultantReq;
import com.ffii.lioner.modules.lioner.pdf.service.ConsultantService;

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


@RestController
@RequestMapping("/consultant")
@CrossOrigin(origins = "", allowedHeaders = "")

public class ConsultantController {

    private ConsultantService consultantService;
    private static final Logger logger = LoggerFactory.getLogger(ConsultantController.class);

    public ConsultantController(ConsultantService consultantService) {
        this.consultantService = consultantService;
    }

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

    @GetMapping
	public RecordsRes<Map<String, Object>> list(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(consultantService.list(
            CriteriaArgsBuilder.withRequest(request)
                        .addStringLike("name")
                        .addDate("createDateFrom")
                        .addDate("createDateTo")
                        .addStringLike("remarks")
						.build()));
	}

    @GetMapping("/{id}")
	public Map<String, Object> get(@PathVariable Long id) {
        return consultantService.loadConsultant(id);
	}
    
    @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false)
	@PostMapping("/save")
	public Map<String, Object> saveOrUpdate(@RequestBody @Valid UpdateConsultantReq req) {
        logger.info("saveOrUpdate");
		return Map.of(
				Params.DATA,consultantService.saveOrUpdate(req)
			);   
	}

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
	@PostMapping("/delete")
	public Map<String, Object> delete(@RequestBody @Valid UpdateConsultantReq req) {
		logger.info("Deleting consultant with id: {}", req.getId());
		consultantService.markDelete(req);  // assuming this handles soft/hard delete
		return Map.of(Params.DATA, true);  // or whatever your frontend expects
	}

    @GetMapping("/combo")
	public RecordsRes<Map<String, Object>> getCombo(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(consultantService.list(
				CriteriaArgsBuilder.withRequest(request)
						.build()));
	}
}
