package storage

import (
	"fmt"
	"os"
	"path/filepath"
)

// ListBlobDigests scans the layers directory and returns all blob digests
// found on disk. Used by GC reconciliation to find orphaned files.
func (s *Store) ListBlobDigests() ([]string, error) {
	sha256Dir := filepath.Join(s.layersPath, "sha256")
	prefixEntries, err := os.ReadDir(sha256Dir)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("storage: list prefix dirs: %w", err)
	}

	var digests []string
	for _, prefix := range prefixEntries {
		if !prefix.IsDir() || len(prefix.Name()) != 2 {
			continue
		}
		prefixPath := filepath.Join(sha256Dir, prefix.Name())
		blobEntries, err := os.ReadDir(prefixPath)
		if err != nil {
			continue
		}
		for _, blob := range blobEntries {
			if blob.IsDir() {
				continue
			}
			digest := "sha256:" + blob.Name()
			if validateDigest(digest) == nil {
				digests = append(digests, digest)
			}
		}
	}

	return digests, nil
}
