package mappers

import (
	"apskel-pos-be/internal/entities"
	"apskel-pos-be/internal/models"
)

func MapIngredientEntityToModel(entity *entities.Ingredient) *models.Ingredient {
	if entity == nil {
		return nil
	}

	return &models.Ingredient{
		ID:             entity.ID,
		OrganizationID: entity.OrganizationID,
		OutletID:       entity.OutletID,
		Name:           entity.Name,
		UnitID:         entity.UnitID,
		Cost:           entity.Cost,
		Stock:          entity.Stock,
		IsSemiFinished: entity.IsSemiFinished,
		IsActive:       entity.IsActive,
		Metadata:       entity.Metadata,
		CreatedAt:      entity.CreatedAt,
		UpdatedAt:      entity.UpdatedAt,
		Unit:           MapUnitEntityToModel(entity.Unit),
	}
}

func MapIngredientModelToEntity(model *models.Ingredient) *entities.Ingredient {
	if model == nil {
		return nil
	}

	return &entities.Ingredient{
		ID:             model.ID,
		OrganizationID: model.OrganizationID,
		OutletID:       model.OutletID,
		Name:           model.Name,
		UnitID:         model.UnitID,
		Cost:           model.Cost,
		Stock:          model.Stock,
		IsSemiFinished: model.IsSemiFinished,
		IsActive:       model.IsActive,
		Metadata:       model.Metadata,
		CreatedAt:      model.CreatedAt,
		UpdatedAt:      model.UpdatedAt,
		Unit:           MapUnitModelToEntity(model.Unit),
	}
}

func MapIngredientEntitiesToModels(entities []*entities.Ingredient) []*models.Ingredient {
	if entities == nil {
		return nil
	}

	models := make([]*models.Ingredient, len(entities))
	for i, entity := range entities {
		models[i] = MapIngredientEntityToModel(entity)
	}

	return models
}

func MapIngredientModelsToEntities(models []*models.Ingredient) []*entities.Ingredient {
	if models == nil {
		return nil
	}

	entities := make([]*entities.Ingredient, len(models))
	for i, model := range models {
		entities[i] = MapIngredientModelToEntity(model)
	}

	return entities
}

// Entity to Response conversions
func MapIngredientEntityToResponse(entity *entities.Ingredient) *models.IngredientResponse {
	if entity == nil {
		return nil
	}

	return &models.IngredientResponse{
		ID:             entity.ID,
		OrganizationID: entity.OrganizationID,
		OutletID:       entity.OutletID,
		Name:           entity.Name,
		UnitID:         entity.UnitID,
		Cost:           entity.Cost,
		Stock:          entity.Stock,
		IsSemiFinished: entity.IsSemiFinished,
		IsActive:       entity.IsActive,
		Metadata:       entity.Metadata,
		CreatedAt:      entity.CreatedAt,
		UpdatedAt:      entity.UpdatedAt,
		Unit:           MapUnitEntityToModel(entity.Unit),
	}
}
