package de.avatic.lcc.controller;

import de.avatic.lcc.dto.error.CalculationJobDumpDTO;
import de.avatic.lcc.dto.error.ErrorLogDTO;
import de.avatic.lcc.dto.error.FrontendErrorDTO;
import de.avatic.lcc.repositories.error.DumpRepository;
import de.avatic.lcc.repositories.pagination.SearchQueryResult;
import de.avatic.lcc.service.error.SysErrorService;
import jakarta.validation.constraints.Min;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping({"/api/error", "/api/error/"})
public class ErrorController {

    private final SysErrorService sysErrorService;

    public ErrorController(SysErrorService sysErrorService) {
        this.sysErrorService = sysErrorService;
    }

    @PostMapping
    public void error(@RequestBody List<FrontendErrorDTO> errors) {
        sysErrorService.addErrors(errors);
    }

    @GetMapping
    public ResponseEntity<List<ErrorLogDTO>> listErrors(@RequestParam(defaultValue = "20") @Min(1) int limit,
                                                           @RequestParam(defaultValue = "1") @Min(1) int page,
                                                           @RequestParam(required = false) Optional<String> filter) {
        SearchQueryResult<ErrorLogDTO> errors = sysErrorService.listErrors(filter, page, limit);

        return ResponseEntity.ok()
                .header("X-Total-Count", String.valueOf(errors.getTotalElements()))
                .header("X-Page-Count", String.valueOf(errors.getTotalPages()))
                .header("X-Current-Page", String.valueOf(errors.getPage()))
                .body(errors.toList());
    }



}
