package db

import (
	"time"

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

type PinnedObjectDB struct {
	*DB
}

func (db *DB) PinnedObject() *PinnedObjectDB {
	return &PinnedObjectDB{DB: db}
}

func (*PinnedObjectDB) GetByUserSpaceID(ctx SQLContext, spaceID types.UserSpaceID) ([]types.PinnedObject, error) {
	var ret []types.PinnedObject
	err := ctx.Table("PinnedObject").Find(&ret, "UserSpaceID = ?", spaceID).Error
	return ret, err
}

func (*PinnedObjectDB) GetObjectsByUserSpaceID(ctx SQLContext, spaceID types.UserSpaceID) ([]types.Object, error) {
	var ret []types.Object
	err := ctx.Table("Object").Joins("inner join PinnedObject on Object.ObjectID = PinnedObject.ObjectID").Where("UserSpaceID = ?", spaceID).Find(&ret).Error
	return ret, err
}

func (*PinnedObjectDB) Create(ctx SQLContext, spaceID types.UserSpaceID, objectID types.ObjectID, createTime time.Time) error {
	return ctx.Table("PinnedObject").Create(&types.PinnedObject{UserSpaceID: spaceID, ObjectID: objectID, CreateTime: createTime}).Error
}

func (*PinnedObjectDB) BatchGetByObjectID(ctx SQLContext, objectIDs []types.ObjectID) ([]types.PinnedObject, error) {
	if len(objectIDs) == 0 {
		return nil, nil
	}

	var pinneds []types.PinnedObject
	err := ctx.Table("PinnedObject").Where("ObjectID in (?)", objectIDs).Order("ObjectID asc").Find(&pinneds).Error
	return pinneds, err
}

func (*PinnedObjectDB) TryCreate(ctx SQLContext, spaceID types.UserSpaceID, objectID types.ObjectID, createTime time.Time) error {
	return ctx.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "ObjectID"}, {Name: "UserSpaceID"}},
		DoUpdates: clause.AssignmentColumns([]string{"CreateTime"}),
	}).Create(&types.PinnedObject{UserSpaceID: spaceID, ObjectID: objectID, CreateTime: createTime}).Error
}

func (*PinnedObjectDB) BatchTryCreate(ctx SQLContext, pinneds []types.PinnedObject) error {
	if len(pinneds) == 0 {
		return nil
	}

	return ctx.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "ObjectID"}, {Name: "UserSpaceID"}},
		DoUpdates: clause.AssignmentColumns([]string{"CreateTime"}),
	}).Create(&pinneds).Error
}

func (*PinnedObjectDB) CreateFromPackage(ctx SQLContext, packageID types.PackageID, spaceID types.UserSpaceID) error {
	err := ctx.Exec(
		"insert ignore into PinnedObject(UserSpaceID, ObjectID, CreateTime) select ? as UserSpaceID, ObjectID, ? as CreateTime from Object where PackageID = ?",
		spaceID,
		time.Now(),
		packageID,
	).Error
	return err
}

func (db *PinnedObjectDB) ObjectBatchCreate(ctx SQLContext, objectID types.ObjectID, spaceIDs []types.UserSpaceID) error {
	if len(spaceIDs) == 0 {
		return nil
	}

	for _, id := range spaceIDs {
		err := db.TryCreate(ctx, id, objectID, time.Now())
		if err != nil {
			return err
		}
	}
	return nil
}

func (*PinnedObjectDB) Delete(ctx SQLContext, spaceID types.UserSpaceID, objectID types.ObjectID) error {
	err := ctx.Exec("delete from PinnedObject where UserSpaceID = ? and ObjectID = ?", spaceID, objectID).Error
	return err
}

func (*PinnedObjectDB) DeleteByObjectID(ctx SQLContext, objectID types.ObjectID) error {
	err := ctx.Exec("delete from PinnedObject where ObjectID = ?", objectID).Error
	return err
}

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

	err := ctx.Table("PinnedObject").Where("ObjectID in (?)", objectIDs).Delete(&types.PinnedObject{}).Error
	return err
}

func (*PinnedObjectDB) DeleteInPackage(ctx SQLContext, packageID types.PackageID) error {
	err := ctx.Table("PinnedObject").Where("ObjectID in (select ObjectID from Object where PackageID = ?)", packageID).Delete(&types.PinnedObject{}).Error
	return err
}

func (*PinnedObjectDB) DeleteInPackageAtStorage(ctx SQLContext, packageID types.PackageID, spaceID types.UserSpaceID) error {
	err := ctx.Exec("delete PinnedObject from PinnedObject inner join Object on PinnedObject.ObjectID = Object.ObjectID where PackageID = ? and UserSpaceID = ?", packageID, spaceID).Error
	return err
}

func (*PinnedObjectDB) BatchDelete(ctx SQLContext, spaceID types.UserSpaceID, objectIDs []types.ObjectID) error {
	if len(objectIDs) == 0 {
		return nil
	}

	err := ctx.Table("PinnedObject").Where("UserSpaceID = ? and ObjectID in (?)", spaceID, objectIDs).Delete(&types.PinnedObject{}).Error
	return err
}
