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

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
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.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.client.entity.Client;
import com.ffii.lioner.modules.lioner.userActionLog.req.UpdateUserActionLogReq;
import com.ffii.lioner.modules.lioner.userActionLog.service.UserActionLogService;

import aj.org.objectweb.asm.Type;

import com.ffii.lioner.modules.common.service.ExcelReportService;
import com.ffii.core.exception.NotFoundException;
import com.ffii.core.response.DataRes;
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("/userActionLog")
public class UserActionLogController{

    private final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private UserActionLogService userActionLogService;

	private ExcelReportService excelReportService;

    public UserActionLogController(UserActionLogService userActionLogService, ExcelReportService excelReportService) {
        this.userActionLogService = userActionLogService;
		this.excelReportService = excelReportService;
	}

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

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false)
	@PostMapping("/save")
	public Map<String, Object> saveOrUpdate(@RequestBody @Valid UpdateUserActionLogReq req) {
		return Map.of(
				Params.DATA,userActionLogService.saveOrUpdate(req)
			);
	}

	@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class, readOnly = false)
	@DeleteMapping("/{id}")
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable Long id) {
		userActionLogService.markDelete(userActionLogService.find(id).get());
		// userActionLogService.markDeleteWithAuditLog(userActionLogService.find(id).get());
	}

    @GetMapping
	public RecordsRes<Map<String, Object>> list(HttpServletRequest request) throws ServletRequestBindingException {
		return new RecordsRes<>(userActionLogService.list(
				CriteriaArgsBuilder.withRequest(request)
						.addString("username")
						.addStringLike("actionDesc")
                        .addDate("fromDate")
						.addDateTo("toDate")
						.build()));
	}
	
}
