package de.avatic.lcc.service.transformer.premise;

import de.avatic.lcc.dto.calculation.DestinationDTO;
import de.avatic.lcc.model.db.premises.route.Destination;
import de.avatic.lcc.repositories.NodeRepository;
import de.avatic.lcc.repositories.premise.RouteRepository;
import de.avatic.lcc.service.transformer.generic.NodeTransformer;
import org.springframework.stereotype.Service;

@Service
public class DestinationTransformer {

    private final NodeRepository nodeRepository;
    private final NodeTransformer nodeTransformer;
    private final RouteRepository routeRepository;
    private final RouteTransformer routeTransformer;

    public DestinationTransformer(NodeRepository nodeRepository, NodeTransformer nodeTransformer, RouteRepository routeRepository, RouteTransformer routeTransformer) {
        this.nodeRepository = nodeRepository;
        this.nodeTransformer = nodeTransformer;
        this.routeRepository = routeRepository;
        this.routeTransformer = routeTransformer;
    }

    public DestinationDTO toDestinationDTO(Destination destination) {
        var dto = new DestinationDTO();

        dto.setId(destination.getId());
        dto.setRepackingCosts(destination.getRepackingCost());
        dto.setDisposalCosts(destination.getDisposalCost());
        dto.setHandlingCosts(destination.getHandlingCost());
        dto.setDestinationNode(nodeTransformer.toNodeDTO(nodeRepository.getById(destination.getDestinationNodeId()).orElseThrow()));
        dto.setRoutes(routeRepository.getByDestinationId(destination.getId()).stream().map(routeTransformer::toRouteDTO).toList());
        dto.setAnnualAmount(destination.getAnnualAmount());

        dto.setD2d(destination.getD2d());
        dto.setRateD2d(destination.getRateD2d());
        dto.setLeadTimeD2d(destination.getLeadTimeD2d());

        return dto;
    }

}
