package logic

import (
	"context"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveAiChatHistoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSaveAiChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveAiChatHistoryLogic {
	return &SaveAiChatHistoryLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *SaveAiChatHistoryLogic) SaveAiChatHistory(in *plant.SaveAiChatHistoryReq) (*plant.CommonResp, error) {
	history := plantModel.AiChatHistory{
		UserID:   in.UserId,
		Question: in.Question,
		Answer:   in.Answer,
		Role:     "assistant",
		Content:  in.Answer,
	}
	if err := l.svcCtx.DB.Create(&history).Error; err != nil {
		return nil, err
	}
	return &plant.CommonResp{Code: 0, Msg: history.ID}, nil
}
