package testing

import (
	"bytes"
	"context"
	"sort"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/influxdata/influxdb/v2"
	platform "github.com/influxdata/influxdb/v2"
	"github.com/influxdata/influxdb/v2/mock"
)

const (
	userOneID   = "020f755c3c082000"
	userTwoID   = "020f755c3c082001"
	userThreeID = "020f755c3c082002"
)

var userCmpOptions = cmp.Options{
	cmp.Comparer(func(x, y []byte) bool {
		return bytes.Equal(x, y)
	}),
	cmp.Comparer(func(x, y *influxdb.User) bool {
		if x == nil && y == nil {
			return true
		}
		if x != nil && y == nil || y != nil && x == nil {
			return false
		}
		return x.Name == y.Name && x.OAuthID == y.OAuthID && x.Status == y.Status
	}),
	cmp.Transformer("Sort", func(in []*platform.User) []*platform.User {
		out := append([]*platform.User(nil), in...) // Copy input to avoid mutating it
		sort.Slice(out, func(i, j int) bool {
			return out[i].ID.String() > out[j].ID.String()
		})
		return out
	}),
}

// UserFields will include the IDGenerator, and users
type UserFields struct {
	IDGenerator platform.IDGenerator
	Users       []*platform.User
}

// UserService tests all the service functions.
func UserService(
	init func(UserFields, *testing.T) (platform.UserService, string, func()), t *testing.T,
) {
	tests := []struct {
		name string
		fn   func(init func(UserFields, *testing.T) (platform.UserService, string, func()),
			t *testing.T)
	}{
		{
			name: "CreateUser",
			fn:   CreateUser,
		},
		{
			name: "FindUserByID",
			fn:   FindUserByID,
		},
		{
			name: "FindUsers",
			fn:   FindUsers,
		},
		{
			name: "DeleteUser",
			fn:   DeleteUser,
		},
		{
			name: "FindUser",
			fn:   FindUser,
		},
		{
			name: "UpdateUser",
			fn:   UpdateUser,
		},
		{
			name: "UpdateUser_IndexHygiene",
			fn:   UpdateUser_IndexHygiene,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.fn(init, t)
		})
	}
}

// CreateUser testing
func CreateUser(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		user *platform.User
	}
	type wants struct {
		err   error
		users []*platform.User
	}

	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "create users with empty set",
			fields: UserFields{
				IDGenerator: mock.NewIDGenerator(userOneID, t),
				Users:       []*platform.User{},
			},
			args: args{
				user: &platform.User{
					Name:   "name1",
					Status: platform.Active,
				},
			},
			wants: wants{
				users: []*platform.User{
					{
						Name:   "name1",
						ID:     MustIDBase16(userOneID),
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "basic create user",
			fields: UserFields{
				IDGenerator: mock.NewIDGenerator(userTwoID, t),
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
				},
			},
			args: args{
				user: &platform.User{
					Name:   "user2",
					Status: platform.Active,
				},
			},
			wants: wants{
				users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "names should be unique",
			fields: UserFields{
				IDGenerator: &mock.IDGenerator{
					IDFn: func() platform.ID {
						return MustIDBase16(userOneID)
					},
				},
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
				},
			},
			args: args{
				user: &platform.User{
					Name: "user1",
				},
			},
			wants: wants{
				users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
				},
				err: &platform.Error{
					Code: platform.EConflict,
					Op:   platform.OpCreateUser,
					Msg:  "user with name user1 already exists",
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()
			err := s.CreateUser(ctx, tt.args.user)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			// Delete only created users - ie., having a not nil ID
			if tt.args.user.ID.Valid() {
				defer s.DeleteUser(ctx, tt.args.user.ID)
			}

			users, _, err := s.FindUsers(ctx, platform.UserFilter{})
			if err != nil {
				t.Fatalf("failed to retrieve users: %v", err)
			}
			if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
				t.Errorf("users are different -got/+want\ndiff %s", diff)
			}
		})
	}
}

// FindUserByID testing
func FindUserByID(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		id platform.ID
	}
	type wants struct {
		err  error
		user *platform.User
	}

	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "basic find user by id",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
			args: args{
				id: MustIDBase16(userTwoID),
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userTwoID),
					Name:   "user2",
					Status: platform.Active,
				},
			},
		},
		{
			name: "find user by id not exists",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
			args: args{
				id: MustIDBase16(threeID),
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Op:   platform.OpFindUserByID,
					Msg:  "user not found",
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()

			user, err := s.FindUserByID(ctx, tt.args.id)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			if diff := cmp.Diff(user, tt.wants.user, userCmpOptions...); diff != "" {
				t.Errorf("user is different -got/+want\ndiff %s", diff)
			}
		})
	}
}

// FindUsers testing
func FindUsers(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		ID   platform.ID
		name string
	}

	type wants struct {
		users []*platform.User
		err   error
	}
	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "find all users",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{},
			wants: wants{
				users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "find user by id",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{
				ID: MustIDBase16(userTwoID),
			},
			wants: wants{
				users: []*platform.User{
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "find user by name",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{
				name: "xyz",
			},
			wants: wants{
				users: []*platform.User{
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "find user by id not exists",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:   MustIDBase16(userOneID),
						Name: "abc",
					},
					{
						ID:   MustIDBase16(userTwoID),
						Name: "xyz",
					},
				},
			},
			args: args{
				ID: MustIDBase16(threeID),
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Op:   platform.OpFindUsers,
					Msg:  "user not found",
				},
			},
		},
		{
			name: "find user by name not exists",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:   MustIDBase16(userOneID),
						Name: "abc",
					},
					{
						ID:   MustIDBase16(userTwoID),
						Name: "xyz",
					},
				},
			},
			args: args{
				name: "no_exist",
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Op:   platform.OpFindUsers,
					Msg:  "user not found",
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()

			filter := platform.UserFilter{}
			if tt.args.ID.Valid() {
				filter.ID = &tt.args.ID
			}
			if tt.args.name != "" {
				filter.Name = &tt.args.name
			}

			users, _, err := s.FindUsers(ctx, filter)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
				t.Errorf("users are different -got/+want\ndiff %s", diff)
			}
		})
	}
}

// DeleteUser testing
func DeleteUser(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		ID platform.ID
	}
	type wants struct {
		err   error
		users []*platform.User
	}

	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "delete users using exist id",
			fields: UserFields{
				Users: []*platform.User{
					{
						Name:   "orgA",
						ID:     MustIDBase16(userOneID),
						Status: platform.Active,
					},
					{
						Name:   "orgB",
						ID:     MustIDBase16(userTwoID),
						Status: platform.Active,
					},
				},
			},
			args: args{
				ID: MustIDBase16(userOneID),
			},
			wants: wants{
				users: []*platform.User{
					{
						Name:   "orgB",
						ID:     MustIDBase16(userTwoID),
						Status: platform.Active,
					},
				},
			},
		},
		{
			name: "delete users using id that does not exist",
			fields: UserFields{
				Users: []*platform.User{
					{
						Name:   "orgA",
						ID:     MustIDBase16(userOneID),
						Status: platform.Active,
					},
					{
						Name:   "orgB",
						ID:     MustIDBase16(userTwoID),
						Status: platform.Active,
					},
				},
			},
			args: args{
				ID: MustIDBase16(userThreeID),
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Op:   platform.OpDeleteUser,
					Msg:  "user not found",
				},
				users: []*platform.User{
					{
						Name:   "orgA",
						ID:     MustIDBase16(userOneID),
						Status: platform.Active,
					},
					{
						Name:   "orgB",
						ID:     MustIDBase16(userTwoID),
						Status: platform.Active,
					},
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()
			err := s.DeleteUser(ctx, tt.args.ID)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			filter := platform.UserFilter{}
			users, _, err := s.FindUsers(ctx, filter)
			if err != nil {
				t.Fatalf("failed to retrieve users: %v", err)
			}
			if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
				t.Errorf("users are different -got/+want\ndiff %s", diff)
			}
		})
	}
}

// FindUser testing
func FindUser(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		filter platform.UserFilter
	}

	type wants struct {
		user *platform.User
		err  error
	}

	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "find user by name",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{
				filter: platform.UserFilter{
					Name: func(s string) *string { return &s }("abc"),
				},
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "abc",
					Status: platform.Active,
				},
			},
		},
		{
			name: "find existing user by its id",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{
				filter: platform.UserFilter{
					ID: func(id platform.ID) *platform.ID { return &id }(MustIDBase16(userOneID)),
				},
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "abc",
					Status: platform.Active,
				},
			},
		},
		{
			name: "user with name does not exist",
			fields: UserFields{
				Users: []*platform.User{},
			},
			args: args{
				filter: platform.UserFilter{
					Name: func(s string) *string { return &s }("abc"),
				},
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Msg:  "user not found",
					Op:   platform.OpFindUser,
				},
			},
		},
		{
			name: "user with id does not exist",
			fields: UserFields{
				Users: []*platform.User{},
			},
			args: args{
				filter: platform.UserFilter{
					ID: func(id platform.ID) *platform.ID { return &id }(MustIDBase16(userOneID)),
				},
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Msg:  "user not found",
					Op:   platform.OpFindUser,
				},
			},
		},
		{
			name: "filter with both name and ID prefers ID",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "abc",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "xyz",
						Status: platform.Active,
					},
				},
			},
			args: args{
				filter: platform.UserFilter{
					ID:   func(id platform.ID) *platform.ID { return &id }(MustIDBase16(userOneID)),
					Name: func(s string) *string { return &s }("xyz"),
				},
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "abc",
					Status: platform.Active,
				},
			},
		},
		{
			name: "filter with no name nor id returns error",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:   MustIDBase16(userTwoID),
						Name: "xyz",
					},
				},
			},
			args: args{
				filter: platform.UserFilter{},
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Msg:  "user not found",
					Op:   platform.OpFindUser,
				},
			},
		},
		{
			name: "filter both name and non-existent id returns no user",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:   MustIDBase16(userTwoID),
						Name: "xyz",
					},
				},
			},
			args: args{
				filter: platform.UserFilter{
					ID:   func(id platform.ID) *platform.ID { return &id }(MustIDBase16(userOneID)),
					Name: func(s string) *string { return &s }("xyz"),
				},
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Msg:  "user not found",
					Op:   platform.OpFindUser,
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()
			user, err := s.FindUser(ctx, tt.args.filter)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			if diff := cmp.Diff(user, tt.wants.user, userCmpOptions...); diff != "" {
				t.Errorf("users are different -got/+want\ndiff %s", diff)
			}
		})
	}
}

// UpdateUser testing
func UpdateUser(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {
	type args struct {
		name   string
		id     platform.ID
		status string
	}
	type wants struct {
		err  error
		user *platform.User
	}

	tests := []struct {
		name   string
		fields UserFields
		args   args
		wants  wants
	}{
		{
			name: "update name",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
			args: args{
				id:   MustIDBase16(userOneID),
				name: "changed",
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "changed",
					Status: platform.Active,
				},
			},
		},
		{
			name: "update name to same name",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
			args: args{
				id:   MustIDBase16(userOneID),
				name: "user1",
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "user1",
					Status: platform.Active,
				},
			},
		},
		{
			name: "update status",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:     MustIDBase16(userOneID),
						Name:   "user1",
						Status: platform.Active,
					},
					{
						ID:     MustIDBase16(userTwoID),
						Name:   "user2",
						Status: platform.Active,
					},
				},
			},
			args: args{
				id:     MustIDBase16(userOneID),
				status: "inactive",
			},
			wants: wants{
				user: &platform.User{
					ID:     MustIDBase16(userOneID),
					Name:   "user1",
					Status: "inactive",
				},
			},
		},
		{
			name: "update name with id not exists",
			fields: UserFields{
				Users: []*platform.User{
					{
						ID:   MustIDBase16(userOneID),
						Name: "user1",
					},
					{
						ID:   MustIDBase16(userTwoID),
						Name: "user2",
					},
				},
			},
			args: args{
				id:   MustIDBase16(threeID),
				name: "changed",
			},
			wants: wants{
				err: &platform.Error{
					Code: platform.ENotFound,
					Op:   platform.OpUpdateUser,
					Msg:  "user not found",
				},
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s, opPrefix, done := init(tt.fields, t)
			defer done()
			ctx := context.Background()

			upd := platform.UserUpdate{}
			if tt.args.name != "" {
				upd.Name = &tt.args.name
			}

			switch tt.args.status {
			case "inactive":
				status := platform.Inactive
				upd.Status = &status
			case "active":
				status := platform.Inactive
				upd.Status = &status
			}

			user, err := s.UpdateUser(ctx, tt.args.id, upd)
			diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)

			if diff := cmp.Diff(user, tt.wants.user, userCmpOptions...); diff != "" {
				t.Errorf("user is different -got/+want\ndiff %s", diff)
			}
		})
	}
}

func UpdateUser_IndexHygiene(
	init func(UserFields, *testing.T) (platform.UserService, string, func()),
	t *testing.T,
) {

	oldUserName := "user1"
	users := UserFields{
		Users: []*platform.User{
			{
				ID:     MustIDBase16(userOneID),
				Name:   oldUserName,
				Status: "active",
			},
		},
	}
	s, _, done := init(users, t)
	defer done()

	newUserName := "user1Updated"
	upd := platform.UserUpdate{
		Name: &newUserName,
	}

	ctx := context.Background()
	_, err := s.UpdateUser(ctx, MustIDBase16(userOneID), upd)
	if err != nil {
		t.Error(err)
	}

	// Ensure we can find the user with the new name.
	_, nerr := s.FindUser(ctx, platform.UserFilter{
		Name: &newUserName,
	})
	if nerr != nil {
		t.Error("unexpected error when finding user by name", nerr)
	}

	// Ensure we cannot find a user with the old name. The index used when
	// searching by name should have been cleared out by the UpdateUser
	// operation.
	_, oerr := s.FindUser(ctx, platform.UserFilter{
		Name: &oldUserName,
	})
	ErrorsEqual(t, oerr, &platform.Error{
		Code: platform.ENotFound,
		Op:   platform.OpFindUser,
		Msg:  "user not found",
	})
}
