package ru.nbch.credit_tracker.service.signal;

import org.springframework.stereotype.Service;
import ru.nbch.credit_tracker.entities.dto.UserPackage;
import ru.nbch.credit_tracker.enums.Status;
import ru.nbch.credit_tracker.mapper.signal.UserPackageMapper;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class UserPackageService {

    private final UserPackageMapper mapper;

    public UserPackageService(UserPackageMapper mapper) {
        this.mapper = mapper;
    }

    public boolean isPackageNotUnique(String userId, String packageName) {
        return mapper.isPackageNotUnique(userId.substring(0, 6), packageName);
    }

    public boolean isPackageExists(String userId, String packageName, Status status) {
        return mapper.isPackageExists(userId.substring(0, 6), packageName, status.name());
    }

    public boolean hasActivePackages(String userId, String packageName, Status... status) {
        return mapper.hasActivePackages(userId.substring(0, 6), packageName,  Arrays.stream(status).map(Enum::name).toList());
    }

    public void deletePackage(String userId, String packageName, Status newStatus, Status previousStatus, LocalDateTime deactivatedAt) {
        mapper.deletePackage(userId.substring(0, 6), packageName, newStatus.name(), previousStatus.name(), deactivatedAt);
    }

    public Integer insertPackage(UserPackage userPackage) {
        return mapper.insertPackage(userPackage);
    }

    public void updatePackageStatus(Integer packageId, Status status) {
        mapper.updatePackageStatus(packageId, status.name());
    }

    public void updatePackageLastDownloadedReport(Integer id, String fileName) {
        mapper.updatePackageLastDownloadedReport(id, fileName);
    }

    public UserPackage findPackage(Integer id) {
        return mapper.findPackage(id);
    }

    public List<UserPackage> findUserPackages(String userId, Status... status) {
        return mapper.findUserPackages(userId.substring(0, 6), Arrays.stream(status).map(Enum::name).toList());
    }

    public List<UserPackage> findAllPackagesByUserId(String userId) {
        return mapper.findAllPackagesByUserId(userId.substring(0, 6));
    }

    public List<UserPackage> findAllPackagesByStatus(Status status) {
        return mapper.findAllPackagesByStatus(status.name());
    }
}
