package svc

import (
	"errors"

	"git.simanc.com/software/golib/v4/features/mo"
	"git.simanc.com/software/golib/v4/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 []Row
	if err := s.FindWith(name, filter, nil, nil, 0, 0, &data); err != nil {
		return nil, err
	}
	return data, nil
}

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

func (s *WithUser) FindWith(name ii.Name, filter, sort, project mo.Filter, skip, limit int64, v any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.Find.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	if err := s.checkBindType(v); err != nil {
		s.Log.Error("svc.Find.%s: bind type: %T", name, v)
		return err
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.Find.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return errors.Join(ErrPermissionDenied, err)
	}

	opts := mo.Options.Find()
	if project != nil {
		opts.SetProjection(project.Done())
	}
	if sort != nil {
		opts.SetSort(sort.Done())
	} else {
		opts.SetSort(mo.NewSorter(ii.CreationTime, mo.SortDESC).Done())
	}
	if skip > 0 {
		opts.SetSkip(skip)
	}
	if limit > 0 {
		opts.SetLimit(limit)
	} else {
		if len(query) == 0 {
			opts.SetLimit(findLimitRows) // 如果没有过滤条件, 限制返回数量
		}
	}

	ctx, cancel := s.newContext()
	defer cancel()
	cursor, err := s.openColl(info).Find(ctx, query)
	if err != nil {
		s.Log.Error("svc.Find.%s: %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.%s: CursorDecodeAll: %s UID: %s", name, err, s.User.ID().Hex())
		return errors.Join(ErrInternalError, err)
	}
	return nil
}

func (s *WithUser) FindOneWith(name ii.Name, filter, sort, project mo.Filter, v any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.FindOne.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOne.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}

	opts := mo.Options.FindOne()
	if project != nil {
		opts.SetProjection(project.Done())
	}
	if sort != nil {
		opts.SetSort(sort.Done())
	} else {
		opts.SetSort(mo.NewSorter(ii.CreationTime, mo.SortDESC).Done())
	}

	ctx, cancel := s.newContext()
	defer cancel()
	cursor := s.openColl(info).FindOne(ctx, query)
	if err := cursor.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOne.%s: %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.%s: CursorDecode: %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.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOneAndDelete.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret := s.openColl(info).FindOneAndDelete(ctx, query)
	if err := ret.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOneAndDelete.%s: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.FindOneAndDelete.%s: one document has been deleted. filter: %v", name, 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.%s: item not found 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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).DeleteOne(ctx, query)
	if err != nil {
		s.Log.Error("svc.DeleteOne.%s: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.DeleteOne.%s: %d document has been deleted. filter: %v UID: %s", name, 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.%s: item not found 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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).DeleteMany(ctx, query)
	if err != nil {
		s.Log.Error("svc.DeleteMany.%s: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.DeleteMany.%s: %d documents has been deleted. filter: %v UID: %s", name, 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.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.FindOneAndUpdate.%s: QueryFilterCheck: %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.%s: PrepareUpdater: %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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret := s.openColl(info).FindOneAndUpdate(ctx, query, update)
	if err := ret.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Error("svc.FindOneAndUpdate.%s: %s filter: %v updater: %v UID: %s", name, err, query, update, s.User.ID().Hex())
		return err
	}
	s.Log.Debug("svc.FindOneAndUpdate.%s: one document has been updated. filter: %v UID: %s", name, 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.%s: item not found 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.%s: setAC: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return 0, ErrPermissionDenied
	}

	var (
		length int64
		err    error
	)

	ctx, cancel := s.newContext()
	defer cancel()
	if len(filter) > 0 {
		length, err = s.openColl(info).CountDocuments(ctx, filter)
	} else {
		length, err = s.openColl(info).EstimatedDocumentCount(ctx)
	}

	if err != nil {
		s.Log.Error("svc.EstimatedDocumentCount.%s: %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.%s: item not found 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.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, err, query, s.User.ID().Hex())
		return 0, ErrPermissionDenied
	}
	ctx, cancel := s.newContext()
	defer cancel()
	length, err := s.openColl(info).CountDocuments(ctx, query)
	if err != nil {
		s.Log.Error("svc.CountDocuments.%s: %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, value any) (mo.ObjectID, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.InsertOne.%s: item not found UID: %s", name, s.User.ID().Hex())
		return mo.NilObjectID, ErrItemNotfound
	}
	params, err := s.resolveInsert(value)
	if err != nil {
		s.Log.Error("svc.InsertOne.%s: resolveInsert: %s", name, err)
		return mo.NilObjectID, err
	}
	doc, err := info.PrepareInsert(params, s.User)
	if err != nil {
		s.Log.Error("svc.InsertOne.%s: %s data: %v UID: %s", name, err, doc, s.User.ID().Hex())
		return mo.NilObjectID, errors.Join(ErrDataError, err)
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).InsertOne(ctx, doc)
	if err != nil {
		s.Log.Error("svc.InsertOne.%s: %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, value any) (mo.A, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Error("svc.InsertMany.%s: item not found UID: %s", name, s.User.ID().Hex())
		return nil, ErrItemNotfound
	}
	docs, err := s.toDocs(value, func(doc mo.D) (mo.D, error) {
		return info.PrepareInsert(doc, nil)
	})
	if err != nil {
		s.Log.Error("svc.InsertMany.%s: %s UID: %s", name, err, s.User.ID().Hex())
		return nil, errors.Join(ErrDataError, err)
	}
	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).InsertMany(ctx, docs)
	if err != nil {
		s.Log.Error("svc.InsertMany.%s: %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.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateOne.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, 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.%s: PrepareUpdater: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}

	opts := mo.Options.UpdateOne()
	_, upsert := mo.HasOptIn(update, mo.OptSetOnInsert)
	opts.SetUpsert(upsert)

	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).UpdateOne(ctx, query, update, opts)
	if err != nil {
		s.Log.Error("svc.UpdateOne.%s: %s filter: %v updater: %v UID: %s", name, err, query, update, s.User.ID().Hex())
		return ErrInternalError
	}
	s.Log.Debug("svc.UpdateOne.%s: %d document has been updated. filter: %v updater: %v", name, 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.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	query := filter.Done()
	if err := info.QueryFilterCheck(query); err != nil {
		s.Log.Error("svc.UpdateMany.%s: QueryFilterCheck: %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.%s: setAC: %s filter: %v UID: %s", name, 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.%s: PrepareUpdater: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}

	opts := mo.Options.UpdateMany()
	_, upsert := mo.HasOptIn(update, mo.OptSetOnInsert)
	opts.SetUpsert(upsert)

	ctx, cancel := s.newContext()
	defer cancel()
	ret, err := s.openColl(info).UpdateMany(ctx, filter, update, opts)
	if err != nil {
		s.Log.Error("svc.UpdateMany.%s: %s filter: %v updater: %v UID: %s", name, err, filter, update, s.User.ID().Hex())
		return ErrInternalError
	}
	s.Log.Debug("svc.UpdateOne.%s: %d documents has been updated. filter: %v updater: %v", name, 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.%s: item not found UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}

	// 如果存在 mo.ArgMatch 操作符时则追加
	if i, d, o := mo.HasOptWith(pipe, mo.ArgMatch); o {
		filter, ok := d.(mo.D)
		if !ok {
			return ErrDataError
		}
		if err := s.setAC(info.Name, &filter); err != nil {
			s.Log.Error("svc.Aggregate.%s: setAC: %s Pipeline: %v UID: %s", name, err, pipe, s.User.ID().Hex())
			return ErrPermissionDenied
		}
		pipe[i] = mo.D{{Key: mo.ArgMatch, Value: filter}}
	} else {
		// 不存在时则新建一个 mo.ArgMatch
		var filter mo.D
		if err := s.setAC(info.Name, &filter); err != nil {
			s.Log.Error("svc.Aggregate.%s: setAC: %s Pipeline: %v UID: %s", name, err, pipe, s.User.ID().Hex())
			return ErrPermissionDenied
		}
		if filter != nil {
			pipe = append(mo.Pipeline{mo.D{{Key: mo.ArgMatch, 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, cancel := s.newContext()
	defer cancel()
	cursor, err := s.openColl(info).Aggregate(ctx, stage)
	if err != nil {
		s.Log.Error("svc.Aggregate.%s: %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.%s: CursorDecodeAll: %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", info.Name, tim)
		}
	}

	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
}
