//go:build ignore

// Quick SMTP diagnostic — run with:
//   SMTP_USER=dharnaud77@gmail.com SMTP_PASS=xsmtpsib-... go run test_smtp.go
package main

import (
	"crypto/tls"
	"fmt"
	"net"
	"net/smtp"
	"os"
)

func main() {
	host := "smtp-relay.brevo.com"
	port := 587
	user := os.Getenv("SMTP_USER")
	pass := os.Getenv("SMTP_PASS")
	if user == "" || pass == "" {
		fmt.Fprintln(os.Stderr, "Usage: SMTP_USER=... SMTP_PASS=... go run test_smtp.go")
		os.Exit(1)
	}

	addr := fmt.Sprintf("%s:%d", host, port)
	fmt.Printf("Dialing %s ...\n", addr)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		fmt.Printf("FAIL dial: %v\n", err)
		os.Exit(1)
	}

	client, err := smtp.NewClient(conn, host)
	if err != nil {
		fmt.Printf("FAIL smtp.NewClient: %v\n", err)
		os.Exit(1)
	}
	defer client.Close() //nolint:errcheck

	if ok, _ := client.Extension("STARTTLS"); ok {
		fmt.Println("OK   STARTTLS advertised")
		if err = client.StartTLS(&tls.Config{ServerName: host}); err != nil {
			fmt.Printf("FAIL StartTLS: %v\n", err)
			os.Exit(1)
		}
		fmt.Println("OK   STARTTLS negotiated")
	} else {
		fmt.Println("WARN STARTTLS not advertised — credentials will be sent in clear")
	}

	if ok, params := client.Extension("AUTH"); ok {
		fmt.Printf("OK   AUTH methods advertised: %s\n", params)
	} else {
		fmt.Println("WARN no AUTH advertised in EHLO")
	}

	auth := smtp.PlainAuth("", user, pass, host)
	if err = client.Auth(auth); err != nil {
		fmt.Printf("FAIL AUTH PLAIN: %v\n\n", err)
		fmt.Println("→ Le SMTP key est invalide ou révoqué.")
		fmt.Println("  Génère-en un nouveau sur app.brevo.com → Settings → SMTP & API → SMTP Keys")
		os.Exit(1)
	}
	fmt.Println("OK   AUTH PLAIN success — credentials valides !")

	_ = client.Quit()
}
