package main

import (
	"context"
	"fmt"
	"log"

	"chorus.services/bzzz/pkg/dht"
	"chorus.services/bzzz/pkg/ucxl"
)

func main() {
	fmt.Println("Testing BZZZ Mock Implementation...")

	// Test Mock DHT
	ctx := context.Background()
	mockDHT := dht.NewMockDHT()

	// Test basic operations
	key := "test-key"
	value := []byte("test-value")

	err := mockDHT.PutValue(ctx, key, value)
	if err != nil {
		log.Fatalf("Failed to put value: %v", err)
	}

	retrieved, err := mockDHT.GetValue(ctx, key)
	if err != nil {
		log.Fatalf("Failed to get value: %v", err)
	}

	if string(retrieved) != string(value) {
		log.Fatalf("Retrieved value doesn't match: got %s, want %s", retrieved, value)
	}

	fmt.Println("✓ Mock DHT: Put/Get operations working")

	// Test UCXL address parsing
	addresses := []string{
		"ucxl://agent-001:coordinator@bzzz:config/",
		"ucxl://*:*@*:*/*^/",
		"ucxl://test:role@project:task/path*~/",
	}

	for _, addr := range addresses {
		parsed, err := ucxl.ParseUCXLAddress(addr)
		if err != nil {
			log.Fatalf("Failed to parse UCXL address %s: %v", addr, err)
		}

		if parsed.Raw != addr {
			log.Fatalf("Parsed address doesn't match original: got %s, want %s", parsed.Raw, addr)
		}

		// Test round-trip
		regenerated, err := ucxl.GenerateUCXLAddress(
			parsed.Agent, parsed.Role, parsed.Project, parsed.Task, parsed.Path, parsed.Temporal,
		)
		if err != nil {
			log.Fatalf("Failed to generate UCXL address: %v", err)
		}

		// Parse again to ensure consistency
		_, err = ucxl.ParseUCXLAddress(regenerated)
		if err != nil {
			log.Fatalf("Failed to parse regenerated UCXL address: %v", err)
		}
	}

	fmt.Println("✓ UCXL Parser: Address parsing and generation working")

	// Test stats
	stats := mockDHT.GetStats()
	fmt.Printf("✓ Mock DHT Stats: %d keys, %d peers, %v latency\n", 
		stats.TotalKeys, stats.TotalPeers, stats.Latency)

	fmt.Println("\n🎉 BZZZ Mock Implementation: All tests passed!")
}