package mock

import (
	"bytes"
	"context"
	"io"

	"github.com/alcionai/clues"

	"github.com/alcionai/corso/src/internal/streamstore"
	"github.com/alcionai/corso/src/pkg/backup/details"
	"github.com/alcionai/corso/src/pkg/backup/identity"
	"github.com/alcionai/corso/src/pkg/fault"
)

var _ streamstore.Streamer = &Streamer{}

type Streamer struct {
	Deets  map[string]*details.Details
	Errors map[string]*fault.Errors
}

func (ms Streamer) Collect(context.Context, streamstore.Collectable) error {
	return clues.New("not implemented")
}

func (ms Streamer) Read(
	ctx context.Context,
	snapshotID string,
	col streamstore.Collectable,
	errs *fault.Bus,
) error {
	ctx = clues.Add(
		ctx,
		"read_snapshot_id", snapshotID,
		"collectable_type", col.Type)

	var mr streamstore.Marshaller

	switch col.Type {
	case streamstore.DetailsType:
		mr = ms.Deets[snapshotID]
	case streamstore.FaultErrorsType:
		mr = ms.Errors[snapshotID]
	default:
		return clues.NewWC(ctx, "unknown type: "+col.Type)
	}

	if mr == nil {
		return clues.NewWC(ctx, "collectable "+col.Type+" has no marshaller")
	}

	bs, err := mr.Marshal()
	if err != nil {
		return err
	}

	return col.Unmr(io.NopCloser(bytes.NewReader(bs)))
}

func (ms Streamer) Write(
	context.Context,
	[]identity.Reasoner,
	*fault.Bus,
) (string, error) {
	return "", clues.New("not implemented")
}

func (ms Streamer) Delete(context.Context, string) error {
	return clues.New("not implemented")
}
