package de.avatic.lcc.service.bulk.bulkImport;

import de.avatic.lcc.model.db.rates.MatrixRate;
import de.avatic.lcc.repositories.rates.MatrixRateRepository;
import de.avatic.lcc.repositories.rates.ValidityPeriodRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MatrixRateImportService {

    private final MatrixRateRepository matrixRateRepository;
    private final ValidityPeriodRepository validityPeriodRepository;

    public MatrixRateImportService(MatrixRateRepository matrixRateRepository, ValidityPeriodRepository validityPeriodRepository) {
        this.matrixRateRepository = matrixRateRepository;
        this.validityPeriodRepository = validityPeriodRepository;
    }

    public void processMatrixRate(MatrixRate rate, Integer periodId) {
        rate.setValidityPeriodId(periodId);
        matrixRateRepository.insert(rate);
    }

    public void processMatrixRates(List<MatrixRate> matrixRates) {
        Integer periodId = validityPeriodRepository.getDraftPeriodId();
        matrixRates.forEach(rate -> processMatrixRate(rate,periodId));
    }
}
