package snapshotfs

import (
	"context"

	"github.com/kopia/kopia/fs"
	"github.com/kopia/kopia/snapshot"
	"github.com/kopia/kopia/snapshot/policy"
)

type scanResults struct {
	numFiles      int
	totalFileSize int64
}

func (e *scanResults) Error(ctx context.Context, filename string, err error, isIgnored bool) {}

func (e *scanResults) Processing(ctx context.Context, pathname string) {}

func (e *scanResults) Stats(ctx context.Context, s *snapshot.Stats, includedFiles, excludedFiles SampleBuckets, excludedDirs []string, final bool) {
	if final {
		e.numFiles = int(s.TotalFileCount)
		e.totalFileSize = s.TotalFileSize
	}
}

var _ EstimateProgress = (*scanResults)(nil)

// scanDirectory computes the number of files and their total size in a given directory recursively descending
// into subdirectories. The scan teminates early as soon as the provided context is canceled.
func (u *Uploader) scanDirectory(ctx context.Context, dir fs.Directory, policyTree *policy.Tree) (scanResults, error) {
	var res scanResults

	if u.disableEstimation {
		return res, nil
	}

	err := Estimate(ctx, u.repo, dir, policyTree, &res, 1)

	return res, err
}
