package stores

import (
	"fmt"
	"path/filepath"

	"unprism.ru/KRBL/agate/store"
	"unprism.ru/KRBL/agate/store/filesystem"
	"unprism.ru/KRBL/agate/store/sqlite"
)

// NewDefaultMetadataStore creates a new SQLite-based metadata store.
func NewDefaultMetadataStore(metadataDir string) (store.MetadataStore, error) {
	dbPath := filepath.Join(metadataDir, "snapshots.db")
	return sqlite.NewSQLiteStore(dbPath)
}

// NewDefaultBlobStore creates a new filesystem-based blob store.
func NewDefaultBlobStore(blobsDir string) (store.BlobStore, error) {
	return filesystem.NewFileSystemStore(blobsDir)
}

// InitDefaultStores initializes both metadata and blob stores with default implementations.
// Returns the initialized stores or an error if initialization fails.
func InitDefaultStores(baseDir string) (store.MetadataStore, store.BlobStore, error) {
	metadataDir := filepath.Join(baseDir, "metadata")
	blobsDir := filepath.Join(baseDir, "blobs")

	metadataStore, err := NewDefaultMetadataStore(metadataDir)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to initialize metadata store: %w", err)
	}

	blobStore, err := NewDefaultBlobStore(blobsDir)
	if err != nil {
		// Clean up if blob store initialization fails
		metadataStore.Close()
		return nil, nil, fmt.Errorf("failed to initialize blob store: %w", err)
	}

	return metadataStore, blobStore, nil
}