package main

import (
	"context"
	"log"
	"os"
	"os/signal"
	"syscall"

	"chorus.services/bzzz/internal/common/runtime"
	"chorus.services/bzzz/internal/hap"
	"chorus.services/bzzz/logging"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Create logger for HAP
	logger := logging.NewStandardLogger("bzzz-hap")

	// Create runtime
	rt := runtime.NewRuntime(logger)

	// Initialize shared runtime with HAP-specific configuration
	runtimeConfig := runtime.RuntimeConfig{
		ConfigPath:      getConfigPath(),
		BinaryType:      runtime.BinaryTypeHAP,
		EnableSetupMode: needsSetup(),
		CustomPorts: runtime.PortConfig{
			HTTPPort:   8090, // Different from agent to avoid conflicts
			HealthPort: 8091,
			UCXIPort:   8092,
		},
	}

	// Check for instance collision
	if err := runtime.CheckForRunningInstance("hap", runtime.BinaryTypeHAP); err != nil {
		log.Fatalf("Instance check failed: %v", err)
	}
	defer runtime.RemoveInstanceLock("hap", runtime.BinaryTypeHAP)

	// Initialize runtime services
	services, err := rt.Initialize(ctx, runtimeConfig)
	if err != nil {
		log.Fatalf("Failed to initialize runtime: %v", err)
	}

	// Start shared services
	if err := rt.Start(ctx, services); err != nil {
		log.Fatalf("Failed to start runtime: %v", err)
	}

	// Initialize HAP-specific components
	hapInterface := hap.NewTerminalInterface(services, logger)
	if err := hapInterface.Start(ctx); err != nil {
		log.Fatalf("Failed to start HAP interface: %v", err)
	}

	logger.Info("👤 BZZZ Human Agent Portal started successfully")
	logger.Info("📍 Node ID: %s", services.Node.ID().ShortString())
	logger.Info("🎯 Agent ID: %s", services.Config.Agent.ID)

	if services.Config.Agent.Role != "" {
		authority, err := services.Config.GetRoleAuthority(services.Config.Agent.Role)
		if err == nil {
			logger.Info("🎭 Role: %s (Authority: %s)", services.Config.Agent.Role, authority)
		}
	}

	logger.Info("💬 Terminal interface ready for human interaction")
	logger.Info("🌐 HTTP API available at http://localhost:%d", runtimeConfig.CustomPorts.HTTPPort)
	logger.Info("🏥 Health endpoints at http://localhost:%d/health", runtimeConfig.CustomPorts.HealthPort)

	if services.UCXIServer != nil {
		logger.Info("🔗 UCXI server available at http://localhost:%d", runtimeConfig.CustomPorts.UCXIPort)
	}

	// Start HAP-specific background processes
	startHAPBackgroundProcesses(hapInterface, services, logger)

	logger.Info("✅ BZZZ Human Agent Portal fully operational")
	logger.Info("💡 Use the terminal interface to interact with the P2P network")

	// Wait for shutdown signals or terminal interface to stop
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

	// Wait for either signal or terminal interface to stop
	go func() {
		for hapInterface.IsRunning() {
			select {
			case <-ctx.Done():
				return
			default:
				// Keep checking if terminal interface is still running
				continue
			}
		}
		// If terminal interface stops, trigger shutdown
		sigChan <- syscall.SIGTERM
	}()

	<-sigChan

	logger.Info("🛑 Shutting down Human Agent Portal...")

	// Stop HAP interface
	if err := hapInterface.Stop(ctx); err != nil {
		logger.Error("HAP interface shutdown error: %v", err)
	}

	// Stop runtime services
	if err := rt.Stop(ctx, services); err != nil {
		logger.Error("Runtime shutdown error: %v", err)
	}

	logger.Info("✅ BZZZ Human Agent Portal shutdown completed")
}

// startHAPBackgroundProcesses starts HAP-specific background processes
func startHAPBackgroundProcesses(hapInterface *hap.TerminalInterface, services *runtime.RuntimeServices, logger logging.Logger) {
	// HAP-specific background processes can be added here
	// For example: message monitoring, peer discovery notifications, etc.

	logger.Info("🔍 HAP monitoring P2P network for collaboration opportunities")
	logger.Info("📡 Ready to facilitate human-AI coordination")
	logger.Info("🎯 HMMM collaborative reasoning monitoring active")
	logger.Info("💬 Interactive terminal ready for commands")

	// Example: Start monitoring for important P2P events
	go func() {
		// This could monitor for specific message types or events
		// and display notifications to the human user
		logger.Info("📊 Background monitoring started")
	}()
}

// getConfigPath determines the configuration file path
func getConfigPath() string {
	return runtime.GetConfigPath()
}

// needsSetup checks if the system needs to run setup mode
func needsSetup() bool {
	return runtime.NeedsSetup()
}