package svc

import (
	"errors"
	"fmt"
	"strings"
	"time"

	"golib/v2/features/mo"
	"golib/v2/infra/ii"
	"golib/v2/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")
)

type Service struct {
	Items  ii.Items
	Perms  ii.Permission
	User   ii.User
	Client *mo.Client
	Log    log.Printer

	cache     *Cache
	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.D) ([]mo.M, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.Find: item not found: %s UID: %s", name, s.User.ID().Hex())
		return nil, ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.Find: PrepareFilter: %s data error: %s. filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return nil, ErrDataError
	}

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

	cursor, err := info.Open(s.Client).Find(filter)
	if err != nil {
		s.Log.Println("svc.Find: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return nil, ErrInternalError
	}

	var data []mo.M
	if err = mo.CursorDecodeAll(cursor, &data); err != nil {
		s.Log.Println("svc.Find: CursorDecodeAll: %s internal error: %s UID: %s", name, err, s.User.ID().Hex())
		return nil, ErrInternalError
	}
	return data, nil
}

// FindOne 查询一个文档
func (s *Service) FindOne(name ii.Name, filter mo.D) (mo.M, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.FindOne: item not found: %s UID: %s", name, s.User.ID().Hex())
		return nil, ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.FindOne: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return nil, ErrDataError
	}

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

	cursor := info.Open(s.Client).FindOne(filter)
	if err := cursor.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return nil, err
		}
		s.Log.Println("svc.FindOne: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return nil, ErrInternalError
	}

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

	return data, nil
}

// FindOneAndDelete 查找并删除文档
func (s *Service) FindOneAndDelete(name ii.Name, filter mo.D) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.FindOneAndDelete: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.FindOneAndDelete: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.FindOneAndDelete: AC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	result := info.Open(s.Client).FindOneAndDelete(filter)
	if err := result.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Println("svc.FindOneAndDelete: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return err
	}

	s.refreshCache(info)
	return nil
}

func (s *Service) DeleteOne(name ii.Name, filter mo.D) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.DeleteOne: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}

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

	result, err := info.Open(s.Client).DeleteOne(filter)
	if err != nil {
		s.Log.Println("svc.DeleteOne: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return err
	}
	s.Log.Println("svc.DeleteOne: %d documents has been deleted. filter: %v UID: %s", result.DeletedCount, filter, s.User.ID().Hex())

	s.refreshCache(info)
	return nil
}

func (s *Service) DeleteMany(name ii.Name, filter mo.D) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.DeleteMany: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}

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

	result, err := info.Open(s.Client).DeleteMany(filter)
	if err != nil {
		s.Log.Println("svc.DeleteMany: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return err
	}
	s.Log.Println("svc.DeleteMany: %d documents has been deleted. filter: %v UID: %s", result.DeletedCount, filter, s.User.ID().Hex())

	s.refreshCache(info)
	return nil
}

// FindOneAndUpdate 查找并更新文档, 详情见 mo.SingleResult
func (s *Service) FindOneAndUpdate(name ii.Name, filter mo.D, update mo.D) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.FindOneAndUpdate: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.FindOneAndUpdate: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return ErrDataError
	}
	if err := info.PrepareUpdater(update, s.User); err != nil {
		s.Log.Println("svc.FindOneAndUpdate: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.FindOneAndUpdate: AC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	result := info.Open(s.Client).FindOneAndUpdate(filter, update)
	if err := result.Err(); err != nil {
		if errors.Is(err, mo.ErrNoDocuments) {
			return err
		}
		s.Log.Println("svc.FindOneAndUpdate: %s internal error: %s filter: %v updater: %v UID: %s", name, err, filter, update, s.User.ID().Hex())
		return err
	}

	s.refreshCache(info)
	return nil
}

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

	var filter mo.D
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.EstimatedDocumentCount: AC: %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 = info.Open(s.Client).CountDocuments(filter)
	} else {
		length, err = info.Open(s.Client).EstimatedDocumentCount()
	}

	if err != nil {
		s.Log.Println("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 *Service) CountDocuments(name ii.Name, filter mo.D) (int64, error) {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.CountDocuments: item not found: %s UID: %s", name, s.User.ID().Hex())
		return 0, ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.CountDocuments: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return 0, ErrDataError
	}
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.CountDocuments: AC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return 0, ErrPermissionDenied
	}
	length, err := info.Open(s.Client).CountDocuments(filter)
	if err != nil {
		s.Log.Println("svc.CountDocuments: %s internal error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return 0, ErrInternalError
	}
	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.Println("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.Println("svc.InsertOne: %s data error: %s data: %v UID: %s", name, err, doc, s.User.ID().Hex())
		return mo.NilObjectID, ErrDataError
	}

	result, err := info.Open(s.Client).InsertOne(doc)
	if err != nil {
		s.Log.Println("svc.InsertOne: %s internal error: %s data: %v UID: %s", name, err, doc, s.User.ID().Hex())
		return mo.NilObjectID, ErrInternalError
	}

	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.Println("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.Println("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.Println("svc.InsertMany: %s data error: %s UID: %s", name, err, s.User.ID().Hex())
		return nil, ErrDataError
	}
	result, err := info.Open(s.Client).InsertMany(docs)
	if err != nil {
		s.Log.Println("svc.InsertMany: %s internal error: %s UID: %s", name, err, s.User.ID().Hex())
		return nil, ErrInternalError
	}

	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 mo.D, update any) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.UpdateOne: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.UpdateOne: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.UpdateOne: AC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	updater, err := s.handleUpdater(update)
	if err != nil {
		s.Log.Println("svc.UpdateOne: handleUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}
	if err = info.PrepareUpdater(updater, s.User); err != nil {
		s.Log.Println("svc.UpdateOne: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, updater, s.User.ID().Hex())
		return ErrDataError
	}
	_, err = info.Open(s.Client).UpdateOne(filter, updater)
	if err != nil {
		s.Log.Println("svc.UpdateOne: %s internal error: %s filter: %v updater: %v UID: %s", name, err, filter, updater, s.User.ID().Hex())
		return ErrInternalError
	}

	s.refreshCache(info)
	return nil
}

// UpdateByID 使用 _id 作为条件更新 1 条数据
// 注意: 兼容性解释见 UpdateOne
func (s *Service) UpdateByID(name ii.Name, id mo.ObjectID, update mo.D) error {
	return s.UpdateOne(name, mo.D{{Key: mo.ID.Key(), Value: id}}, update)
}

// UpdateMany 使用 filter 作为条件批量更新数据
// 注意: 兼容性解释见 UpdateOne
func (s *Service) UpdateMany(name ii.Name, filter mo.D, update mo.D) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("svc.UpdateMany: item not found: %s UID: %s", name, s.User.ID().Hex())
		return ErrItemNotfound
	}
	if err := info.PrepareFilter(filter); err != nil {
		s.Log.Println("svc.UpdateMany: PrepareFilter: %s data error: %s filter: %v UID: %s", name, err, filter, s.User.ID().Hex())
		return ErrDataError
	}
	if err := s.AC(info.Name, &filter); err != nil {
		s.Log.Println("svc.UpdateMany: AC: %s filter: %v UID: %s", err, filter, s.User.ID().Hex())
		return ErrPermissionDenied
	}
	updater, err := s.handleUpdater(update)
	if err != nil {
		s.Log.Println("svc.UpdateOne: handleUpdater: %s data error: %s updater: %v UID: %s", name, err, update, s.User.ID().Hex())
		return ErrDataError
	}
	if err = info.PrepareUpdater(updater, s.User); err != nil {
		s.Log.Println("svc.UpdateMany: PrepareUpdater: %s data error: %s updater: %v UID: %s", name, err, updater, s.User.ID().Hex())
		return ErrDataError
	}
	_, err = info.Open(s.Client).UpdateMany(filter, updater)
	if err != nil {
		s.Log.Println("svc.UpdateMany: %s internal error: %s filter: %v updater: %v UID: %s", name, err, filter, updater, s.User.ID().Hex())
		return ErrInternalError
	}

	s.refreshCache(info)
	return nil
}

// Aggregate 聚合查询
// v 必须传入指针类型
// Aggregate 不传入 XML 配置中的 Lookup/Set 等聚合操作, 当需要时可通过 itemInfo.Aggregation 函数创建后传入
func (s *Service) Aggregate(name ii.Name, pipe mo.Pipeline, v interface{}) error {
	info, ok := s.HasItem(name)
	if !ok {
		s.Log.Println("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.AC(info.Name, &filter); err != nil {
			s.Log.Println("svc.Aggregate: AC: %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.AC(info.Name, &filter); err != nil {
			s.Log.Println("svc.Aggregate: AC: %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...)
		}
	}

	stage, lookup := s.cache.SpitPipe(info, pipe)
	cursor, err := info.Open(s.Client).Aggregate(stage)
	if err != nil {
		s.Log.Println("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.Println("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.Println("svc.cache.Format: %s -> %s", s.User.ID().Hex(), tim, info.Name)
		}
	}

	return nil
}

func (s *Service) handleUpdater(update any) (mo.D, error) {
	updater := &mo.Updater{}
	switch val := update.(type) {
	case mo.M:
		doc, err := mo.Convert.DE(val)
		if err != nil {
			return nil, err
		}
		updater.Setter = doc
		return updater.Done(), nil
	case mo.D:
		if len(val) == 1 && !strings.HasPrefix(val[0].Key, "$") {
			updater.Setter = val
			return updater.Done(), nil
		}
		return val, nil
	}
	return nil, fmt.Errorf("unsupport update type")
}

func (s *Service) AC(name ii.Name, filter *mo.D) error {
	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
}

// refreshCache 刷新缓存
// 仅用于写操作时刷新缓存, 必须在所中调用, 否则可能会导致 panic
func (s *Service) refreshCache(info *ii.ItemInfo) {
	if s.cache == nil {
		return
	}
	if _, ok := s.cache.Include(info.Name); !ok {
		return
	}
	s.refreshCh <- info
}

func (s *Service) handleRefresh() {
	for info := range s.refreshCh {
		qt := time.Now()
		cursor, err := info.Open(s.Client).Find(mo.D{})
		if err != nil {
			s.Log.Println("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.Println("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.Println("svc.refreshCache: %s query: %s decode: %s set: %s count: %s total: %d",
				info.Name, qts, dts, sts, qts+dts+sts, len(data))
		}
	}
}
