package auth

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *ListIdentitiesLogic) ListIdentities() (resp *types.AuthIdentitiesData, err error) {
	uid, _ := middleware.UIDFrom(l.ctx)
	ids, err := l.svcCtx.Auth.ListIdentities(l.ctx, uid)
	if err != nil {
		return nil, err
	}
	list := make([]types.IdentityPublic, 0, len(ids))
	for _, id := range ids {
		list = append(list, types.IdentityFromModel(id))
	}
	return &types.AuthIdentitiesData{List: list}, nil
}
