package ru.soune.nocopy.service.complaint;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import ru.soune.nocopy.entity.complaint.LawCase;
import ru.soune.nocopy.entity.complaint.LawCasePriority;
import ru.soune.nocopy.entity.complaint.LawCaseType;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.repository.LawCaseRepository;

import java.math.BigDecimal;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class LawCaseService {

    private final LawCaseRepository lawCaseRepository;

    public LawCase addLawCase(BigDecimal amount, String description, String name,
                               LawCasePriority priority, Long violationId, User user) {
        LawCase lawCase = new LawCase();

        BigDecimal damage = amount == null ? BigDecimal.ZERO : amount;

        lawCase.setAmount(damage);
        lawCase.setName(name);
        lawCase.setDescription(description);
        lawCase.setPriority(priority);
        lawCase.setViolationId(violationId);
        lawCase.setUser(user);

        return lawCaseRepository.save(lawCase);
    }

    public LawCase getById(Long id) {
        return lawCaseRepository.findById(id).orElse(null);
    }

    public LawCase changeStatus(Long id, LawCaseType type) {
        LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
        if (lawCase == null) {
            return null;
        }

        lawCase.setType(type);
        return lawCaseRepository.save(lawCase);
    }

    public LawCase changePriority(Long id, LawCasePriority priority) {
        LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
        if (lawCase == null) {
            return null;
        }

        lawCase.setPriority(priority);
        return lawCaseRepository.save(lawCase);
    }

    public LawCase changeDamage(Long id, BigDecimal damage) {
        LawCase lawCase = lawCaseRepository.findById(id).orElse(null);
        if (lawCase == null) {
            return null;
        }
        lawCase.setAmount(damage);
        return lawCaseRepository.save(lawCase);
    }

    public LawCase fetchByViolationId(Long violationId) {
        Optional<LawCase> lawCaseByViolationIdAndType =
                lawCaseRepository.getLawCaseByViolationIdAndType(violationId, LawCaseType.ACTIVE);

        return lawCaseByViolationIdAndType.orElse(null);
    }

    public Page<LawCase> getAllUserLawCases(Long userId, int pageSize, int pageNumber,
                                            LawCaseType lawCaseType, String lawyer,
                                            LawCasePriority lawCasePriority,
                                            Long violationId,
                                            String propertySort, String ascDesc) {
        Sort.Direction direction = ascDesc.equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC;
        Sort sort = Sort.by(direction, propertySort);
        Pageable pageable = PageRequest.of(pageNumber, pageSize, sort);

        return lawCaseRepository.getUserLawCases(userId, lawCaseType, lawyer, lawCasePriority, violationId, pageable);
    }

    public void deleteById(Long id) {
        lawCaseRepository.deleteById(id);
    }

}

