File size: 105,455 Bytes
dcf70a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
from fastapi import FastAPI, HTTPException, Response, Request
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, validator
import requests
import os
import base64
import json
from typing import Optional, List
import random
import time
from datetime import datetime
import psutil
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def update_deployment_status(repo_owner, repo_name, workflow_run_id, status, conclusion=None, workflow_url=None):
    """Forward deployment status to VistaPanel Backend API"""
    try:
        payload = {
            'repo_owner': repo_owner,
            'repo_name': repo_name,
            'workflow_run_id': workflow_run_id,
            'status': status,
            'conclusion': conclusion,
            'workflow_url': workflow_url
        }
        
        # The VistaPanel endpoint URL
        api_url = os.getenv('CELESTINE_API_URL', 'https://celestine.indevs.in/api/webhook-receiver.php')
        secret = os.getenv('WEBHOOK_SECRET', 'savage')
        
        headers = {
            'X-Celestine-Secret': secret,
            'Content-Type': 'application/json',
            'User-Agent': 'Celestine-HF-Space/1.0'
        }
        
        logger.info(f"πŸ“€ Forwarding to VistaPanel: {api_url}")
        logger.info(f"   Payload: {json.dumps(payload, indent=2)}")
        
        # Increase timeout and add retry logic
        response = requests.post(
            api_url, 
            json=payload, 
            headers=headers, 
            timeout=15,
            allow_redirects=True
        )
        
        logger.info(f"   Response Status: {response.status_code}")
        logger.info(f"   Response Body: {response.text[:200]}")
        
        if response.status_code == 200:
            logger.info(f"βœ“ Forwarded status to Celestine: {repo_owner}/{repo_name} -> {status}")
            return True
        else:
            logger.error(f"Failed to forward status: HTTP {response.status_code} - {response.text}")
            return False
    except requests.exceptions.Timeout:
        logger.error(f"Webhook forwarding timeout after 15s")
        return False
    except requests.exceptions.ConnectionError as e:
        logger.error(f"Webhook forwarding connection error: {e}")
        return False
    except Exception as e:
        logger.error(f"Webhook forwarding error: {e}")
        return False

app = FastAPI(title="MOFH API Proxy + IONA AI")

# Track uptime
START_TIME = time.time()
REQUEST_COUNT = {"total": 0, "generate": 0, "create": 0, "errors": 0}

# High-quality image library for professional websites
WEBSITE_IMAGES = [
    
    "https://images.unsplash.com/photo-1497366754035-f200968a6e72?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1497366811353-6870744d04b2?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1497215728101-856f4ea42174?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1497366216548-37526070297c?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1441986300917-64674bd600d8?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1517245386807-bb43f82c33c4?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1519389950473-47ba0277781c?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1542744094-24638eff58bb?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1460925895917-afdab827c52f?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1522202176988-66273c2fd55f?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1552664730-d307ca884978?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1531482615713-2afd69097998?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=1920&h=1080",
    "https://images.unsplash.com/photo-1516321318423-f06f85e504b3?auto=format&fit=crop&q=80&w=1920&h=1080"
]

def get_random_images(count=10):
    """Get random images from the library"""
    return random.sample(WEBSITE_IMAGES, min(count, len(WEBSITE_IMAGES)))

MOFH_API_URL = "https://panel.myownfreehost.net:2087/xml-api/"
MOFH_API_USERNAME = "ZqHK1H5BBnrrywnsXXW2pY42QNqDxHru74HtwZofkn1LRawd4TDcKCPnIViBiPaJg1sUXUlKErn4EhXmqu0vx1STF263TxiTQuyKzYcErjSax5W4lBoLXqpE0aQqnLLKi8Pk7fncTbcfwqnKVRNOzLd34xdakdiHH1Z4gmDbetSmYauCId4Cj8SVizSBTuN6TMIrz2oMKAoXPypSocVOOUI0rSOIld34MY6yXtcHdSgh3Jd5J8mgM8SiW8BvHtE"
MOFH_API_PASSWORD = "Uj39SyF5okKP73Ml6laLEay40MZEXSe81lStBEc9dQC2odK49Tw4K5l4BXI3DfnIVA5ZoIH0GFKvBRBO1cLIU1djjSBCABBkp2GO2oeH4BROw238dZwODu0kklR0uezHPfezHMCbcbjSKLRKJX5Ns7gZ9wgXxZbO3e8Rf4MoZnQVk5IUNLvKPWHDP43PsQnAFAYOhDNnKPBBg2CoHXsJ1Kq8C5wRuBd64GPyPhY7sbSCibAMUWKRkUSzRi7aq5i"

# IONA AI - Multiple API Keys for Load Balancing (No Rate Limits)
GROQ_API_KEYS = [
    "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",  # Replace with real keys
    "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",
    "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",
    "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"
]

# Model Configuration - Use ONLY the most capable model
AI_MODELS = {
    "groq": [
        "llama-3.3-70b-versatile",        # Most capable, reliable for complex generation
    ]
}

# Track API usage for rotation
api_usage_counter = {key: 0 for key in GROQ_API_KEYS}

def get_next_api_key():
    """Round-robin API key selection for load balancing"""
    min_usage_key = min(api_usage_counter, key=api_usage_counter.get)
    api_usage_counter[min_usage_key] += 1
    return min_usage_key

def get_random_model():
    """Select model (always use 70B for reliability)"""
    return AI_MODELS["groq"][0]  # Always use llama-3.3-70b-versatile

class AIWebsiteRequest(BaseModel):
    prompt: str
    business_type: Optional[str] = "general"
    color_scheme: Optional[str] = "modern"
    include_database: Optional[bool] = True
    php_only: Optional[bool] = False
    features: Optional[List[str]] = []
    
    @validator('prompt')
    def validate_prompt(cls, v):
        if not v or len(v.strip()) < 10:
            raise ValueError('Prompt must be at least 10 characters')
        if len(v) > 10000:
            raise ValueError('Prompt too long (max 10000 characters)')
        return v.strip()

class AccountRequest(BaseModel):
    username: str
    password: str
    email: str
    domain: str
    plan: str = "free"
    
    @validator('username')
    def validate_username(cls, v):
        if not v or len(v) < 3:
            raise ValueError('Username must be at least 3 characters')
        if len(v) > 20:
            raise ValueError('Username too long (max 20 characters)')
        return v.strip()
    
    @validator('email')
    def validate_email(cls, v):
        if '@' not in v or '.' not in v:
            raise ValueError('Invalid email format')
        return v.strip()

@app.get("/", response_class=HTMLResponse)
def root():
    """Uptime dashboard with external IP display"""
    import socket
    
    hostname = socket.gethostname()
    local_ip = socket.gethostbyname(hostname)
    
    # Get external IP
    try:
        external_ip = requests.get('https://api.ipify.org', timeout=5).text
    except:
        external_ip = "Unable to detect"
    
    # Calculate uptime
    uptime_seconds = int(time.time() - START_TIME)
    uptime_hours = uptime_seconds // 3600
    uptime_minutes = (uptime_seconds % 3600) // 60
    uptime_secs = uptime_seconds % 60
    
    # Get system stats
    try:
        cpu_percent = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory()
        memory_percent = memory.percent
        memory_used = memory.used / (1024**3)  # GB
        memory_total = memory.total / (1024**3)  # GB
    except:
        cpu_percent = 0
        memory_percent = 0
        memory_used = 0
        memory_total = 0
    
    # API key status
    api_key_usage = []
    for key, count in api_usage_counter.items():
        api_key_usage.append(f"{key[:20]}... β†’ {count} requests")
    
    html_content = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>IONA AI - System Status</title>
        <style>
            * {{
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }}
            body {{
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                min-height: 100vh;
                padding: 20px;
                color: #fff;
            }}
            .container {{
                max-width: 1200px;
                margin: 0 auto;
            }}
            .header {{
                text-align: center;
                margin-bottom: 40px;
                animation: fadeIn 0.5s;
            }}
            .header h1 {{
                font-size: 3rem;
                margin-bottom: 10px;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
            }}
            .header p {{
                font-size: 1.2rem;
                opacity: 0.9;
            }}
            .status-badge {{
                display: inline-block;
                background: #10b981;
                padding: 8px 20px;
                border-radius: 50px;
                font-weight: bold;
                margin-top: 10px;
                box-shadow: 0 4px 6px rgba(0,0,0,0.2);
                animation: pulse 2s infinite;
            }}
            @keyframes pulse {{
                0%, 100% {{ transform: scale(1); }}
                50% {{ transform: scale(1.05); }}
            }}
            @keyframes fadeIn {{
                from {{ opacity: 0; transform: translateY(-20px); }}
                to {{ opacity: 1; transform: translateY(0); }}
            }}
            .grid {{
                display: grid;
                grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
                gap: 20px;
                margin-bottom: 30px;
            }}
            .card {{
                background: rgba(255, 255, 255, 0.1);
                backdrop-filter: blur(10px);
                border-radius: 15px;
                padding: 25px;
                box-shadow: 0 8px 32px rgba(0,0,0,0.2);
                border: 1px solid rgba(255, 255, 255, 0.2);
                animation: fadeIn 0.5s;
                transition: transform 0.3s;
            }}
            .card:hover {{
                transform: translateY(-5px);
            }}
            .card h2 {{
                font-size: 1.5rem;
                margin-bottom: 15px;
                display: flex;
                align-items: center;
                gap: 10px;
            }}
            .card-icon {{
                font-size: 2rem;
            }}
            .stat {{
                display: flex;
                justify-content: space-between;
                padding: 10px 0;
                border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            }}
            .stat:last-child {{
                border-bottom: none;
            }}
            .stat-label {{
                opacity: 0.8;
            }}
            .stat-value {{
                font-weight: bold;
                font-size: 1.1rem;
            }}
            .ip-box {{
                background: rgba(0, 0, 0, 0.3);
                padding: 15px;
                border-radius: 10px;
                margin-top: 15px;
                font-family: 'Courier New', monospace;
                font-size: 1.1rem;
                text-align: center;
                border: 2px solid rgba(255, 255, 255, 0.3);
            }}
            .progress-bar {{
                width: 100%;
                height: 20px;
                background: rgba(0, 0, 0, 0.3);
                border-radius: 10px;
                overflow: hidden;
                margin-top: 10px;
            }}
            .progress-fill {{
                height: 100%;
                background: linear-gradient(90deg, #10b981, #3b82f6);
                transition: width 0.3s;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 0.85rem;
                font-weight: bold;
            }}
            .endpoint-list {{
                list-style: none;
                margin-top: 15px;
            }}
            .endpoint-list li {{
                padding: 10px;
                background: rgba(0, 0, 0, 0.2);
                margin-bottom: 8px;
                border-radius: 8px;
                border-left: 4px solid #10b981;
            }}
            .endpoint-method {{
                display: inline-block;
                background: #3b82f6;
                padding: 3px 10px;
                border-radius: 5px;
                font-size: 0.85rem;
                font-weight: bold;
                margin-right: 10px;
            }}
            .footer {{
                text-align: center;
                margin-top: 40px;
                opacity: 0.8;
            }}
            .refresh-btn {{
                background: rgba(255, 255, 255, 0.2);
                border: 2px solid rgba(255, 255, 255, 0.5);
                color: white;
                padding: 12px 30px;
                border-radius: 50px;
                cursor: pointer;
                font-size: 1rem;
                font-weight: bold;
                transition: all 0.3s;
                margin-top: 20px;
            }}
            .refresh-btn:hover {{
                background: rgba(255, 255, 255, 0.3);
                transform: scale(1.05);
            }}
            .api-key-list {{
                font-size: 0.9rem;
                margin-top: 10px;
            }}
            .api-key-list div {{
                padding: 5px;
                background: rgba(0, 0, 0, 0.2);
                margin-bottom: 5px;
                border-radius: 5px;
                font-family: 'Courier New', monospace;
            }}
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Celestine Hosting Backend</h1>
                <p>Intelligent Website Generator & MOFH API Proxy</p>
                <div class="status-badge">🟒 ONLINE</div>
            </div>
            
            <div class="grid">
                <div class="card">
                    <h2><span class="card-icon">⏱️</span> Uptime</h2>
                    <div class="stat">
                        <span class="stat-label">Running Since</span>
                        <span class="stat-value">{datetime.fromtimestamp(START_TIME).strftime('%Y-%m-%d %H:%M:%S')}</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Uptime</span>
                        <span class="stat-value">{uptime_hours}h {uptime_minutes}m {uptime_secs}s</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Total Requests</span>
                        <span class="stat-value">{REQUEST_COUNT['total']}</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Errors</span>
                        <span class="stat-value">{REQUEST_COUNT['errors']}</span>
                    </div>
                </div>
                
                <div class="card">
                    <h2><span class="card-icon">🌐</span> Network Info</h2>
                    <div class="stat">
                        <span class="stat-label">Hostname</span>
                        <span class="stat-value">{hostname}</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Local IP</span>
                        <span class="stat-value">{local_ip}</span>
                    </div>
                    <div class="ip-box">
                        <div style="opacity: 0.7; font-size: 0.9rem; margin-bottom: 5px;">External IP Address</div>
                        <div style="font-size: 1.3rem; font-weight: bold;">{external_ip}</div>
                    </div>
                    <div style="margin-top: 15px; padding: 10px; background: rgba(255, 193, 7, 0.2); border-radius: 8px; font-size: 0.85rem;">
                        ⚠️ Add this IP to MOFH API whitelist
                    </div>
                </div>
                
                <div class="card">
                    <h2><span class="card-icon">πŸ’»</span> System Resources</h2>
                    <div class="stat">
                        <span class="stat-label">CPU Usage</span>
                        <span class="stat-value">{cpu_percent:.1f}%</span>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {cpu_percent}%">{cpu_percent:.1f}%</div>
                    </div>
                    <div class="stat" style="margin-top: 15px;">
                        <span class="stat-label">Memory Usage</span>
                        <span class="stat-value">{memory_percent:.1f}%</span>
                    </div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: {memory_percent}%">{memory_percent:.1f}%</div>
                    </div>
                    <div class="stat" style="margin-top: 10px;">
                        <span class="stat-label">Memory</span>
                        <span class="stat-value">{memory_used:.2f} GB / {memory_total:.2f} GB</span>
                    </div>
                </div>
            </div>
            
            <div class="grid">
                <div class="card">
                    <h2><span class="card-icon">πŸ€–</span> AI Configuration</h2>
                    <div class="stat">
                        <span class="stat-label">Active Models</span>
                        <span class="stat-value">{len(AI_MODELS['groq'])}</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">API Keys</span>
                        <span class="stat-value">{len(GROQ_API_KEYS)}</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Generations</span>
                        <span class="stat-value">{REQUEST_COUNT['generate']}</span>
                    </div>
                    <div class="api-key-list">
                        {''.join([f'<div>{usage}</div>' for usage in api_key_usage])}
                    </div>
                </div>
                
                <div class="card">
                    <h2><span class="card-icon">πŸ“‘</span> API Endpoints</h2>
                    <ul class="endpoint-list">
                        <li>
                            <span class="endpoint-method">GET</span>
                            <span>/</span>
                            <div style="font-size: 0.85rem; opacity: 0.8; margin-top: 5px;">System status dashboard</div>
                        </li>
                        <li>
                            <span class="endpoint-method">POST</span>
                            <span>/generate-website</span>
                            <div style="font-size: 0.85rem; opacity: 0.8; margin-top: 5px;">Generate website with IONA AI</div>
                        </li>
                        <li>
                            <span class="endpoint-method">POST</span>
                            <span>/create</span>
                            <div style="font-size: 0.85rem; opacity: 0.8; margin-top: 5px;">Create MOFH hosting account</div>
                        </li>
                        <li>
                            <span class="endpoint-method">GET</span>
                            <span>/health</span>
                            <div style="font-size: 0.85rem; opacity: 0.8; margin-top: 5px;">Health check endpoint</div>
                        </li>
                        <li>
                            <span class="endpoint-method">GET</span>
                            <span>/test</span>
                            <div style="font-size: 0.85rem; opacity: 0.8; margin-top: 5px;">Test AI generation</div>
                        </li>
                    </ul>
                </div>
                
                <div class="card">
                    <h2><span class="card-icon">πŸ‘¨β€πŸ’»</span> Developer Info</h2>
                    <div class="stat">
                        <span class="stat-label">Developer</span>
                        <span class="stat-value">Pratyush Srivastava</span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">GitHub</span>
                        <span class="stat-value"><a href="https://github.com/pratyush" style="color: #fff; text-decoration: none;">@pratyush</a></span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Portfolio</span>
                        <span class="stat-value"><a href="https://pratyush.dev" style="color: #fff; text-decoration: none;">pratyush.dev</a></span>
                    </div>
                    <div class="stat">
                        <span class="stat-label">Version</span>
                        <span class="stat-value">2026.1.0</span>
                    </div>
                </div>
            </div>
            
            <div style="text-align: center;">
                <button class="refresh-btn" onclick="location.reload()">πŸ”„ Refresh Status</button>
            </div>
            
            <div class="footer">
                <p>IONA AI - Intelligent Website Generator</p>
                <p style="font-size: 0.9rem; margin-top: 10px;">Powered by Groq AI β€’ FastAPI β€’ Python</p>
            </div>
        </div>
        
        <script>
            // Auto-refresh every 30 seconds
            setTimeout(() => location.reload(), 30000);
        </script>
    </body>
    </html>
    """
    
    return html_content

@app.get("/health")
def health_check():
    """Health check endpoint for monitoring"""
    return {
        "status": "healthy",
        "uptime_seconds": int(time.time() - START_TIME),
        "timestamp": datetime.now().isoformat(),
        "requests": REQUEST_COUNT
    }

@app.get("/test")
def test_generation():
    """Test AI generation with a simple prompt"""
    try:
        api_key = get_next_api_key()
        model = get_random_model()
        
        test_prompt = "Create a simple HTML page with a heading 'Hello World' and a paragraph."
        
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "model": model,
            "messages": [
                {"role": "system", "content": "You are a helpful assistant. Return only valid JSON."},
                {"role": "user", "content": test_prompt}
            ],
            "temperature": 0.7,
            "max_tokens": 500
        }
        
        response = requests.post(
            "https://api.groq.com/openai/v1/chat/completions",
            headers=headers,
            json=payload,
            timeout=30
        )
        
        if response.status_code == 200:
            result = response.json()
            return {
                "success": True,
                "model": model,
                "response_length": len(result['choices'][0]['message']['content']),
                "tokens_used": result.get('usage', {}),
                "message": "AI is working correctly!"
            }
        else:
            return {
                "success": False,
                "error": f"HTTP {response.status_code}",
                "message": "AI test failed"
            }
    except Exception as e:
        return {
            "success": False,
            "error": str(e),
            "message": "AI test failed with exception"
        }

@app.post("/generate-website")
def generate_website(req: AIWebsiteRequest):
    """IONA AI - Generate complete website with HTML, CSS, JS, and MySQL"""
    REQUEST_COUNT["total"] += 1
    REQUEST_COUNT["generate"] += 1
    
    try:
        logger.info("=== IONA AI Generation Started ===")
        logger.info(f"Business Type: {req.business_type}")
        logger.info(f"Prompt Length: {len(req.prompt)} chars")
        logger.info(f"PHP Only Mode: {req.php_only}")
        
        api_key = get_next_api_key()
        model = get_random_model()
        logger.info(f"Model: {model}, API Key: {api_key[:20]}...")
        
        # Get random images for this website
        selected_images = get_random_images(15)
        images_list = '\n'.join([f"- {img}" for img in selected_images])
        
        # Check if PHP-only mode
        if req.php_only:
            # PHP-only generation prompt
            system_prompt = f"""You are IONA AI, an expert PHP backend developer.

🎯 HOSTING ENVIRONMENT: VistaPanel (Free Hosting)
- PHP Version: 7.4 or 8.0
- MySQL Database available
- PDO and MySQLi supported

🎯 MISSION: Generate ONLY PHP BACKEND FILES (NO HTML/CSS/JS).

πŸ“¦ REQUIRED OUTPUT FORMAT:
Return ONLY a valid JSON object with these keys:
{{
  "contact_php": "Complete contact form handler with validation and email sending",
  "api_php": "RESTful API endpoints with JSON responses",
  "functions_php": "Helper functions for database, validation, sanitization",
  "config": "Database configuration with PDO connection",
  "sql": "MySQL schema with sample data",
  "readme": "Setup guide for VistaPanel"
}}

🐘 PHP FILE REQUIREMENTS:

1. **contact_php** (Contact Form Handler):
   - Handle POST requests
   - Validate: name, email, phone, message
   - Sanitize all inputs (htmlspecialchars, filter_var)
   - Send email using mail() function
   - Store in database
   - Return JSON response
   - CSRF protection
   - Rate limiting (session-based)
   - Error handling

2. **api_php** (API Endpoints):
   - RESTful structure
   - GET /api.php?action=list (list items)
   - POST /api.php?action=create (create item)
   - PUT /api.php?action=update (update item)
   - DELETE /api.php?action=delete (delete item)
   - JSON responses
   - Authentication (if needed)
   - Input validation
   - Error handling

3. **functions_php** (Helper Functions):
   - Database query helpers (select, insert, update, delete)
   - Input validation functions
   - Sanitization functions
   - Email sending function
   - Authentication helpers
   - Session management
   - Error logging

4. **config** (Database Configuration):
   - PDO connection setup
   - Database credentials (localhost, username, password, dbname)
   - Error handling (try-catch)
   - Timezone settings
   - Error reporting settings
   - Session configuration

5. **sql** (MySQL Schema):
   - CREATE DATABASE statement
   - CREATE TABLE statements
   - Proper data types (VARCHAR, INT, TEXT, DATETIME)
   - Primary keys (AUTO_INCREMENT)
   - Foreign keys and indexes
   - Timestamps (created_at, updated_at)
   - Sample data (5-10 realistic entries)

PHP SECURITY REQUIREMENTS:
- Use prepared statements (PDO) for ALL database queries
- Sanitize ALL user inputs
- Validate email addresses with filter_var()
- Use password_hash() for passwords
- Implement CSRF tokens
- Use htmlspecialchars() for output
- Set proper error reporting
- Use sessions securely

PHP BEST PRACTICES:
- PSR-12 coding standards
- Proper indentation (4 spaces)
- Clear variable names
- Comments for complex logic
- Separate concerns
- Return JSON for AJAX requests
- Use HTTP status codes
- Error handling (try-catch)

CODE FORMATTING:
- Proper indentation (4 spaces per level)
- Blank lines between functions
- Comments for each function
- Use \n for newlines in JSON strings

IMPORTANT JSON RULES:
- Return ONLY the JSON object
- NO markdown code blocks
- NO explanations
- Escape quotes with backslash
- Use \n for newlines
- Do NOT return null values
- Generate COMPLETE, FUNCTIONAL code

CREATE PROFESSIONAL PHP BACKEND FOR VISTAPANEL! πŸš€"""
            
            user_prompt = f"""Generate PHP backend files for: {req.prompt}

Requirements:
- Complete contact form handler
- RESTful API endpoints
- Helper functions
- Database configuration
- MySQL schema with sample data

Return as JSON with contact_php, api_php, functions_php, config, sql, and readme keys."""
        else:
            # Full website generation prompt
            system_prompt = f"""You are IONA AI, an elite full-stack web developer with 15+ years of experience.

🎯 HOSTING ENVIRONMENT: VistaPanel (Free Hosting)
- PHP Version: 7.4 or 8.0
- MySQL Database available
- File Manager: VistaPanel
- No SSH access
- Standard PHP functions available
- PDO and MySQLi supported

🎯 MISSION: Generate COMPLETE, PRODUCTION-READY, MULTI-FILE websites with REAL CONTENT.

πŸ“¦ REQUIRED OUTPUT FORMAT:
Return ONLY a valid JSON object with these keys:
{{
  "html": "Complete HTML5 document with PROPER INDENTATION (2 spaces per level)",
  "css": "Complete CSS with PROPER FORMATTING and line breaks",
  "js": "Complete JavaScript with PROPER FORMATTING",
  "php": "PHP backend files (contact.php, api.php, etc.) with PROPER FORMATTING",
  "sql": "MySQL schema with sample data (if database needed)",
  "config": "PHP config file with database connection (if database needed)",
  "readme": "Setup and customization guide for VistaPanel"
}}

πŸ–ΌοΈ USE THESE REAL IMAGES (randomly selected for this website):
{images_list}

IMPORTANT IMAGE USAGE:
- Use different images for hero, gallery, team, testimonials, blog posts
- Add proper alt text describing each image
- Use responsive image techniques (srcset if needed)
- Images are high-quality 1920x1080, perfect for hero sections

✨ CODE FORMATTING RULES (CRITICAL):
1. HTML: Proper indentation with 2 spaces per level
2. CSS: One rule per line, organized by sections with comments
3. JavaScript: Proper function formatting with line breaks
4. PHP: PSR-12 coding standards, proper indentation
5. NO MINIFIED CODE - Make it readable and maintainable
6. Add blank lines between major sections
7. Use \n for newlines in JSON strings
8. Properly indent nested elements

🐘 PHP FILE GENERATION (IMPORTANT):
When generating PHP files, create COMPLETE, FUNCTIONAL code:

1. **contact.php** (Contact Form Handler):
   - Validate all inputs (name, email, message)
   - Sanitize data (htmlspecialchars, filter_var)
   - Send email using mail() function
   - Store in database if requested
   - Return JSON response
   - Include CSRF protection
   - Rate limiting (session-based)

2. **config.php** (Database Configuration):
   - PDO connection setup
   - Error handling (try-catch)
   - Database credentials (localhost, username, password, dbname)
   - Timezone settings
   - Error reporting settings
   - Session configuration

3. **functions.php** (Helper Functions):
   - Database query helpers
   - Input validation functions
   - Sanitization functions
   - Email sending function
   - Authentication helpers (if needed)

4. **api.php** (API Endpoints):
   - RESTful API structure
   - JSON responses
   - Error handling
   - Input validation
   - CORS headers (if needed)

PHP SECURITY REQUIREMENTS:
- Use prepared statements (PDO) for all database queries
- Sanitize ALL user inputs
- Validate email addresses with filter_var()
- Use password_hash() for passwords
- Implement CSRF tokens
- Use htmlspecialchars() for output
- Set proper error reporting
- Use sessions securely

PHP BEST PRACTICES:
- PSR-12 coding standards
- Proper error handling (try-catch)
- Clear variable names
- Comments for complex logic
- Separate concerns (config, functions, handlers)
- Return JSON for AJAX requests
- Use HTTP status codes

Example HTML formatting:
```
<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"UTF-8\">\n  <title>Page</title>\n</head>\n<body>\n  <header>\n    <nav>\n      <ul>\n        <li><a href=\"#home\">Home</a></li>\n      </ul>\n    </nav>\n  </header>\n</body>\n</html>
```

Example PHP formatting:
```
<?php\nsession_start();\n\nif ($_SERVER['REQUEST_METHOD'] === 'POST') {{\n  $name = htmlspecialchars($_POST['name']);\n  $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);\n  \n  if (empty($name) || empty($email)) {{\n    echo json_encode(['success' => false, 'error' => 'Required fields missing']);\n    exit;\n  }}\n  \n  // Process form\n  echo json_encode(['success' => true, 'message' => 'Form submitted']);\n}}\n?>
```

πŸ“ CONTENT REQUIREMENTS:
1. Write REAL, PROFESSIONAL content (not Lorem Ipsum)
2. Create compelling headlines and descriptions
3. Write realistic testimonials with names
4. Generate actual blog post content
5. Create detailed service/product descriptions
6. Write engaging About Us content
7. Include realistic contact information (use placeholders)

🎨 DESIGN EXCELLENCE:
- Modern, professional, visually stunning
- Consistent spacing (8px grid system)
- Smooth animations (0.3s transitions)
- Hover effects on interactive elements
- Mobile-first responsive design
- Glassmorphism, gradients, shadows
- Professional color palette
- Clean typography (2-3 fonts max)

πŸ”§ TECHNICAL REQUIREMENTS:
- Semantic HTML5 (header, nav, main, section, article, footer)
- CSS Grid + Flexbox layouts
- Vanilla JavaScript (ES6+)
- Responsive breakpoints: 320px, 768px, 1024px, 1440px
- Form validation with error messages
- Smooth scroll behavior
- Loading animations
- Interactive elements (accordions, tabs, modals)

πŸ“± MUST INCLUDE SECTIONS:
1. Hero section with CTA button
2. Navigation (sticky on scroll, mobile menu)
3. About/Services section
4. Features/Benefits cards
5. Gallery/Portfolio (if applicable)
6. Testimonials (if applicable)
7. Pricing tables (if applicable)
8. Team members (if applicable)
9. Blog posts grid (if applicable)
10. Contact form with validation
11. Footer with social links
12. Back-to-top button

πŸ—„οΈ DATABASE (if requested):
- Normalized MySQL schema
- Proper indexes and foreign keys
- Sample data (5-10 realistic entries)
- PHP config with PDO connection
- CREATE DATABASE statement
- CREATE TABLE statements
- INSERT sample data
- Proper data types (VARCHAR, INT, TEXT, DATETIME)
- Auto-increment primary keys
- Timestamps (created_at, updated_at)

⚑ INTERACTIVE FEATURES:
- Smooth scroll to sections
- Fade-in animations on scroll
- Form validation (real-time)
- AJAX form submission (with PHP backend)
- Image lightbox/modal
- Mobile hamburger menu
- Loading spinner
- Success/error messages
- Accordion/FAQ
- Tabs for content
- Carousel/slider (if needed)

πŸš€ PERFORMANCE:
- Optimized CSS (no unused styles)
- Efficient JavaScript
- Lazy loading for images
- Minimal HTTP requests
- Fast animations

πŸ“Š SEO & ACCESSIBILITY:
- Proper meta tags (title, description, keywords)
- Open Graph tags
- Semantic HTML structure
- Alt text for all images
- ARIA labels where needed
- Keyboard navigation
- Focus indicators
- Sufficient color contrast

πŸ“‹ VISTAPANEL SETUP INSTRUCTIONS (in README):
1. Upload files via File Manager
2. Create MySQL database in VistaPanel
3. Import SQL file using phpMyAdmin
4. Update config.php with database credentials
5. Set file permissions (755 for directories, 644 for files)
6. Test contact form
7. Customize content and images

IMPORTANT JSON RULES:
- Return ONLY the JSON object
- NO markdown code blocks
- NO explanations before or after
- Escape quotes with backslash
- Use \n for newlines
- Ensure valid JSON syntax
- Do NOT return null values
- Generate COMPLETE, FUNCTIONAL code for ALL files

CREATE PROFESSIONAL, PRODUCTION-READY CODE FOR VISTAPANEL! πŸš€"""
        
        # Enhanced user prompt with detailed specifications
        features_text = ', '.join(req.features) if req.features else 'Standard features'
        color_guide = _get_color_scheme_guide(req.color_scheme)
        
        user_prompt = f"""🎨 PROJECT BRIEF:

πŸ“‹ WEBSITE TYPE: {req.business_type.upper()}

πŸ’¬ CLIENT DESCRIPTION:
{req.prompt}

🎨 DESIGN SPECIFICATIONS:
- Color Scheme: {req.color_scheme}
- Style: Modern, professional, visually stunning
- Layout: Clean, spacious, well-organized
- Typography: Professional, readable, hierarchical

✨ REQUIRED FEATURES:
{features_text}

πŸ—„οΈ DATABASE: {'YES - Include full MySQL schema with sample data' if req.include_database else 'NO - Static website only'}

🎯 SPECIFIC REQUIREMENTS:

1. HERO SECTION:
   - Eye-catching headline
   - Compelling subheadline
   - Clear call-to-action button
   - Background: gradient or image
   - Smooth scroll-down indicator

2. NAVIGATION:
   - Sticky header on scroll
   - Smooth scroll to sections
   - Mobile hamburger menu
   - Active section highlighting

3. CONTENT SECTIONS:
   - About/Services section with icons
   - Features/Benefits with cards
   - Testimonials with star ratings (if requested)
   - Gallery with lightbox (if requested)
   - Pricing tables (if requested)
   - Team members (if requested)
   - Blog posts grid (if requested)
   - FAQ accordion (if applicable)

4. CONTACT SECTION:
   - Working contact form with validation
   - Email, phone, address display
   - Social media links
   - Google Maps embed (placeholder)

5. FOOTER:
   - Multi-column layout
   - Quick links
   - Social media icons
   - Copyright notice
   - Back-to-top button

6. INTERACTIVE ELEMENTS:
   - Smooth scroll animations (fade-in, slide-up)
   - Hover effects on cards and buttons
   - Loading states for forms
   - Success/error messages
   - Image lazy loading
   - Parallax effects (subtle)

7. FORMS (if applicable):
   - Real-time validation
   - Clear error messages
   - Success confirmation
   - Spam protection (honeypot)
   - Required field indicators

8. PERFORMANCE:
   - Optimized CSS (no unused styles)
   - Efficient JavaScript (no jQuery)
   - Fast loading animations
   - Minimal HTTP requests

9. SEO:
   - Proper meta tags (title, description, keywords)
   - Open Graph tags for social sharing
   - Structured data (JSON-LD)
   - Semantic HTML structure
   - Alt text for all images

10. ACCESSIBILITY:
    - ARIA labels where needed
    - Keyboard navigation support
    - Focus indicators
    - Sufficient color contrast
    - Screen reader friendly

{f'''11. DATABASE STRUCTURE (if applicable):
    - Users table (id, name, email, password, created_at)
    - Content table (id, title, description, image, created_at)
    - Messages/Contacts table (id, name, email, message, created_at)
    - Categories/Tags (if blog)
    - Proper relationships and indexes
    - Sample data for testing''' if req.include_database else ''}

🎨 COLOR SCHEME GUIDANCE:
{color_guide}

πŸ“± RESPONSIVE BREAKPOINTS:
- Mobile: 320px - 767px (single column, stacked layout)
- Tablet: 768px - 1023px (2 columns where appropriate)
- Desktop: 1024px+ (full layout, max-width 1400px)

⚑ MUST INCLUDE:
- Favicon link (placeholder)
- Google Fonts (1-2 professional fonts)
- Font Awesome icons (CDN)
- Smooth scroll behavior
- Loading animation on page load
- 404 error handling
- Print stylesheet basics

🚫 AVOID:
- jQuery or heavy frameworks
- Inline styles (except critical CSS)
- !important in CSS (unless necessary)
- Console.log in production code
- Hardcoded sensitive data
- Broken links or placeholder text

πŸ“¦ DELIVERABLES:
Return ONLY valid JSON with these exact keys:
- html: Complete HTML5 document
- css: Complete stylesheet with comments
- js: Complete JavaScript with error handling
- sql: MySQL schema with sample data (if database requested)
- config: PHP database config (if database requested)
- readme: Detailed setup and customization guide

CREATE A WEBSITE THAT WILL IMPRESS AND DELIGHT! πŸš€βœ¨"""
        
        logger.info(f"Prompt sizes - System: {len(system_prompt)}, User: {len(user_prompt)}")
        
        # Call Groq API
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "model": model,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            "temperature": 0.7,
            "max_tokens": 8000,
            "top_p": 0.9
        }
        
        logger.info("Calling Groq API...")
        response = requests.post(
            "https://api.groq.com/openai/v1/chat/completions",
            headers=headers,
            json=payload,
            timeout=60
        )
        
        logger.info(f"Groq Response: {response.status_code}")
        
        if response.status_code != 200:
            logger.error(f"Groq Error: {response.text}")
            raise HTTPException(status_code=response.status_code, detail=response.text)
        
        result = response.json()
        raw_content = result['choices'][0]['message']['content']
        logger.info(f"Response length: {len(raw_content)} chars")
        
        # Try to extract JSON from markdown code blocks if present
        if '```json' in raw_content:
            import re
            json_match = re.search(r'```json\s*(.+?)\s*```', raw_content, re.DOTALL)
            if json_match:
                raw_content = json_match.group(1)
                logger.info("Extracted JSON from markdown block")
        elif '```' in raw_content:
            import re
            json_match = re.search(r'```\s*(.+?)\s*```', raw_content, re.DOTALL)
            if json_match:
                raw_content = json_match.group(1)
                logger.info("Extracted content from code block")
        
        logger.info(f"Preview: {raw_content[:200]}...")
        
        try:
            generated_content = json.loads(raw_content)
            
            # Validate that we have the expected structure
            if not isinstance(generated_content, dict):
                raise ValueError("Response is not a JSON object")
            
            # Check if we have the required files
            required_keys = ['html', 'css', 'js']
            has_required = any(key in generated_content for key in required_keys)
            
            if not has_required:
                # Check if the response is double-encoded (JSON string containing JSON)
                if len(generated_content) == 1:
                    first_key = list(generated_content.keys())[0]
                    first_value = generated_content[first_key]
                    if isinstance(first_value, str) and first_value.strip().startswith('{'):
                        try:
                            inner_json = json.loads(first_value)
                            if isinstance(inner_json, dict) and any(k in inner_json for k in required_keys):
                                generated_content = inner_json
                                logger.info("Unwrapped double-encoded JSON")
                        except:
                            pass
            
            # Final validation
            if 'html' not in generated_content:
                logger.warning("No HTML in response, using fallback")
                raise ValueError("Missing HTML content")
            
            logger.info(f"Files: {list(generated_content.keys())}")
            
        except (json.JSONDecodeError, ValueError) as je:
            logger.error(f"JSON parse failed: {str(je)}")
            
            # Try to fix common JSON issues
            try:
                # Remove markdown code blocks if still present
                if '```' in raw_content:
                    parts = raw_content.split('```')
                    for part in parts:
                        if '{' in part and '}' in part:
                            raw_content = part.replace('json\n', '').replace('json', '').strip()
                            break
                
                # Try parsing again
                generated_content = json.loads(raw_content)
                
                # Validate again
                if not isinstance(generated_content, dict) or 'html' not in generated_content:
                    raise ValueError("Invalid structure after cleanup")
                
                logger.info("Fixed JSON after cleanup")
                logger.info(f"Files: {list(generated_content.keys())}")
                
            except Exception as cleanup_error:
                # Final fallback: create minimal structure only if we have HTML-like content
                logger.warning(f"Using fallback structure: {str(cleanup_error)}")
                
                if '<html' in raw_content.lower() or '<!doctype' in raw_content.lower():
                    # Extract HTML if present
                    generated_content = {
                        "html": raw_content,
                        "css": "/* Generated by IONA AI */\nbody { font-family: Arial, sans-serif; margin: 0; padding: 20px; }",
                        "js": "// Generated by IONA AI\nconsole.log('Website loaded');",
                        "readme": "# Generated Website\n\nThis website was generated by IONA AI.\n\nNote: AI response had formatting issues, minimal CSS/JS provided."
                    }
                else:
                    # Cannot recover - throw error
                    raise HTTPException(
                        status_code=500, 
                        detail=f"AI returned invalid response format. Please try again. Error: {str(je)}"
                    )
        
        logger.info("=== Generation Complete ===")
        
        return {
            "success": True,
            "model_used": model,
            "files": generated_content,
            "tokens_used": result.get('usage', {}),
            "generation_time": result.get('created', time.time())
        }
        
    except json.JSONDecodeError as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"JSON Error: {str(e)}")
        raise HTTPException(
            status_code=500, 
            detail=f"AI response parsing failed: {str(e)}. The AI may be overloaded, please try again."
        )
    
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Error: {str(e)}")
        logger.exception("Traceback:")
        raise HTTPException(status_code=500, detail=f"AI Error: {str(e)}")

def _get_color_scheme_guide(color_scheme: str) -> str:
    """Return detailed color guidance based on selected scheme"""
    schemes = {
        "modern": """Primary: #6366f1 (Indigo), Secondary: #8b5cf6 (Purple)
Accent: #ec4899 (Pink), Background: #0f172a (Dark Blue)
Text: #f1f5f9 (Light Gray), Use gradients and glassmorphism""",
        "dark": """Primary: #3b82f6 (Blue), Secondary: #10b981 (Green)
Accent: #f59e0b (Amber), Background: #111827 (Very Dark)
Text: #f9fafb (White), High contrast, neon accents""",
        "light": """Primary: #2563eb (Blue), Secondary: #7c3aed (Purple)
Accent: #dc2626 (Red), Background: #ffffff (White)
Text: #1f2937 (Dark Gray), Clean, minimal, lots of whitespace""",
        "vibrant": """Primary: #f43f5e (Rose), Secondary: #8b5cf6 (Purple)
Accent: #f59e0b (Amber), Background: #1e293b (Dark Slate)
Text: #f1f5f9 (Light), Bold colors, high energy""",
        "professional": """Primary: #1e40af (Navy), Secondary: #475569 (Slate)
Accent: #0891b2 (Cyan), Background: #f8fafc (Off White)
Text: #0f172a (Dark), Corporate, trustworthy""",
        "warm": """Primary: #ea580c (Orange), Secondary: #dc2626 (Red)
Accent: #facc15 (Yellow), Background: #292524 (Warm Dark)
Text: #fafaf9 (Warm White), Cozy, inviting""",
        "nature": """Primary: #16a34a (Green), Secondary: #65a30d (Lime)
Accent: #0891b2 (Teal), Background: #1c1917 (Earth Dark)
Text: #fafaf9 (Natural White), Organic, earthy"""
    }
    return schemes.get(color_scheme, schemes["modern"])

@app.post("/create")
def create_account(req: AccountRequest):
    """Create MOFH hosting account"""
    REQUEST_COUNT["total"] += 1
    REQUEST_COUNT["create"] += 1
    
    try:
        # Use HTTP Basic Authentication (the correct method)
        auth_string = f"{MOFH_API_USERNAME}:{MOFH_API_PASSWORD}"
        auth_b64 = base64.b64encode(auth_string.encode('utf-8')).decode('utf-8')
        
        headers = {
            'Authorization': f'Basic {auth_b64}',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        
        # Data WITHOUT api_user/api_key (they're in the header)
        api_data = {
            'username': req.username,
            'password': req.password,
            'contactemail': req.email,
            'domain': req.domain,
            'plan': req.plan
        }
        
        response = requests.post(
            f"{MOFH_API_URL}createacct.php",
            data=api_data,
            headers=headers,
            timeout=30,
            verify=False
        )
        
        return {
            'success': response.status_code == 200,
            'http_code': response.status_code,
            'response': response.text,
            'debug': {
                'api_url': f"{MOFH_API_URL}createacct.php",
                'auth_method': 'Basic Auth (HTTP Header)',
                'api_user_length': len(MOFH_API_USERNAME),
                'api_key_length': len(MOFH_API_PASSWORD),
                'sent_params': list(api_data.keys())
            }
        }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        raise HTTPException(status_code=500, detail=str(e))

class FTPDeployRequest(BaseModel):
    ftp_host: str
    ftp_user: str
    ftp_pass: str
    files: dict  # {"index.html": "content", "style.css": "content", ...}
    
    @validator('ftp_host')
    def validate_host(cls, v):
        if not v or len(v) < 3:
            raise ValueError('Invalid FTP host')
        return v.strip()

@app.post("/deploy-ftp")
def deploy_via_ftp(req: FTPDeployRequest):
    """Deploy files to FTP server (HF Space has no firewall restrictions)"""
    REQUEST_COUNT["total"] += 1
    
    try:
        import ftplib
        from io import BytesIO
        
        logger.info(f"[FTP-DEPLOY] Connecting to {req.ftp_host}")
        
        deployed_files = []
        errors = []
        
        # Try regular FTP first
        ftp = None
        try:
            ftp = ftplib.FTP(timeout=30)
            ftp.connect(req.ftp_host, 21)
            ftp.login(req.ftp_user, req.ftp_pass)
            logger.info("[FTP-DEPLOY] Connected via regular FTP")
        except Exception as e:
            logger.warning(f"[FTP-DEPLOY] Regular FTP failed: {e}")
            # Try FTP_TLS
            try:
                ftp = ftplib.FTP_TLS(timeout=30)
                ftp.connect(req.ftp_host, 21)
                ftp.login(req.ftp_user, req.ftp_pass)
                ftp.prot_p()  # Enable encryption
                logger.info("[FTP-DEPLOY] Connected via FTP_TLS")
            except Exception as e2:
                logger.error(f"[FTP-DEPLOY] FTP_TLS also failed: {e2}")
                raise HTTPException(status_code=500, detail=f"FTP connection failed: {str(e2)}")
        
        # Set passive mode
        ftp.set_pasv(True)
        
        # Try to change to web directory
        web_dirs = ['htdocs', 'public_html', 'www', 'html']
        current_dir = '/'
        
        for web_dir in web_dirs:
            try:
                ftp.cwd(web_dir)
                current_dir = ftp.pwd()
                logger.info(f"[FTP-DEPLOY] Changed to directory: {current_dir}")
                break
            except:
                continue
        
        logger.info(f"[FTP-DEPLOY] Working directory: {current_dir}")
        
        # Upload each file
        for filename, content in req.files.items():
            try:
                logger.info(f"[FTP-DEPLOY] Uploading {filename} ({len(content)} bytes)")
                
                # Convert string to bytes
                file_bytes = BytesIO(content.encode('utf-8'))
                
                # Upload file
                ftp.storbinary(f'STOR {filename}', file_bytes)
                
                deployed_files.append(filename)
                logger.info(f"[FTP-DEPLOY] βœ“ {filename} uploaded")
                
            except Exception as e:
                error_msg = f"{filename}: {str(e)}"
                errors.append(error_msg)
                logger.error(f"[FTP-DEPLOY] βœ— {error_msg}")
        
        # Close FTP connection
        try:
            ftp.quit()
        except:
            ftp.close()
        
        logger.info(f"[FTP-DEPLOY] Deployment complete. Success: {len(deployed_files)}, Errors: {len(errors)}")
        
        if not deployed_files:
            raise HTTPException(status_code=500, detail=f"No files deployed. Errors: {', '.join(errors)}")
        
        return {
            "success": True,
            "deployed_files": deployed_files,
            "errors": errors,
            "message": f"Successfully deployed {len(deployed_files)} file(s)"
        }
        
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"[FTP-DEPLOY] Error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Deployment failed: {str(e)}")


class GenerateAndDeployRequest(BaseModel):
    prompt: str
    business_type: Optional[str] = "general"
    color_scheme: Optional[str] = "modern"
    include_database: Optional[bool] = False
    features: Optional[List[str]] = []
    ftp_host: str
    ftp_user: str
    ftp_pass: str
    
    @validator('prompt')
    def validate_prompt(cls, v):
        if not v or len(v.strip()) < 10:
            raise ValueError('Prompt must be at least 10 characters')
        if len(v) > 10000:
            raise ValueError('Prompt too long (max 10000 characters)')
        return v.strip()

@app.post("/generate-and-deploy")
def generate_and_deploy(req: GenerateAndDeployRequest):
    """Generate website with AI and deploy to FTP in one call"""
    REQUEST_COUNT["total"] += 1
    REQUEST_COUNT["generate"] += 1
    
    logs = []
    
    try:
        import ftplib
        from io import BytesIO
        
        logs.append({"time": time.time(), "type": "info", "message": "Starting AI generation..."})
        
        logger.info("=== IONA AI Generation + Deployment Started ===")
        logger.info(f"Prompt: {req.prompt[:100]}...")
        logger.info(f"FTP Host: {req.ftp_host}, User: {req.ftp_user}")
        
        api_key = get_next_api_key()
        model = get_random_model()
        
        logs.append({"time": time.time(), "type": "info", "message": f"Using model: {model}"})
        
        # Generate with AI - FORCE JSON MODE WITH DETAILED INSTRUCTIONS
        system_prompt = """You are a web development code generator.

Generate a complete, functional website with HTML, CSS, and JavaScript.

Return a JSON object with these keys:
- html: A complete HTML5 document with proper structure
- css: Complete CSS stylesheet with modern styling
- js: JavaScript code for interactivity
- readme: Brief setup instructions

The website must be:
- Fully functional and complete
- Mobile responsive
- Modern and professional
- Include all requested features

IMPORTANT CODE FORMATTING RULES:
- Use proper indentation (2 spaces per level)
- Add newlines between sections
- Format code readably with line breaks
- Do NOT put everything on one line
- Use \n for newlines in JSON strings
- Properly indent nested elements

IMPORTANT: Do NOT return null values. Generate actual code for each field."""
        
        user_prompt = f"""Generate a {req.business_type} website with this description:

{req.prompt}

Use {req.color_scheme} color scheme and include {', '.join(req.features) if req.features else 'standard'} features.

Return as JSON with html, css, js, and readme keys."""
        
        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json"
        }
        
        payload = {
            "model": model,
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            "temperature": 0.7,  # Balanced for creativity and consistency
            "max_tokens": 32000,  # Maximum tokens for complete generation
            "response_format": {"type": "json_object"}  # Force JSON mode
        }
        
        logs.append({"time": time.time(), "type": "info", "message": "Calling Groq AI..."})
        
        response = requests.post(
            "https://api.groq.com/openai/v1/chat/completions",
            headers=headers,
            json=payload,
            timeout=180  # 3 minutes for complex generation
        )
        
        if response.status_code != 200:
            logs.append({"time": time.time(), "type": "error", "message": f"Groq API error: HTTP {response.status_code}"})
            raise HTTPException(status_code=response.status_code, detail=response.text)
        
        result = response.json()
        raw_content = result['choices'][0]['message']['content']
        
        logger.info(f"[GENERATE-DEPLOY] Raw response length: {len(raw_content)}")
        logger.info(f"[GENERATE-DEPLOY] Raw response preview: {raw_content[:200]}")
        
        logs.append({"time": time.time(), "type": "success", "message": "AI generation complete"})
        logs.append({"time": time.time(), "type": "info", "message": "Parsing AI response..."})
        
        # Parse JSON with better error handling
        generated_content = None
        
        # Try extracting from markdown code blocks
        if '```json' in raw_content:
            import re
            json_match = re.search(r'```json\s*(.+?)\s*```', raw_content, re.DOTALL)
            if json_match:
                raw_content = json_match.group(1)
                logger.info("[GENERATE-DEPLOY] Extracted from ```json block")
        elif '```' in raw_content:
            import re
            json_match = re.search(r'```\s*(.+?)\s*```', raw_content, re.DOTALL)
            if json_match:
                raw_content = json_match.group(1)
                logger.info("[GENERATE-DEPLOY] Extracted from ``` block")
        
        # Try parsing JSON
        try:
            generated_content = json.loads(raw_content)
            logger.info(f"[GENERATE-DEPLOY] Parsed JSON successfully, keys: {list(generated_content.keys())}")
        except json.JSONDecodeError as e:
            logger.error(f"[GENERATE-DEPLOY] JSON parse failed: {e}")
            logger.error(f"[GENERATE-DEPLOY] Content: {raw_content[:500]}")
            
            # Try to find JSON object in the response
            import re
            json_pattern = r'\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}'
            matches = re.findall(json_pattern, raw_content, re.DOTALL)
            
            for match in matches:
                try:
                    generated_content = json.loads(match)
                    if 'html' in generated_content:
                        logger.info("[GENERATE-DEPLOY] Found valid JSON in response")
                        break
                except:
                    continue
            
            if not generated_content:
                logs.append({"time": time.time(), "type": "error", "message": "AI returned invalid JSON format"})
                raise ValueError(f"AI response is not valid JSON. Error: {str(e)}. Response preview: {raw_content[:200]}")
        
        if 'html' not in generated_content:
            logs.append({"time": time.time(), "type": "error", "message": "No HTML in AI response"})
            raise ValueError("No HTML in AI response")
        
        # Check if HTML is null or empty
        html_content = generated_content.get('html', '')
        if not html_content or html_content == 'null' or len(html_content.strip()) < 100:
            logs.append({"time": time.time(), "type": "error", "message": f"AI returned invalid HTML (length: {len(html_content)})"})
            logs.append({"time": time.time(), "type": "error", "message": f"Model used: {model}"})
            logs.append({"time": time.time(), "type": "error", "message": f"Response preview: {str(generated_content)[:200]}"})
            raise ValueError(f"AI returned null or empty HTML content. Model: {model}. Please try again.")
        
        logs.append({"time": time.time(), "type": "success", "message": "AI response parsed successfully"})
        logs.append({"time": time.time(), "type": "info", "message": f"Generated {len(generated_content.get('html', ''))} chars of HTML"})
        
        logs.append({"time": time.time(), "type": "info", "message": "FTP blocked - returning files to PHP for deployment"})
        
        # HF Space cannot connect to FTP (port 21 blocked)
        # Return generated files to PHP for deployment
        logger.info("[GENERATE-DEPLOY] FTP blocked by HF network - returning files")
        
        return {
            "success": True,
            "model_used": model,
            "deployed_files": [],
            "files": {
                'html': generated_content.get('html', ''),
                'css': generated_content.get('css', ''),
                'js': generated_content.get('js', ''),
                'php': generated_content.get('php', ''),
                'sql': generated_content.get('sql', ''),
                'config': generated_content.get('config', ''),
                'readme': generated_content.get('readme', '')
            },
            "ftp_blocked": True,
            "logs": logs,
            "message": "Website generated successfully. FTP blocked - files returned for PHP deployment."
        }
        
    except HTTPException:
        raise
    except json.JSONDecodeError as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"[GENERATE-DEPLOY] JSON Error: {str(e)}")
        logs.append({"time": time.time(), "type": "error", "message": f"JSON parsing failed: {str(e)}"})
        raise HTTPException(status_code=500, detail=f"AI returned invalid JSON format. Please try again. Error: {str(e)}")
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"[GENERATE-DEPLOY] Error: {str(e)}")
        logs.append({"time": time.time(), "type": "error", "message": str(e)})
        raise HTTPException(status_code=500, detail=f"Generation/Deployment failed: {str(e)}")


# ============================================================================
# GITHUB DEPLOYMENT ENDPOINTS
# ============================================================================

# Import GitHub deployment module
github_deploy = None
try:
    from github_deploy import GitHubDeployment
    
    # Initialize GitHub Deployment (will be configured via environment variables)
    GITHUB_APP_ID = os.getenv('GITHUB_APP_ID', '')
    GITHUB_CLIENT_ID = os.getenv('GITHUB_CLIENT_ID', '')
    GITHUB_CLIENT_SECRET = os.getenv('GITHUB_CLIENT_SECRET', '')
    GITHUB_PRIVATE_KEY = os.getenv('GITHUB_PRIVATE_KEY', '').replace('\\n', '\n')
    
    if GITHUB_APP_ID and GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET and GITHUB_PRIVATE_KEY:
        try:
            github_deploy = GitHubDeployment(
                GITHUB_APP_ID,
                GITHUB_CLIENT_ID,
                GITHUB_CLIENT_SECRET,
                GITHUB_PRIVATE_KEY
            )
            logger.info("βœ… GitHub Deployment initialized successfully")
        except Exception as e:
            logger.error(f"❌ GitHub Deployment initialization failed: {e}")
    else:
        logger.info("ℹ️ GitHub Deployment not configured (environment variables not set)")
        
except ImportError as e:
    logger.error(f"❌ Failed to import github_deploy module: {e}")
    logger.error("❌ Make sure github_deploy.py is in the same directory as app.py")
except Exception as e:
    logger.error(f"❌ Error loading GitHub deployment: {e}")


class GitHubOAuthRequest(BaseModel):
    code: str


class GitHubInstallationTokenRequest(BaseModel):
    installation_id: str


class GitHubDeploymentStatusRequest(BaseModel):
    owner: str
    repo: str
    run_id: int
    installation_id: str


class GitHubRepoRequest(BaseModel):
    access_token: str


class GitHubSetupDeploymentRequest(BaseModel):
    owner: str
    repo: str
    access_token: str
    ftp_server: str
    ftp_username: str
    ftp_password: str
    branch: Optional[str] = 'main'


class GitHubWorkflowRunsRequest(BaseModel):
    owner: str
    repo: str
    access_token: str
    limit: Optional[int] = 10


class GitHubWorkflowLogsRequest(BaseModel):
    owner: str
    repo: str
    run_id: int
    access_token: str


class GitHubTriggerWorkflowRequest(BaseModel):
    owner: str
    repo: str
    access_token: str
    workflow_file: Optional[str] = 'celestine_deploy.yml'
    branch: Optional[str] = 'main'


@app.get("/github/status")
def github_status():
    """Check if GitHub integration is configured"""
    return {
        "configured": github_deploy is not None,
        "app_id": GITHUB_APP_ID if GITHUB_APP_ID else None,
        "client_id": GITHUB_CLIENT_ID if GITHUB_CLIENT_ID else None,
        "message": "GitHub integration is ready" if github_deploy else "GitHub integration not configured"
    }


@app.post("/github/oauth/exchange")
def github_oauth_exchange(req: GitHubOAuthRequest):
    """Exchange OAuth code for access token AND get installation ID"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        # Get OAuth token
        result = github_deploy.exchange_code_for_token(req.code)
        access_token = result.get('access_token')
        
        # Get user's GitHub App installations
        headers = {
            'Authorization': f'token {access_token}',
            'Accept': 'application/vnd.github.v3+json'
        }
        
        logger.info("πŸ“¦ Fetching user's GitHub App installations...")
        response = requests.get(
            'https://api.github.com/user/installations',
            headers=headers,
            timeout=30
        )
        
        installation_id = None
        if response.status_code == 200:
            installations = response.json().get('installations', [])
            if installations:
                installation_id = str(installations[0]['id'])
                logger.info(f"βœ“ Found installation ID: {installation_id}")
            else:
                logger.warning("⚠ No installations found for user")
        else:
            logger.warning(f"⚠ Failed to get installations: {response.status_code}")
        
        return {
            "success": True,
            "access_token": access_token,  # Only for initial repo listing
            "installation_id": installation_id,  # Store this! Never expires!
            "token_type": result.get('token_type'),
            "scope": result.get('scope')
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"OAuth exchange failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/installation-token")
def get_installation_token(req: GitHubInstallationTokenRequest):
    """
    Get fresh installation token (valid for 1 hour)
    This is the KEY to solving token expiration issues!
    """
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        logger.info(f"πŸ” Generating installation token for installation_id: {req.installation_id}")
        token = github_deploy.get_installation_access_token(req.installation_id)
        logger.info(f"βœ“ Installation token generated (length: {len(token)})")
        
        return {
            "success": True,
            "token": token,
            "expires_in": 3600,  # 1 hour
            "message": "Fresh installation token generated"
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get installation token: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/deployment-status")
def get_deployment_status(req: GitHubDeploymentStatusRequest):
    """
    Get deployment status using installation token (never expires!)
    This replaces the need for stored OAuth tokens
    """
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        # Generate fresh installation token
        installation_token = github_deploy.get_installation_access_token(req.installation_id)
        
        # Get workflow run status
        headers = {
            'Authorization': f'token {installation_token}',
            'Accept': 'application/vnd.github.v3+json'
        }
        
        response = requests.get(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs/{req.run_id}',
            headers=headers,
            timeout=30
        )
        
        if response.status_code != 200:
            raise HTTPException(status_code=response.status_code, detail=response.text)
        
        data = response.json()
        
        return {
            "success": True,
            "status": data.get('status'),
            "conclusion": data.get('conclusion'),
            "created_at": data.get('created_at'),
            "updated_at": data.get('updated_at'),
            "html_url": data.get('html_url')
        }
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get deployment status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/repos")
def github_get_repos(req: GitHubRepoRequest):
    """Get user's GitHub repositories"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        repos = github_deploy.get_user_repos(req.access_token)
        return {
            "success": True,
            "repositories": repos,
            "count": len(repos)
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get repos: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/detect-project")
def github_detect_project(req: GitHubSetupDeploymentRequest):
    """Detect project type and build configuration"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        project_config = github_deploy.detect_project_type(
            req.owner,
            req.repo,
            req.access_token
        )
        return {
            "success": True,
            "project_config": project_config
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Project detection failed: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/setup-deployment")
def github_setup_deployment(req: GitHubSetupDeploymentRequest):
    """
    Complete deployment setup:
    1. Detect project type
    2. Create FTP secrets
    3. Inject workflow file
    """
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        logger.info("=" * 80)
        logger.info("πŸš€ GITHUB DEPLOYMENT SETUP STARTED")
        logger.info("=" * 80)
        logger.info(f"πŸ“¦ Repository: {req.owner}/{req.repo}")
        logger.info(f"🌿 Branch: {req.branch}")
        logger.info(f"πŸ” Access Token: {'βœ“ Present' if req.access_token else 'βœ— Missing'} (length: {len(req.access_token) if req.access_token else 0})")
        logger.info(f"🌐 FTP Server: {req.ftp_server}")
        logger.info(f"πŸ‘€ FTP Username: {req.ftp_username if req.ftp_username else 'βœ— EMPTY'}")
        logger.info(f"πŸ”‘ FTP Password: {'βœ“ Present' if req.ftp_password else 'βœ— EMPTY'} (length: {len(req.ftp_password) if req.ftp_password else 0})")
        logger.info("-" * 80)
        
        if not req.ftp_username:
            logger.error("❌ FTP Username is empty!")
            raise HTTPException(status_code=400, detail="FTP Username is required")
        
        if not req.ftp_password:
            logger.error("❌ FTP Password is empty!")
            raise HTTPException(status_code=400, detail="FTP Password is required")
        
        result = github_deploy.setup_deployment(
            req.owner,
            req.repo,
            req.access_token,
            req.ftp_server,
            req.ftp_username,
            req.ftp_password,
            req.branch
        )
        
        if not result['success']:
            logger.error(f"❌ Deployment setup failed: {result.get('error', 'Unknown error')}")
            raise HTTPException(status_code=500, detail=result.get('error', 'Setup failed'))
        
        logger.info("=" * 80)
        logger.info("βœ… GITHUB DEPLOYMENT SETUP COMPLETED SUCCESSFULLY")
        logger.info(f"πŸ“¦ Project Type: {result.get('project_type')}")
        logger.info(f"πŸ”¨ Build Command: {result.get('build_command')}")
        logger.info(f"πŸ“‚ Output Directory: {result.get('output_dir')}")
        logger.info(f"πŸ”— Workflow URL: {result.get('workflow_url')}")
        logger.info(f"🎬 Actions URL: {result.get('actions_url')}")
        logger.info("=" * 80)
        
        return result
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error("=" * 80)
        logger.error(f"❌ DEPLOYMENT SETUP EXCEPTION: {str(e)}")
        logger.error("=" * 80)
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/workflow-runs")
def github_workflow_runs(req: GitHubWorkflowRunsRequest):
    """Get workflow run history"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        runs = github_deploy.get_workflow_runs(
            req.owner,
            req.repo,
            req.access_token,
            req.limit
        )
        return {
            "success": True,
            "runs": runs,
            "count": len(runs)
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get workflow runs: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/workflow-logs")
def github_workflow_logs(req: GitHubWorkflowLogsRequest):
    """Get workflow run logs"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        logs = github_deploy.get_workflow_logs(
            req.owner,
            req.repo,
            req.run_id,
            req.access_token
        )
        return {
            "success": True,
            "logs": logs
        }
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get logs: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/trigger-workflow")
def github_trigger_workflow(req: GitHubTriggerWorkflowRequest):
    """Manually trigger a workflow"""
    REQUEST_COUNT["total"] += 1
    
    if not github_deploy:
        raise HTTPException(status_code=503, detail="GitHub integration not configured")
    
    try:
        success = github_deploy.trigger_workflow(
            req.owner,
            req.repo,
            req.workflow_file,
            req.access_token,
            req.branch
        )
        
        if not success:
            raise HTTPException(status_code=500, detail="Failed to trigger workflow")
        
        return {
            "success": True,
            "message": "Workflow triggered successfully"
        }
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to trigger workflow: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# GITHUB DEPLOYMENT STATUS TRACKING (WITH CACHING)
# ============================================================================

# In-memory cache for deployment status (reduces GitHub API calls)
deployment_status_cache = {}
CACHE_TTL = 30  # seconds

class GitHubDeploymentStatusRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    branch: str = "main"

class GitHubLatestCommitRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    branch: str = "main"

class GitHubTriggerDeployRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    branch: str = "main"

class GitHubCreateDeploymentRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    ref: str = "main"
    environment: str = "production"
    description: str = "Deploying via CELESTINE"

class GitHubUpdateDeploymentStatusRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    deployment_id: int
    state: str  # pending, success, failure, error
    environment_url: Optional[str] = ""
    description: Optional[str] = ""


@app.post("/github/deployment-status")
def get_deployment_status(req: GitHubDeploymentStatusRequest):
    """
    Get real-time deployment status from GitHub Actions
    Returns: status, conclusion, commit info, workflow URL
    Caches results for 30 seconds to reduce API calls
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        cache_key = f"{req.owner}/{req.repo}/{req.branch}"
        now = time.time()
        
        # Check cache first
        if cache_key in deployment_status_cache:
            cached_data, cached_time = deployment_status_cache[cache_key]
            if now - cached_time < CACHE_TTL:
                logger.info(f"βœ“ Cache hit for {cache_key} (age: {int(now - cached_time)}s)")
                cached_data['cached'] = True
                cached_data['cache_age'] = int(now - cached_time)
                return cached_data
        
        logger.info(f"Fetching deployment status for {req.owner}/{req.repo} (branch: {req.branch})")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting'
        }
        
        # Get latest workflow runs for the branch
        response = requests.get(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs',
            headers=headers,
            params={
                'branch': req.branch,
                'per_page': 5,
                'status': 'completed,in_progress,queued'
            },
            timeout=15
        )
        
        if response.status_code != 200:
            logger.error(f"GitHub API error: {response.status_code} - {response.text}")
            raise HTTPException(status_code=response.status_code, detail=f"GitHub API error: {response.text}")
        
        data = response.json()
        workflow_runs = data.get('workflow_runs', [])
        
        if not workflow_runs:
            result = {
                'success': True,
                'status': 'unknown',
                'conclusion': None,
                'commit_sha': '',
                'commit_msg': '',
                'workflow_url': '',
                'run_id': None,
                'cached': False,
                'message': 'No workflow runs found'
            }
            deployment_status_cache[cache_key] = (result, now)
            return result
        
        # Get the most recent run
        latest_run = workflow_runs[0]
        
        # Map GitHub status to our status
        gh_status = latest_run.get('status', 'unknown')
        gh_conclusion = latest_run.get('conclusion')
        
        # Determine final status
        if gh_status == 'completed':
            if gh_conclusion == 'success':
                status = 'success'
            elif gh_conclusion in ['failure', 'cancelled', 'timed_out']:
                status = 'failure'
            else:
                status = 'unknown'
        elif gh_status == 'in_progress':
            status = 'in_progress'
        elif gh_status == 'queued':
            status = 'queued'
        else:
            status = 'unknown'
        
        result = {
            'success': True,
            'status': status,
            'conclusion': gh_conclusion,
            'commit_sha': latest_run.get('head_sha', ''),
            'commit_msg': latest_run.get('head_commit', {}).get('message', ''),
            'workflow_url': latest_run.get('html_url', ''),
            'run_id': latest_run.get('id'),
            'created_at': latest_run.get('created_at'),
            'updated_at': latest_run.get('updated_at'),
            'cached': False,
            'cache_age': 0
        }
        
        # Cache the result
        deployment_status_cache[cache_key] = (result, now)
        logger.info(f"βœ“ Cached deployment status for {cache_key}: {status}")
        
        return result
        
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get deployment status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/latest-commit")
def get_latest_commit(req: GitHubLatestCommitRequest):
    """
    Get latest commit on a branch
    Used for auto-deploy detection
    Caches results for 60 seconds
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        cache_key = f"commit_{req.owner}/{req.repo}/{req.branch}"
        now = time.time()
        
        # Check cache (longer TTL for commits)
        if cache_key in deployment_status_cache:
            cached_data, cached_time = deployment_status_cache[cache_key]
            if now - cached_time < 60:  # 60 second cache for commits
                logger.info(f"βœ“ Cache hit for commit {cache_key}")
                cached_data['cached'] = True
                return cached_data
        
        logger.info(f"Fetching latest commit for {req.owner}/{req.repo} (branch: {req.branch})")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting'
        }
        
        # Get latest commit
        response = requests.get(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/commits/{req.branch}',
            headers=headers,
            timeout=15
        )
        
        if response.status_code != 200:
            logger.error(f"GitHub API error: {response.status_code}")
            raise HTTPException(status_code=response.status_code, detail="Failed to fetch commit")
        
        commit_data = response.json()
        
        result = {
            'success': True,
            'sha': commit_data.get('sha', ''),
            'message': commit_data.get('commit', {}).get('message', ''),
            'author': commit_data.get('commit', {}).get('author', {}).get('name', ''),
            'date': commit_data.get('commit', {}).get('author', {}).get('date', ''),
            'cached': False
        }
        
        # Cache the result
        deployment_status_cache[cache_key] = (result, now)
        logger.info(f"βœ“ Cached commit info for {cache_key}: {result['sha'][:7]}")
        
        return result
        
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get latest commit: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/trigger-deploy")
def trigger_deploy(req: GitHubTriggerDeployRequest):
    """
    Trigger a deployment by dispatching the workflow
    Clears cache to force fresh status check
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        logger.info(f"Triggering deployment for {req.owner}/{req.repo} (branch: {req.branch})")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting',
            'Content-Type': 'application/json'
        }
        
        # Trigger workflow dispatch
        response = requests.post(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/workflows/deploy.yml/dispatches',
            headers=headers,
            json={'ref': req.branch},
            timeout=15
        )
        
        # 204 = success (no content)
        if response.status_code == 204:
            # Clear cache to force fresh status check
            cache_key = f"{req.owner}/{req.repo}/{req.branch}"
            if cache_key in deployment_status_cache:
                del deployment_status_cache[cache_key]
                logger.info(f"βœ“ Cleared cache for {cache_key}")
            
            logger.info(f"βœ“ Deployment triggered successfully for {req.owner}/{req.repo}")
            return {
                'success': True,
                'message': 'Deployment triggered successfully'
            }
        else:
            logger.error(f"Failed to trigger deployment: {response.status_code} - {response.text}")
            return {
                'success': False,
                'error': f"GitHub API returned {response.status_code}",
                'details': response.text
            }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to trigger deployment: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/create-deployment")
def create_github_deployment(req: GitHubCreateDeploymentRequest):
    """
    Create a GitHub deployment (for status tracking on repo page)
    This fixes the yellow dots issue
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        logger.info(f"Creating GitHub deployment for {req.owner}/{req.repo}")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting',
            'Content-Type': 'application/json'
        }
        
        # Create deployment
        response = requests.post(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/deployments',
            headers=headers,
            json={
                'ref': req.ref,
                'environment': req.environment,
                'description': req.description,
                'auto_merge': False,
                'required_contexts': [],
                'production_environment': True
            },
            timeout=15
        )
        
        if response.status_code in [200, 201]:
            deployment_data = response.json()
            logger.info(f"βœ“ GitHub deployment created: {deployment_data.get('id')}")
            return {
                'success': True,
                'deployment_id': deployment_data.get('id'),
                'sha': deployment_data.get('sha')
            }
        else:
            logger.error(f"Failed to create deployment: {response.status_code} - {response.text}")
            return {
                'success': False,
                'error': f"GitHub API returned {response.status_code}",
                'details': response.text
            }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to create deployment: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/update-deployment-status")
def update_github_deployment_status(req: GitHubUpdateDeploymentStatusRequest):
    """
    Update GitHub deployment status (success/failure)
    This updates the dots on the GitHub repo page
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        logger.info(f"Updating deployment status for {req.owner}/{req.repo} (deployment: {req.deployment_id}, state: {req.state})")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting',
            'Content-Type': 'application/json'
        }
        
        # Update deployment status
        response = requests.post(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/deployments/{req.deployment_id}/statuses',
            headers=headers,
            json={
                'state': req.state,
                'environment_url': req.environment_url,
                'description': req.description,
                'auto_inactive': False
            },
            timeout=15
        )
        
        if response.status_code in [200, 201]:
            logger.info(f"βœ“ Deployment status updated to {req.state}")
            return {
                'success': True,
                'message': f'Deployment status updated to {req.state}'
            }
        else:
            logger.error(f"Failed to update deployment status: {response.status_code} - {response.text}")
            return {
                'success': False,
                'error': f"GitHub API returned {response.status_code}",
                'details': response.text
            }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to update deployment status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# Cache cleanup task (runs every 5 minutes)
@app.on_event("startup")
async def startup_cache_cleanup():
    """Clean up old cache entries periodically"""
    import asyncio
    
    async def cleanup_task():
        while True:
            await asyncio.sleep(300)  # 5 minutes
            now = time.time()
            expired_keys = []
            
            for key, (data, cached_time) in deployment_status_cache.items():
                if now - cached_time > 300:  # 5 minutes
                    expired_keys.append(key)
            
            for key in expired_keys:
                del deployment_status_cache[key]
            
            if expired_keys:
                logger.info(f"🧹 Cleaned up {len(expired_keys)} expired cache entries")
    
    asyncio.create_task(cleanup_task())


# ============================================================================
# GITHUB WEBHOOK ENDPOINT (Fixes 404 errors)
# ============================================================================

import hmac
import hashlib

def verify_github_webhook_signature(payload_body: bytes, secret_token: str, signature_header: str) -> bool:
    """Verify that the webhook payload was sent from GitHub."""
    if not signature_header:
        return False
        
    hash_object = hmac.new(secret_token.encode('utf-8'), msg=payload_body, digestmod=hashlib.sha256)
    expected_signature = "sha256=" + hash_object.hexdigest()
    
    return hmac.compare_digest(expected_signature, signature_header)

@app.post("/github/webhook")
async def github_webhook(request: Request):
    """
    Handle GitHub webhook events
    Used for real-time deployment status updates
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        # 1. Verify Webhook Signature
        github_secret = os.getenv('WEBHOOK_SECRET', 'savage')
        signature_header = request.headers.get('X-Hub-Signature-256', '')
        
        # Read raw body for signature verification
        payload_body = await request.body()
        
        if not verify_github_webhook_signature(payload_body, github_secret, signature_header):
            logger.warning("Invalid GitHub webhook signature")
            return {"success": False, "error": "Invalid signature"}

        event_type = request.headers.get('X-GitHub-Event', 'unknown')
        
        # Handle ping and installation events immediately (required for GitHub Apps)
        if event_type == 'ping':
            logger.info("Received ping event from GitHub App")
            return {"success": True, "message": "pong"}
            
        if event_type == 'installation' or event_type == 'installation_repositories':
            logger.info(f"Received {event_type} event from GitHub App")
            return {"success": True, "message": f"{event_type} event received"}

        payload = await request.json()
        
        logger.info("=" * 80)
        logger.info("πŸ“¨ GitHub Webhook Received")
        logger.info("=" * 80)
        logger.info(f"Event Type: {event_type}")
        
        repo_full_name = "unknown"
        if 'repository' in payload:
            repo_full_name = payload['repository']['full_name']
            logger.info(f"Repository: {repo_full_name}")
            
            # Clear cache for this repo to force fresh status check
            cache_keys_to_clear = [k for k in deployment_status_cache.keys() if repo_full_name in k]
            for key in cache_keys_to_clear:
                del deployment_status_cache[key]
                logger.info(f"βœ“ Cleared cache for {key}")
        
        # Handle workflow_run events
        if event_type == 'workflow_run':
            workflow_run = payload.get('workflow_run', {})
            action = payload.get('action', '')
            
            repo_owner = payload['repository']['owner']['login']
            repo_name = payload['repository']['name']
            run_id = workflow_run.get('id')
            status = workflow_run.get('status')
            conclusion = workflow_run.get('conclusion')
            html_url = workflow_run.get('html_url')
            
            logger.info(f"Workflow Run: {action}")
            logger.info(f"Run ID: {run_id}")
            logger.info(f"Status: {status}")
            logger.info(f"Conclusion: {conclusion}")
            
            # Update database
            if run_id:
                update_deployment_status(
                    repo_owner=repo_owner,
                    repo_name=repo_name,
                    workflow_run_id=run_id,
                    status=status,
                    conclusion=conclusion,
                    workflow_url=html_url
                )
        
        # Handle deployment_status events
        elif event_type == 'deployment_status':
            deployment = payload.get('deployment', {})
            deployment_status = payload.get('deployment_status', {})
            
            repo_owner = payload['repository']['owner']['login']
            repo_name = payload['repository']['name']
            state = deployment_status.get('state')
            
            logger.info(f"Deployment Status: {state}")
            
            # Map to our status
            status_map = {
                'pending': 'pending',
                'queued': 'queued',
                'in_progress': 'in_progress',
                'success': 'completed',
                'failure': 'failed',
                'error': 'failed'
            }
            
            mapped_status = status_map.get(state, 'pending')
            # This could fall back to other updates if needed
        
        return {
            "success": True,
            "message": "Webhook processed",
            "event": event_type,
            "repo": repo_full_name
        }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Webhook processing error: {e}")
        return {
            "success": False,
            "error": str(e)
        }


# ============================================================================
# GITHUB ACTIONS LOGS ENDPOINT (Real-time workflow logs)
# ============================================================================

class GitHubWorkflowLogsRequest(BaseModel):
    access_token: str
    owner: str
    repo: str
    run_id: Optional[int] = None
    branch: str = "main"

@app.post("/github/workflow-logs")
def get_workflow_logs(req: GitHubWorkflowLogsRequest):
    """
    Get real-time GitHub Actions workflow logs
    Returns actual logs from the running/completed workflow
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        logger.info(f"Fetching workflow logs for {req.owner}/{req.repo}")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting'
        }
        
        # If run_id not provided, get the latest run
        if not req.run_id:
            response = requests.get(
                f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs',
                headers=headers,
                params={'branch': req.branch, 'per_page': 1},
                timeout=15
            )
            
            if response.status_code != 200:
                raise HTTPException(status_code=response.status_code, detail="Failed to fetch runs")
            
            runs = response.json().get('workflow_runs', [])
            if not runs:
                return {
                    'success': True,
                    'logs': [],
                    'jobs': [],
                    'message': 'No workflow runs found'
                }
            
            req.run_id = runs[0]['id']
        
        # Get jobs for this run
        jobs_response = requests.get(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs/{req.run_id}/jobs',
            headers=headers,
            timeout=15
        )
        
        if jobs_response.status_code != 200:
            raise HTTPException(status_code=jobs_response.status_code, detail="Failed to fetch jobs")
        
        jobs_data = jobs_response.json()
        jobs = jobs_data.get('jobs', [])
        
        # Extract steps from all jobs
        all_steps = []
        for job in jobs:
            job_name = job.get('name', 'Unknown Job')
            steps = job.get('steps', [])
            
            for step in steps:
                step_name = step.get('name', 'Unknown Step')
                status = step.get('status', 'pending')
                conclusion = step.get('conclusion')
                started_at = step.get('started_at', '')
                completed_at = step.get('completed_at', '')
                
                # Map status to our format
                if status == 'completed':
                    if conclusion == 'success':
                        step_status = 'completed'
                    else:
                        step_status = 'failed'
                elif status == 'in_progress':
                    step_status = 'in-progress'
                else:
                    step_status = 'pending'
                
                all_steps.append({
                    'job': job_name,
                    'name': step_name,
                    'status': step_status,
                    'conclusion': conclusion,
                    'started_at': started_at,
                    'completed_at': completed_at,
                    'number': step.get('number', 0)
                })
        
        # Try to get logs (may not be available for in-progress runs)
        logs_text = []
        try:
            logs_response = requests.get(
                f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs/{req.run_id}/logs',
                headers=headers,
                timeout=15,
                allow_redirects=True
            )
            
            if logs_response.status_code == 200:
                # Parse logs (they come as a zip file, but we'll get the text)
                logs_text = logs_response.text.split('\n')[:100]  # First 100 lines
        except:
            pass
        
        return {
            'success': True,
            'run_id': req.run_id,
            'jobs': jobs,
            'steps': all_steps,
            'logs': logs_text,
            'total_steps': len(all_steps)
        }
        
    except HTTPException:
        raise
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get workflow logs: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/github/workflow-steps")
def get_workflow_steps(req: GitHubWorkflowLogsRequest):
    """
    Get workflow steps with real-time status
    Optimized for deployment viewer UI
    """
    REQUEST_COUNT["total"] += 1
    
    try:
        logger.info(f"Fetching workflow steps for {req.owner}/{req.repo}")
        
        headers = {
            'Authorization': f'token {req.access_token}',
            'Accept': 'application/vnd.github.v3+json',
            'User-Agent': 'CELESTINE-Hosting'
        }
        
        # Get latest run if run_id not provided
        if not req.run_id:
            response = requests.get(
                f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs',
                headers=headers,
                params={'branch': req.branch, 'per_page': 1},
                timeout=15
            )
            
            if response.status_code != 200:
                return {'success': False, 'error': 'Failed to fetch runs'}
            
            runs = response.json().get('workflow_runs', [])
            if not runs:
                return {'success': True, 'steps': [], 'logs': []}
            
            req.run_id = runs[0]['id']
        
        # Get jobs
        jobs_response = requests.get(
            f'https://api.github.com/repos/{req.owner}/{req.repo}/actions/runs/{req.run_id}/jobs',
            headers=headers,
            timeout=15
        )
        
        if jobs_response.status_code != 200:
            return {'success': False, 'error': 'Failed to fetch jobs'}
        
        jobs = jobs_response.json().get('jobs', [])
        
        # Build simplified steps list for UI
        steps = []
        logs = []
        
        for job in jobs:
            for step in job.get('steps', []):
                step_name = step.get('name', '')
                status = step.get('status', 'pending')
                conclusion = step.get('conclusion')
                
                # Map to UI status
                if status == 'completed':
                    ui_status = 'completed' if conclusion == 'success' else 'failed'
                elif status == 'in_progress':
                    ui_status = 'in-progress'
                else:
                    ui_status = 'pending'
                
                steps.append({
                    'id': step.get('number', len(steps) + 1),
                    'title': step_name,
                    'status': ui_status
                })
                
                # Add log entry
                if status != 'pending':
                    timestamp = step.get('started_at', '')
                    if timestamp:
                        time_str = timestamp.split('T')[1][:8] if 'T' in timestamp else ''
                        
                        if ui_status == 'completed':
                            logs.append({
                                'time': time_str,
                                'text': f"βœ“ {step_name}",
                                'color': 'text-emerald-400'
                            })
                        elif ui_status == 'failed':
                            logs.append({
                                'time': time_str,
                                'text': f"βœ— {step_name} failed",
                                'color': 'text-red-400'
                            })
                        elif ui_status == 'in-progress':
                            logs.append({
                                'time': time_str,
                                'text': f"⟳ {step_name}...",
                                'color': 'text-indigo-400'
                            })
        
        return {
            'success': True,
            'run_id': req.run_id,
            'steps': steps,
            'logs': logs
        }
        
    except Exception as e:
        REQUEST_COUNT["errors"] += 1
        logger.error(f"Failed to get workflow steps: {e}")
        return {
            'success': False,
            'error': str(e),
            'steps': [],
            'logs': []
        }