package de.avatic.lcc.service.error;

import de.avatic.lcc.dto.error.ErrorLogDTO;
import de.avatic.lcc.dto.error.FrontendErrorDTO;
import de.avatic.lcc.repositories.error.SysErrorRepository;
import de.avatic.lcc.repositories.pagination.SearchQueryPagination;
import de.avatic.lcc.repositories.pagination.SearchQueryResult;
import de.avatic.lcc.service.transformer.error.SysErrorTransformer;
import jakarta.validation.constraints.Min;
import org.springframework.stereotype.Service;

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

@Service
public class SysErrorService {


    private final SysErrorRepository sysErrorRepository;
    private final SysErrorTransformer sysErrorTransformer;

    public SysErrorService(SysErrorRepository sysErrorRepository, SysErrorTransformer sysErrorTransformer) {
        this.sysErrorRepository = sysErrorRepository;
        this.sysErrorTransformer = sysErrorTransformer;
    }

    public void addErrors(List<FrontendErrorDTO> dto) {
        sysErrorRepository.insert(dto.stream().map(sysErrorTransformer::toSysErrorEntity).toList());
    }

    public SearchQueryResult<ErrorLogDTO> listErrors(Optional<String> filter, @Min(1) int page, @Min(1) int limit) {
        return SearchQueryResult.map(sysErrorRepository.listErrors(filter, new SearchQueryPagination(page, limit)), sysErrorTransformer::toSysErrorDto);
    }
}
