package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("Usage: test-profile-query <jwt-token>")
		os.Exit(1)
	}

	token := os.Args[1]
	
	// Try different queries to get user profile
	queries := []struct{
		name string
		query string
	}{
		{
			name: "me query",
			query: `query { me { id displayName username email avatarUrl } }`,
		},
		{
			name: "currentUser query",
			query: `query { currentUser { id displayName username email avatarUrl } }`,
		},
		{
			name: "user query",
			query: `query { user { id displayName username email avatarUrl } }`,
		},
		{
			name: "viewer query",
			query: `query { viewer { id displayName username email avatarUrl } }`,
		},
	}
	
	for _, q := range queries {
		fmt.Printf("\n=== Testing: %s ===\n", q.name)
		testQuery(token, q.query)
	}
}

func testQuery(token, query string) {
	payload := map[string]interface{}{
		"query": query,
	}
	
	payloadBytes, err := json.Marshal(payload)
	if err != nil {
		fmt.Printf("Failed to marshal query: %v\n", err)
		return
	}
	
	req, err := http.NewRequest("POST", "https://engine.kosmi.io/", bytes.NewBuffer(payloadBytes))
	if err != nil {
		fmt.Printf("Failed to create request: %v\n", err)
		return
	}
	
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
	
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("Request failed: %v\n", err)
		return
	}
	defer resp.Body.Close()
	
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Failed to read response: %v\n", err)
		return
	}
	
	fmt.Printf("Status: %d\n", resp.StatusCode)
	
	// Pretty print JSON
	var result map[string]interface{}
	if err := json.Unmarshal(body, &result); err != nil {
		fmt.Printf("Response: %s\n", string(body))
	} else {
		prettyJSON, _ := json.MarshalIndent(result, "", "  ")
		fmt.Printf("Response:\n%s\n", string(prettyJSON))
	}
}

