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

import de.effigenix.domain.inventory.*;
import de.effigenix.domain.masterdata.ArticleId;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.UnitOfMeasure;
import de.effigenix.infrastructure.inventory.persistence.entity.StockMovementEntity;
import org.springframework.stereotype.Component;

@Component
public class StockMovementMapper {

    public StockMovementEntity toEntity(StockMovement movement) {
        return new StockMovementEntity(
                movement.id().value(),
                movement.stockId().value(),
                movement.articleId().value(),
                movement.stockBatchId().value(),
                movement.batchReference().batchId(),
                movement.batchReference().batchType().name(),
                movement.movementType().name(),
                movement.direction().name(),
                movement.quantity().amount(),
                movement.quantity().uom().name(),
                movement.reason(),
                movement.referenceDocumentId(),
                movement.performedBy(),
                movement.performedAt()
        );
    }

    public StockMovement toDomain(StockMovementEntity entity) {
        return StockMovement.reconstitute(
                StockMovementId.of(entity.getId()),
                StockId.of(entity.getStockId()),
                ArticleId.of(entity.getArticleId()),
                StockBatchId.of(entity.getStockBatchId()),
                new BatchReference(entity.getBatchId(), BatchType.valueOf(entity.getBatchType())),
                MovementType.valueOf(entity.getMovementType()),
                MovementDirection.valueOf(entity.getDirection()),
                Quantity.reconstitute(
                        entity.getQuantityAmount(),
                        UnitOfMeasure.valueOf(entity.getQuantityUnit())
                ),
                entity.getReason(),
                entity.getReferenceDocumentId(),
                entity.getPerformedBy(),
                entity.getPerformedAt()
        );
    }
}
