package mappers

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

// Entity to Model conversions
func IngredientUnitConverterEntityToModel(entity *entities.IngredientUnitConverter) *models.IngredientUnitConverter {
	if entity == nil {
		return nil
	}

	model := &models.IngredientUnitConverter{
		ID:               entity.ID,
		OrganizationID:   entity.OrganizationID,
		IngredientID:     entity.IngredientID,
		FromUnitID:       entity.FromUnitID,
		ToUnitID:         entity.ToUnitID,
		ConversionFactor: entity.ConversionFactor,
		IsActive:         entity.IsActive,
		CreatedAt:        entity.CreatedAt,
		UpdatedAt:        entity.UpdatedAt,
		CreatedBy:        entity.CreatedBy,
		UpdatedBy:        entity.UpdatedBy,
	}

	// Map related entities
	if entity.Ingredient != nil {
		model.Ingredient = MapIngredientEntityToModel(entity.Ingredient)
	}
	if entity.FromUnit != nil {
		model.FromUnit = MapUnitEntityToModel(entity.FromUnit)
	}
	if entity.ToUnit != nil {
		model.ToUnit = MapUnitEntityToModel(entity.ToUnit)
	}

	return model
}

// Entity to Response conversions
func IngredientUnitConverterEntityToResponse(entity *entities.IngredientUnitConverter) *models.IngredientUnitConverterResponse {
	if entity == nil {
		return nil
	}

	response := &models.IngredientUnitConverterResponse{
		ID:               entity.ID,
		OrganizationID:   entity.OrganizationID,
		IngredientID:     entity.IngredientID,
		FromUnitID:       entity.FromUnitID,
		ToUnitID:         entity.ToUnitID,
		ConversionFactor: entity.ConversionFactor,
		IsActive:         entity.IsActive,
		CreatedAt:        entity.CreatedAt,
		UpdatedAt:        entity.UpdatedAt,
		CreatedBy:        entity.CreatedBy,
		UpdatedBy:        entity.UpdatedBy,
	}

	// Map related entities
	if entity.Ingredient != nil {
		response.Ingredient = MapIngredientEntityToResponse(entity.Ingredient)
	}
	if entity.FromUnit != nil {
		response.FromUnit = MapUnitEntityToResponse(entity.FromUnit)
	}
	if entity.ToUnit != nil {
		response.ToUnit = MapUnitEntityToResponse(entity.ToUnit)
	}

	return response
}

// Batch conversion methods
func IngredientUnitConverterEntitiesToModels(entities []*entities.IngredientUnitConverter) []*models.IngredientUnitConverter {
	models := make([]*models.IngredientUnitConverter, len(entities))
	for i, entity := range entities {
		models[i] = IngredientUnitConverterEntityToModel(entity)
	}
	return models
}

func IngredientUnitConverterEntitiesToResponses(entities []*entities.IngredientUnitConverter) []*models.IngredientUnitConverterResponse {
	responses := make([]*models.IngredientUnitConverterResponse, len(entities))
	for i, entity := range entities {
		responses[i] = IngredientUnitConverterEntityToResponse(entity)
	}
	return responses
}
