package svc

import (
	"errors"

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

type WithUser struct {
	User  ii.User
	Perms ii.Permission
	*Service
}

func (s *WithUser) 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 *WithUser) FindOne(name ii.Name, filter mo.Filter) (*Row, error) {
	var data mo.M
	if err := s.FindOneWith(name, filter, &data); err != nil {
		return nil, ErrInternalError
	}
	info, _ := s.HasItem(name)
	return s.toRow(info, data), nil
}

func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		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 UID: %s", name, err, query, s.User.ID().Hex())
		return errors.Join(ErrDataError, err)
	}

	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.Find: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return errors.Join(ErrPermissionDenied, err)
	}

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

	if err = mo.CursorDecodeAll(cursor, v); err != nil {
		s.Log.Error("svc.Find: CursorDecodeAll: %s internal error: %s UID: %s", name, err, s.User.ID().Hex())
		return errors.Join(ErrInternalError, err)
	}
	return nil
}

func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOne: QueryFilterCheck: %s data error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrDataError
	}

	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.FindOne: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}

	cursor := s.openColl(info).FindOne(gio.ContextTimeout(s.Timeout), query)
	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 UID: %s", name, err, query, s.User.ID().Hex())
		return ErrInternalError
	}

	if err := cursor.Decode(&v); err != nil {
		s.Log.Error("svc.FindOne: CursorDecode: %s internal error: %s UID: %s", name, err, s.User.ID().Hex())
		return ErrInternalError
	}
	return nil
}

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

	s.refreshCache(info)
	return nil
}

func (s *WithUser) DeleteOne(name ii.Name, filter mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.DeleteOne: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.DeleteOne: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}

	ret, err := s.openColl(info).DeleteOne(gio.ContextTimeout(s.Timeout), query)
	if err != nil {
		s.Log.Error("svc.DeleteOne: %s internal error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.DeleteOne: %d document has been deleted. filter: %v UID: %s", ret.DeletedCount, query, s.User.ID().Hex())

	s.refreshCache(info)
	return nil
}

func (s *WithUser) DeleteMany(name ii.Name, filter mo.Filter) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.DeleteMany: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.DeleteMany: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}

	ret, err := s.openColl(info).DeleteMany(gio.ContextTimeout(s.Timeout), query)
	if err != nil {
		s.Log.Error("svc.DeleteMany: %s internal error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.DeleteMany: %d documents has been deleted. filter: %v UID: %s", ret.DeletedCount, query, s.User.ID().Hex())

	s.refreshCache(info)
	return nil
}

// FindOneAndUpdate 查找并更新文档, 详情见 mo.SingleResult
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOneAndUpdate: QueryFilterCheck: %s data error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrDataError
	}
	update := updater.Done()
	if err := info.PrepareUpdater(update, s.User); err != nil {
		s.Log.Error("svc.FindOneAndUpdate: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.FindOneAndUpdate: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	ret := s.openColl(info).FindOneAndUpdate(gio.ContextTimeout(s.Timeout), query, update)
	if err := ret.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOneAndUpdate: %s internal error: %s filter: %v updater: %v UID: %s", name, err, query, update, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.FindOneAndUpdate: document has been updated. filter: %v UID: %s", query, s.User.ID().Hex())

	s.refreshCache(info)
	return nil
}

// EstimatedDocumentCount 合计合集中的文档数量
func (s *WithUser) EstimatedDocumentCount(name ii.Name) (int64, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.EstimatedDocumentCount: item not found: %s UID: %s", name, s.User.ID().Hex())
		return 0, ErrItemNotfound
	}

	var filter mo.D
	if err := s.setAC(info.Name, &filter); err != nil {
		s.Log.Error("svc.EstimatedDocumentCount: setAC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return 0, ErrPermissionDenied
	}

	var (
		length int64
		err    error
	)

	if len(filter) > 0 {
		length, err = s.openColl(info).CountDocuments(gio.ContextTimeout(s.Timeout), filter)
	} else {
		length, err = s.openColl(info).EstimatedDocumentCount(gio.ContextTimeout(s.Timeout))
	}

	if err != nil {
		s.Log.Error("svc.EstimatedDocumentCount: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return 0, ErrInternalError
	}

	return length, nil
}

// CountDocuments 有条件的合集文档中的数量
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		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 UID: %s", name, err, query, s.User.ID().Hex())
		return 0, ErrDataError
	}
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.CountDocuments: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return 0, ErrPermissionDenied
	}
	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 UID: %s", name, err, query, s.User.ID().Hex())
		return 0, ErrInternalError
	}
	return length, nil
}

// InsertOne 插入一条文档
// MongoDB 在插入文档时对于 _id 的做法: 即 doc 中不存在 _id 字段时会在数据编码时补充 _id 字段并且值使用 mo.ObjectID 而不修改源文档.
// 当 _id 字段存在时不会修改其数据类型. 但为了保持数据类型的统一性, 此处当 _id 存在时其必须为 mo.ObjectID 类型
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return mo.NilObjectID, ErrItemNotfound
	}

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

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

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

// InsertMany 插入多条文档
// 对于 _id 的处理参见 InsertOne
// MongoDB 插入多条文档时并不要求列表内所有元素的数据类型一致, 但为了保持数据类型的统一性, docs 内的所有元素数据类型必须为 map/object
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return nil, ErrItemNotfound
	}

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

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

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

// UpdateOne 更新一条文档
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateOne: QueryFilterCheck: %s data error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.UpdateOne: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	update := updater.Done()
	if err := info.PrepareUpdater(update, s.User); err != nil {
		s.Log.Error("svc.UpdateOne: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}

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

	ret, 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 UID: %s", name, err, query, update, s.User.ID().Hex())
		return ErrInternalError
	}
	s.Log.Debug("svc.UpdateOne: %d document has been updated. filter: %v updater: %v", ret.ModifiedCount+ret.UpsertedCount, query, update)

	s.refreshCache(info)
	return nil
}

// UpdateByID 使用 _id 作为条件更新 1 条数据
func (s *WithUser) 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 *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateMany: QueryFilterCheck: %s data error: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.setAC(info.Name, &query); err != nil {
		s.Log.Error("svc.UpdateMany: setAC: %s filter: %v UID: %s", err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	update := updater.Done()
	if err := info.PrepareUpdater(update, s.User); err != nil {
		s.Log.Error("svc.UpdateMany: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}

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

	ret, err := s.openColl(info).UpdateMany(gio.ContextTimeout(s.Timeout), filter, update, opts)
	if err != nil {
		s.Log.Error("svc.UpdateMany: %s internal error: %s filter: %v updater: %v UID: %s", name, err, filter, update, s.User.ID().Hex())
		return ErrInternalError
	}
	s.Log.Debug("svc.UpdateOne: %d documents has been updated. filter: %v updater: %v", ret.ModifiedCount+ret.UpsertedCount, filter, update)

	s.refreshCache(info)
	return nil
}

// Aggregate 聚合查询
// v 必须传入指针类型
// Aggregate 不传入 XML 配置中的 Lookup/Set 等聚合操作, 当需要时可通过 itemInfo.Aggregation 函数创建后传入
func (s *WithUser) 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 UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}

	// 如果存在 mo.PsMatch 操作符时则追加
	if i, d, o := mo.HasOperator(pipe, mo.PsMatch); o {
		filter, ok := d.(mo.D)
		if !ok {
			return ErrDataError
		}
		if err := s.setAC(info.Name, &filter); err != nil {
			s.Log.Error("svc.Aggregate: setAC: %s Pipeline: %v UID: %s", err, pipe, s.User.ID().Hex())
			return ErrPermissionDenied
		}
		pipe[i] = mo.D{{Key: mo.PsMatch, Value: filter}}
	} else {
		// 不存在时则新建一个 mo.PsMatch
		var filter mo.D
		if err := s.setAC(info.Name, &filter); err != nil {
			s.Log.Error("svc.Aggregate: setAC: %s Pipeline: %v UID: %s", err, pipe, s.User.ID().Hex())
			return ErrPermissionDenied
		}
		if filter != nil {
			pipe = append(mo.Pipeline{mo.D{{Key: mo.PsMatch, Value: filter}}}, pipe...)
		}
	}

	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 UID: %s", name, err, pipe, s.User.ID().Hex())
		return ErrInternalError
	}

	if err = mo.CursorDecodeAll(cursor, v); err != nil {
		s.Log.Error("svc.Aggregate: CursorDecodeAll: %s internal error: %s pipe: %v UID: %s", name, err, pipe, s.User.ID().Hex())
		return ErrInternalError
	}

	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", s.User.ID().Hex(), tim, info.Name)
		}
	}

	return nil
}

func (s *WithUser) setAC(name ii.Name, filter *mo.D) error {
	if s.Perms == nil {
		return nil
	}
	perms, ok := s.Perms.Has(name, s.User)
	if !ok {
		return ErrPermissionDenied
	}
	// perms 应当在 filter 后面, 假设 filter 与 perms 同时存在 name=1 的条件, 按照权限限制应当以 perms 为准
	// MongoDB 对于同一个字段出现多次时, 以最后出现的字段生效
	*filter = append(*filter, perms...)
	return nil
}
