package spindle

import (
	"context"

	"github.com/bluesky-social/indigo/atproto/syntax"
)

func (s *Spindle) AllowMember(ctx context.Context, did syntax.DID) error {
	if err := s.db.UpsertMember(ctx, did, false); err != nil {
		return err
	}
	s.jc.AddDid(did.String())
	// tap is best-effort: jetstream carries sh.tangled.repo too, and onConnect re-declares
	if err := s.tap.tap.AddRepos(ctx, []syntax.DID{did}); err != nil {
		s.l.Warn("tap: failed to add member did", "did", did, "err", err)
	}
	return nil
}

func (s *Spindle) BlockMember(ctx context.Context, did syntax.DID) error {
	if err := s.db.UpsertMember(ctx, did, true); err != nil {
		return err
	}
	s.jc.RemoveDid(did.String())
	if err := s.tap.tap.RemoveRepos(ctx, []syntax.DID{did}); err != nil {
		s.l.Warn("tap: failed to remove member did", "did", did, "err", err)
	}
	return nil
}
