package proxy

import (
	"context"
	"testing"

	"github.com/bytedance/mockey"
	"github.com/stretchr/testify/assert"
	"go.opentelemetry.io/otel/trace"

	"github.com/milvus-io/milvus-proto/go-api/v3/commonpb"
	"github.com/milvus-io/milvus-proto/go-api/v3/milvuspb"
	"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
	"github.com/milvus-io/milvus/internal/parser/planparserv2"
	"github.com/milvus-io/milvus/internal/util/segcore"
	"github.com/milvus-io/milvus/pkg/v3/proto/internalpb"
	"github.com/milvus-io/milvus/pkg/v3/proto/planpb"
	"github.com/milvus-io/milvus/pkg/v3/util/merr"
)

func TestSearchTask_PlanNamespace_AfterPreExecute(t *testing.T) {
	mockey.PatchConvey("TestSearchTask_PlanNamespace_AfterPreExecute", t, func() {
		// Setup global meta cache and common mocks
		globalMetaCache = &MetaCache{}
		mockey.Mock((*MetaCache).GetCollectionID).Return(int64(1001), nil).Build()
		mockey.Mock((*MetaCache).GetCollectionInfo).Return(&collectionInfo{updateTimestamp: 12345, consistencyLevel: commonpb.ConsistencyLevel_Strong}, nil).Build()
		mockey.Mock(isPartitionKeyMode).Return(false, nil).Build()
		mockey.Mock(isIgnoreGrowing).Return(false, nil).Build()

		// Schema with namespace enabled and a vector field
		mockey.Mock((*MetaCache).GetCollectionSchema).To(func(_ *MetaCache, _ context.Context, _ string, _ string) (*schemaInfo, error) {
			schema := &schemapb.CollectionSchema{
				Name: "test_collection",
				Fields: []*schemapb.FieldSchema{
					{FieldID: 100, Name: "id", IsPrimaryKey: true, DataType: schemapb.DataType_Int64},
					{FieldID: 101, Name: "vec", DataType: schemapb.DataType_FloatVector, TypeParams: []*commonpb.KeyValuePair{{Key: "dim", Value: "4"}}},
				},
				EnableNamespace: true,
			}
			return mustNewSchemaInfo(schema), nil
		}).Build()

		// Patch checkNq to bypass placeholder parsing
		mockey.Mock((*searchTask).checkNq).Return(int64(1), nil).Build()

		// Capture plan to verify namespace by mocking tryGeneratePlan
		var capturedPlan *planpb.PlanNode
		mockey.Mock((*searchTask).tryGeneratePlan).To(func(_ *searchTask, _ []*commonpb.KeyValuePair, _ string, _ map[string]*schemapb.TemplateValue) (*planpb.PlanNode, *planpb.QueryInfo, int64, bool, []OrderByField, internalpb.SearchType, error) {
			capturedPlan = &planpb.PlanNode{}
			qi := &planpb.QueryInfo{Topk: 10, MetricType: "L2", QueryFieldId: 101, GroupByFieldId: -1}
			return capturedPlan, qi, 0, false, nil, internalpb.SearchType_DEFAULT, nil
		}).Build()

		// Build task
		task := &searchTask{
			Condition:     NewTaskCondition(context.Background()),
			SearchRequest: &internalpb.SearchRequest{Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_Search}},
			ctx:           context.Background(),
			request:       &milvuspb.SearchRequest{CollectionName: "test_collection"},
			result:        &milvuspb.SearchResults{Status: merr.Success()},
		}
		ns := "ns-1"
		task.request.Namespace = &ns

		err := task.PreExecute(context.Background())
		assert.NoError(t, err)
		assert.NotNil(t, capturedPlan)
		assert.NotNil(t, capturedPlan.Namespace)
		assert.Equal(t, *task.request.Namespace, *capturedPlan.Namespace)
	})
}

func TestSearchTask_NamespaceSetsPartitionIDs(t *testing.T) {
	mockey.PatchConvey("TestSearchTask_NamespaceSetsPartitionIDs", t, func() {
		globalMetaCache = &MetaCache{}

		partitionNames := []string{"_default_0", "_default_1"}
		partitionIDs := map[string]int64{"_default_0": 101, "_default_1": 102}
		namespaces := []string{"ns-0", "ns-1", "ns-2", "ns-3", "ns-4", "ns-5", "ns-6", "ns-7"}
		schema := namespaceEnabledSchema(
			&schemapb.FieldSchema{FieldID: 100, Name: "id", IsPrimaryKey: true, DataType: schemapb.DataType_Int64},
			&schemapb.FieldSchema{FieldID: 101, Name: "vec", DataType: schemapb.DataType_FloatVector, TypeParams: []*commonpb.KeyValuePair{{Key: "dim", Value: "4"}}},
			&schemapb.FieldSchema{FieldID: 102, Name: "value", DataType: schemapb.DataType_Int64},
		)

		mockey.Mock((*MetaCache).GetCollectionID).Return(int64(1001), nil).Build()
		mockey.Mock((*MetaCache).GetCollectionInfo).Return(&collectionInfo{updateTimestamp: 12345, consistencyLevel: commonpb.ConsistencyLevel_Strong}, nil).Build()
		mockey.Mock((*MetaCache).GetCollectionSchema).Return(mustNewSchemaInfo(schema), nil).Build()
		mockey.Mock((*MetaCache).GetPartitionsIndex).Return(partitionNames, nil).Build()
		mockey.Mock((*MetaCache).GetPartitions).Return(partitionIDs, nil).Build()
		mockey.Mock(isIgnoreGrowing).Return(false, nil).Build()
		mockey.Mock((*searchTask).checkNq).Return(int64(1), nil).Build()
		mockey.Mock((*searchTask).tryGeneratePlan).To(func(_ *searchTask, _ []*commonpb.KeyValuePair, _ string, _ map[string]*schemapb.TemplateValue) (*planpb.PlanNode, *planpb.QueryInfo, int64, bool, []OrderByField, internalpb.SearchType, error) {
			plan := &planpb.PlanNode{
				Node: &planpb.PlanNode_VectorAnns{
					VectorAnns: &planpb.VectorANNS{
						Predicates: nonPartitionKeyPredicate(102, schemapb.DataType_Int64),
					},
				},
			}
			qi := &planpb.QueryInfo{Topk: 10, MetricType: "L2", QueryFieldId: 101, GroupByFieldId: -1}
			return plan, qi, 0, false, nil, internalpb.SearchType_DEFAULT, nil
		}).Build()

		for _, ns := range namespaces {
			namespace := ns
			task := &searchTask{
				Condition:     NewTaskCondition(context.Background()),
				SearchRequest: &internalpb.SearchRequest{Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_Search}},
				ctx:           context.Background(),
				request:       &milvuspb.SearchRequest{CollectionName: "test_collection", Namespace: &namespace},
				result:        &milvuspb.SearchResults{Status: merr.Success()},
			}

			err := task.PreExecute(context.Background())
			assert.NoError(t, err)
			assert.Equal(t, []int64{expectedNamespacePartitionID(namespace, partitionNames, partitionIDs)}, task.GetPartitionIDs())
		}
	})
}

func TestSearchTask_RequeryPlanNamespace(t *testing.T) {
	mockey.PatchConvey("TestSearchTask_RequeryPlanNamespace", t, func() {
		// Minimal searchTask with schema and namespace
		schema := &schemapb.CollectionSchema{
			Name: "test_collection",
			Fields: []*schemapb.FieldSchema{
				{FieldID: 100, Name: "id", IsPrimaryKey: true, DataType: schemapb.DataType_Int64},
				{FieldID: 101, Name: "vec", DataType: schemapb.DataType_FloatVector, TypeParams: []*commonpb.KeyValuePair{{Key: "dim", Value: "4"}}},
			},
			EnableNamespace: true,
		}
		tsk := &searchTask{
			Condition:     NewTaskCondition(context.Background()),
			ctx:           context.Background(),
			schema:        mustNewSchemaInfo(schema),
			request:       &milvuspb.SearchRequest{CollectionName: "test_collection"},
			SearchRequest: &internalpb.SearchRequest{Base: &commonpb.MsgBase{MsgType: commonpb.MsgType_Search}},
			node:          &Proxy{},
		}
		ns := "ns-1"
		tsk.request.Namespace = &ns

		// Capture plan created in requery
		var capturedPlan *planpb.PlanNode
		mockey.Mock(planparserv2.CreateRequeryPlan).To(func(_ *schemapb.FieldSchema, _ *schemapb.IDs) *planpb.PlanNode {
			capturedPlan = &planpb.PlanNode{}
			return capturedPlan
		}).Build()

		// Capture qt.request as well to ensure request namespace is wired
		mockey.Mock((*Proxy).query).To(func(_ *Proxy, _ context.Context, qt *queryTask, _ trace.Span) (*milvuspb.QueryResults, segcore.StorageCost, error) {
			if qt.plan == nil || qt.plan.Namespace == nil || *qt.plan.Namespace != *tsk.request.Namespace {
				t.Fatalf("requery plan namespace mismatch, got=%v want=%v", qt.plan.Namespace, *tsk.request.Namespace)
			}
			return &milvuspb.QueryResults{Status: merr.Success()}, segcore.StorageCost{}, nil
		}).Build()

		op, err := newRequeryOperator(tsk, nil)
		assert.NoError(t, err)

		ids := &schemapb.IDs{IdField: &schemapb.IDs_IntId{IntId: &schemapb.LongArray{Data: []int64{1}}}}
		_, runErr := op.run(context.Background(), nil, ids, segcore.StorageCost{})
		assert.NoError(t, runErr)
		assert.NotNil(t, capturedPlan)
		assert.NotNil(t, capturedPlan.Namespace)
		assert.Equal(t, *tsk.request.Namespace, *capturedPlan.Namespace)
	})
}
