package ai

import (
	"context"

	libprompt "haixun-backend/internal/library/prompt"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type ChatLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatLogic {
	return &ChatLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *ChatLogic) Chat(req *types.AIChatReq, token string) (*types.AIChatData, error) {
	system, err := libprompt.AIChatSystem(req.System)
	if err != nil {
		return nil, err
	}
	result, err := l.svcCtx.AI.GenerateText(l.ctx, toGenerateRequest(req, token, system))
	if err != nil {
		return nil, err
	}

	return &types.AIChatData{Text: result.Text, FinishReason: result.FinishReason}, nil
}
