package storage

import (
	"fmt"
	"net/url"

	"github.com/emersion/go-webdav/carddav"
)

func NewFromURL(storageURL string) (carddav.Backend, error) {
	u, err := url.Parse(storageURL)
	if err != nil {
		return nil, fmt.Errorf("error parsing storage URL: %s", err.Error())
	}

	switch u.Scheme {
	case "file":
		return NewFilesystem(u.Path)
	case "postgresql":
		return NewPostgreSQL(), nil
	default:
		return nil, fmt.Errorf("no storage provider found for %s:// URL", u.Scheme)
	}
}