package filestorage

import (
	"context"
	"errors"
	"io"
	"os"
	"strings"
)

var ErrNotFound = errors.New("file storage object not found")

type Object struct {
	Body        io.ReadCloser
	ContentType string
	Size        int64
}

type Storage interface {
	Provider() string
	Probe(ctx context.Context) error
	Put(ctx context.Context, key string, reader io.Reader, contentType string, size int64) error
	Get(ctx context.Context, key string) (Object, error)
	Delete(ctx context.Context, key string) error
}

func NewFromEnv() (Storage, error) {
	provider := strings.ToLower(strings.TrimSpace(os.Getenv("FILE_STORAGE_PROVIDER")))
	if provider == "" || provider == "local" {
		root := strings.TrimSpace(os.Getenv("FILE_STORAGE_DIR"))
		if root == "" {
			root = "./data/uploads"
		}
		return NewLocal(root), nil
	}
	if provider == "s3" {
		return NewS3FromEnv()
	}
	return nil, errors.New("unsupported file storage provider")
}
