package services

import (
	"time"

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

// BucketService 是对存储桶进行操作的服务类
type BucketService struct {
	*Service
}

// BucketSvc 创建并返回一个BucketService实例
func (svc *Service) BucketSvc() *BucketService {
	return &BucketService{Service: svc}
}

// GetBucket 根据用户ID和桶ID获取桶信息
// userID: 用户的唯一标识
// bucketID: 桶的唯一标识
// 返回值: 桶的信息和可能发生的错误
func (svc *BucketService) GetBucket(bucketID types.BucketID) (types.Bucket, error) {
	return svc.DB.Bucket().GetByID(svc.DB.DefCtx(), bucketID)
}

func (svc *BucketService) GetBucketByName(bucketName string) (types.Bucket, error) {
	return svc.DB.Bucket().GetByName(svc.DB.DefCtx(), bucketName)
}

func (svc *BucketService) ListAllBuckets() ([]types.Bucket, error) {
	return svc.DB.Bucket().GetAll(svc.DB.DefCtx())
}

// GetBucketPackages 获取指定用户和桶的所有包
// userID: 用户的唯一标识
// bucketID: 桶的唯一标识
// 返回值: 桶的所有包列表和可能发生的错误
func (svc *BucketService) GetBucketPackages(bucketID types.BucketID) ([]types.Package, error) {
	return svc.DB.Package().GetBucketPackages(svc.DB.DefCtx(), bucketID)
}

func (svc *BucketService) CreateBucket(bucketName string, createTime time.Time) (types.Bucket, error) {
	return svc.DB.Bucket().Create(svc.DB.DefCtx(), bucketName, createTime)
}

// DeleteBucket 删除指定的桶
// userID: 用户的唯一标识
// bucketID: 桶的唯一标识
// 返回值: 可能发生的错误
func (svc *BucketService) DeleteBucket(bucketID types.BucketID) error {
	return svc.DB.Bucket().DeleteComplete(svc.DB.DefCtx(), bucketID)
}
