package status

import (
	"strings"
	"sync"
)

// TailBuffer holds a bounded buffer of terminal output.
type TailBuffer struct {
	mu  sync.Mutex
	buf strings.Builder
	max int
}

// NewTailBuffer creates a new TailBuffer with the given maximum capacity in bytes.
func NewTailBuffer(max int) *TailBuffer {
	return &TailBuffer{max: max}
}

// Append appends a string to the buffer, truncating from the front if it exceeds capacity.
func (tb *TailBuffer) Append(s string) {
	tb.mu.Lock()
	defer tb.mu.Unlock()
	tb.buf.WriteString(s)
	raw := tb.buf.String()
	if len(raw) <= tb.max {
		return
	}
	tb.buf.Reset()
	tb.buf.WriteString(raw[len(raw)-tb.max:])
}

// String returns the contents of the buffer.
func (tb *TailBuffer) String() string {
	tb.mu.Lock()
	defer tb.mu.Unlock()
	return tb.buf.String()
}

// Snapshot holds a snapshot of a terminal session.
type Snapshot struct {
	Tail string
}
