package handler import ( "net/http" "dealroom/templates" ) func (h *Handler) handleAnalytics(w http.ResponseWriter, r *http.Request) { profile := getProfile(r.Context()) dealID := r.URL.Query().Get("deal_id") deals := h.getDeals(profile) var dealCount, fileCount, requestCount, fulfilledCount int if dealID != "" { h.db.QueryRow("SELECT COUNT(*) FROM deals WHERE id = ? AND organization_id = ? AND is_archived = 0", dealID, profile.OrganizationID).Scan(&dealCount) h.db.QueryRow("SELECT COUNT(*) FROM files WHERE deal_id = ?", dealID).Scan(&fileCount) h.db.QueryRow("SELECT COUNT(*) FROM diligence_requests WHERE deal_id = ?", dealID).Scan(&requestCount) h.db.QueryRow("SELECT COUNT(*) FROM diligence_requests WHERE deal_id = ? AND atlas_status = 'fulfilled'", dealID).Scan(&fulfilledCount) } else { h.db.QueryRow("SELECT COUNT(*) FROM deals WHERE organization_id = ? AND is_archived = 0", profile.OrganizationID).Scan(&dealCount) h.db.QueryRow("SELECT COUNT(*) FROM files f JOIN deals d ON f.deal_id = d.id WHERE d.organization_id = ?", profile.OrganizationID).Scan(&fileCount) h.db.QueryRow("SELECT COUNT(*) FROM diligence_requests r JOIN deals d ON r.deal_id = d.id WHERE d.organization_id = ?", profile.OrganizationID).Scan(&requestCount) h.db.QueryRow("SELECT COUNT(*) FROM diligence_requests r JOIN deals d ON r.deal_id = d.id WHERE d.organization_id = ? AND r.atlas_status = 'fulfilled'", profile.OrganizationID).Scan(&fulfilledCount) } completionPct := 0 if requestCount > 0 { completionPct = (fulfilledCount * 100) / requestCount } stats := &templates.AnalyticsStats{ DealCount: dealCount, FileCount: fileCount, RequestCount: requestCount, CompletionPct: completionPct, } templates.AnalyticsPage(profile, stats, deals, dealID).Render(r.Context(), w) }