package pl.polskalokalnie.i18n;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/i18n")
public class TranslationController {

    private final RuntimeTranslationService runtimeTranslationService;

    public TranslationController(RuntimeTranslationService runtimeTranslationService) {
        this.runtimeTranslationService = runtimeTranslationService;
    }

    @PostMapping("/translate")
    public TranslateResponse translate(@Valid @RequestBody TranslateRequest request) {
        List<String> normalizedTexts = request.texts().stream()
                .map(text -> text == null ? "" : text)
                .distinct()
                .collect(Collectors.toList());
        Map<String, String> translations = runtimeTranslationService.translateBatch(request.language(), normalizedTexts);
        return new TranslateResponse(request.language(), translations);
    }
}
