package daemon

import (
	"context"
	"io"
	"os"
	"time"

	"banger/internal/guest"
)

// guestSSHClient is the narrow guest-SSH surface the daemon uses for
// workspace prepare / export and ad-hoc guest interactions.
type guestSSHClient interface {
	Close() error
	RunScript(context.Context, string, io.Writer) error
	RunScriptOutput(context.Context, string) ([]byte, error)
	UploadFile(context.Context, string, os.FileMode, []byte, io.Writer) error
	StreamTar(context.Context, string, string, io.Writer) error
	StreamTarEntries(context.Context, string, []string, string, io.Writer) error
}

func (d *Daemon) waitForGuestSSH(ctx context.Context, address string, interval time.Duration) error {
	if d != nil && d.guestWaitForSSH != nil {
		return d.guestWaitForSSH(ctx, address, d.config.SSHKeyPath, interval)
	}
	return guest.WaitForSSH(ctx, address, d.config.SSHKeyPath, d.layout.KnownHostsPath, interval)
}

func (d *Daemon) dialGuest(ctx context.Context, address string) (guestSSHClient, error) {
	if d != nil && d.guestDial != nil {
		return d.guestDial(ctx, address, d.config.SSHKeyPath)
	}
	return guest.Dial(ctx, address, d.config.SSHKeyPath, d.layout.KnownHostsPath)
}
