// monorepo/cloud/maplefile-backend/internal/maplefile/repo/filemetadata/get_by_owner_id.go
package filemetadata

import (
	"fmt"

	"github.com/gocql/gocql"
	dom_file "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/file"
)

func (impl *fileMetadataRepositoryImpl) GetByOwnerID(ownerID gocql.UUID) ([]*dom_file.File, error) {
	var fileIDs []gocql.UUID

	query := `SELECT id FROM maplefile.files_by_owner
		WHERE owner_id = ?`

	iter := impl.Session.Query(query, ownerID).Iter()

	var fileID gocql.UUID
	for iter.Scan(&fileID) {
		fileIDs = append(fileIDs, fileID)
	}

	if err := iter.Close(); err != nil {
		return nil, fmt.Errorf("failed to get files by owner: %w", err)
	}

	return impl.loadMultipleFiles(fileIDs)
}
