File size: 34,157 Bytes
1875b13
 
 
 
 
 
 
 
 
 
84fb786
1875b13
84fb786
 
1875b13
 
84fb786
1875b13
 
 
 
 
4ec4472
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84fb786
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec4472
 
 
 
1875b13
 
 
 
 
 
90c0e10
1875b13
6d74982
1875b13
 
88da572
 
1875b13
 
 
0af208b
 
 
1875b13
 
4ec4472
ebe8fa5
84fb786
 
 
1875b13
 
 
90c0e10
 
 
 
 
 
 
f75687e
90c0e10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8240ff4
90c0e10
 
 
 
 
 
 
0af208b
 
 
90c0e10
 
2f90681
90c0e10
 
 
 
 
1875b13
 
 
 
 
 
 
 
6d74982
 
 
 
 
 
1875b13
 
 
 
 
 
 
3b977fc
1875b13
3b977fc
1875b13
3b977fc
 
 
 
 
 
 
1875b13
3b977fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88da572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d74982
 
1875b13
6d74982
 
 
 
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c009bc5
1875b13
 
 
 
 
 
 
0af208b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
84fb786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec4472
 
 
88da572
 
 
 
4ec4472
 
 
88da572
 
4ec4472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d012f99
4ec4472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88da572
 
 
 
 
 
 
4ec4472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88da572
 
 
 
 
 
 
4ec4472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88da572
 
4ec4472
 
 
 
 
 
 
 
 
 
88da572
 
 
 
 
 
 
4ec4472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88da572
 
 
 
 
 
 
4ec4472
 
 
 
 
 
 
 
 
 
 
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90c0e10
1875b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe8fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4bc605
ebe8fa5
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
// main.go — GridMind-RL HTTP server (OpenEnv-compliant)
// Exposes: POST /step, POST /reset, GET /state, GET /health, GET /replay, GET /grade, GET /metrics
// Port: 7860 (Hugging Face Spaces compatible)
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"gridmind-rl/env"

	"github.com/gorilla/websocket"
)

// ──────────────────────────────────────────────
// Prometheus-style metrics (OpenTelemetry)
// ──────────────────────────────────────────────

type Metrics struct {
	mu               sync.Mutex
	stepCount        int64
	stepLatencySum   float64
	stepLatencyCount int64
	rewardSum        float64
	rewardCount      int64
	rewardMin        float64
	rewardMax        float64
	// Histograms
	actionBuckets map[string]int64 // hvac bucket counts
	errorCount    int64
}

var metrics = &Metrics{
	rewardMin:     math.MaxFloat64,
	rewardMax:     -math.MaxFloat64,
	actionBuckets: map[string]int64{"low": 0, "mid": 0, "high": 0},
}

func (m *Metrics) recordStep(latencyMs float64, reward float64) {
	atomic.AddInt64(&m.stepCount, 1)
	m.mu.Lock()
	defer m.mu.Unlock()
	m.stepLatencySum += latencyMs
	m.stepLatencyCount++
	m.rewardSum += reward
	m.rewardCount++
	if reward < m.rewardMin {
		m.rewardMin = reward
	}
	if reward > m.rewardMax {
		m.rewardMax = reward
	}
}

func (m *Metrics) recordAction(hvac float64) {
	m.mu.Lock()
	defer m.mu.Unlock()
	switch {
	case hvac < 0.33:
		m.actionBuckets["low"]++
	case hvac < 0.66:
		m.actionBuckets["mid"]++
	default:
		m.actionBuckets["high"]++
	}
}

func (m *Metrics) prometheus() string {
	m.mu.Lock()
	defer m.mu.Unlock()
	avgLatency := 0.0
	if m.stepLatencyCount > 0 {
		avgLatency = m.stepLatencySum / float64(m.stepLatencyCount)
	}
	avgReward := 0.0
	if m.rewardCount > 0 {
		avgReward = m.rewardSum / float64(m.rewardCount)
	}
	return fmt.Sprintf(`# HELP gridmind_steps_total Total environment steps taken
# TYPE gridmind_steps_total counter
gridmind_steps_total %d

# HELP gridmind_step_latency_ms_avg Average step latency (ms)
# TYPE gridmind_step_latency_ms_avg gauge
gridmind_step_latency_ms_avg %.4f

# HELP gridmind_reward_avg Average reward per step
# TYPE gridmind_reward_avg gauge
gridmind_reward_avg %.4f

# HELP gridmind_reward_min Minimum reward seen
# TYPE gridmind_reward_min gauge
gridmind_reward_min %.4f

# HELP gridmind_reward_max Maximum reward seen
# TYPE gridmind_reward_max gauge
gridmind_reward_max %.4f

# HELP gridmind_action_hvac_bucket HVAC power level distribution
# TYPE gridmind_action_hvac_bucket counter
gridmind_action_hvac_bucket{bin="low"} %d
gridmind_action_hvac_bucket{bin="mid"} %d
gridmind_action_hvac_bucket{bin="high"} %d

# HELP gridmind_errors_total Total request errors
# TYPE gridmind_errors_total counter
gridmind_errors_total %d
`,
		atomic.LoadInt64(&m.stepCount),
		avgLatency, avgReward,
		m.rewardMin, m.rewardMax,
		m.actionBuckets["low"], m.actionBuckets["mid"], m.actionBuckets["high"],
		atomic.LoadInt64(&m.errorCount),
	)
}

// ──────────────────────────────────────────────
// Server
// ──────────────────────────────────────────────

type Server struct {
	envMgr *env.Environment
}

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool { return true },
}

func newServer() *Server {
	return &Server{envMgr: env.NewEnvironment()}
}

func (s *Server) routes() *http.ServeMux {
	mux := http.NewServeMux()
	mux.HandleFunc("/", s.handleRoot)
	mux.HandleFunc("/health", s.handleHealth)
	mux.HandleFunc("/ping", s.handlePing)
	mux.HandleFunc("/reset", s.handleReset)
	mux.HandleFunc("/step", s.handleStep)
	mux.HandleFunc("/coordinator/reset", s.handleCoordinatorReset)
	mux.HandleFunc("/coordinator/step", s.handleCoordinatorStep)
	mux.HandleFunc("/state", s.handleState)
	mux.HandleFunc("/replay", s.handleReplay)
	mux.HandleFunc("/grade", s.handleGrade)
	mux.HandleFunc("/feeder", s.handleFeeder)
	mux.HandleFunc("/coordinate", s.handleCoordinate)
	mux.HandleFunc("/simulate", s.handleSimulate)
	mux.HandleFunc("/tasks", s.handleTasks)
	mux.HandleFunc("/metrics", s.handleMetrics)
	mux.HandleFunc("/ws", s.handleWebSocket)
	mux.HandleFunc("/info", s.handleInfo)
	// Reverse proxy for dashboard (runs on port 7861 internally)
	mux.HandleFunc("/dashboard", s.handleDashboardProxy)
	mux.HandleFunc("/dashboard/", s.handleDashboardProxy)
	return mux
}

// ── / (Root) ────────────────────────────────────────────────────────────────

func (s *Server) handleRoot(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	html := `<!DOCTYPE html>
<html>
<head><title>GridMind-RL</title>
<style>
  body { font-family: monospace; background: #0d1117; color: #e6edf3; padding: 40px; }
  a { color: #58a6ff; text-decoration: none; }
  a:hover { text-decoration: underline; }
  .badge { background: #238636; padding: 4px 10px; border-radius: 4px; color: white; display: inline-block; }
  ul { line-height: 1.8; }
  pre { background: #0d1117; border: 1px solid #30363d; padding: 16px; border-radius: 6px; overflow-x: auto; }
</style>
</head>
<body>
<h1>⚡ GridMind-RL</h1>
<p><span class="badge">● RUNNING</span></p>
<p>Industrial building energy management RL environment — OpenEnv compliant.</p>
<h3>🔗 Quick Links</h3>
<ul>
  <li><a href="/dashboard">→ Live Dashboard</a></li>
  <li><a href="/health">→ API Health Check</a></li>
  <li><a href="/tasks">→ Available Tasks</a></li>
  <li><a href="/metrics">→ Prometheus Metrics</a></li>
  <li><a href="https://colab.research.google.com/github/LO-Kyu/gridmind/blob/main/scripts/gridmind_grpo_colab.ipynb" target="_blank" rel="noopener noreferrer">→ Google Colab</a></li>
</ul>
<h3>📡 API Endpoints</h3>
<pre>GET  /health           → health check
GET  /ping             → ping pong
GET  /state            → current environment state
GET  /replay           → episode replay data
GET  /grade            → episode grade score
GET  /feeder           → aggregate fleet status (for coordinator)
POST /coordinate       → apply price multipliers (for coordinator)
POST /simulate {action}→ predict next state (world model API)
POST /reset {task_id}  → start new episode
POST /step {action}    → take action</pre>

</body>
</html>`
	w.Write([]byte(html))
}

// ── /health ──────────────────────────────────────────────────────────────────

func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"status": "ok", "version": "1.0.0"})
}

func (s *Server) handlePing(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

// ── /reset ───────────────────────────────────────────────────────────────────

func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	var req map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		req = make(map[string]interface{})
	}
	taskID := 1
	if t, ok := req["task_id"].(float64); ok {
		taskID = int(t)
	} else if t, ok := req["task_id"].(int64); ok {
		taskID = int(t)
	} else if t, ok := req["task_id"].(int); ok {
		taskID = t
	}
	if taskID == 0 {
		taskID = 1
	}
	seed := int64(0)
	if s, ok := req["seed"].(float64); ok {
		seed = int64(s)
	} else if s, ok := req["seed"].(int64); ok {
		seed = s
	}
	numBuildings := 1
	if nb, ok := req["num_buildings"].(float64); ok {
		numBuildings = int(nb)
	}
	resetReq := env.ResetRequest{
		TaskID:       taskID,
		NumBuildings: numBuildings,
	}
	if seed > 0 {
		resetReq.Seed = &seed
	}
	resp := s.envMgr.Reset(resetReq)
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

// ── /step ────────────────────────────────────────────────────────────────────

func (s *Server) handleStep(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	start := time.Now()

	// Accept both single action and array of actions
	var actions []env.ActionModel
	var single env.ActionModel

	body := make([]byte, 0, 512)
	buf := make([]byte, 512)
	for {
		n, err := r.Body.Read(buf)
		body = append(body, buf[:n]...)
		if err != nil {
			break
		}
	}

	if len(body) > 0 && body[0] == '[' {
		if err := json.Unmarshal(body, &actions); err != nil {
			atomic.AddInt64(&metrics.errorCount, 1)
			http.Error(w, "invalid action array: "+err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		if err := json.Unmarshal(body, &single); err != nil {
			atomic.AddInt64(&metrics.errorCount, 1)
			http.Error(w, "invalid action: "+err.Error(), http.StatusBadRequest)
			return
		}
		actions = []env.ActionModel{single}
	}

	responses, done := s.envMgr.Step(actions)

	latency := float64(time.Since(start).Microseconds()) / 1000.0
	for _, resp := range responses {
		metrics.recordStep(latency, resp.Reward)
	}
	if len(actions) > 0 {
		metrics.recordAction(actions[0].HVACPowerLevel)
	}

	w.Header().Set("Content-Type", "application/json")
	if done && len(responses) == 1 {
		responses[0].Done = true
	}
	// Return single response if single building, array otherwise
	if len(responses) == 1 {
		json.NewEncoder(w).Encode(responses[0])
	} else {
		json.NewEncoder(w).Encode(responses)
	}
}

// ── /coordinator/reset ──────────────────────────────────────────────────────

func (s *Server) handleCoordinatorReset(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	var req env.ResetRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		// Allow empty body → defaults
		req = env.ResetRequest{TaskID: 1, NumBuildings: 3}
	}
	if req.TaskID == 0 {
		req.TaskID = 1
	}
	if req.NumBuildings == 0 {
		req.NumBuildings = 3
	}
	resp := s.envMgr.Reset(req)
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(resp)
}

// ── /coordinator/step ───────────────────────────────────────────────────────

func (s *Server) handleCoordinatorStep(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}

	start := time.Now()

	// Accept array of actions (one per building)
	var actions []env.ActionModel

	body := make([]byte, 0, 512)
	buf := make([]byte, 512)
	for {
		n, err := r.Body.Read(buf)
		body = append(body, buf[:n]...)
		if err != nil {
			break
		}
	}

	if err := json.Unmarshal(body, &actions); err != nil {
		atomic.AddInt64(&metrics.errorCount, 1)
		http.Error(w, "invalid action array: "+err.Error(), http.StatusBadRequest)
		return
	}

	// If empty array provided, use defaults
	if len(actions) == 0 {
		actions = []env.ActionModel{{HVACPowerLevel: 0.5, BuildingID: 0}}
	}

	responses, _ := s.envMgr.Step(actions)

	latency := float64(time.Since(start).Microseconds()) / 1000.0
	for _, resp := range responses {
		metrics.recordStep(latency, resp.Reward)
	}
	if len(actions) > 0 {
		metrics.recordAction(actions[0].HVACPowerLevel)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")

	// Always return array format for coordinator
	json.NewEncoder(w).Encode(responses)
}

// ── /state ───────────────────────────────────────────────────────────────────

func (s *Server) handleState(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	state := s.envMgr.GetState()
	// Add CORS for dashboard
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(state)
}

// ── /replay ──────────────────────────────────────────────────────────────────

func (s *Server) handleReplay(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	replay := s.envMgr.GetReplay()
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"replay": replay,
		"steps":  len(replay),
	})
}

// ── /grade ───────────────────────────────────────────────────────────────────

func (s *Server) handleGrade(w http.ResponseWriter, r *http.Request) {
	state := s.envMgr.GetState()
	replay := s.envMgr.GetReplay()

	// Collect per-building exploit penalties
	penalties := make([]float64, len(state.Buildings))
	for i := range state.Buildings {
		_, pen := s.envMgr.ExploitDetected(i)
		penalties[i] = pen
	}

	// Build building states from public state
	buildings := make([]*env.BuildingState, len(state.Buildings))
	for i, pub := range state.Buildings {
		jobsCopy := make([]env.BatchJob, len(pub.Jobs))
		copy(jobsCopy, pub.Jobs)
		buildings[i] = &env.BuildingState{
			CumulativeCost:   pub.CumulativeCost,
			BaselineCost:     pub.BaselineCost,
			CumulativeCarbon: pub.CumulativeCarbon,
			BaselineCarbon:   pub.BaselineCarbon,
			Jobs:             jobsCopy,
		}
	}

	// Reconstruct temp history from public state
	tempHistory := make([][]float64, len(state.Buildings))
	for i, pub := range state.Buildings {
		tempHistory[i] = pub.TempHistory
	}

	grade := env.GradeEpisode(env.GradeEpisodeInput{
		TaskID:           state.TaskID,
		Buildings:        buildings,
		Replay:           replay,
		TempHistory:      tempHistory,
		TMin:             env.TMinDefault,
		TMax:             env.TMaxDefault,
		ExploitPenalties: penalties,
		InstructionCard:  state.InstructionCard,
	})

	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	json.NewEncoder(w).Encode(grade)
}

// ── /feeder ──────────────────────────────────────────────────────────────────

func (s *Server) handleFeeder(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	state := s.envMgr.GetFeederState()
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	json.NewEncoder(w).Encode(state)
}

// ── /coordinate ──────────────────────────────────────────────────────────────

func (s *Server) handleCoordinate(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	var req env.CoordinateRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	s.envMgr.SetCoordinatorSignals(req.PriceMultipliers)
	w.WriteHeader(http.StatusOK)
}

// ── /simulate ────────────────────────────────────────────────────────────────

func (s *Server) handleSimulate(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}
	var actions []env.ActionModel
	if err := json.NewDecoder(r.Body).Decode(&actions); err != nil {
		http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest)
		return
	}
	responses, done := s.envMgr.SimulateStep(actions)

	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	json.NewEncoder(w).Encode(map[string]interface{}{
		"results": responses,
		"done":    done,
	})
}

// ── /tasks ───────────────────────────────────────────────────────────────────

func (s *Server) handleTasks(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(env.AllTasks())
}

// ── /metrics ─────────────────────────────────────────────────────────────────

func (s *Server) handleMetrics(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain; version=0.0.4")
	fmt.Fprint(w, metrics.prometheus())
}

// ── /dashboard (Reverse Proxy) ────────────────────────────────────────────

func (s *Server) handleDashboardProxy(w http.ResponseWriter, r *http.Request) {
	// Target URL for the dashboard service (running on localhost:7861)
	target, err := url.Parse("http://localhost:7861")
	if err != nil {
		http.Error(w, "proxy configuration error", http.StatusInternalServerError)
		return
	}

	// Create a custom director to modify the request
	director := func(req *http.Request) {
		// Strip /dashboard prefix
		path := req.URL.Path
		if strings.HasPrefix(path, "/dashboard") {
			path = strings.TrimPrefix(path, "/dashboard")
			if path == "" {
				path = "/"
			}
		}

		// Set up the new request
		req.URL.Scheme = target.Scheme
		req.URL.Host = target.Host
		if target.Path != "" {
			req.URL.Path = target.Path + path
		} else {
			req.URL.Path = path
		}
		req.RequestURI = ""

		// Preserve original host header for dashboard API calls
		if req.Header.Get("X-Forwarded-Host") == "" {
			req.Header.Set("X-Forwarded-For", getClientIP(r))
			req.Header.Set("X-Forwarded-Proto", "https")
		}
	}

	// Use ReverseProxy with custom director
	proxy := &httputil.ReverseProxy{Director: director}
	proxy.ServeHTTP(w, r)
}

// Helper: extract client IP from request
func getClientIP(r *http.Request) string {
	ip, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		return r.RemoteAddr
	}
	return ip
}

// ── /ws (WebSocket) ───────────────────────────────────────────────────────────

type WSMessage struct {
	Type   string          `json:"type"`
	Data   json.RawMessage `json:"data,omitempty"`
	Seed   *int64          `json:"seed,omitempty"`
	TaskID int             `json:"task_id,omitempty"`
}

type WSResetMessage struct {
	Seed         *int64 `json:"seed,omitempty"`
	TaskID       int    `json:"task_id,omitempty"`
	NumBuildings int    `json:"num_buildings,omitempty"`
}

type WSStepMessage struct {
	Action json.RawMessage `json:"action"`
}

func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Printf("WebSocket upgrade error: %v", err)
		return
	}
	defer conn.Close()

	for {
		// Read message from client
		_, msgBytes, err := conn.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
				log.Printf("WebSocket error: %v", err)
			}
			break
		}

		var msg WSMessage
		if err := json.Unmarshal(msgBytes, &msg); err != nil {
			errMsg, _ := json.Marshal(map[string]string{"error": "invalid message format"})
			conn.WriteMessage(websocket.TextMessage, errMsg)
			continue
		}

		switch msg.Type {
		case "reset":
			// GenericEnvClient sends: {"type": "reset", "data": {"seed": 42}}
			// We need to handle data payload if present
			if len(msg.Data) > 0 {
				s.handleWSReset(conn, msg.Data)
			} else {
				// Fallback to top-level fields (seed, task_id)
				s.handleWSResetDirect(conn, msg.Seed, msg.TaskID)
			}
		case "step":
			// GenericEnvClient sends: {"type": "step", "data": {"action": {...}}}
			if len(msg.Data) > 0 {
				s.handleWSStep(conn, msg.Data)
			} else {
				// Fallback to top-level action
				s.handleWSStepDirect(conn, msgBytes)
			}
		case "state":
			s.handleWSState(conn)
		case "close":
			return
		default:
			errMsg, _ := json.Marshal(map[string]string{"error": "unknown message type: " + msg.Type})
			conn.WriteMessage(websocket.TextMessage, errMsg)
		}
	}
}

func (s *Server) handleWSReset(conn *websocket.Conn, data json.RawMessage) {
	// GenericEnvClient sends: {"data": {"seed": 42}}
	// Or: {"data": {"task_id": 1, "seed": 42}}
	var reqData map[string]interface{}
	if err := json.Unmarshal(data, &reqData); err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid reset data: " + err.Error()})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	var seed *int64
	if seedVal, ok := reqData["seed"].(float64); ok {
		s := int64(seedVal)
		seed = &s
	} else if seedVal, ok := reqData["seed"].(int64); ok {
		seed = &seedVal
	} else if seedVal, ok := reqData["seed"].(int); ok {
		s := int64(seedVal)
		seed = &s
	}

	taskID := 1
	if taskIDVal, ok := reqData["task_id"].(float64); ok {
		taskID = int(taskIDVal)
	} else if taskIDVal, ok := reqData["task_id"].(int64); ok {
		taskID = int(taskIDVal)
	} else if taskIDVal, ok := reqData["task_id"].(int); ok {
		taskID = taskIDVal
	}

	numBuildings := 1
	if nbVal, ok := reqData["num_buildings"].(float64); ok {
		numBuildings = int(nbVal)
	} else if nbVal, ok := reqData["num_buildings"].(int64); ok {
		numBuildings = int(nbVal)
	} else if nbVal, ok := reqData["num_buildings"].(int); ok {
		numBuildings = nbVal
	}

	resp := s.envMgr.Reset(env.ResetRequest{
		Seed:         seed,
		TaskID:       taskID,
		NumBuildings: numBuildings,
	})

	// Build observation response
	obs := resp.Observations[0]
	respData := map[string]interface{}{
		"observation": map[string]interface{}{
			"indoor_temperature":    obs.IndoorTemperature,
			"thermal_storage_level": obs.ThermalStorageLevel,
			"process_demand":        obs.ProcessDemand,
			"current_price":         obs.CurrentPrice,
			"grid_stress_signal":    obs.GridStressSignal,
			"carbon_intensity":      obs.CarbonIntensity,
			"hour_of_day":           obs.HourOfDay,
			"batch_queue":           obs.BatchQueue,
			"cumulative_cost":       obs.CumulativeCost,
			"step":                  obs.Step,
			"building_id":           obs.BuildingID,
		},
		"reward": nil,
		"done":   false,
		"info":   map[string]interface{}{"episode": resp.Episode, "task_id": resp.TaskID},
	}

	// Wrap in "data" field for GenericEnvClient compatibility
	response := map[string]interface{}{
		"data": respData,
	}

	respBytes, _ := json.Marshal(response)
	conn.WriteMessage(websocket.TextMessage, respBytes)
}

func (s *Server) handleWSStep(conn *websocket.Conn, data json.RawMessage) {
	// GenericEnvClient sends action directly in data: {"data": {...action fields...}}
	var reqData map[string]interface{}
	if err := json.Unmarshal(data, &reqData); err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid step data: " + err.Error()})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	// Handle two formats:
	// 1. Direct action: {"data": {"hvac_power_level": 0.5, ...}}
	// 2. Wrapped action: {"data": {"action": {"hvac_power_level": 0.5, ...}}}
	var actionBytes []byte
	if actionData, ok := reqData["action"]; ok {
		// Wrapped format
		actionBytes, _ = json.Marshal(actionData)
	} else {
		// Direct format - use the whole reqData as action
		actionBytes = data
	}

	var action env.ActionModel
	if err := json.Unmarshal(actionBytes, &action); err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid action: " + err.Error()})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	responses, done := s.envMgr.Step([]env.ActionModel{action})

	// Record metrics
	if len(responses) > 0 {
		metrics.recordStep(0, responses[0].Reward)
		metrics.recordAction(action.HVACPowerLevel)
	}

	obs := responses[0]
	respData := map[string]interface{}{
		"observation": map[string]interface{}{
			"indoor_temperature":    obs.Observation.IndoorTemperature,
			"thermal_storage_level": obs.Observation.ThermalStorageLevel,
			"process_demand":        obs.Observation.ProcessDemand,
			"current_price":         obs.Observation.CurrentPrice,
			"grid_stress_signal":    obs.Observation.GridStressSignal,
			"carbon_intensity":      obs.Observation.CarbonIntensity,
			"hour_of_day":           obs.Observation.HourOfDay,
			"batch_queue":           obs.Observation.BatchQueue,
			"cumulative_cost":       obs.Observation.CumulativeCost,
			"step":                  obs.Observation.Step,
			"building_id":           obs.Observation.BuildingID,
		},
		"reward": obs.Reward,
		"done":   done,
		"info":   obs.Info,
	}
	response := map[string]interface{}{"data": respData}

	respBytes, _ := json.Marshal(response)
	conn.WriteMessage(websocket.TextMessage, respBytes)
}

func (s *Server) handleWSState(conn *websocket.Conn) {
	state := s.envMgr.GetState()
	stateBytes, _ := json.Marshal(state)
	conn.WriteMessage(websocket.TextMessage, stateBytes)
}

// Direct handlers for OpenEnv client format (action at top level)

func (s *Server) handleWSResetDirect(conn *websocket.Conn, seed *int64, taskID int) {
	if seed == nil {
		var s int64 = 42
		seed = &s
	}
	if taskID == 0 {
		taskID = 1
	}

	resp := s.envMgr.Reset(env.ResetRequest{
		Seed:         seed,
		TaskID:       taskID,
		NumBuildings: 1,
	})

	obs := resp.Observations[0]
	respData := map[string]interface{}{
		"observation": map[string]interface{}{
			"indoor_temperature":    obs.IndoorTemperature,
			"thermal_storage_level": obs.ThermalStorageLevel,
			"process_demand":        obs.ProcessDemand,
			"current_price":         obs.CurrentPrice,
			"grid_stress_signal":    obs.GridStressSignal,
			"carbon_intensity":      obs.CarbonIntensity,
			"hour_of_day":           obs.HourOfDay,
			"batch_queue":           obs.BatchQueue,
			"cumulative_cost":       obs.CumulativeCost,
			"step":                  obs.Step,
			"building_id":           obs.BuildingID,
		},
		"reward": nil,
		"done":   false,
		"info":   map[string]interface{}{"episode": resp.Episode, "task_id": resp.TaskID},
	}
	response := map[string]interface{}{"data": respData}

	respBytes, _ := json.Marshal(response)
	conn.WriteMessage(websocket.TextMessage, respBytes)
}

func (s *Server) handleWSStepDirect(conn *websocket.Conn, msgBytes []byte) {
	// Parse the original message to get action directly
	var rawMsg map[string]interface{}
	if err := json.Unmarshal(msgBytes, &rawMsg); err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid step message: " + err.Error()})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	actionData, ok := rawMsg["action"]
	if !ok {
		errMsg, _ := json.Marshal(map[string]string{"error": "missing action field"})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	actionBytes, err := json.Marshal(actionData)
	if err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid action format"})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	var action env.ActionModel
	if err := json.Unmarshal(actionBytes, &action); err != nil {
		errMsg, _ := json.Marshal(map[string]string{"error": "invalid action: " + err.Error()})
		conn.WriteMessage(websocket.TextMessage, errMsg)
		return
	}

	responses, done := s.envMgr.Step([]env.ActionModel{action})

	if len(responses) > 0 {
		metrics.recordStep(0, responses[0].Reward)
		metrics.recordAction(action.HVACPowerLevel)
	}

	obs := responses[0]
	respData := map[string]interface{}{
		"observation": map[string]interface{}{
			"indoor_temperature":    obs.Observation.IndoorTemperature,
			"thermal_storage_level": obs.Observation.ThermalStorageLevel,
			"process_demand":        obs.Observation.ProcessDemand,
			"current_price":         obs.Observation.CurrentPrice,
			"grid_stress_signal":    obs.Observation.GridStressSignal,
			"carbon_intensity":      obs.Observation.CarbonIntensity,
			"hour_of_day":           obs.Observation.HourOfDay,
			"batch_queue":           obs.Observation.BatchQueue,
			"cumulative_cost":       obs.Observation.CumulativeCost,
			"step":                  obs.Observation.Step,
			"building_id":           obs.Observation.BuildingID,
		},
		"reward": obs.Reward,
		"done":   done,
		"info":   obs.Info,
	}
	response := map[string]interface{}{"data": respData}

	respBytes, _ := json.Marshal(response)
	conn.WriteMessage(websocket.TextMessage, respBytes)
}

// ──────────────────────────────────────────────
// Entry point
// ──────────────────────────────────────────────

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = "7860"
	}
	// Validate port
	if _, err := strconv.Atoi(port); err != nil {
		log.Fatalf("invalid PORT: %s", port)
	}

	srv := newServer()

	// Perform initial reset so /state is always valid
	var seed int64 = 42
	srv.envMgr.Reset(env.ResetRequest{Seed: &seed, TaskID: 1, NumBuildings: 1})

	log.Printf("GridMind-RL environment server starting on :%s", port)
	log.Printf("Endpoints: GET / (landing) | GET /health /ping /state /replay /grade /tasks /metrics /dashboard | POST /reset /step")

	mux := withCORS(withLogging(srv.routes()))
	if err := http.ListenAndServe(":"+port, mux); err != nil {
		log.Fatalf("server error: %v", err)
	}
}

// ──────────────────────────────────────────────
// Middleware
// ──────────────────────────────────────────────

func withLogging(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		next.ServeHTTP(w, r)
		log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
	})
}

func withCORS(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
		if r.Method == http.MethodOptions {
			w.WriteHeader(http.StatusNoContent)
			return
		}
		next.ServeHTTP(w, r)
	})
}

// handleInfo returns OpenEnv-standard metadata for automated validators and judges.
func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	info := map[string]interface{}{
		"name":        "gridmind-rl",
		"version":     "2.0.0",
		"description": "Multi-building industrial energy management RL environment with instruction-following, world modeling, fault injection, and curriculum learning.",
		"multi_agent": true,
		"themes": []string{
			"multi-agent",
			"long-horizon-planning",
			"world-modeling",
			"self-improvement",
		},
		"observation_space": map[string]interface{}{
			"type": "dict",
			"fields": []string{
				"indoor_temperature", "thermal_storage_level", "current_price",
				"grid_stress_signal", "carbon_intensity", "hour_of_day", "step",
				"hvac_efficiency", "process_demand", "cumulative_cost",
				"batch_queue", "active_faults", "instruction_card",
			},
		},
		"action_space": map[string]interface{}{
			"type": "dict",
			"fields": map[string]string{
				"hvac_power_level":    "float [0.0, 1.0]",
				"thermal_charge_rate": "float [-1.0, 1.0]",
				"batch_job_slot":      "int [0, 4]",
				"load_shed_fraction":  "float [0.0, 0.5]",
				"building_id":         "int [0, N_buildings-1]",
			},
		},
		"endpoints": []string{
			"POST /reset", "POST /step", "GET /grade", "GET /tasks",
			"GET /state", "POST /simulate", "GET /feeder", "POST /coordinate",
			"GET /health", "GET /info",
		},
		"hf_space": "https://prajwal782007-gridmind.hf.space",
		"github":   "https://github.com/LO-Kyu/gridmind",
	}
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	json.NewEncoder(w).Encode(info)
}