// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package threads_account

import (
	"context"

	domusecase "haixun-backend/internal/model/threads_account/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

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

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

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

func (l *GetThreadsAccountAiSettingsLogic) GetThreadsAccountAiSettings(req *types.ThreadsAccountPath) (resp *types.ThreadsAccountAiSettingsData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	data, err := l.svcCtx.ThreadsAccount.GetAiSettings(l.ctx, tenantID, uid, req.ID)
	if err != nil {
		return nil, err
	}
	return toAiSettingsData(data), nil
}

func toAiSettingsData(data *domusecase.AiSettings) *types.ThreadsAccountAiSettingsData {
	if data == nil {
		return nil
	}
	configured := map[string]interface{}{}
	for provider, ok := range data.ApiKeysConfigured {
		configured[provider] = ok
	}
	return &types.ThreadsAccountAiSettingsData{
		AccountID:         data.AccountID,
		Provider:          data.Provider,
		Model:             data.Model,
		ResearchProvider:  data.ResearchProvider,
		ResearchModel:     data.ResearchModel,
		ApiKeys:           data.ApiKeys,
		ApiKeysConfigured: configured,
	}
}
