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

import de.effigenix.domain.production.*;
import de.effigenix.infrastructure.production.persistence.entity.ProductionOrderEntity;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.UnitOfMeasure;
import org.springframework.stereotype.Component;

@Component
public class ProductionOrderMapper {

    public ProductionOrderEntity toEntity(ProductionOrder order) {
        return new ProductionOrderEntity(
                order.id().value(),
                order.recipeId().value(),
                order.status().name(),
                order.plannedQuantity().amount(),
                order.plannedQuantity().uom().name(),
                order.plannedDate(),
                order.priority().name(),
                order.notes(),
                order.createdAt(),
                order.updatedAt()
        );
    }

    public void updateEntity(ProductionOrderEntity entity, ProductionOrder order) {
        entity.setStatus(order.status().name());
        entity.setPriority(order.priority().name());
        entity.setNotes(order.notes());
        entity.setUpdatedAt(order.updatedAt());
    }

    public ProductionOrder toDomain(ProductionOrderEntity entity) {
        return ProductionOrder.reconstitute(
                ProductionOrderId.of(entity.getId()),
                RecipeId.of(entity.getRecipeId()),
                ProductionOrderStatus.valueOf(entity.getStatus()),
                Quantity.reconstitute(
                        entity.getPlannedQuantityAmount(),
                        UnitOfMeasure.valueOf(entity.getPlannedQuantityUnit())
                ),
                entity.getPlannedDate(),
                Priority.valueOf(entity.getPriority()),
                entity.getNotes(),
                entity.getCreatedAt(),
                entity.getUpdatedAt(),
                entity.getVersion()
        );
    }
}
