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

import de.avatic.lcc.dto.configuration.material.view.MaterialDetailDTO;
import de.avatic.lcc.dto.configuration.material.view.MaterialDetailPackagingDTO;
import de.avatic.lcc.model.db.materials.Material;
import de.avatic.lcc.repositories.packaging.PackagingRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Transforms {@link Material} entities into {@link MaterialDetailDTO} objects.
 * This is used to map material entity data to a DTO for viewing material details.
 */
@Service
public class MaterialDetailTransformer {

    private final PackagingRepository packagingRepository;
    private final MaterialDetailPackagingTransformer materialDetailPackagingTransformer;

    public MaterialDetailTransformer(PackagingRepository packagingRepository, MaterialDetailPackagingTransformer materialDetailPackagingTransformer) {
        this.packagingRepository = packagingRepository;
        this.materialDetailPackagingTransformer = materialDetailPackagingTransformer;
    }

    /**
     * Converts a {@link Material} entity into a {@link MaterialDetailDTO}.
     *
     * @param entity the material entity to convert
     * @return a {@link MaterialDetailDTO} containing detailed information about the material
     */
    public MaterialDetailDTO toMaterialDetailDTO(Material entity) {
        MaterialDetailDTO dto = new MaterialDetailDTO();
        List<MaterialDetailPackagingDTO> packaging = packagingRepository.getByMaterialId(entity.getId()).stream().map(materialDetailPackagingTransformer::toMaterialViewPackagingDTO).toList();

        dto.setId(entity.getId());
        dto.setPartNumber(entity.getPartNumber());
        dto.setNormalizedPartNumber(entity.getNormalizedPartNumber());
        dto.setName(entity.getName());
        dto.setHsCode(entity.getHsCode());
        dto.setHandlingUnits(packaging);
        dto.setDeprecated(entity.getDeprecated());

        return dto;


    }

}
