package filesengine

import (
	"context"
	"fmt"

	"git.optclblast.xyz/draincloud/draincloud-core/internal/storage"
	"git.optclblast.xyz/draincloud/draincloud-core/internal/storage/models/files"
	// "git.optclblast.xyz/draincloud/draincloud-core/internal/storage/models/files"
	"github.com/google/uuid"
)

type FilesEngine struct {
	blobStorage storage.BlobStorage
	metaStorage storage.MetaStorage
}

func NewFilesEngine(
	blobStorage storage.BlobStorage,
	metaStorage storage.MetaStorage,
) *FilesEngine {
	return &FilesEngine{
		blobStorage: blobStorage,
		metaStorage: metaStorage,
	}
}

type File struct {
	Name   string
	UserID int64
	Ext    string
	Type   string
	Size   int64
	Data   []byte
}

// TODO save file
func (e *FilesEngine) SaveFile(
	ctx context.Context,
	file File,
) (uuid.UUID, error) {
	fileID, err := e.metaStorage.SaveMetadata(ctx, files.FileMetadata{
		Name:   file.Name,
		UserID: file.UserID,
		Ext:    file.Ext,
		Type:   file.Type,
		// FSLink: f,
	})
	if err != nil {
		return uuid.Nil, fmt.Errorf("failed to create new file metadata: %w", err)
	}

	if err = e.blobStorage.SaveBlob(ctx, fileID, file.Data); err != nil {
		return uuid.Nil, fmt.Errorf("failed to save file data: %w", err)
	}

	return fileID, nil
}
