package generator

import (
	"bytes"
	"fmt"
	"os"
	"text/template"
)

type PromptContext struct {
	ScenarioYAML string
	// Constraints removed in S51 — was always rendered as the empty
	// string at the call sites and the scenario YAML no longer
	// carries a constraints map. Per-criterion params are visible
	// inline in AcceptanceCriteria.
	ResolvedMappings   string
	Overrides          string
	ArchitecturePlan   string
	AcceptanceCriteria string
	GeneratedFiles     string
	FeedbackJSON       string
	ProviderSchema     string
	Layer3Guidance     string
	Pitfalls           string
}

func RenderPromptTemplate(phase string, templateBody string, ctx PromptContext) (string, error) {
	tmpl, err := template.New(phase).Option("missingkey=error").Parse(templateBody)
	if err != nil {
		return "", NewGenerateError(ErrPromptRenderFailed, phase, fmt.Errorf("parse template: %w", err))
	}

	var out bytes.Buffer
	if err := tmpl.Execute(&out, ctx); err != nil {
		return "", NewGenerateError(ErrPromptRenderFailed, phase, fmt.Errorf("execute template: %w", err))
	}

	return out.String(), nil
}

func RenderPromptFile(phase string, templatePath string, ctx PromptContext) (string, error) {
	payload, err := os.ReadFile(templatePath)
	if err != nil {
		return "", NewGenerateError(ErrPromptRenderFailed, phase, fmt.Errorf("read template %q: %w", templatePath, err))
	}

	return RenderPromptTemplate(phase, string(payload), ctx)
}
