// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

// Package schema implements CRUD schema.
package schema

import (
	"context"
	"io"

	"github.com/pkg/errors"
	"google.golang.org/protobuf/proto"

	commonv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/common/v1"
	databasev1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
)

// SchemaGroup is the reserved group name for schema property storage.
const SchemaGroup = "_schema"

// ErrUnsupportedEntityType indicates an unsupported entity type.
var ErrUnsupportedEntityType = errors.New("unsupported entity type")

// HasMetadata allows getting Metadata.
type HasMetadata interface {
	GetMetadata() *commonv1.Metadata
	proto.Message
}

// EventHandler allows receiving and handling the resource change events.
type EventHandler interface {
	OnInit([]Kind) (bool, []int64)
	OnAddOrUpdate(Metadata)
	OnDelete(Metadata)
}

// DataInfoCollector provides methods to collect data node info.
type DataInfoCollector interface {
	CollectDataInfo(ctx context.Context, group string) (*databasev1.DataInfo, error)
}

// LiaisonInfoCollector provides methods to collect liaison node info.
type LiaisonInfoCollector interface {
	CollectLiaisonInfo(ctx context.Context, group string) (*databasev1.LiaisonInfo, error)
}

// UnimplementedOnInitHandler is a placeholder for unimplemented OnInitHandler.
type UnimplementedOnInitHandler struct{}

// OnInit is a placeholder for unimplemented OnInitHandler.
func (u UnimplementedOnInitHandler) OnInit([]Kind) (bool, []int64) {
	return false, nil
}

// ListOpt contains options to list resources.
type ListOpt struct {
	Group string
}

// Registry allowing depositing resources.
type Registry interface {
	io.Closer
	Stream
	IndexRule
	IndexRuleBinding
	Measure
	Trace
	Group
	TopNAggregation
	Property
	RegisterHandler(string, Kind, EventHandler)
	Start(context.Context) error
}

// NodeDiscovery provides node discovery and management capabilities.
type NodeDiscovery interface {
	io.Closer
	Node
	RegisterHandler(string, Kind, EventHandler)
	Start(context.Context) error
}

// TypeMeta defines the identity and type of an Event.
type TypeMeta struct {
	Name        string
	Group       string
	ModRevision int64
	Kind        Kind
}

// Metadata wrap dedicated serialized resource and its TypeMeta.
type Metadata struct {
	Spec Spec
	TypeMeta
}

// Spec is a placeholder of a serialized resource.
type Spec interface{}

// Stream allows CRUD stream schemas in a group.
//
//go:generate mockgen -destination=schema_stream_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema Stream
type Stream interface {
	GetStream(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.Stream, error)
	ListStream(ctx context.Context, opt ListOpt) ([]*databasev1.Stream, error)
	CreateStream(ctx context.Context, stream *databasev1.Stream) (int64, error)
	UpdateStream(ctx context.Context, stream *databasev1.Stream) (int64, error)
	DeleteStream(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}

// IndexRule allows CRUD index rule schemas in a group.
type IndexRule interface {
	GetIndexRule(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.IndexRule, error)
	ListIndexRule(ctx context.Context, opt ListOpt) ([]*databasev1.IndexRule, error)
	CreateIndexRule(ctx context.Context, indexRule *databasev1.IndexRule) (int64, error)
	UpdateIndexRule(ctx context.Context, indexRule *databasev1.IndexRule) (int64, error)
	DeleteIndexRule(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}

// IndexRuleBinding allows CRUD index rule binding schemas in a group.
type IndexRuleBinding interface {
	GetIndexRuleBinding(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.IndexRuleBinding, error)
	ListIndexRuleBinding(ctx context.Context, opt ListOpt) ([]*databasev1.IndexRuleBinding, error)
	CreateIndexRuleBinding(ctx context.Context, indexRuleBinding *databasev1.IndexRuleBinding) (int64, error)
	UpdateIndexRuleBinding(ctx context.Context, indexRuleBinding *databasev1.IndexRuleBinding) (int64, error)
	DeleteIndexRuleBinding(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}

// Measure allows CRUD measure schemas in a group.
//
//go:generate mockgen -destination=schema_measure_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema Measure
type Measure interface {
	GetMeasure(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.Measure, error)
	ListMeasure(ctx context.Context, opt ListOpt) ([]*databasev1.Measure, error)
	CreateMeasure(ctx context.Context, measure *databasev1.Measure) (int64, error)
	UpdateMeasure(ctx context.Context, measure *databasev1.Measure) (int64, error)
	DeleteMeasure(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
	TopNAggregations(ctx context.Context, metadata *commonv1.Metadata) ([]*databasev1.TopNAggregation, error)
}

// Trace allows CRUD trace schemas in a group.
//
//go:generate mockgen -destination=schema_trace_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema Trace
type Trace interface {
	GetTrace(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.Trace, error)
	ListTrace(ctx context.Context, opt ListOpt) ([]*databasev1.Trace, error)
	CreateTrace(ctx context.Context, trace *databasev1.Trace) (int64, error)
	UpdateTrace(ctx context.Context, trace *databasev1.Trace) (int64, error)
	DeleteTrace(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}

// Group allows CRUD groups which is namespaces of resources.
//
//go:generate mockgen -destination=group_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema Group
type Group interface {
	GetGroup(ctx context.Context, group string) (*commonv1.Group, error)
	ListGroup(ctx context.Context) ([]*commonv1.Group, error)
	// DeleteGroup delete all items belonging to the group
	DeleteGroup(ctx context.Context, group string) (bool, int64, error)
	CreateGroup(ctx context.Context, group *commonv1.Group) (int64, error)
	UpdateGroup(ctx context.Context, group *commonv1.Group) (int64, error)
}

// TopNAggregation allows CRUD top-n aggregation schemas in a group.
//
//go:generate mockgen -destination=schema_topn_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema TopNAggregation
type TopNAggregation interface {
	GetTopNAggregation(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.TopNAggregation, error)
	ListTopNAggregation(ctx context.Context, opt ListOpt) ([]*databasev1.TopNAggregation, error)
	CreateTopNAggregation(ctx context.Context, measure *databasev1.TopNAggregation) (int64, error)
	UpdateTopNAggregation(ctx context.Context, measure *databasev1.TopNAggregation) (int64, error)
	DeleteTopNAggregation(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}

// Node allows CRUD node schemas in a group.
type Node interface {
	ListNode(ctx context.Context, role databasev1.Role) ([]*databasev1.Node, error)
	RegisterNode(ctx context.Context, node *databasev1.Node, forced bool) error
	GetNode(ctx context.Context, node string) (*databasev1.Node, error)
	UpdateNode(ctx context.Context, node *databasev1.Node) error
}

// Property allows CRUD property schemas in a group.
//
//go:generate mockgen -destination=schema_property_mock.go -package=schema github.com/apache/skywalking-banyandb/banyand/metadata/schema Property
type Property interface {
	GetProperty(ctx context.Context, metadata *commonv1.Metadata) (*databasev1.Property, error)
	ListProperty(ctx context.Context, opt ListOpt) ([]*databasev1.Property, error)
	CreateProperty(ctx context.Context, property *databasev1.Property) error
	UpdateProperty(ctx context.Context, property *databasev1.Property) error
	DeleteProperty(ctx context.Context, metadata *commonv1.Metadata) (bool, int64, error)
}
