package debug

import (
	"strings"

	"gitea.maximumdirect.net/eric/notarius/internal/core/fileio"
	"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)

type FilesystemRecorder struct {
	root string
}

func NewFilesystemRecorder(root string) (pipeline.DebugRecorder, error) {
	root = strings.TrimSpace(root)
	if strings.TrimSpace(root) == "" {
		return pipeline.NoopDebugRecorder(), nil
	}
	return &FilesystemRecorder{root: root}, nil
}

func (r *FilesystemRecorder) Enabled() bool {
	return r != nil && strings.TrimSpace(r.root) != ""
}

func (r *FilesystemRecorder) WriteJSON(name string, payload any) error {
	if !r.Enabled() {
		return nil
	}
	return fileio.WriteJSON(r.root, name, payload, 0o700, 0o600)
}

func (r *FilesystemRecorder) WriteBytes(name string, data []byte) error {
	if !r.Enabled() {
		return nil
	}
	return fileio.WriteBytes(r.root, name, data, 0o700, 0o600)
}
