package ru.kayashov.bar.mapper;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import ru.kayashov.bar.controller.dto.ingredient.IngredientResponseDto;
import ru.kayashov.bar.controller.dto.ingredient.IngredientSimpleResponseDto;
import ru.kayashov.bar.model.entity.IngredientEntity;
import ru.kayashov.bar.service.VisitorService;

import java.util.List;

@Component
@RequiredArgsConstructor
public class IngredientMapper {

    private final VisitorService visitorService;

    public List<IngredientResponseDto> mapIngredientsToDtoList(List<IngredientEntity> ingredients) {
        List<Long> allowedIngredients = visitorService.getAllowedIngredients();
        return ingredients.stream()
                .map(i -> mapIngredientToDto(i, allowedIngredients))
                .toList();
    }

    private IngredientResponseDto mapIngredientToDto(IngredientEntity i, List<Long> allowedIngredients) {
        return IngredientResponseDto
                .builder()
                .id(i.getId())
                .name(i.getName())
                .alcohol(i.getAlcohol())
                .type(i.getType() != null ? i.getType().getName() : null)
                .abv(i.getAbv())
                .isHave(allowedIngredients.contains(i.getId()))
                .image("https://thecocktaildb.com/images/ingredients/" + i.getEnName() + "-Medium.png")
                .description(i.getDescription())
                .build();
    }

    public List<IngredientSimpleResponseDto> mapIngredientsToSimpleDtoList(List<IngredientEntity> ingredients) {
        List<Long> allowedIngredients = visitorService.getAllowedIngredients();
        return ingredients.stream()
                .map(i -> mapIngredientToSimpleDto(i, allowedIngredients))
                .toList();
    }

    private IngredientSimpleResponseDto mapIngredientToSimpleDto(IngredientEntity i, List<Long> allowedIngredients) {
        return new IngredientSimpleResponseDto(i.getId(), i.getName(), allowedIngredients.contains(i.getId()));
    }
}
