package api

import (
	"encoding/json"
	"log/slog"
	"net/http"
	"sync"

	"github.com/go-chi/chi/v5"
	"nerd-monitor/internal/stats"
	"nerd-monitor/internal/store"
)

// Broadcaster manages Server-Sent Events clients.
type Broadcaster struct {
	clients map[chan string]bool
	mu      sync.RWMutex
}

var apiBroadcaster = &Broadcaster{
	clients: make(map[chan string]bool),
}

// Broadcast sends a message to all connected SSE clients.
func (b *Broadcaster) Broadcast(message string) {
	b.mu.RLock()
	defer b.mu.RUnlock()

	for clientChan := range b.clients {
		select {
		case clientChan <- message:
		default:
			// Client channel is full, skip
		}
	}
}

// AddClient adds a new SSE client.
func (b *Broadcaster) AddClient(clientChan chan string) {
	b.mu.Lock()
	defer b.mu.Unlock()
	b.clients[clientChan] = true
}

// RemoveClient removes an SSE client.
func (b *Broadcaster) RemoveClient(clientChan chan string) {
	b.mu.Lock()
	defer b.mu.Unlock()
	delete(b.clients, clientChan)
	close(clientChan)
}

// GetAPIBroadcaster returns the API broadcaster instance.
func GetAPIBroadcaster() *Broadcaster {
	return apiBroadcaster
}

// Handler manages HTTP requests.
type Handler struct {
	store *store.Store
}

// New creates a new API handler.
func New(s *store.Store) *Handler {
	return &Handler{store: s}
}

// ReportStats handles agent stats reports.
func (h *Handler) ReportStats(w http.ResponseWriter, r *http.Request) {
	slog.Debug("Incoming stats report request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)

	agentID := r.URL.Query().Get("id")
	if agentID == "" {
		slog.Warn("Missing agent ID in stats report", "remoteAddr", r.RemoteAddr)
		http.Error(w, "missing agent id", http.StatusBadRequest)
		return
	}

	var stat stats.Stats
	if err := json.NewDecoder(r.Body).Decode(&stat); err != nil {
		slog.Error("Invalid request body in stats report", "agentID", agentID, "error", err)
		http.Error(w, "invalid request body", http.StatusBadRequest)
		return
	}

	slog.Debug("Updating agent stats", "agentID", agentID, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
	h.store.UpdateAgent(agentID, &stat)

	// Broadcast update to all connected SSE clients
	message := "event: stats-update\ndata: {\"type\": \"stats-update\", \"agentId\": \"" + agentID + "\"}\n\n"
	slog.Info("Broadcasting stats update", "agentID", agentID)
	apiBroadcaster.Broadcast(message)

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
	slog.Debug("Stats report processed successfully", "agentID", agentID)
}

// GetAgent returns stats for a single agent.
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
	slog.Debug("Incoming get agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)

	agentID := r.URL.Query().Get("id")
	if agentID == "" {
		slog.Warn("Missing agent ID in get agent request", "remoteAddr", r.RemoteAddr)
		http.Error(w, "missing agent id", http.StatusBadRequest)
		return
	}

	slog.Debug("Retrieving agent stats", "agentID", agentID)
	agent := h.store.GetAgent(agentID)
	if agent == nil {
		slog.Warn("Agent not found", "agentID", agentID)
		http.Error(w, "agent not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(agent)
	slog.Debug("Agent stats retrieved successfully", "agentID", agentID)
}

// ListAgents returns all agents.
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
	slog.Debug("Incoming list agents request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)

	slog.Debug("Retrieving all agents")
	agents := h.store.GetAllAgents()
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(agents)
	slog.Debug("All agents listed successfully", "count", len(agents))
}

// DeleteAgent removes an agent.
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
	slog.Debug("Incoming delete agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)

	agentID := r.URL.Query().Get("id")
	if agentID == "" {
		slog.Warn("Missing agent ID in delete agent request", "remoteAddr", r.RemoteAddr)
		http.Error(w, "missing agent id", http.StatusBadRequest)
		return
	}

	slog.Info("Deleting agent", "agentID", agentID)
	h.store.DeleteAgent(agentID)
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
	slog.Info("Agent deleted successfully", "agentID", agentID)
}

// UpdateHostname updates the hostname for an agent.
func (h *Handler) UpdateHostname(w http.ResponseWriter, r *http.Request) {
	slog.Debug("Incoming update hostname request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)

	if r.Method != http.MethodPost {
		slog.Warn("Invalid method for update hostname", "method", r.Method)
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	agentID := chi.URLParam(r, "id")
	if agentID == "" {
		slog.Warn("Missing agent ID in update hostname request", "remoteAddr", r.RemoteAddr)
		http.Error(w, "missing agent id", http.StatusBadRequest)
		return
	}

	hostname := r.FormValue("hostname")
	if hostname == "" {
		slog.Warn("Missing hostname in update request", "agentID", agentID)
		http.Error(w, "missing hostname", http.StatusBadRequest)
		return
	}

	slog.Info("Updating agent hostname", "agentID", agentID, "newHostname", hostname)
	err := h.store.UpdateHostname(agentID, hostname)
	if err != nil {
		slog.Warn("Failed to update hostname - agent not found", "agentID", agentID, "error", err)
		http.Error(w, "agent not found", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(map[string]string{"status": "updated"})
	slog.Info("Agent hostname updated successfully", "agentID", agentID, "hostname", hostname)
}
