package api

import (
	"context"

	pb "gitlab.ost.ch/marketminds/marketminds/pkg/protobuf"
	"google.golang.org/grpc"
)

type SentimentResponse struct {
	PositiveScore float32
	NegativeScore float32
	NeutralScore  float32
}

func NewSentimentResponse(positiveScore, negativeScore, neutralScore float32) *SentimentResponse {
	return &SentimentResponse{
		PositiveScore: positiveScore,
		NegativeScore: negativeScore,
		NeutralScore:  neutralScore,
	}
}

type AnalyzeSentiment struct {
	Client pb.SentimentAnalyzerClient
}

func NewAnalyzeSentiment(conn *grpc.ClientConn) *AnalyzeSentiment {
	client := pb.NewSentimentAnalyzerClient(conn)
	return &AnalyzeSentiment{Client: client}
}

func (as *AnalyzeSentiment) Analyze(text string) (*SentimentResponse, error) {
	request := &pb.AnalyzeSentimentRequest{
		Text: text,
	}

	response, err := as.Client.AnalyzeSentiment(context.Background(), request)
	if err != nil {
		return nil, err
	}

	return NewSentimentResponse(response.PositiveScore, response.NegativeScore, response.NeutralScore), nil
}
