// PUBLIC DOMAIN - NO LICENSE, NO WARRANTY
// Copyright 2025 TimeHexOn & foxhop & russell@unturf
// https://www.permacomputer.com

// UncloseAI Go Library - Usage Examples
//
// Demonstrates how to use the UncloseAI library for:
// - Model discovery
// - Non-streaming chat completions
// - Streaming chat completions
// - Text-to-speech generation

package main

import (
	"context"
	"fmt"
	"os"
	"strings"

	"uncloseai.com/uncloseai"
)

func main() {
	fmt.Println(strings.Repeat("=", 60))
	fmt.Println("UncloseAI Go Library - Examples")
	fmt.Println(strings.Repeat("=", 60))
	fmt.Println()

	ctx := context.Background()

	// Run examples
	if err := exampleModelDiscovery(ctx); err != nil {
		fmt.Printf("\nFatal error: %v\n", err)
		fmt.Println("\nMake sure environment variables are set:")
		fmt.Println("  MODEL_ENDPOINT_1=https://your-endpoint/v1")
		fmt.Println("  TTS_ENDPOINT_1=https://your-tts-endpoint/v1")
		os.Exit(1)
	}

	exampleChat(ctx)
	exampleChatStreaming(ctx)
	exampleChatStreamingWithContext(ctx)
	exampleMultipleModels(ctx)
	exampleTTS(ctx)
	exampleErrorHandling(ctx)

	fmt.Println(strings.Repeat("=", 60))
	fmt.Println("All examples completed successfully!")
	fmt.Println(strings.Repeat("=", 60))
}

// Example: Discover available models
func exampleModelDiscovery(ctx context.Context) error {
	fmt.Println("=== Model Discovery Example ===\n")

	// Initialize client (auto-discovers from environment variables)
	client, err := uncloseai.New(&uncloseai.Config{
		Debug: true,
	})
	if err != nil {
		return err
	}

	// List discovered models
	models := client.ListModels()
	fmt.Printf("\nDiscovered %d model(s):\n", len(models))
	for _, model := range models {
		fmt.Printf("  - %s\n", model.ID)
		fmt.Printf("    Endpoint: %s\n", model.Endpoint)
		fmt.Printf("    Max tokens: %d\n", model.MaxTokens)
	}
	fmt.Println()

	return nil
}

// Example: Non-streaming chat completion
func exampleChat(ctx context.Context) {
	fmt.Println("=== Non-Streaming Chat Example ===\n")

	client, _ := uncloseai.New(nil)

	response, err := client.Chat(ctx, []uncloseai.Message{
		{Role: "system", Content: "You are a helpful AI assistant."},
		{Role: "user", Content: "Explain quantum computing in one sentence."},
	}, &uncloseai.ChatOptions{
		MaxTokens: 100,
	})

	if err != nil {
		fmt.Printf("Error: %v\n\n", err)
		return
	}

	// Extract and print the response
	content := response.Choices[0].Message.Content
	fmt.Printf("Assistant: %s\n\n", content)
}

// Example: Streaming chat completion
func exampleChatStreaming(ctx context.Context) {
	fmt.Println("=== Streaming Chat Example ===\n")

	client, _ := uncloseai.New(nil)

	fmt.Println("User: Write a short haiku about programming.\n")
	fmt.Print("Assistant: ")

	chunkChan, errChan := client.ChatStream(ctx, []uncloseai.Message{
		{Role: "system", Content: "You are a poetic AI that writes haikus."},
		{Role: "user", Content: "Write a short haiku about programming."},
	}, &uncloseai.ChatOptions{
		MaxTokens: 100,
	})

	// Process streaming chunks
	for {
		select {
		case chunk, ok := <-chunkChan:
			if !ok {
				goto done
			}
			if len(chunk.Choices) > 0 {
				content := chunk.Choices[0].Delta.Content
				if content != "" {
					fmt.Print(content)
				}
			}
		case err := <-errChan:
			if err != nil {
				fmt.Printf("\nError: %v\n", err)
			}
			goto done
		}
	}

done:
	fmt.Println("\n")
}

// Example: Streaming chat with conversation context
func exampleChatStreamingWithContext(ctx context.Context) {
	fmt.Println("=== Streaming Chat with Context ===\n")

	client, _ := uncloseai.New(nil)

	// Simulated conversation
	messages := []uncloseai.Message{
		{Role: "system", Content: "You are a helpful coding assistant."},
		{Role: "user", Content: "What is Go used for?"},
	}

	fmt.Println("User: What is Go used for?\n")
	fmt.Print("Assistant: ")

	// First response
	var fullResponse strings.Builder
	chunkChan, errChan := client.ChatStream(ctx, messages, &uncloseai.ChatOptions{
		MaxTokens: 150,
	})

	for {
		select {
		case chunk, ok := <-chunkChan:
			if !ok {
				goto firstDone
			}
			if len(chunk.Choices) > 0 {
				content := chunk.Choices[0].Delta.Content
				if content != "" {
					fullResponse.WriteString(content)
					fmt.Print(content)
				}
			}
		case err := <-errChan:
			if err != nil {
				fmt.Printf("\nError: %v\n", err)
			}
			goto firstDone
		}
	}

firstDone:
	fmt.Println("\n")

	// Add assistant response to context
	messages = append(messages, uncloseai.Message{
		Role:    "assistant",
		Content: fullResponse.String(),
	})
	messages = append(messages, uncloseai.Message{
		Role:    "user",
		Content: "Can you give me a simple example?",
	})

	fmt.Println("User: Can you give me a simple example?\n")
	fmt.Print("Assistant: ")

	// Second response with context
	chunkChan, errChan = client.ChatStream(ctx, messages, &uncloseai.ChatOptions{
		MaxTokens: 200,
	})

	for {
		select {
		case chunk, ok := <-chunkChan:
			if !ok {
				goto secondDone
			}
			if len(chunk.Choices) > 0 {
				content := chunk.Choices[0].Delta.Content
				if content != "" {
					fmt.Print(content)
				}
			}
		case err := <-errChan:
			if err != nil {
				fmt.Printf("\nError: %v\n", err)
			}
			goto secondDone
		}
	}

secondDone:
	fmt.Println("\n")
}

// Example: Text-to-speech generation
func exampleTTS(ctx context.Context) {
	fmt.Println("=== Text-to-Speech Example ===\n")

	client, _ := uncloseai.New(nil)

	// Generate speech
	audioData, err := client.TTS(
		ctx,
		"Hello from UncloseAI Go library! This demonstrates text to speech generation.",
		"alloy", // Options: alloy, echo, fable, onyx, nova, shimmer
		"tts-1",
	)

	if err != nil {
		fmt.Printf("[ERROR] TTS Error: %v\n\n", err)
		return
	}

	// Save to file
	if err := os.WriteFile("speech.mp3", audioData, 0644); err != nil {
		fmt.Printf("[ERROR] Failed to write file: %v\n\n", err)
		return
	}

	fmt.Printf("[OK] Speech generated: speech.mp3 (%d bytes)\n\n", len(audioData))
}

// Example: Using different models for different tasks
func exampleMultipleModels(ctx context.Context) {
	fmt.Println("=== Multiple Models Example ===\n")

	client, _ := uncloseai.New(nil)

	models := client.ListModels()
	if len(models) < 2 {
		fmt.Println("Note: Only one model available, using it for both examples\n")
	}

	// Use first model for general chat
	fmt.Println("Using first model for general question:")
	response1, err := client.Chat(ctx, []uncloseai.Message{
		{Role: "user", Content: "What is AI?"},
	}, &uncloseai.ChatOptions{
		Model:     models[0].ID,
		MaxTokens: 50,
	})

	if err != nil {
		fmt.Printf("  Error: %v\n", err)
	} else {
		fmt.Printf("  %s\n\n", response1.Choices[0].Message.Content)
	}

	// Use second model (or first if only one available) for coding
	modelIdx := 0
	if len(models) > 1 {
		modelIdx = 1
	}
	modelName := "first"
	if modelIdx == 1 {
		modelName = "second"
	}

	fmt.Printf("Using %s model for coding question:\n", modelName)
	response2, err := client.Chat(ctx, []uncloseai.Message{
		{Role: "system", Content: "You are a coding expert."},
		{Role: "user", Content: "Write a Go function to check if a number is prime"},
	}, &uncloseai.ChatOptions{
		Model:     models[modelIdx].ID,
		MaxTokens: 200,
	})

	if err != nil {
		fmt.Printf("  Error: %v\n", err)
	} else {
		fmt.Printf("  %s\n\n", response2.Choices[0].Message.Content)
	}
}

// Example: Error handling
func exampleErrorHandling(ctx context.Context) {
	fmt.Println("=== Error Handling Example ===\n")

	client, _ := uncloseai.New(nil)

	// Try to use non-existent model
	_, err := client.Chat(ctx, []uncloseai.Message{
		{Role: "user", Content: "Hello"},
	}, &uncloseai.ChatOptions{
		Model: "non-existent-model",
	})

	if err != nil {
		fmt.Printf("Caught error (expected): %v\n\n", err)
	}
}
