package main

import (
	"fmt"
	"os"

	bkosmi "github.com/42wim/matterbridge/bridge/kosmi"
	"github.com/sirupsen/logrus"
)

func main() {
	if len(os.Args) < 3 {
		fmt.Println("Usage: test-browser-auth <email> <password>")
		fmt.Println("")
		fmt.Println("This tests the browser-based authentication by:")
		fmt.Println("1. Launching headless Chrome")
		fmt.Println("2. Logging in to Kosmi")
		fmt.Println("3. Extracting the JWT token")
		fmt.Println("4. Parsing token expiry")
		os.Exit(1)
	}

	email := os.Args[1]
	password := os.Args[2]

	// Set up logging
	log := logrus.New()
	log.SetLevel(logrus.DebugLevel)
	entry := logrus.NewEntry(log)

	fmt.Println("🚀 Testing browser-based authentication...")
	fmt.Println()

	// Create browser auth manager
	browserAuth := bkosmi.NewBrowserAuthManager(email, password, entry)

	// Get token
	token, err := browserAuth.GetToken()
	if err != nil {
		fmt.Printf("❌ Authentication failed: %v\n", err)
		os.Exit(1)
	}

	fmt.Println()
	fmt.Println("✅ Authentication successful!")
	fmt.Println()
	fmt.Printf("Token (first 50 chars): %s...\n", token[:min(50, len(token))])
	fmt.Printf("Token length: %d characters\n", len(token))
	fmt.Println()

	// Check if authenticated
	if browserAuth.IsAuthenticated() {
		fmt.Println("✅ Token is valid")
	} else {
		fmt.Println("❌ Token is invalid or expired")
	}

	// Get user ID
	userID := browserAuth.GetUserID()
	if userID != "" {
		fmt.Printf("User ID: %s\n", userID)
	}

	fmt.Println()
	fmt.Println("🎉 Test completed successfully!")
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

