package settings

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/module/member/domain"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *GetPlacementLogic) GetPlacement() (resp *types.PlacementSettingsData, err error) {
	uid, _ := middleware.UIDFrom(l.ctx)
	st, err := l.svcCtx.Members.GetSettings(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	if st == nil {
		st = domain.DefaultSettings(uid)
	}
	return &types.PlacementSettingsData{
		WebSearchProvider:     "exa",
		ExpandStrategy:        st.ExpandStrategy,
		BraveApiKeyConfigured: false,
		ExaApiKeyConfigured:   st.ExaAPIKeyConfigured,
		DevModeEnabled:        st.DevModeEnabled,
		PlatformKeyAvailable:  l.svcCtx.Config.Platform.ExaKey != "",
	}, nil
}
