package user_test

import (
	"context"
	"reflect"
	"testing"

	"hyapp/pkg/appcode"
	userdomain "hyapp/services/user-service/internal/domain/user"
	userservice "hyapp/services/user-service/internal/service/user"
	"hyapp/services/user-service/internal/testutil/mysqltest"
)

func TestListUserIDsAllRegisteredIncludesNonActiveUsers(t *testing.T) {
	ctx := appcode.WithContext(context.Background(), "lalu")
	repository := mysqltest.NewRepository(t)
	for _, user := range []userdomain.User{
		{AppCode: "lalu", UserID: 1001, DefaultDisplayUserID: "1001", CurrentDisplayUserID: "1001", Status: userdomain.StatusActive, RegionID: 11, Country: "PH"},
		{AppCode: "lalu", UserID: 1002, DefaultDisplayUserID: "1002", CurrentDisplayUserID: "1002", Status: userdomain.StatusDisabled, RegionID: 11, Country: "PH"},
		{AppCode: "lalu", UserID: 1003, DefaultDisplayUserID: "1003", CurrentDisplayUserID: "1003", Status: userdomain.StatusBanned, RegionID: 11, Country: "PH"},
	} {
		repository.PutUser(user)
	}
	svc := userservice.New(repository)

	activeIDs, nextCursor, done, err := svc.ListUserIDs(ctx, userdomain.UserIDPageFilter{
		TargetScope: userdomain.UserIDTargetAllActiveUsers,
		PageSize:    10,
	})
	if err != nil {
		t.Fatalf("ListUserIDs active failed: %v", err)
	}
	if !reflect.DeepEqual(activeIDs, []int64{1001}) || nextCursor != 1001 || !done {
		t.Fatalf("active query mismatch: ids=%v next=%d done=%v", activeIDs, nextCursor, done)
	}

	regionIDs, nextCursor, done, err := svc.ListUserIDs(ctx, userdomain.UserIDPageFilter{
		TargetScope: userdomain.UserIDTargetRegion,
		RegionID:    11,
		PageSize:    10,
	})
	if err != nil {
		t.Fatalf("ListUserIDs region failed: %v", err)
	}
	if !reflect.DeepEqual(regionIDs, []int64{1001}) || nextCursor != 1001 || !done {
		t.Fatalf("region query mismatch: ids=%v next=%d done=%v", regionIDs, nextCursor, done)
	}

	countryIDs, nextCursor, done, err := svc.ListUserIDs(ctx, userdomain.UserIDPageFilter{
		TargetScope: userdomain.UserIDTargetCountry,
		Country:     "PH",
		PageSize:    10,
	})
	if err != nil {
		t.Fatalf("ListUserIDs country failed: %v", err)
	}
	if !reflect.DeepEqual(countryIDs, []int64{1001}) || nextCursor != 1001 || !done {
		t.Fatalf("country query mismatch: ids=%v next=%d done=%v", countryIDs, nextCursor, done)
	}

	registeredIDs, nextCursor, done, err := svc.ListUserIDs(ctx, userdomain.UserIDPageFilter{
		TargetScope: userdomain.UserIDTargetAllRegistered,
		PageSize:    2,
	})
	if err != nil {
		t.Fatalf("ListUserIDs registered first page failed: %v", err)
	}
	if !reflect.DeepEqual(registeredIDs, []int64{1001, 1002}) || nextCursor != 1002 || done {
		t.Fatalf("registered first page mismatch: ids=%v next=%d done=%v", registeredIDs, nextCursor, done)
	}

	registeredIDs, nextCursor, done, err = svc.ListUserIDs(ctx, userdomain.UserIDPageFilter{
		TargetScope:  userdomain.UserIDTargetAllRegistered,
		CursorUserID: nextCursor,
		PageSize:     2,
	})
	if err != nil {
		t.Fatalf("ListUserIDs registered second page failed: %v", err)
	}
	if !reflect.DeepEqual(registeredIDs, []int64{1003}) || nextCursor != 1003 || !done {
		t.Fatalf("registered second page mismatch: ids=%v next=%d done=%v", registeredIDs, nextCursor, done)
	}
}
