package kz.konturai.parser.controller;

import kz.konturai.parser.model.MarketItem;
import kz.konturai.parser.service.OpenAIAnalyticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/openai")
public class OpenAITestController {

    @Autowired
    private OpenAIAnalyticsService openAIAnalyticsService;

    @PostMapping("/analyze")
    public MarketItem.Analytics analyzeText(@RequestParam String text, 
                                          @RequestParam(defaultValue = "ru") String language) {
        return openAIAnalyticsService.analyzeText(text, language);
    }

    @PostMapping("/generate")
    public String generateWithInstruction(@RequestParam String text, 
                                        @RequestParam String instruction,
                                        @RequestParam(defaultValue = "ru") String language) {
        return openAIAnalyticsService.generateWithInstruction(text, instruction, language);
    }

    @GetMapping("/test")
    public String testConnection() {
        String testText = "Это тестовая новость о развитии технологий в Казахстане. Компания Kaspi Bank объявила о новых инвестициях в цифровые сервисы.";
        MarketItem.Analytics analytics = openAIAnalyticsService.analyzeText(testText);
        
        StringBuilder result = new StringBuilder();
        result.append("OpenAI Analytics Service Test:\n");
        result.append("Summary: ").append(analytics.getSummary()).append("\n");
        result.append("Sentiment: ").append(analytics.getSentiment()).append("\n");
        result.append("Tags: ").append(String.join(", ", analytics.getTags())).append("\n");
        result.append("Entities: ").append(analytics.getEntities()).append("\n");
        
        return result.toString();
    }
}
