package drivercontract

import (
	"fmt"

	"github.com/tidbcloud/AgentTeam/desktop/internal/promptcompiler"
)

// RequireCompiledPrompt enforces the runtime boundary: semantic launch text is
// usable only when promptcompiler produced a valid complete launch carrier.
func (c LaunchConfig) RequireCompiledPrompt() (*promptcompiler.CompiledPrompt, error) {
	if c.CompiledPrompt == nil {
		// Lifecycle-only adapter operations (for example Codex resume probes)
		// have no semantic launch text. Keep the carrier compiler-created rather
		// than restoring a mutable string fallback.
		carrier, err := promptcompiler.CompileEmptyLaunchPrompt(nil, string(c.RuntimeType))
		if err != nil {
			return nil, err
		}
		return &carrier, nil
	}
	if !c.CompiledPrompt.HasLaunchCarrier() {
		return nil, fmt.Errorf("launch config requires a compiler-produced complete prompt carrier")
	}
	return c.CompiledPrompt, nil
}

// CompiledEnvelope returns the compiler envelope for attribution or transport
// serialization without exposing mutable surface text.
func (c LaunchConfig) CompiledEnvelope() (promptcompiler.RuntimeInputEnvelope, bool) {
	if c.CompiledPrompt == nil || !c.CompiledPrompt.Valid() {
		return promptcompiler.RuntimeInputEnvelope{}, false
	}
	return c.CompiledPrompt.Envelope(), true
}
