package main

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

// krokiBaseURL is the Kroki instance to use. Override with the -kroki flag.
// Self-host: docker run -d -p 8000:8000 yuzutech/kroki
var krokiBaseURL = "https://kroki.io"

// krokiClient is a shared HTTP client with a short timeout so a dead Kroki
// backend fails fast instead of hanging for ~60 s per diagram.
var krokiClient = &http.Client{Timeout: 10 * time.Second}

// RenderDiagramViaKroki converts a diagram source (Mermaid, PlantUML, etc.)
// to a PNG image via a Kroki rendering service and caches the result in the
// OS temp directory. The base URL is controlled by krokiBaseURL (-kroki flag).
//
// Cache key: SHA-256 of the diagram source — unchanged diagrams are not re-fetched.
// 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("%s/%s/png/%s", krokiBaseURL, 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 := krokiClient.Get(url) //nolint:gosec
	if err != nil {
		return "", fmt.Errorf("kroki request failed (is %s reachable?): %w", krokiBaseURL, err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("kroki returned HTTP %d — try a local instance: docker run -d -p 8000:8000 yuzutech/kroki", 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 {
		_ = os.Remove(cachePath) // remove incomplete cache file
		return "", fmt.Errorf("cache file write: %w", err)
	}
	return cachePath, nil
}
