package main

import (
	"bytes"
	"compress/zlib"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

// RenderDiagramViaKroki converts a diagram source (Mermaid, PlantUML, etc.)
// to a PNG image using the Kroki.io public rendering service and caches the
// result in the OS temp directory.
//
// The cache key is the SHA-256 hash of the diagram source, so unchanged
// diagrams are not re-fetched between runs.
//
// Supported languages: "mermaid", "plantuml" / "puml"
func RenderDiagramViaKroki(lang, code string) (string, error) {
	if lang == "puml" {
		lang = "plantuml"
	}

	// Kroki encoding: deflate (zlib) then base64url
	var buf bytes.Buffer
	w := zlib.NewWriter(&buf)
	if _, err := w.Write([]byte(code)); err != nil {
		return "", fmt.Errorf("zlib write: %w", err)
	}
	if err := w.Close(); err != nil {
		return "", fmt.Errorf("zlib close: %w", err)
	}
	encoded := base64.URLEncoding.EncodeToString(buf.Bytes())
	url := fmt.Sprintf("https://kroki.io/%s/png/%s", lang, encoded)

	// Deterministic cache path based on content hash
	hash := fmt.Sprintf("%x", sha256.Sum256([]byte(code)))
	cachePath := filepath.Join(os.TempDir(), "ihk_kroki_"+hash+".png")

	if _, err := os.Stat(cachePath); err == nil {
		return cachePath, nil // cache hit
	}

	resp, err := http.Get(url) //nolint:gosec // URL is constructed from user content, acceptable here
	if err != nil {
		return "", fmt.Errorf("kroki request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("kroki returned HTTP %d", resp.StatusCode)
	}

	out, err := os.Create(cachePath)
	if err != nil {
		return "", fmt.Errorf("cache file create: %w", err)
	}
	defer out.Close()

	if _, err = io.Copy(out, resp.Body); err != nil {
		return "", fmt.Errorf("cache file write: %w", err)
	}
	return cachePath, nil
}
