package mongo

import (
	"context"
	"gitea.pena/PenaSide/heruvym/internal/model"
	"gitea.pena/PenaSide/hlog"
	"github.com/stretchr/testify/suite"
	"go.uber.org/zap"
	"testing"
)

var mongoUri = "mongodb://localmongo1:30001,localmongo2:30002,localmongo3:30003/?replicaSet=my-rs"

type AccountTestSuite struct {
	args *model.Account
	dal  *DAL
	suite.Suite
}

func TestAccountTestSuite(t *testing.T) {
	suite.Run(t, new(AccountTestSuite))
}

func (s *AccountTestSuite) SetupSuite() {
	s.args = &model.Account{
		UserID:   "fhka41hakfh41z",
		Nickname: "SomeNick",
		Role:     "manager",
	}

	logger := hlog.New(zap.NewNop())
	dal, err := New(context.Background(), mongoUri, "support", logger)

	if s.NoError(err) {
		s.dal = dal
		result, err := s.dal.InsertAccount(context.Background(), s.args)
		if s.NoError(err) {
			s.args = result
		}
	}
}

func (s *AccountTestSuite) TestGet() {
	r, err := s.dal.GetAccount(context.Background(), s.args.ID)
	s.NoError(err)
	s.NotNil(r)
}

func (s *AccountTestSuite) TestGetByUserID() {
	r, err := s.dal.GetAccountByUserID(context.Background(), s.args.UserID)
	s.NoError(err)
	s.NotNil(r)
}

func (s *AccountTestSuite) TestGetAccountPage() {
	r, err := s.dal.GetAccountPage(context.Background(), "", 0, 10)
	s.NoError(err)
	s.NotNil(r)
}

func (s *AccountTestSuite) TestSetAccountRole() {
	arg := "admin"
	r, err := s.dal.SetAccountRole(context.Background(), s.args.UserID, arg)
	s.NoError(err)
	if s.NotNil(r) {
		s.Equal(arg, r.Role)
	}
}

func (s *AccountTestSuite) TestSetAccountNickname() {
	arg := "NewNickname"
	r, err := s.dal.SetAccountNickname(context.Background(), s.args.UserID, arg)
	s.NoError(err)
	if s.NotNil(r) {
		s.Equal(arg, r.Nickname)
	}
}

func (s *AccountTestSuite) TestSetAccountAvatar() {
	arg := "/new/avatar.jpeg"
	r, err := s.dal.SetAccountAvatar(context.Background(), s.args.UserID, arg)
	s.NoError(err)
	if s.NotNil(r) {
		s.Equal(arg, r.Avatar)
	}

}

func (s *AccountTestSuite) TestSetAccountDelete() {
	arg := true
	r, err := s.dal.SetAccountDelete(context.Background(), s.args.UserID, arg)
	s.NoError(err)
	if s.NotNil(r) {
		s.Equal(arg, r.IsDeleted)
	}
}

func (s *AccountTestSuite) TearDownSuite() {
	r, err := s.dal.DeleteAccount(context.Background(), s.args.UserID)
	s.NoError(err)
	s.NotNil(r)
}
