package de.effigenix.infrastructure.inventory.persistence.mapper;

import de.effigenix.domain.inventory.*;
import de.effigenix.infrastructure.inventory.persistence.entity.StorageLocationEntity;
import org.springframework.stereotype.Component;

@Component
public class StorageLocationMapper {

    public StorageLocationEntity toEntity(StorageLocation location) {
        var entity = new StorageLocationEntity();
        entity.setId(location.id().value());
        entity.setName(location.name().value());
        entity.setStorageType(location.storageType().name());
        entity.setActive(location.active());

        var range = location.temperatureRange();
        if (range != null) {
            entity.setMinTemperature(range.minTemperature());
            entity.setMaxTemperature(range.maxTemperature());
        }

        return entity;
    }

    public StorageLocation toDomain(StorageLocationEntity entity) {
        TemperatureRange temperatureRange = null;
        if (entity.getMinTemperature() != null && entity.getMaxTemperature() != null) {
            temperatureRange = new TemperatureRange(entity.getMinTemperature(), entity.getMaxTemperature());
        }

        return StorageLocation.reconstitute(
                StorageLocationId.of(entity.getId()),
                new StorageLocationName(entity.getName()),
                StorageType.valueOf(entity.getStorageType()),
                temperatureRange,
                entity.isActive()
        );
    }
}
