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

import de.avatic.lcc.model.db.rates.ContainerRate;
import de.avatic.lcc.repositories.rates.ContainerRateRepository;
import de.avatic.lcc.repositories.rates.ValidityPeriodRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContainerRateImportService {

    private final ValidityPeriodRepository validityPeriodRepository;
    private final ContainerRateRepository containerRateRepository;

    public ContainerRateImportService(ValidityPeriodRepository validityPeriodRepository, ContainerRateRepository containerRateRepository) {
        this.validityPeriodRepository = validityPeriodRepository;
        this.containerRateRepository = containerRateRepository;
    }

    public void processContainerRates(List<ContainerRate> containerRates) {
        Integer periodId = validityPeriodRepository.getDraftPeriodId();
        containerRateRepository.clearDraft();
        containerRates.forEach(rate -> processContainerRate(rate,periodId));
    }

    public void processContainerRate(ContainerRate containerRate, Integer periodId) {
        containerRate.setValidityPeriodId(periodId);
        containerRateRepository.insert(containerRate);
    }
}
