package svc

import (
	"errors"
	"time"

	"golib/v3/features/mo"
	"golib/v3/gio"
	"golib/v3/infra/ii"
	"golib/v3/log"
)

var (
	ErrItemNotfound     = errors.New("item not found")
	ErrInternalError    = errors.New("internal error") // ErrInternalError 上游函数错误时返回
	ErrDataError        = errors.New("data error")     // ErrDataError 数据校验失败
	ErrPermissionDenied = errors.New("permission denied")
	ErrBindTypeError    = errors.New("bind type error")
	ErrNoDocuments      = mo.ErrNoDocuments
)

func IsItemNotFound(err error) bool     { return errors.Is(err, ErrItemNotfound) }
func IsInternalError(err error) bool    { return errors.Is(err, ErrInternalError) }
func IsDataError(err error) bool        { return errors.Is(err, ErrDataError) }
func IsPermissionDenied(err error) bool { return errors.Is(err, ErrPermissionDenied) }
func IsErrNoDocuments(err error) bool   { return errors.Is(err, ErrNoDocuments) }

type Service struct {
	Items   ii.Items
	Client  *mo.Client
	Log     log.Logger
	Cache   *Cache
	Timeout time.Duration

	refreshCh chan *ii.ItemInfo
}

func (s *Service) GetItems() ii.Items {
	return s.Items
}

func (s *Service) HasItem(name ii.Name) (*ii.ItemInfo, bool) {
	return s.Items.Has(name)
}

func (s *Service) Find(name ii.Name, filter mo.Filter) ([]*Row, error) {
	var data []mo.M
	if err := s.FindWith(name, filter, &data); err != nil {
		return nil, err
	}
	info, _ := s.HasItem(name)
	return s.toRows(info, data), nil
}

// FindOne 查询一个文档
func (s *Service) FindOne(name ii.Name, filter mo.Filter) (*Row, error) {
	var data mo.M
	if err := s.FindOneWith(name, filter, &data); err != nil {
		return nil, err
	}
	info, _ := s.HasItem(name)
	return s.toRow(info, data), nil
}

func (s *Service) FindWith(name ii.Name, filter mo.Filter, v any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.Find: item not found: %s", name)
		return ErrItemNotfound
	}
	if err := s.checkBindType(v); err != nil {
		s.Log.Error("svc.Find: bind type: %T item name: %s", v, name)
		return err
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.Find: QueryFilterCheck: %s data error: %s. filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}

	// MongoDB 默认不保证查询顺序
	// 此处使用时间升序排列
	sorter := &mo.Sorter{}
	sorter.AddASC(ii.CreationTime)

	opts := mo.Options.Find()
	opts.SetSort(sorter.Done())

	cursor, err := s.openColl(info).Find(gio.ContextTimeout(s.Timeout), query, opts)
	if err != nil {
		s.Log.Error("svc.Find: %s internal error: %s filter: %v", name, err, query)
		return errors.Join(ErrInternalError, err)
	}
	if err = mo.CursorDecodeAll(cursor, v); err != nil {
		s.Log.Error("svc.Find: CursorDecodeAll: %s internal error: %s", name, err)
		return errors.Join(ErrInternalError, err)
	}
	return nil
}

func (s *Service) FindOneWith(name ii.Name, filter mo.Filter, v any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.FindOne: item not found: %s", name)
		return ErrItemNotfound
	}
	if err := s.checkBindType(v); err != nil {
		s.Log.Error("svc.FindOne: bind type: %T item name: %s", v, name)
		return err
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOne: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}

	// MongoDB 默认不保证查询顺序
	// 此处使用时间升序排列
	sorter := &mo.Sorter{}
	sorter.AddASC(ii.CreationTime)

	opts := mo.Options.FindOne()
	opts.SetSort(sorter.Done())

	cursor := s.openColl(info).FindOne(gio.ContextTimeout(s.Timeout), query, opts)
	if err := cursor.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOne: %s internal error: %s filter: %v", name, err, query)
		return errors.Join(ErrInternalError, err)
	}
	if err := cursor.Decode(v); err != nil {
		s.Log.Error("svc.FindOne: CursorDecode: %s internal error: %s", name, err)
		return errors.Join(ErrInternalError, err)
	}
	return nil
}

// FindOneAndDelete 查找并删除文档
func (s *Service) FindOneAndDelete(name ii.Name, filter mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.FindOneAndDelete: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOneAndDelete: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}
	result := s.openColl(info).FindOneAndDelete(gio.ContextTimeout(s.Timeout), query)
	if err := result.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOneAndDelete: %s internal error: %s filter: %v", name, err, query)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.FindOneAndDelete: document has been deleted. filter: %v", query)

	s.refreshCache(info)
	return nil
}

func (s *Service) DeleteOne(name ii.Name, filter mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.DeleteOne: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	result, err := s.openColl(info).DeleteOne(gio.ContextTimeout(s.Timeout), query)
	if err != nil {
		s.Log.Error("svc.DeleteOne: %s internal error: %s filter: %v", name, err, query)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.DeleteOne: %d document has been deleted. filter: %v", result.DeletedCount, query)

	s.refreshCache(info)
	return nil
}

func (s *Service) DeleteMany(name ii.Name, filter mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.DeleteMany: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	result, err := s.openColl(info).DeleteMany(gio.ContextTimeout(s.Timeout), query)
	if err != nil {
		s.Log.Error("svc.DeleteMany: %s internal error: %s filter: %v", name, err, query)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.DeleteMany: %d documents has been deleted. filter: %v", result.DeletedCount, query)

	s.refreshCache(info)
	return nil
}

// FindOneAndUpdate 查找并更新文档, 详情见 mo.SingleResult
func (s *Service) FindOneAndUpdate(name ii.Name, filter, updater mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.FindOneAndUpdate: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOneAndUpdate: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}
	update := updater.Done()
	result := s.openColl(info).FindOneAndUpdate(gio.ContextTimeout(s.Timeout), query, update)
	if err := result.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOneAndUpdate: %s internal error: %s filter: %v updater: %v", name, err, query, update)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.FindOneAndUpdate: document has been updated. filter: %v updater: %v", query, update)

	s.refreshCache(info)
	return nil
}

// EstimatedDocumentCount 合计合集中的文档数量
func (s *Service) EstimatedDocumentCount(name ii.Name) (int64, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.EstimatedDocumentCount: item not found: %s", name)
		return 0, ErrItemNotfound
	}
	length, err := s.openColl(info).EstimatedDocumentCount(gio.ContextTimeout(s.Timeout))
	if err != nil {
		s.Log.Error("svc.EstimatedDocumentCount: %s internal error: %s", name, err)
		return 0, errors.Join(ErrInternalError, err)
	}
	return length, nil
}

// CountDocuments 有条件的合集文档中的数量
func (s *Service) CountDocuments(name ii.Name, filter mo.Filter) (int64, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.CountDocuments: item not found: %s", name)
		return 0, ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.CountDocuments: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return 0, errors.Join(ErrDataError, err)
	}
	length, err := s.openColl(info).CountDocuments(gio.ContextTimeout(s.Timeout), query)
	if err != nil {
		s.Log.Error("svc.CountDocuments: %s internal error: %s filter: %v", name, err, query)
		return 0, errors.Join(ErrInternalError, err)
	}
	return length, nil
}

// InsertOne 插入一条文档
// MongoDB 在插入文档时对于 _id 的做法: 即 doc 中不存在 _id 字段时会在数据编码时补充 _id 字段并且值使用 mo.ObjectID 而不修改源文档.
// 当 _id 字段存在时不会修改其数据类型. 但为了保持数据类型的统一性, 此处当 _id 存在时其必须为 mo.ObjectID 类型
func (s *Service) InsertOne(name ii.Name, doc mo.M) (mo.ObjectID, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.InsertOne: item not found: %s", name)
		return mo.NilObjectID, ErrItemNotfound
	}

	if err := info.PrepareInsert(doc, nil); err != nil {
		s.Log.Error("svc.InsertOne: %s data error: %s data: %v", name, err, doc)
		return mo.NilObjectID, errors.Join(ErrDataError, err)
	}

	result, err := s.openColl(info).InsertOne(gio.ContextTimeout(s.Timeout), doc)
	if err != nil {
		s.Log.Error("svc.InsertOne: %s internal error: %s data: %v", name, err, doc)
		return mo.NilObjectID, errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.InsertOne: %s->%v", name, doc)

	s.refreshCache(info)
	return result.InsertedID.(mo.ObjectID), nil
}

// InsertMany 插入多条文档
// 对于 _id 的处理参见 InsertOne
// MongoDB 插入多条文档时并不要求列表内所有元素的数据类型一致, 但为了保持数据类型的统一性, docs 内的所有元素数据类型必须为 map/object
func (s *Service) InsertMany(name ii.Name, docs mo.A) (mo.A, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.InsertMany: item not found: %s", name)
		return nil, ErrItemNotfound
	}

	err := s.toMaps(docs, func(row mo.M) error {
		if err := info.PrepareInsert(row, nil); err != nil {
			s.Log.Error("svc.InsertMany: %s data error: %s data: %v", name, err, row)
			return errors.Join(ErrDataError, err)
		}
		return nil
	})

	if err != nil {
		s.Log.Error("svc.InsertMany: %s data error: %s", name, err)
		return nil, errors.Join(ErrDataError, err)
	}
	result, err := s.openColl(info).InsertMany(gio.ContextTimeout(s.Timeout), docs)
	if err != nil {
		s.Log.Error("svc.InsertMany: %s internal error: %s", name, err)
		return nil, errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.InsertMany: %s->%v", name, result.InsertedIDs)

	s.refreshCache(info)
	return result.InsertedIDs, nil
}

// UpdateOne 更新一条文档, 通常情况下 update 参数需要使用 mo.Updater 构建
// 注意: 为了兼容此前非 mo.Updater 构建的更新参数, 此处 update 参数支持 mo.M 和 mo.D 两种类型的参数, 其他类型会返回错误
// update 类型为 mo.M 时, 会用作 mo.PoSet 形式处理
// update 类型为 mo.D 时: 当 update 长度为 1 且 Key 未指定 mo.PoSet 时则按 mo.PoSet 处理
func (s *Service) UpdateOne(name ii.Name, filter, updater mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.UpdateOne: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateOne: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}
	update := updater.Done()
	if err := info.PrepareUpdater(update, nil); err != nil {
		s.Log.Error("svc.UpdateOne: PrepareUpdater: %s data error: %s updater: %v", name, err, update)
		return errors.Join(ErrDataError, err)
	}

	opts := mo.Options.Update()
	upsert := mo.OperatorHas(update, mo.PoSetOnInsert)
	opts.Upsert = &upsert

	_, err := s.openColl(info).UpdateOne(gio.ContextTimeout(s.Timeout), query, update, opts)
	if err != nil {
		s.Log.Error("svc.UpdateOne: %s internal error: %s filter: %v updater: %v", name, err, filter, update)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.UpdateOne: document has been updated. filter: %v updater: %v", filter, update)

	s.refreshCache(info)
	return nil
}

// UpdateByID 使用 _id 作为条件更新 1 条数据
func (s *Service) UpdateByID(name ii.Name, id mo.ObjectID, update mo.Filter) error {
	filter := &mo.Matcher{
		Filter: mo.D{
			{Key: mo.OID, Value: id},
		},
	}
	return s.UpdateOne(name, filter, update)
}

// UpdateMany 使用 filter 作为条件批量更新数据
// 注意: 兼容性解释见 UpdateOne
func (s *Service) UpdateMany(name ii.Name, filter, updater mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.UpdateMany: item not found: %s", name)
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateMany: QueryFilterCheck: %s data error: %s filter: %v", name, err, query)
		return errors.Join(ErrDataError, err)
	}
	update := updater.Done()
	if err := info.PrepareUpdater(update, nil); err != nil {
		s.Log.Error("svc.UpdateMany: PrepareUpdater: %s data error: %s updater: %v", name, err, update)
		return errors.Join(ErrDataError, err)
	}

	opts := mo.Options.Update()
	upsert := mo.OperatorHas(update, mo.PoSetOnInsert)
	opts.Upsert = &upsert

	result, err := s.openColl(info).UpdateMany(gio.ContextTimeout(s.Timeout), query, update, opts)
	if err != nil {
		s.Log.Error("svc.UpdateMany: %s internal error: %s filter: %v updater: %v", name, err, query, update)
		return errors.Join(ErrInternalError, err)
	}
	s.Log.Debug("svc.UpdateMany: %d documents has been updated. filter: %v updater: %v", result.ModifiedCount, query, update)

	s.refreshCache(info)
	return nil
}

// Aggregate 聚合查询
// v 必须传入指针类型
// Aggregate 不传入 XML 配置中的 Lookup/Set 等聚合操作, 当需要时可通过 itemInfo.Aggregation 函数创建后传入
func (s *Service) Aggregate(name ii.Name, pipe mo.Pipeline, v any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.Aggregate: item not found: %s", name)
		return ErrItemNotfound
	}

	// 如果存在 mo.PsMatch 操作符时则追加
	if i, d, o := mo.HasOperator(pipe, mo.PsMatch); o {
		filter, ok := d.(mo.D)
		if !ok {
			return ErrDataError
		}
		pipe[i] = mo.D{{Key: mo.PsMatch, Value: filter}}
	}

	var (
		stage  mo.Pipeline
		lookup []ii.Lookup
	)
	copy(stage, pipe)

	if s.Cache != nil {
		stage, lookup = s.Cache.SpitPipe(info, pipe)
	}

	ctx := gio.ContextTimeout(s.Timeout)
	cursor, err := s.openColl(info).Aggregate(ctx, stage)
	if err != nil {
		s.Log.Error("svc.Aggregate: %s internal error: %s pipe: %v", name, err, pipe)
		return errors.Join(ErrInternalError, err)
	}
	if err = mo.CursorDecodeAll(cursor, v); err != nil {
		s.Log.Error("svc.Aggregate: CursorDecodeAll: %s internal error: %s pipe: %v", name, err, pipe)
		return errors.Join(ErrInternalError, err)
	}

	if rows, o := v.(*[]mo.M); o && len(lookup) > 0 {
		if tim := s.Cache.Format(info, lookup, rows); tim.Milliseconds() > 100 {
			s.Log.Warn("svc.Cache.Format: %s -> %s", tim, info.Name)
		}
	}

	return nil
}

func (s *Service) toRows(itemInfo *ii.ItemInfo, data []mo.M) []*Row {
	rows := make([]*Row, len(data))
	for i := 0; i < len(rows); i++ {
		rows[i] = &Row{itemInfo: itemInfo, m: data[i]}
	}
	return rows
}

func (s *Service) toRow(itemInfo *ii.ItemInfo, data mo.M) *Row {
	return &Row{itemInfo: itemInfo, m: data}
}

// refreshCache 刷新缓存
func (s *Service) refreshCache(info *ii.ItemInfo) {
	if s.Cache == nil {
		return
	}
	if _, ok := s.Cache.Include(info.Name); !ok {
		return
	}
	if len(s.refreshCh) == 0 {
		s.refreshCh = make(chan *ii.ItemInfo, 128)
		go s.handleRefresh()
	}
	s.refreshCh <- info
}

func (s *Service) handleRefresh() {
	for info := range s.refreshCh {
		qt := time.Now()
		cursor, err := s.openColl(info).Find(gio.ContextTimeout(s.Timeout), mo.D{})
		if err != nil {
			s.Cache.Clear(info.Name) // 查询失败时则清除内存缓存, 防止信息不一致
			s.Log.Error("svc.refreshCache: %s internal error: %s", info.Name, err)
			continue
		}
		qts := time.Now().Sub(qt)

		dt := time.Now()
		var data []mo.M
		if err = mo.CursorDecodeAll(cursor, &data); err != nil {
			s.Log.Error("svc.refreshCache: CursorDecodeAll: %s internal error: %s", info.Name, err)
			continue
		}
		dts := time.Now().Sub(dt)

		st := time.Now()
		s.Cache.SetData(info.Name, data)
		sts := time.Now().Sub(st)

		if qts.Milliseconds() >= 100 || dts.Milliseconds() >= 100 || sts.Milliseconds() >= 100 {
			s.Log.Warn("svc.refreshCache: %s query: %s decode: %s set: %s count: %s total: %d",
				info.Name, qts, dts, sts, qts+dts+sts, len(data))
		}
	}
}

func (s *Service) openColl(info *ii.ItemInfo) *mo.Collection {
	return s.Client.Database(info.Name.Database()).Collection(info.Name.Collection())
}
