package threads_account

import (
	"context"

	"haixun-backend/internal/model/ai/domain/enum"
	aiuc "haixun-backend/internal/model/ai/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *ListThreadsAccountAiProviderModelsLogic) ListThreadsAccountAiProviderModels(
	req *types.ThreadsAccountAiProviderModelsHandlerReq,
) (*types.ThreadsAccountAiProviderModelsData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	apiKey, err := l.svcCtx.ThreadsAccount.ResolveAiProviderAPIKey(
		l.ctx,
		tenantID,
		uid,
		req.ID,
		req.Provider,
		req.ApiKey,
	)
	if err != nil {
		return nil, err
	}
	result := l.svcCtx.AI.ListProviderModels(
		l.ctx,
		enum.ProviderID(req.Provider),
		aiuc.Credential{APIKey: apiKey},
	)
	return &types.ThreadsAccountAiProviderModelsData{
		ID:      result.ID,
		Label:   result.Label,
		Models:  result.Models,
		Streams: result.Streams,
		Error:   result.Error,
	}, nil
}
