package llm

import (
	"context"
	"fmt"
)

// Router calls Primary first; on any error falls back to Fallback.
// Fallback may be nil, in which case primary errors are returned directly.
type Router struct {
	Primary  *Client
	Fallback *Client
}

// Complete routes through Primary then Fallback.
func (r *Router) Complete(ctx context.Context, system, user string) (string, error) {
	out, err := r.Primary.Complete(ctx, system, user)
	if err == nil {
		return out, nil
	}
	if r.Fallback == nil {
		return "", fmt.Errorf("primary llm: %w", err)
	}
	out, err2 := r.Fallback.Complete(ctx, system, user)
	if err2 != nil {
		return "", fmt.Errorf("primary llm: %w; fallback llm: %v", err, err2)
	}
	return out, nil
}
