package llm

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"strings"
	"time"
)

func (s *llmService) Send(systemPrompt string, userPrompt string) (string, error) {
	apiURL := getEnvConfig("OPENAI_API_URL")
	if apiURL == "" {
		return "", errors.New("OPENAI_API_URL not found in environment")
	}

	token := getEnvConfig("OPENAI_TOKEN")
	if token == "" {
		return "", errors.New("OPENAI_TOKEN not found in environment")
	}

	model := getEnvConfig("OPENAI_MODEL")
	if model == "" {
		return "", errors.New("OPENAI_MODEL not found in environment")
	}

	url := fmt.Sprintf("%s/chat/completions", strings.TrimRight(apiURL, "/"))

	reqBody := map[string]interface{}{
		"model": model,
		"messages": []map[string]string{
			{"role": "system", "content": systemPrompt},
			{"role": "user", "content": userPrompt},
		},
	}

	jsonBody, err := json.Marshal(reqBody)
	if err != nil {
		return "", err
	}

	var lastErr error
	// Retry up to 5 times (total 5 attempts)
	for i := 0; i < 5; i++ {
		req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
		if err != nil {
			return "", err
		}
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("Authorization", "Bearer "+token)

		client := &http.Client{Timeout: 120 * time.Second}
		resp, err := client.Do(req)

		if err != nil {
			lastErr = err
			time.Sleep(time.Second * time.Duration(1<<i))
			continue
		}

		bodyBytes, err := io.ReadAll(resp.Body)
		resp.Body.Close()
		if err != nil {
			lastErr = err
			time.Sleep(time.Second * time.Duration(1<<i))
			continue
		}

		if resp.StatusCode != http.StatusOK {
			lastErr = fmt.Errorf("API error status %d: %s", resp.StatusCode, string(bodyBytes))
			time.Sleep(time.Second * time.Duration(1<<i))
			continue
		}

		var result struct {
			Choices []struct {
				Message struct {
					Content string `json:"content"`
				} `json:"message"`
			} `json:"choices"`
		}
		if err := json.Unmarshal(bodyBytes, &result); err != nil {
			return "", err
		}

		if len(result.Choices) > 0 {
			return result.Choices[0].Message.Content, nil
		}
		return "", errors.New("empty response from API")
	}

	return "", fmt.Errorf("failed to get response after 5 attempts. Last error: %v", lastErr)
}
