package ru.soune.nocopy.service.statistic;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import ru.soune.nocopy.dto.statistic.ViolationStatisticResponse;
import ru.soune.nocopy.repository.ComplaintEntityRepository;
import ru.soune.nocopy.repository.LawCaseRepository;
import ru.soune.nocopy.repository.ViolationRepository;

@Service
@RequiredArgsConstructor
@Slf4j
public class ViolationStatisticService {
    private final ViolationRepository violationRepository;

    private final ComplaintEntityRepository complaintRepository;

    private final LawCaseRepository lawCaseRepository;

    public ViolationStatisticResponse getViolationStatistics() {
        Long totalViolations = violationRepository.getTotalViolationsCount();
        Long totalComplaints = complaintRepository.getTotalComplaintsCount();
        Long totalLawCases = lawCaseRepository.getTotalLawCasesCount();
        Long complaintsWithoutCase = complaintRepository.getComplaintsWithoutLawCaseCount();
        Long violationsWithComplaint = violationRepository.getViolationsWithComplaintCount();

        Long violationsWithoutComplaint = totalViolations - violationsWithComplaint;

        return ViolationStatisticResponse.builder()
                .totalViolations(totalViolations != null ? totalViolations : 0L)
                .totalComplaints(totalComplaints != null ? totalComplaints : 0L)
                .totalLawCases(totalLawCases != null ? totalLawCases : 0L)
                .complaintsWithoutCase(complaintsWithoutCase != null ? complaintsWithoutCase : 0L)
                .violationsWithComplaint(violationsWithComplaint != null ? violationsWithComplaint : 0L)
                .violationsWithoutComplaint(violationsWithoutComplaint)
                .build();
    }
}
