package db

import (
	"gitlink.org.cn/cloudream/jcs-pub/client/types"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

type ObjectAccessStatDB struct {
	*DB
}

func (db *DB) ObjectAccessStat() *ObjectAccessStatDB {
	return &ObjectAccessStatDB{db}
}

func (*ObjectAccessStatDB) Get(ctx SQLContext, objID types.ObjectID, spaceID types.UserSpaceID) (types.ObjectAccessStat, error) {
	var ret types.ObjectAccessStat
	err := ctx.Table("ObjectAccessStat").
		Where("ObjectID = ? AND UserSpaceID = ?", objID, spaceID).
		First(&ret).Error
	return ret, err
}

func (*ObjectAccessStatDB) GetByObjectID(ctx SQLContext, objID types.ObjectID) ([]types.ObjectAccessStat, error) {
	var ret []types.ObjectAccessStat
	err := ctx.Table("ObjectAccessStat").
		Where("ObjectID = ?", objID).
		Find(&ret).Error
	return ret, err
}

func (*ObjectAccessStatDB) BatchGetByObjectID(ctx SQLContext, objIDs []types.ObjectID) ([]types.ObjectAccessStat, error) {
	if len(objIDs) == 0 {
		return nil, nil
	}

	var ret []types.ObjectAccessStat
	err := ctx.Table("ObjectAccessStat").
		Where("ObjectID IN ?", objIDs).
		Find(&ret).Error
	return ret, err
}

func (*ObjectAccessStatDB) BatchGetByObjectIDOnStorage(ctx SQLContext, objIDs []types.ObjectID, spaceID types.UserSpaceID) ([]types.ObjectAccessStat, error) {
	if len(objIDs) == 0 {
		return nil, nil
	}

	var ret []types.ObjectAccessStat
	err := ctx.Table("ObjectAccessStat").
		Where("ObjectID IN ? AND UserSpaceID = ?", objIDs, spaceID).
		Find(&ret).Error
	return ret, err
}

func (*ObjectAccessStatDB) BatchAddCounter(ctx SQLContext, entries []AddAccessStatEntry) error {
	if len(entries) == 0 {
		return nil
	}

	for _, entry := range entries {
		acc := types.ObjectAccessStat{
			ObjectID:    entry.ObjectID,
			UserSpaceID: entry.UserSpaceID,
			Counter:     entry.Counter,
		}

		err := ctx.Table("ObjectAccessStat").
			Clauses(clause.OnConflict{
				Columns: []clause.Column{{Name: "ObjectID"}, {Name: "UserSpaceID"}},
				DoUpdates: clause.Assignments(map[string]any{
					"Counter": gorm.Expr("Counter + values(Counter)"),
				}),
			}).Create(&acc).Error
		if err != nil {
			return err
		}
	}
	return nil
}

func (*ObjectAccessStatDB) BatchUpdateAmountInPackage(ctx SQLContext, pkgIDs []types.PackageID, historyWeight float64) error {
	if len(pkgIDs) == 0 {
		return nil
	}

	err := ctx.Exec("UPDATE ObjectAccessStat AS o INNER JOIN Object AS obj ON o.ObjectID = obj.ObjectID SET o.Amount = o.Amount * ? + o.Counter * (1 - ?), o.Counter = 0 WHERE obj.PackageID IN ?", historyWeight, historyWeight, pkgIDs).Error
	return err
}

func (*ObjectAccessStatDB) UpdateAllAmount(ctx SQLContext, historyWeight float64) error {
	err := ctx.Exec("UPDATE ObjectAccessStat SET Amount = Amount * ? + Counter * (1 - ?), Counter = 0", historyWeight, historyWeight).Error
	return err
}

func (*ObjectAccessStatDB) DeleteByObjectID(ctx SQLContext, objID types.ObjectID) error {
	err := ctx.Table("ObjectAccessStat").Where("ObjectID = ?", objID).Delete(nil).Error
	return err
}

func (*ObjectAccessStatDB) BatchDeleteByObjectID(ctx SQLContext, objIDs []types.ObjectID) error {
	if len(objIDs) == 0 {
		return nil
	}

	err := ctx.Table("ObjectAccessStat").Where("ObjectID IN ?", objIDs).Delete(nil).Error
	return err
}

func (*ObjectAccessStatDB) DeleteInPackage(ctx SQLContext, packageID types.PackageID) error {
	err := ctx.Exec("DELETE o FROM ObjectAccessStat o INNER JOIN Object obj ON o.ObjectID = obj.ObjectID WHERE obj.PackageID = ?", packageID).Error
	return err
}
