package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.BatchExecutionLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/batch-execution-log")
@RequiredArgsConstructor
@Slf4j
public class BatchExecutionLogController {
    private final BatchExecutionLogService batchExecutionLogService;

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBatchExecutionLogList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.getBatchExecutionLogList(params)));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBatchExecutionLogInfo(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.getBatchExecutionLogInfo(params)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertBatchExecutionLog(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.insertBatchExecutionLog(body)));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateBatchExecutionLog(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.updateBatchExecutionLog(body)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteBatchExecutionLog(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.deleteBatchExecutionLog(params)));
    }

    @GetMapping("/latest/{batchConfigId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBatchExecutionLogLatest(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long batchConfigId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("batch_config_id", batchConfigId);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.getBatchExecutionLogLatest(params)));
    }

    @GetMapping("/stats")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBatchExecutionLogStats(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(batchExecutionLogService.getBatchExecutionLogStats(params)));
    }
}
