package handlers import ( "fmt" "net/http" ) func (a *App) Dashboard(w http.ResponseWriter, r *http.Request) { accountID, err := a.getSession(r) if err != nil { http.Redirect(w, r, "/auth/login", http.StatusFound) return } var clientID int64 a.DB.QueryRow("SELECT client_id FROM clients WHERE account_id = ? LIMIT 1", accountID).Scan(&clientID) var reviewCount int if clientID > 0 { a.DB.QueryRow("SELECT COUNT(*) FROM leads WHERE client_id = ? AND needs_review = 1", clientID).Scan(&reviewCount) } waStatus := "disconnected" waStyle := "color:#dc3545" connectLink := "" if a.WAConnector != nil { if connected, _ := a.WAConnector.IsConnected(r.Context(), clientID); connected { waStatus = "connected" waStyle = "color:#28a745" } else if clientID > 0 { // Fallback: check DB column when in-memory state not available. var dbConnected int a.DB.QueryRow("SELECT whatsapp_connected FROM clients WHERE client_id = ?", clientID).Scan(&dbConnected) if dbConnected == 1 { waStatus = "connected" waStyle = "color:#28a745" } else { connectLink = fmt.Sprintf(` Connect`, clientID) } } else { connectLink = ` Create client to connect` } } badge := "" if reviewCount > 0 { badge = fmt.Sprintf(` %d`, reviewCount) } w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, ` Dashboard

CRM Dashboard

WhatsApp: %s%s

`, waStyle, waStatus, connectLink, badge) } // --- package-level shim kept for existing tests --- func Dashboard(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).Dashboard(w, r) }