package storage

import (
	"github.com/mitchellh/mapstructure"
	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger"
	"github.com/tech/sendico/pkg/mservice"
	"github.com/tech/sendico/server/interface/api"
	fsc "github.com/tech/sendico/server/interface/services/fileservice/config"
	"github.com/tech/sendico/server/internal/server/fileserviceimp/storage/config"
)

func Create(logger mlogger.Logger, a api.API, service mservice.Type, directory, subDir string) (FileManager, error) {
	if a.Config().Storage.Driver == fsc.LocalFS {
		var conf config.LocalFSSConfig
		if err := mapstructure.Decode(a.Config().Storage.Settings, &conf); err != nil {
			return nil, err
		}
		return CreateLocalFileStorage(logger, service, directory, subDir, a.DomainProvider(), conf)
	}
	if a.Config().Storage.Driver == fsc.AwsS3 {
		var conf config.AWSS3SConfig
		if err := mapstructure.Decode(a.Config().Storage.Settings, &conf); err != nil {
			return nil, err
		}
		return CreateAWSS3Storage(logger, service, directory, conf)
	}
	return nil, merrors.Internal("Unknown storage driver: " + string(a.Config().Storage.Driver))
}
