// Code generated by candi v1.3.1.

package usecase

import (
	"context"
	"io"
	"os"

	"monorepo/services/storage-service/internal/modules/storage/domain"
	"monorepo/services/storage-service/pkg/shared/repository"

	"github.com/minio/minio-go/v6"
	"pkg.agungdp.dev/candi/codebase/factory/dependency"
	"pkg.agungdp.dev/candi/codebase/interfaces"
	"pkg.agungdp.dev/candi/tracer"
)

type storageUsecaseImpl struct {
	cache interfaces.Cache

	repoMongo   *repository.RepoMongo
	minioClient *minio.Client
}

// NewStorageUsecase usecase impl constructor
func NewStorageUsecase(deps dependency.Dependency) StorageUsecase {
	return &storageUsecaseImpl{
		cache: deps.GetRedisPool().Cache(),

		repoMongo: repository.GetSharedRepoMongo(),
	}
}

func (uc *storageUsecaseImpl) Hello(ctx context.Context) (msg string) {
	trace := tracer.StartTrace(ctx, "StorageUsecase:Hello")
	defer trace.Finish()
	ctx = trace.Context()

	msg, _ = uc.repoMongo.StorageRepo.FindHello(ctx)
	return
}

func (uc *storageUsecaseImpl) Upload(ctx context.Context, file io.Reader, metadata *domain.UploadMetadata) (err error) {
	trace := tracer.StartTrace(ctx, "StorageUsecase:Upload")
	defer trace.Finish()
	ctx = trace.Context()

	// _, err = uc.minioClient.PutObjectWithContext(ctx, "tong", metadata.Filename, file, -1,
	// 	minio.PutObjectOptions{ContentType: metadata.ContentType})
	// if err != nil {
	// 	logger.LogE(err.Error())
	// 	return err
	// }

	fl, err := os.Create(metadata.Folder + metadata.Filename)
	if err != nil {
		return err
	}
	defer fl.Close()

	io.Copy(fl, file)

	// fmt.Println("Uploaded", " size: ", n, "Successfully.", "localhost:9000/tong/...")

	return
}
