// sentry-lark-send reads a plain-text message from stdin and posts it to a
// Lark custom-robot webhook using sentry's own notify.LarkClient (which
// includes HMAC-SHA256 signing and retry). Useful for one-off tests and
// for piping the sentry-probe report to an ops channel.
//
// Env:
//
//	LARK_WEBHOOK_URL      (required)
//	LARK_SIGNING_SECRET   (optional — omit if the bot was configured without signing)
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"os"
	"time"

	"github.com/nexuscrm/sentry/internal/notify"
)

func main() {
	url := os.Getenv("LARK_WEBHOOK_URL")
	if url == "" {
		log.Fatal("LARK_WEBHOOK_URL is required")
	}
	secret := os.Getenv("LARK_SIGNING_SECRET")

	body, err := io.ReadAll(os.Stdin)
	if err != nil {
		log.Fatalf("read stdin: %v", err)
	}
	if len(body) == 0 {
		log.Fatal("stdin is empty")
	}

	client := notify.NewLarkClient(url, secret, 15*time.Second)
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	if err := client.SendText(ctx, string(body)); err != nil {
		log.Fatalf("send: %v", err)
	}
	fmt.Println("OK — message delivered to Lark webhook")
}
