File size: 89,645 Bytes
6ad773f
 
 
 
 
77c432f
 
 
0df4616
3728b87
 
265448e
6ad773f
 
 
0df4616
 
 
 
3071683
 
e504229
cd5d2e8
48ba960
01a97fd
 
3071683
 
 
 
 
 
0df4616
 
 
 
 
3071683
 
0df4616
 
 
3071683
 
 
 
 
 
 
 
 
 
 
 
 
6ad773f
 
 
990ddb6
47d9351
 
990ddb6
77c432f
 
 
 
 
3486397
77c432f
3f4a17f
 
 
938557c
 
 
4778604
 
77c432f
ed30c22
3728b87
 
0df4616
7b91e2d
 
0df4616
7b91e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
 
3728b87
 
0df4616
3728b87
 
894efa2
3728b87
0df4616
 
894efa2
0df4616
 
 
 
 
 
894efa2
3728b87
0df4616
3728b87
0df4616
3728b87
0df4616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a9e883
 
0df4616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a9e883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
1a9e883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
1a9e883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
1a9e883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
 
 
 
 
 
 
 
 
1a9e883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
77c432f
 
 
990ddb6
 
0df4616
 
 
 
 
 
 
 
 
77c432f
 
 
3f4a17f
 
 
990ddb6
 
3f4a17f
 
 
938557c
 
 
990ddb6
 
4778604
 
 
 
 
 
990ddb6
 
938557c
 
 
77c432f
3f4a17f
938557c
4778604
77c432f
 
894efa2
77c432f
 
 
3f4a17f
894efa2
3f4a17f
 
 
938557c
894efa2
938557c
 
 
4778604
894efa2
4778604
 
 
894efa2
6ad773f
3728b87
3071683
 
 
 
 
0df4616
 
6ad773f
77c432f
 
 
 
 
 
 
47d9351
 
 
 
894efa2
3071683
 
 
 
 
 
 
 
 
0df4616
3071683
 
0df4616
3071683
 
 
3728b87
 
 
 
 
 
 
 
 
0df4616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3728b87
3071683
 
0df4616
3071683
 
 
 
 
0df4616
 
 
 
 
 
 
 
 
 
 
 
 
 
894efa2
 
 
 
 
1a9e883
009d37a
1a9e883
 
 
 
 
3728b87
894efa2
 
 
 
3728b87
3071683
0df4616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894efa2
 
6ad773f
 
3728b87
6ad773f
 
 
990ddb6
 
 
 
 
 
6ad773f
3071683
3728b87
 
3071683
 
3728b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3071683
 
3728b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3071683
 
3728b87
 
 
 
 
 
 
 
3071683
 
894efa2
3728b87
894efa2
 
3071683
 
 
894efa2
 
 
3728b87
 
 
 
3071683
 
894efa2
3728b87
894efa2
 
3071683
 
 
894efa2
 
 
3728b87
 
 
 
3071683
 
894efa2
3728b87
 
894efa2
 
3071683
 
 
894efa2
 
 
 
 
3071683
894efa2
 
 
3728b87
 
 
 
 
 
 
0df4616
3728b87
 
 
 
0df4616
3728b87
 
 
 
 
0df4616
 
3728b87
 
0df4616
3728b87
 
0df4616
 
3728b87
0df4616
 
3728b87
0df4616
3728b87
 
 
 
 
 
 
 
 
 
 
0df4616
 
3728b87
 
 
 
 
0df4616
 
3728b87
 
 
0df4616
6ad773f
 
3071683
0df4616
3071683
990ddb6
6ad773f
 
47d9351
0df4616
 
 
 
 
 
990ddb6
 
0df4616
 
 
 
990ddb6
0df4616
 
 
 
 
 
 
 
6ad773f
 
3728b87
 
 
 
 
 
 
6ad773f
3728b87
990ddb6
6ad773f
0df4616
990ddb6
 
 
 
 
 
 
0df4616
 
6ad773f
894efa2
6ad773f
 
0df4616
 
 
 
 
 
990ddb6
6ad773f
 
47d9351
990ddb6
0df4616
990ddb6
 
0df4616
265448e
 
0df4616
265448e
0df4616
265448e
6ad773f
 
990ddb6
6ad773f
 
 
 
3728b87
 
 
 
 
 
 
 
 
0df4616
3728b87
 
0df4616
 
3728b87
894efa2
3728b87
 
0df4616
 
 
3728b87
 
 
0df4616
3728b87
 
0df4616
3728b87
265448e
0df4616
265448e
0df4616
3728b87
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
 
3728b87
0df4616
 
3728b87
0df4616
 
3728b87
0df4616
 
3728b87
 
 
 
 
 
 
 
 
990ddb6
3728b87
 
 
 
 
 
 
 
990ddb6
3728b87
 
990ddb6
 
3728b87
6ad773f
990ddb6
0df4616
990ddb6
 
0df4616
 
6ad773f
894efa2
1e4ebab
 
0df4616
 
 
 
 
 
1e4ebab
 
cba15e1
1e4ebab
47d9351
990ddb6
0df4616
990ddb6
1e4ebab
894efa2
990ddb6
894efa2
990ddb6
 
1e4ebab
 
894efa2
 
 
 
 
 
 
 
990ddb6
894efa2
 
 
990ddb6
894efa2
0df4616
 
894efa2
c3f0ef0
894efa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ec3131
894efa2
 
 
 
 
 
 
 
 
 
 
990ddb6
894efa2
0df4616
894efa2
 
 
 
 
0df4616
894efa2
 
 
 
 
0df4616
894efa2
 
 
 
 
 
 
 
 
0df4616
 
894efa2
 
 
 
 
3728b87
990ddb6
894efa2
1e4ebab
894efa2
0df4616
894efa2
 
0df4616
 
894efa2
009d37a
 
 
 
 
0df4616
 
 
009d37a
 
 
 
 
 
 
 
0df4616
009d37a
 
0df4616
009d37a
 
 
0df4616
 
 
009d37a
 
 
 
 
 
 
 
0df4616
 
 
009d37a
 
 
 
 
 
1a9e883
 
 
 
 
 
 
 
 
 
009d37a
 
 
 
0df4616
009d37a
 
0df4616
 
009d37a
894efa2
 
 
ed30c22
 
0df4616
 
 
990ddb6
ed30c22
990ddb6
0df4616
990ddb6
ed30c22
47d9351
ed30c22
 
 
 
0df4616
ed30c22
 
 
 
 
 
0df4616
ed30c22
3728b87
 
 
 
 
 
 
0df4616
 
 
 
 
 
 
4778604
 
 
990ddb6
4778604
894efa2
77c432f
 
 
 
 
 
 
990ddb6
77c432f
 
 
 
 
 
 
 
990ddb6
77c432f
 
 
 
 
990ddb6
 
77c432f
 
 
990ddb6
 
 
 
 
 
 
 
77c432f
 
 
 
 
 
990ddb6
 
 
 
77c432f
990ddb6
77c432f
 
990ddb6
77c432f
990ddb6
 
 
 
 
 
 
 
77c432f
894efa2
77c432f
 
 
 
 
990ddb6
77c432f
 
 
 
990ddb6
77c432f
 
 
 
990ddb6
77c432f
 
 
 
990ddb6
77c432f
 
 
 
 
990ddb6
77c432f
990ddb6
 
 
 
 
77c432f
 
 
 
 
 
990ddb6
77c432f
 
 
 
990ddb6
77c432f
 
 
 
 
 
 
990ddb6
 
77c432f
 
 
990ddb6
77c432f
 
990ddb6
 
 
77c432f
 
 
3f4a17f
 
 
 
 
990ddb6
3f4a17f
 
 
 
990ddb6
3f4a17f
 
 
 
990ddb6
3f4a17f
113e3c6
3f4a17f
 
990ddb6
3f4a17f
 
 
 
113e3c6
990ddb6
3f4a17f
990ddb6
 
 
 
 
3f4a17f
 
 
 
 
 
990ddb6
3f4a17f
 
 
 
990ddb6
3f4a17f
 
113e3c6
3f4a17f
 
 
 
990ddb6
 
3f4a17f
 
 
990ddb6
3f4a17f
 
990ddb6
 
 
3f4a17f
 
 
938557c
 
 
 
 
990ddb6
938557c
 
 
 
990ddb6
938557c
 
 
 
990ddb6
938557c
 
 
 
990ddb6
938557c
 
 
 
3728b87
990ddb6
938557c
990ddb6
 
 
 
 
938557c
 
 
 
 
 
990ddb6
938557c
 
 
 
990ddb6
938557c
 
 
 
 
 
 
990ddb6
 
938557c
 
 
990ddb6
938557c
 
990ddb6
 
 
938557c
 
 
1a9e883
894efa2
6ad773f
 
cdf2162
 
 
990ddb6
 
0df4616
990ddb6
 
cdf2162
0df4616
 
 
990ddb6
0df4616
 
 
cdf2162
 
 
 
0df4616
 
 
 
 
 
 
 
 
 
990ddb6
 
 
0df4616
990ddb6
 
 
3728b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df4616
 
 
 
 
 
 
990ddb6
cdf2162
 
 
990ddb6
 
cdf2162
 
4778604
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
import os
import uuid
import requests
import threading
import time
import sqlite3
import tempfile
import ffmpeg
import json
from datetime import datetime
from flask import Flask, request, jsonify, render_template, send_from_directory, send_file, after_this_request, Response
from deep_translator import GoogleTranslator

app = Flask(__name__)

# --- مپینگ‌های کمکی برای مانیتورینگ زنده اکشن‌ها ---
RUN_ACCOUNT_MAPPING = {}
LAST_RUN_CHECK = {}

# --- پیکربندی حساب‌های گیت‌هاب (سیستم توزیع بار یا Load Balancer) ---
GITHUB_ACCOUNTS_CONFIG = [
    {"repo": "hajiliker840/a", "token_env": "GITHUB_TOKEN2"},
    {"repo": "hajiliker4-cpu/a", "token_env": "GITHUB_TOKEN3"},
    {"repo": "asrasahar744-ops/a", "token_env": "GITHUB_TOKEN4"},
    {"repo": "elias207207/a", "token_env": "GITHUB_TOKEN5"},
    {"repo": "mobin207207/a", "token_env": "GITHUB_TOKEN6"},
]

GITHUB_ACCOUNTS = []
for config in GITHUB_ACCOUNTS_CONFIG:
    token = os.environ.get(config["token_env"])
    if token:
        GITHUB_ACCOUNTS.append({
            "repo": config["repo"], 
            "token": token,
            "token_env": config["token_env"]
        })

print(f"✅ تعداد {len(GITHUB_ACCOUNTS)} حساب گیت‌هاب با موفقیت شناسایی و به سیستم متصل شد.", flush=True)
for i, acc in enumerate(GITHUB_ACCOUNTS):
    token_masked = acc['token'][:4] + "..." + acc['token'][-4:] if len(acc['token']) > 8 else "***"
    print(f"   🔹 حساب {i+1}: مخزن {acc['repo']} متصل با متغیر {acc['token_env']} ({token_masked})", flush=True)

account_lock = threading.Lock()
account_index = 0

def get_next_account():
    global account_index
    if not GITHUB_ACCOUNTS:
        return None
    with account_lock:
        acc = GITHUB_ACCOUNTS[account_index % len(GITHUB_ACCOUNTS)]
        account_index += 1
    return acc

SPACE_HOST = os.environ.get("SPACE_HOST", "")
SPACE_URL = f"https://{SPACE_HOST}"

# --- کلمات ممنوعه ---
FORBIDDEN_KEYWORDS = {"sex", "sexy", "porn", "nude", "erotic", "سکس", "سکسی", "پورن", "شهوانی", "برهنه", "لخت"}

# --- تنظیمات دیتابیس SQLite ---
DB_DIR = "/data" if os.path.exists("/data") else "."
DB_PATH = os.path.join(DB_DIR, "users.db")
TEMP_DIR = "tmp"
USAGE_LIMIT = 5
ONE_WEEK_SECONDS = 7 * 24 * 60 * 60
ONE_DAY_SECONDS = 24 * 60 * 60  

DB_IMAGE_PATH = os.path.join(DB_DIR, "users_image.db")
IMAGE_USAGE_LIMIT = 5

DB_IMAGE_EDIT_PATH = os.path.join(DB_DIR, "users_image_edit.db")
EDIT_USAGE_LIMIT = 5

DB_CHAT_QUEUE_PATH = os.path.join(DB_DIR, "chat_queue.db")

os.makedirs(TEMP_DIR, exist_ok=True)
os.makedirs("static/images", exist_ok=True)
os.makedirs("yml", exist_ok=True)

# --- سیستم پاکسازی خودکار فایل‌های قدیمی ---
def cleanup_old_files():
    while True:
        time.sleep(3600)
        now = time.time()
        max_age_seconds = 24 * 60 * 60  
        
        for folder in ["static/images", "tmp"]:
            if os.path.exists(folder):
                for filename in os.listdir(folder):
                    file_path = os.path.join(folder, filename)
                    if os.path.isfile(file_path):
                        if os.stat(file_path).st_mtime < now - max_age_seconds:
                            try:
                                os.remove(file_path)
                            except Exception:
                                pass

threading.Thread(target=cleanup_old_files, daemon=True).start()

# تابع تولید کارت تصویری خطا
def create_error_image(run_id, message):
    try:
        from PIL import Image, ImageDraw
        img = Image.new('RGB', (800, 500), color='#1e293b')
        draw = ImageDraw.Draw(img)
        
        draw.rectangle([20, 20, 780, 480], outline="#f43f5e", width=4)
        draw.text((50, 60), "⚠️ PROCESS ERROR / خطای اجرای فرآیند", fill="#f43f5e")
        draw.text((50, 110), f"Run ID: {run_id}", fill="#94a3b8")
        
        y_offset = 160
        for line in message.split('\n'):
            draw.text((50, y_offset), line, fill="#f1f5f9")
            y_offset += 40
            
        draw.text((50, y_offset + 30), "برای مشاهده لاگ‌ها، به تب Actions در مخزن گیت هاب خود بروید.", fill="#38bdf8")
        
        img.save(f"static/images/{run_id}.png")
        print(f"🎨 [کارت خطا] کارت تصویری خطا با موفقیت در static/images/{run_id}.png ایجاد شد.", flush=True)
    except Exception as e:
        print(f"Failed to generate error image: {e}", flush=True)

# سیستم مانیتورینگ زنده (تنها برای ثبت لاگ در کنسول ادمین و بدون دخالت در رکوئست‌ها)
def check_github_run_status_and_log(run_id):
    acc = RUN_ACCOUNT_MAPPING.get(run_id)
    if not acc:
        acc = get_next_account()
    if not acc:
        return
        
    headers = {
        "Accept": "application/vnd.github.v3+json", 
        "Authorization": f"token {acc['token']}"
    }
    
    # ۱. بررسی مشخصات مخزن
    try:
        repo_url = f"https://api.github.com/repos/{acc['repo']}"
        repo_r = requests.get(repo_url, headers=headers, timeout=10)
        if repo_r.status_code == 200:
            repo_info = repo_r.json()
            default_branch = repo_info.get("default_branch", "نامشخص")
            is_private = repo_info.get("private", False)
            print(f"   ℹ️ [مشخصات مخزن] شاخه پیش‌فرض گیت‌هاب: {default_branch} | نوع مخزن: {'خصوصی (Private)' if is_private else 'عمومی (Public)'}", flush=True)
    except Exception as e:
        print(f"   ⚠️ [مانیتور مخزن] خطا در ارتباط با گیت‌هاب: {e}", flush=True)

    # ۲. بررسی آخرین وضعیت اجراها (تنها جهت ثبت لاگ در ترمینال ادمین)
    url = f"https://api.github.com/repos/{acc['repo']}/actions/runs?per_page=5"
    try:
        r = requests.get(url, headers=headers, timeout=10)
        if r.status_code == 200:
            runs_data = r.json()
            runs = runs_data.get('workflow_runs', [])
            total_runs = runs_data.get('total_count', 0)
            
            print(f"🔍 [رهگیری زنده اکشنز] بررسی مخزن {acc['repo']} برای شناسه فرآیند {run_id[:8]}... تعداد کل اجراهای یافت شده: {total_runs}", flush=True)
            
            for run in runs:
                created_at_dt = datetime.strptime(run['created_at'], "%Y-%m-%dT%H:%M:%SZ")
                time_diff = (datetime.utcnow() - created_at_dt).total_seconds()
                print(f"   🔸 اکشن شماره {run['id']} | نام: {run['name']} | وضعیت: {run['status']} | نتیجه: {run.get('conclusion')} | زمان ایجاد: {run['created_at']} ({int(time_diff)} ثانیه پیش)", flush=True)
        else:
            print(f"   ⚠️ [مانیتورینگ اکشنز] دریافت وضعیت با خطای API مواجه شد: {r.status_code}", flush=True)
    except Exception as e:
        print(f"   ⚠️ [خطای سیستم مانیتورینگ] خطا در دریافت لیست فرآیندها: {e}", flush=True)

# ایجاد خودکار اسکریپت‌های مورد نیاز
def write_default_scripts():
    scripts = {
        "ghibli.py": """
import os, sys, requests, base64
from gradio_client import Client, handle_file

raw_prompt = os.environ.get('PROMPT', '')
run_id = os.environ.get('RUN_ID', '')
space_url = os.environ.get('SPACE_URL', '')
github_run_id = os.environ.get('GITHUB_RUN_ID', '')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "ghibli",
                "client_payload": {
                    "prompt": raw_prompt,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Decoding packed configurations from safe payload...')
if not raw_prompt.startswith("GHIBLICONFIG_"):
    err_str = "Error: Invalid ghibli configuration payload signature."
    print(err_str)
    report_failure(err_str)
    sys.exit(1)

config_str = raw_prompt[len("GHIBLICONFIG_"):]
parts = config_str.split("_")
config = {}
i = 0
while i < len(parts) - 1:
    key = parts[i]
    val = parts[i+1]
    config[key] = val
    i += 2

user_run_id = config.get("userRunId", run_id)
img_ext = config.get("imgExt", "png")

try:
    b64_style = config.get("style", "")
    style = base64.b64decode(b64_style).decode('utf-8')
except Exception as e:
    style = "Studio Ghibli"

img_url = f"{space_url}/static/images/{user_run_id}_ghibli_img.{img_ext}"
local_img = f"input_img.{img_ext}"

print('2. Downloading reference image from your Space...')
try:
    req_img = requests.get(img_url, timeout=45)
    if req_img.status_code != 200:
        raise Exception(f"Image download failed. Status code: {req_img.status_code}")
    with open(local_img, 'wb') as f:
        f.write(req_img.content)
except Exception as download_err:
    err_str = f"Error downloading source files: {download_err}"
    print(err_str)
    report_failure(err_str)
    sys.exit(1)

print('3. Connecting to Yuanshi/OminiControl_Art Space...')
try:
    client = Client("Yuanshi/OminiControl_Art")
    result = client.predict(
        style,
        handle_file(local_img),
        "High Quality",
        1.5,
        "Auto",
        True,
        42,
        20,
        fn_index=7
    )

    image_path = None
    if isinstance(result, list) and len(result) > 0:
        image_path = result[0].get('image') or result[0].get('path') if isinstance(result[0], dict) else result[0]
    elif isinstance(result, tuple) and len(result) > 0:
        image_path = result[0]
    elif isinstance(result, dict):
        image_path = result.get('image') or result.get('path')
    else:
        image_path = result

    if not image_path or not os.path.exists(str(image_path)):
        raise Exception("Generated output image file not found or invalid.")

    with open(image_path, 'rb') as f:
        res_upload = requests.post(
            f'{space_url}/api/webhook/upload', 
            data={'run_id': run_id, 'github_run_id': github_run_id, 'ext': 'webp'}, 
            files={'file': f}
        )
    if res_upload.status_code == 200:
        print('SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    err_str = str(e)
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-flux.py": """
import os, sys, random, requests
from gradio_client import Client

def get_env_int(name, default=0):
    val = os.environ.get(name, '')
    try:
        return int(val) if val.strip() else default
    except ValueError:
        return default

prompt = os.environ.get('PROMPT', '')
width = get_env_int('WIDTH', 1024)
height = get_env_int('HEIGHT', 1024)
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-flux",
                "client_payload": {
                    "prompt": prompt,
                    "width": width,
                    "height": height,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Connecting to Flux...')
try:
    client = Client('black-forest-labs/FLUX.1-dev')
    seed = random.randint(1, 2147483647)
    
    result = client.predict(prompt, seed, True, width, height, 3.5, 28, api_name='/infer')
    image_path = result[0] if isinstance(result, (tuple, list)) else result
    
    print(f'2. Uploading back to Docker Space...')
    with open(image_path, 'rb') as f:
        res = requests.post(f'{space_url}/api/webhook/upload', data={'run_id': run_id, 'github_run_id': github_run_id}, files={'file': f})
    
    if res.status_code == 200:
        print('3. SUCCESS!')
    else:
        sys.exit(1)
        
except Exception as e:
    err_str = str(e)
    print(f'ERROR: {err_str}')
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-flux2.py": """
import os, sys, random, requests, json
from gradio_client import Client, handle_file

def get_env_int(name, default=0):
    val = os.environ.get(name, '')
    try:
        return int(val) if val.strip() else default
    except ValueError:
        return default

def get_env_float(name, default=0.0):
    val = os.environ.get(name, '')
    try:
        return float(val) if val.strip() else default
    except ValueError:
        return default

prompt = os.environ.get('PROMPT', '')
image_urls_raw = os.environ.get('IMAGE_URLS', '[]')
width = get_env_int('WIDTH', 1024)
height = get_env_int('HEIGHT', 1024)
steps = get_env_int('STEPS', 30)
guidance = get_env_float('GUIDANCE', 4.0)
seed = get_env_int('SEED', 0)
randomize_seed = os.environ.get('RANDOMIZE_SEED', 'true').lower() == 'true'
prompt_upsampling = os.environ.get('PROMPT_UPSAMPLING', 'true').lower() == 'true'
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')
hf_token = os.environ.get('HF_TOKEN', '')

print(f'Starting direct Flux 2 generation for run_id: {run_id}')

print('1. Parsing and downloading images...')
image_urls = []
if image_urls_raw:
    try:
        cleaned_json = image_urls_raw.replace('null', '').strip()
        if cleaned_json and cleaned_json != '[]':
            image_urls = json.loads(cleaned_json)
    except Exception as e:
        print(f'JSON parsing error: {e}')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-flux2",
                "client_payload": {
                    "data": {
                        "prompt": prompt,
                        "image_urls": image_urls,
                        "width": width,
                        "height": height,
                        "steps": steps,
                        "guidance": guidance,
                        "seed": seed,
                        "randomize_seed": randomize_seed,
                        "prompt_upsampling": prompt_upsampling,
                        "run_id": run_id,
                        "space_url": space_url,
                        "hf_token": hf_token
                    }
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

files_to_send = []
for idx, url in enumerate(image_urls):
    if url:
        print(f'Downloading input image {idx}: {url}')
        ext = url.split('.')[-1] if '.' in url else 'png'
        local_filename = f'input_{idx}.{ext}'
        try:
            req = requests.get(url, timeout=30)
            with open(local_filename, 'wb') as f:
                f.write(req.content)
            files_to_send.append({'image': handle_file(local_filename), 'caption': None})
        except Exception as e:
            print(f'Failed to download {url}: {e}')

print('2. Connecting to FLUX.2-dev Space...')
try:
    if hf_token:
        client = Client('black-forest-labs/FLUX.2-dev', token=hf_token)
    else:
        client = Client('black-forest-labs/FLUX.2-dev')
    
    if randomize_seed:
        seed = random.randint(1, 2147483647)
    
    print(f'Predicting with seed={seed}, width={width}, height={height}, steps={steps}, guidance={guidance}, upsampling={prompt_upsampling}...')
    result = client.predict(
        prompt,
        files_to_send,
        seed,
        randomize_seed,
        width,
        height,
        steps,
        guidance,
        prompt_upsampling,
        api_name='/infer'
    )
    
    image_path = result[0] if isinstance(result, (tuple, list)) else result
    seed_used = result[1] if (isinstance(result, (tuple, list)) and len(result) > 1) else seed
    
    print('3. Uploading metadata seed back...')
    with open('seed.txt', 'w') as sf:
        sf.write(str(seed_used))
    requests.post(
        f'{space_url}/api/webhook/upload',
        data={'run_id': f'{run_id}_seed', 'github_run_id': github_run_id, 'ext': 'txt'},
        files={'file': open('seed.txt', 'rb')}
    )
    
    print('4. Uploading generated image back...')
    with open(image_path, 'rb') as f:
        upload_res = requests.post(
            f'{space_url}/api/webhook/upload', 
            data={'run_id': run_id, 'github_run_id': github_run_id}, 
            files={'file': f}
        )
    
    if upload_res.status_code == 200:
        print('SUCCESS!')
    else:
        print(f'Upload failed with status {upload_res.status_code}: {upload_res.text}')
        sys.exit(1)
        
except Exception as e:
    err_str = str(e)
    print(f'Generation error: {err_str}')
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-cartoon.py": """
import os, sys, random, requests
from gradio_client import Client

def get_env_int(name, default=0):
    val = os.environ.get(name, '')
    try:
        return int(val) if val.strip() else default
    except ValueError:
        return default

prompt = os.environ.get('PROMPT', '')
width = get_env_int('WIDTH', 1024)
height = get_env_int('HEIGHT', 1024)
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-cartoon",
                "client_payload": {
                    "prompt": prompt,
                    "width": width,
                    "height": height,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Connecting to LoRA DLC Space...')
try:
    client = Client('prithivMLmods/FLUX-LoRA-DLC')
    print('2. Loading Cartoon Model (Long Toons)...')
    client.predict('prithivMLmods/Flux-Long-Toon-LoRA', api_name='/add_custom_lora')
    
    print('3. Generating Cartoon Image...')
    result = client.predict(
        prompt=f'Long toons {prompt}',
        image_input=None,
        cfg_scale=3.5,
        steps=28,
        randomize_seed=True,
        seed=random.randint(1, 2147483647),
        width=width,
        height=height,
        lora_scale=0.8,
        api_name='/run_lora'
    )
    image_path = result[0] if isinstance(result, (tuple, list)) else result
    
    print('4. Uploading back to Docker Space...')
    with open(image_path, 'rb') as f:
        res = requests.post(f'{space_url}/api/webhook/upload', 
                         data={'run_id': run_id, 'github_run_id': github_run_id}, 
                         files={'file': f})
    if res.status_code == 200:
        print('5. SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    err_str = str(e)
    print(f'CRITICAL ERROR: {err_str}')
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-midjourney.py": """
import os, sys, random, requests
from gradio_client import Client

def get_env_int(name, default=0):
    val = os.environ.get(name, '')
    try:
        return int(val) if val.strip() else default
    except ValueError:
        return default

raw_prompt = os.environ.get('PROMPT', '')
final_prompt = f'MJ v6 {raw_prompt}'
width = get_env_int('WIDTH', 1024)
height = get_env_int('HEIGHT', 1024)
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-midjourney",
                "client_payload": {
                    "prompt": raw_prompt,
                    "width": width,
                    "height": height,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Connecting to LoRA DLC Space...')
try:
    client = Client('prithivMLmods/FLUX-LoRA-DLC')
    print('2. Loading Midjourney Model...')
    client.predict('strangerzonehf/Flux-Midjourney-Mix2-LoRA', api_name='/add_custom_lora')
    
    print(f'3. Generating Image... Prompt: {final_prompt[:50]}...')
    result = client.predict(
        prompt=final_prompt,
        image_input=None,
        cfg_scale=3.5,
        steps=28,
        randomize_seed=True,
        seed=random.randint(1, 2147483647),
        width=width,
        height=height,
        lora_scale=0.8,
        api_name='/run_lora'
    )
    image_path = result[0] if isinstance(result, (tuple, list)) else result
    
    print('4. Uploading back to Docker Space...')
    with open(image_path, 'rb') as f:
        res = requests.post(f'{space_url}/api/webhook/upload', 
                         data={'run_id': run_id, 'github_run_id': github_run_id}, 
                         files={'file': f})
    if res.status_code == 200:
        print('5. SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    err_str = str(e)
    print(f'CRITICAL ERROR: {err_str}')
    report_failure(err_str)
    sys.exit(1)
        """,
        "chat-gemma.py": """
import os, sys, requests
from gradio_client import Client, handle_file

prompt = os.environ.get('PROMPT', '')
file_url = os.environ.get('FILE_URL', '')
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
gh_run_id = os.environ.get('GITHUB_RUN_ID')

files_to_send = []
if file_url:
    print(f'Downloading attachment: {file_url}')
    ext = file_url.split('.')[-1] if '.' in file_url else 'bin'
    local_filename = f'attachment.{ext}'
    try:
        req = requests.get(file_url, timeout=30)
        with open(local_filename, 'wb') as f:
            f.write(req.content)
        files_to_send.append(handle_file(local_filename))
    except Exception as e:
        print(f'Failed to download file: {e}')

print('Connecting to Gemma-4 Space...')
bot_reply = ''
try:
    client = Client('huggingface-projects/gemma-4-e4b-it')
    message_payload = {'text': prompt, 'files': files_to_send}
    result = client.predict(
        message=message_payload,
        thinking=False,
        max_new_tokens=4000,
        max_soft_tokens=1120,
        system_prompt='شما یک دستیار هوش مصنوعی بسیار باهوش، مفید و مودب به زبان فارسی هستید.',
        api_name='/generate'
    )
    bot_reply = result[0] if isinstance(result, (list, tuple)) else result
    if isinstance(bot_reply, dict):
        bot_reply = bot_reply.get('text', str(bot_reply))
except Exception as e:
    err_str = str(e)
    print(f'Generation Error: {err_str}')
    if 'ZeroGPU quota' in err_str or 'quota' in err_str.lower():
        bot_reply = '⚠️ **پردازش متوقف شد:**\\nدرخواست شما طولانی و پرحجم است (پردازش فایل‌های صوتی یا حجیم نیازمند منابع بالایی است و سهمیه سرور موقتاً پر شده است).\\n\\nلطفاً چند ساعت دیگر امتحان کنید یا درخواست و فایل سبک‌تری ارسال نمایید.'
    else:
        bot_reply = f'⚠️ خطای سرور پردازش: {err_str}'

print('Uploading response back to Docker Space...')
try:
    with open('response.txt', 'w', encoding='utf-8') as f:
        f.write(bot_reply)
    with open('response.txt', 'rb') as f:
        res = requests.post(f'{space_url}/api/webhook/upload',
                         data={'run_id': run_id, 'github_run_id': gh_run_id, 'ext': 'txt'},
                         files={'file': f})
    if res.status_code == 200:
        print('SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    sys.exit(1)
        """,
        "generate-editor.py": """
import os, sys, requests, re, time

import httpx
original_client_init = httpx.Client.__init__
def patched_client_init(self, *args, **kwargs):
    kwargs['timeout'] = httpx.Timeout(300.0)
    original_client_init(self, *args, **kwargs)
httpx.Client.__init__ = patched_client_init

original_async_init = httpx.AsyncClient.__init__
def patched_async_init(self, *args, **kwargs):
    kwargs['timeout'] = httpx.Timeout(300.0)
    original_async_init(self, *args, **kwargs)
httpx.AsyncClient.__init__ = patched_async_init

from gradio_client import Client, handle_file

prompt = os.environ.get('PROMPT', '')
image_url = os.environ.get('IMAGE_URL', '')
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-editor",
                "client_payload": {
                    "prompt": prompt,
                    "image_url": image_url,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Downloading input image from Docker Space...')
try:
    req = requests.get(image_url, timeout=30)
    with open('input.png', 'wb') as f:
        f.write(req.content)
    print('   -> Input image ready.')
except Exception as e:
    print(f'Download failed: {e}')
    sys.exit(1)

print('2. Connecting to Omni Editor and Processing...')
result_str = ''
success = False

for attempt in range(3):
    try:
        print(f'   -> Attempt {attempt + 1} of 3...')
        client = Client('selfit-camera/omni-image-editor')
        result = client.predict(
            handle_file('input.png'), 
            prompt,
            api_name='/edit_image_interface'
        )
        result_str = str(result)
        if 'src=' in result_str or 'http' in result_str:
            success = True
            break
        else:
            print(f'   -> Missing valid response format: {result_str[:100]}...')
    except Exception as client_err:
        print(f'   -> Attempt {attempt + 1} failed: {client_err}')
        time.sleep(5)

if not success:
    print(f'CRITICAL ERROR: All attempts failed. Raw result: {result_str}')
    sys.exit(1)

print('3. Parsing result string...')
urls = re.findall(r'src=[\'\"]([^\'\"]+)[\'\"]', result_str)
final_remote_url = None
for url in urls:
    if url.startswith('http'):
        final_remote_url = url
        break

if not final_remote_url:
    direct_urls = re.findall(r'(https?://[^\s\'\"]+\.(?:png|jpg|jpeg|webp|gif))', result_str)
    if direct_urls:
        final_remote_url = direct_urls[0]

if not final_remote_url:
    print(f'ERROR: Could not find valid URL. Raw result: {result_str}')
    sys.exit(1)

print(f'   -> Found Final Image URL: {final_remote_url}')

print('4. Downloading final image from remote host...')
try:
    img_req = requests.get(final_remote_url, timeout=60)
    content_type = img_req.headers.get('Content-Type', '')
    ext = 'png'
    if 'image/webp' in content_type:
        ext = 'webp'
    elif 'image/jpeg' in content_type or 'image/jpg' in content_type:
        ext = 'jpg'
        
    filename = f'output.{ext}'
    with open(filename, 'wb') as f:
        f.write(img_req.content)
    print(f'   -> Saved locally as {filename}')
except Exception as dl_err:
    print(f'Failed to download processed image: {dl_err}')
    sys.exit(1)

print(f'5. Uploading back to Docker Space: {space_url}')
try:
    with open(filename, 'rb') as f:
        res = requests.post(
            f'{space_url}/api/webhook/upload', 
            data={'run_id': run_id, 'github_run_id': github_run_id, 'ext': ext}, 
            files={'file': f},
            timeout=60
        )
    if res.status_code == 200:
        print('6. SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    err_str = str(e)
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-video.py": """
import os, sys, random, requests, time
from gradio_client import Client, handle_file
from PIL import Image, ImageOps

prompt = os.environ.get('PROMPT', '').strip()
if not prompt: 
    prompt = 'make this image come alive, cinematic motion, smooth animation'
else:
    prompt = f'{prompt}, cinematic motion, smooth animation, high quality'

negative_prompt = '色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋의, 残缺의, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形の, 毁容의, 形态畸形の肢体, 手指融合, 静止不动的画面, 杂乱의 背景, 三条腿, 背景人很多, 倒着走'

image_url = os.environ.get('IMAGE_URL', '')
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

try:
    duration = float(os.environ.get('DURATION', 5.0))
except Exception:
    duration = 5.0

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-video",
                "client_payload": {
                    "prompt": prompt,
                    "duration": duration,
                    "image_url": image_url,
                    "run_id": run_id,
                    "space_url": space_url
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

print('1. Downloading input image from Docker Space...')
try:
    req = requests.get(image_url, timeout=30)
    with open('input_raw.png', 'wb') as f:
        f.write(req.content)
except Exception as e:
    print(f'Download failed: {e}')
    sys.exit(1)

print('2. Analyzing image dimensions to prevent stretching...')
img = Image.open('input_raw.png').convert('RGB')
orig_w, orig_h = img.size
ratio = orig_w / orig_h

if ratio > 1.2:
    target_w, target_h = 832, 480  
    print('   -> Format: Landscape (16:9)')
elif ratio < 0.8:
    target_w, target_h = 480, 832  
    print('   -> Format: Portrait (9:16)')
else:
    target_w, target_h = 480, 480  
    print('   -> Format: Square (1:1)')

img_resized = ImageOps.fit(img, (target_w, target_h), Image.Resampling.LANCZOS)
img_resized.save('input.png')
print(f'   -> Image resized to {target_w}x{target_h}')

print('3. Connecting to AI Video Space...')
video_path = None

space_id = 'zerogpu-aoti/wan2-2-fp8da-aoti-faster'
print(f'   -> Loading Space: {space_id}')
try:
    client = Client(space_id)
except Exception as e:
    print(f'CRITICAL ERROR: Could not load space. {e}')
    sys.exit(1)

max_attempts = 4
for attempt in range(max_attempts):
    seed = random.randint(1, 2147483647)
    print(f'   -> Attempt {attempt + 1} of {max_attempts} with seed {seed} and duration {duration}s...')
    
    try:
        result = client.predict(
            handle_file('input.png'), 
            prompt,                    
            6,                         
            negative_prompt,           
            duration,                  
            1,                         
            1,                         
            seed,                      
            True,                      
            fn_index=0
        )
        
        if isinstance(result, list) and len(result) > 0:
            video_path = result[0].get('video') or result[0].get('path') if isinstance(result[0], dict) else result[0]
        elif isinstance(result, tuple) and len(result) > 0:
            video_path = result[0]
        elif isinstance(result, dict):
            video_path = result.get('video') or result.get('path')
        else:
            video_path = result
            
        if video_path and os.path.exists(str(video_path)):
            print('   -> Success! Video generated.')
            break
            
    except Exception as e:
        err_msg = str(e)
        print(f'   -> Failed on attempt {attempt + 1}: {err_msg}')
        time.sleep(5)

if not video_path or not os.path.exists(str(video_path)):
    err_str = "Video generation failed on all attempts"
    report_failure(err_str)
    sys.exit(1)

print('4. Uploading back to your Docker Space...')
try:
    with open(video_path, 'rb') as f:
        res = requests.post(f'{space_url}/api/webhook/upload', 
                         data={'run_id': run_id, 'github_run_id': github_run_id, 'ext': 'mp4'}, 
                         files={'file': f})
    if res.status_code == 200:
        print('5. SUCCESS!')
    else:
        sys.exit(1)
except Exception as e:
    err_str = str(e)
    report_failure(err_str)
    sys.exit(1)
        """,
        "generate-audio.py": """
import os, sys, requests, time
from gradio_client import Client, handle_file

AUDIO_TYPE = '___AUDIO_TYPE___'
NEGATIVE_PROMPT = '''___NEGATIVE_PROMPT___'''

prompt = os.environ.get('PROMPT', '')
try: seed = int(os.environ.get('SEED', '-1'))
except: seed = -1
try: duration = float(os.environ.get('DURATION', '8'))
except: duration = 8.0

file_url = os.environ.get('FILE_URL', '')
run_id = os.environ.get('RUN_ID')
space_url = os.environ.get('SPACE_URL')
github_run_id = os.environ.get('GITHUB_RUN_ID')

def report_failure(error_msg):
    try:
        requests.post(
            f"{space_url}/api/webhook/fail",
            json={
                "run_id": run_id,
                "error": error_msg,
                "event_type": "generate-audio",
                "client_payload": {
                    "data": {
                        "prompt": prompt,
                        "duration": duration,
                        "seed": seed,
                        "audio_type": AUDIO_TYPE,
                        "negative_prompt": NEGATIVE_PROMPT,
                        "file_url": file_url,
                        "run_id": run_id,
                        "space_url": space_url
                    }
                },
                "github_run_id": github_run_id
            },
            timeout=15
        )
    except Exception as e:
        print(f"Failed to report failure: {e}")

try:
    print('1. Connecting to Audio Space...')
    client = Client("hkchengrex/MMAudio")
    
    if AUDIO_TYPE == 'video':
        print('2. Downloading video for audio generation...')
        req = requests.get(file_url, timeout=30)
        ext = file_url.split('.')[-1] if '.' in file_url else 'mp4'
        local_vid = f'input.{ext}'
        with open(local_vid, 'wb') as f:
            f.write(req.content)
            
        print('3. Processing Video-to-Audio...')
        result = client.predict(
            video=handle_file(local_vid),
            prompt=prompt,
            negative_prompt=NEGATIVE_PROMPT,
            seed=seed,
            num_steps=25,
            cfg_strength=4.5,
            duration=duration,
            api_name="/video_to_audio"
        )
    else:
        print('2. Processing Text-to-Audio...')
        result = client.predict(
            prompt=prompt,
            negative_prompt=NEGATIVE_PROMPT,
            seed=seed,
            num_steps=25,
            cfg_strength=4.5,
            duration=duration,
            api_name="/text_to_audio"
        )
        
    print('4. Preparing File Data...')
    audio_path = None
    if isinstance(result, list) and len(result) > 0:
        audio_path = result[0].get('video') or result[0].get('path') or result[0].get('url') if isinstance(result[0], dict) else result[0]
    elif isinstance(result, tuple) and len(result) > 0:
        audio_path = result[0]
    elif isinstance(result, dict):
        audio_path = result.get('video') or result.get('path') or result.get('url')
    else:
        audio_path = result
        
    ext = 'mp4'
    if str(audio_path).endswith('.wav'): ext = 'wav'
    if str(audio_path).endswith('.mp3'): ext = 'mp3'
    
    print('5. Uploading back to Space...')
    with open(audio_path, 'rb') as f:
        res = requests.post(
            f'{space_url}/api/webhook/upload', 
            data={'run_id': run_id, 'github_run_id': github_run_id, 'ext': ext}, 
            files={'file': f}
        )
        
    if res.status_code == 200:
        print('SUCCESS!')
    else:
        sys.exit(1)
        
except Exception as e:
    err_str = str(e)
    print(f'Generation error: {err_str}')
    report_failure(err_str)
    sys.exit(1)
        """
    }
    for name, content in scripts.items():
        path = os.path.join("yml", name)
        
        if os.path.exists(path):
            try:
                with open(path, "r", encoding="utf-8") as f:
                    old_data = f.read()
                if "report_failure" not in old_data:
                    os.remove(path)
            except:
                pass
                
        if not os.path.exists(path):
            with open(path, "w", encoding="utf-8") as f:
                f.write(content.strip())

write_default_scripts()

# پایگاه داده SQLite
def init_db():
    conn = sqlite3.connect(DB_PATH)
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS users
                 (id TEXT PRIMARY KEY, count INTEGER, week_start REAL)''')
    c.execute('''CREATE TABLE IF NOT EXISTS retries
                 (run_id TEXT PRIMARY KEY, attempt INTEGER, event_type TEXT, client_payload TEXT, last_failed_github_run_id INTEGER)''')
    
    for col, col_type in [("event_type", "TEXT"), ("client_payload", "TEXT"), ("last_failed_github_run_id", "INTEGER")]:
        try:
            c.execute(f"ALTER TABLE retries ADD COLUMN {col} {col_type}")
        except sqlite3.OperationalError:
            pass
            
    conn.commit()
    conn.close()

def init_image_db():
    conn = sqlite3.connect(DB_IMAGE_PATH)
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS users
                 (id TEXT PRIMARY KEY, count INTEGER, week_start REAL)''')
    conn.commit()
    conn.close()

def init_image_edit_db():
    conn = sqlite3.connect(DB_IMAGE_EDIT_PATH)
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS users
                 (id TEXT PRIMARY KEY, count INTEGER, week_start REAL)''')
    conn.commit()
    conn.close()

def init_chat_queue_db():
    conn = sqlite3.connect(DB_CHAT_QUEUE_PATH)
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS queue
                 (run_id TEXT PRIMARY KEY, prompt TEXT, file_url TEXT, file_mime TEXT, status TEXT, timestamp REAL)''')
    conn.commit()
    conn.close()

init_db()
init_image_db()
init_image_edit_db()
init_chat_queue_db()

def get_db_connection():
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def get_image_db_connection():
    conn = sqlite3.connect(DB_IMAGE_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def get_image_edit_db_connection():
    conn = sqlite3.connect(DB_IMAGE_EDIT_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def get_chat_queue_db_connection():
    conn = sqlite3.connect(DB_CHAT_QUEUE_PATH)
    conn.row_factory = sqlite3.Row
    return conn

# --- توابع کمکی ---
def delete_github_run(gh_run_id):
    time.sleep(300)
    for acc in GITHUB_ACCOUNTS:
        url = f"https://api.github.com/repos/{acc['repo']}/actions/runs/{gh_run_id}"
        headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {acc['token']}"}
        r = requests.delete(url, headers=headers)
        if r.status_code in [204, 200]:
            print(f"🧹 [حذف موفق اکشن] شناسه {gh_run_id} از روی مخزن {acc['repo']} پاک شد.", flush=True)
            break

def get_user_identifier(data):
    fingerprint = data.get('fingerprint')
    if fingerprint: return str(fingerprint)
    if request.headers.getlist("X-Forwarded-For"):
        return request.headers.getlist("X-Forwarded-For")[0].split(',')[0].strip()
    return request.remote_addr

def has_forbidden_words(text):
    text_lower = text.lower()
    return any(keyword in text_lower for keyword in FORBIDDEN_KEYWORDS)

def get_github_runs():
    all_runs = []
    for acc in GITHUB_ACCOUNTS:
        url = f"https://api.github.com/repos/{acc['repo']}/actions/runs"
        headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {acc['token']}"}
        try:
            r = requests.get(f"{url}?per_page=100", headers=headers)
            if r.status_code == 200:
                runs = r.json().get('workflow_runs', [])
                for run in runs:
                    run['_account'] = acc
                all_runs.extend(runs)
        except Exception as e:
            print("Error fetching workflow runs:", e, flush=True)
            
    all_runs.sort(key=lambda x: x.get('created_at', ''), reverse=True)
    return all_runs

def is_today(created_at_str):
    try:
        run_date_str = created_at_str.split('T')[0]
        today_str = datetime.utcnow().strftime('%Y-%m-%d')
        return run_date_str == today_str
    except Exception:
        return False

# سیستم یکپارچه نجات و اجرای مجدد رانرهای موازی با استفاده از دیتابیس
def trigger_retry(run_id, github_run_id, error_msg):
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    c = conn.cursor()
    c.execute("SELECT * FROM retries WHERE run_id = ?", (run_id,))
    row = c.fetchone()
    if not row:
        conn.close()
        return "No retry record"

    attempt = row['attempt'] if row['attempt'] is not None else 0
    last_failed = row['last_failed_github_run_id']
    event_type = row['event_type']
    client_payload_raw = row['client_payload']

    # جلوگیری از دوبرابر شدن درخواست به ازای شناسه رانر یکسان در نخ‌های موازی
    if github_run_id and last_failed == int(github_run_id):
        conn.close()
        return "Already processed"

    new_attempt = attempt + 1
    max_retries = 20

    # بروزرسانی تعداد دفعات تلاش و شناسه شکست خورده جاری
    c.execute("UPDATE retries SET attempt = ?, last_failed_github_run_id = ? WHERE run_id = ?",
              (new_attempt, github_run_id, run_id))
    conn.commit()
    conn.close()

    if new_attempt <= max_retries:
        try:
            client_payload = json.loads(client_payload_raw)
        except Exception:
            client_payload = {}

        # بررسی و تعدیل تطبیقی ابعاد برای عبور از فیلترهای محدودیت زمانی سخت‌افزار
        is_duration_error = any(term in error_msg.lower() for term in ["duration", "larger", "max", "quota", "capacity", "limit"])
        if is_duration_error:
            print(f"📉 [تنظیم تطبیقی پارامترها] تعدیل پارامترها برای تلاش شماره {new_attempt}...", flush=True)
            if "data" in client_payload:
                original_steps = int(client_payload["data"].get("steps", 30))
                original_width = int(client_payload["data"].get("width", 1024))
                original_height = int(client_payload["data"].get("height", 1024))
                
                if new_attempt == 1:
                    client_payload["data"]["steps"] = min(20, original_steps)
                    client_payload["data"]["width"] = min(896, original_width)
                    client_payload["data"]["height"] = min(896, original_height)
                elif new_attempt == 2:
                    client_payload["data"]["steps"] = min(15, original_steps)
                    client_payload["data"]["width"] = min(768, original_width)
                    client_payload["data"]["height"] = min(768, original_height)
                else:
                    client_payload["data"]["steps"] = min(12, original_steps)
                    client_payload["data"]["width"] = min(512, original_width)
                    client_payload["data"]["height"] = min(512, original_height)
                    
        print(f"🔄 [ نجات خودکار - تلاش مجدد ] اجرای تلاش {new_attempt}/{max_retries} برای فرآیند {run_id} به دلیل خطا: {error_msg}", flush=True)
        dispatch_github_workflow(event_type, client_payload)
        return "Retrying"
    else:
        print(f"❌ [اتمام سقف تلاش مجدد] فرآیند {run_id} به سقف تلاش رسید ({max_retries}). تولید کارت تصویری خطا...", flush=True)
        create_error_image(run_id, f"Error: {error_msg}")
        return "Max retries reached"

# تابع ارسال رویداد داینامیک به گیت‌هاب با پشتیبانی از دیتابیس
def dispatch_github_workflow(event_type, client_payload):
    acc = get_next_account()
    if not acc:
        print("❌ [خطای دیسپچ] هیچ حساب گیت‌هابی پیکربندی نشده یا فعال نیست!", flush=True)
        dummy_response = requests.models.Response()
        dummy_response.status_code = 500
        dummy_response._content = b'No GitHub accounts configured'
        return dummy_response

    run_id = client_payload.get("run_id") or client_payload.get("data", {}).get("run_id")
    if run_id:
        RUN_ACCOUNT_MAPPING[run_id] = acc
        
        # ذخیره یا بازیابی فراداده در پایگاه داده نجات
        conn = sqlite3.connect(DB_PATH)
        c = conn.cursor()
        c.execute("INSERT OR IGNORE INTO retries (run_id, attempt, event_type, client_payload) VALUES (?, 0, ?, ?)",
                  (run_id, event_type, json.dumps(client_payload)))
        c.execute("UPDATE retries SET event_type = ?, client_payload = ? WHERE run_id = ?",
                  (event_type, json.dumps(client_payload), run_id))
        conn.commit()
        conn.close()

    script_path = os.path.join("yml", f"{event_type}.py")
    script_content = ""
    if os.path.exists(script_path):
        with open(script_path, "r", encoding="utf-8") as f:
            script_content = f.read()

    if event_type == "generate-audio":
        data_dict = client_payload.get("data", client_payload)
        audio_type = data_dict.get('audio_type', 'text')
        negative_prompt = data_dict.get('negative_prompt', 'music')
        script_content = script_content.replace("___AUDIO_TYPE___", str(audio_type))
        script_content = script_content.replace("___NEGATIVE_PROMPT___", str(negative_prompt))
    
    if "data" in client_payload:
        client_payload["data"]["script"] = script_content
    else:
        client_payload["script"] = script_content

    url = f"https://api.github.com/repos/{acc['repo']}/dispatches"
    headers = {
        "Accept": "application/vnd.github.v3+json", 
        "Authorization": f"token {acc['token']}"
    }

    print(f"🚀 [ارسال دیسپچ] در حال ارسال درخواست رویداد '{event_type}' به مخزن '{acc['repo']}'...", flush=True)

    try:
        r = requests.post(url, headers=headers, json={
            "event_type": event_type,
            "client_payload": client_payload
        }, timeout=20)
        print(f"📩 [پاسخ دیسپچ گیت‌هاب] مخزن: {acc['repo']} | کد وضعیت: {r.status_code} | متن پاسخ: {r.text[:500]}", flush=True)
        return r
    except Exception as e:
        print(f"❌ [خطای ارتباطی با گیت‌هاب] ارسال دیسپچ به {acc['repo']} ناموفق بود: {e}", flush=True)
        dummy = requests.models.Response()
        dummy.status_code = 502
        dummy._content = str(e).encode('utf-8')
        return dummy

# --- مسیرهای صفحات HTML ---
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/<page_name>')
def serve_html(page_name):
    if not page_name.endswith('.html'):
        page_name += '.html'
    try:
        return render_template(page_name)
    except:
        return "Page not found", 404

# --- API مخصوص مدیریت گیت‌هاب اکشنز در داشبورد ---
@app.route('/api/actions/active', methods=['GET'])
def get_active_actions():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    active = [
        {
            "id": run["id"],
            "name": run["name"],
            "status": run["status"],
            "html_url": run["html_url"],
            "created_at": run["created_at"]
        }
        for run in runs if run["status"] in ["queued", "in_progress", "waiting"]
    ]
    return jsonify({"status": "success", "actions": active})

@app.route('/api/actions/inactive-today', methods=['GET'])
def get_inactive_today():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    inactive_today = [
        {
            "id": run["id"],
            "name": run["name"],
            "status": run["status"],
            "conclusion": run.get("conclusion"),
            "html_url": run["html_url"],
            "created_at": run["created_at"]
        }
        for run in runs if run["status"] == "completed" and is_today(run["created_at"])
    ]
    return jsonify({"status": "success", "actions": inactive_today})

@app.route('/api/actions/completed-today-count', methods=['GET'])
def get_completed_today_count():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    completed_today = [
        run for run in runs if run["status"] == "completed" and run.get("conclusion") == "success" and is_today(run["created_at"])
    ]
    return jsonify({"status": "success", "count": len(completed_today)})

@app.route('/api/actions/delete-inactive', methods=['POST'])
def delete_inactive_actions():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    deleted_count = 0
    for run in runs:
        if run["status"] == "completed":
            acc = run['_account']
            headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {acc['token']}"}
            delete_url = f"https://api.github.com/repos/{acc['repo']}/actions/runs/{run['id']}"
            r = requests.delete(delete_url, headers=headers)
            if r.status_code in [204, 200]:
                deleted_count += 1
    return jsonify({"status": "success", "deleted_count": deleted_count})

@app.route('/api/actions/delete-failed-completed', methods=['POST'])
def delete_failed_completed():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    deleted_count = 0
    for run in runs:
        if run["status"] == "completed" and run.get("conclusion") in ["success", "failure", "cancelled"]:
            acc = run['_account']
            headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {acc['token']}"}
            delete_url = f"https://api.github.com/repos/{acc['repo']}/actions/runs/{run['id']}"
            r = requests.delete(delete_url, headers=headers)
            if r.status_code in [204, 200]:
                deleted_count += 1
    return jsonify({"status": "success", "deleted_count": deleted_count})

@app.route('/api/actions/cancel-delete-active', methods=['POST'])
def cancel_delete_active_actions():
    if not GITHUB_ACCOUNTS:
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    runs = get_github_runs()
    cancelled_count = 0
    deleted_count = 0
    for run in runs:
        if run["status"] in ["queued", "in_progress", "waiting"]:
            acc = run['_account']
            headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {acc['token']}"}
            cancel_url = f"https://api.github.com/repos/{acc['repo']}/actions/runs/{run['id']}/cancel"
            requests.post(cancel_url, headers=headers)
            cancelled_count += 1
            
            time.sleep(0.5)
            
            delete_url = f"https://api.github.com/repos/{acc['repo']}/actions/runs/{run['id']}"
            r_del = requests.delete(delete_url, headers=headers)
            if r_del.status_code in [204, 200]:
                deleted_count += 1
                
    return jsonify({
        "status": "success", 
        "cancelled_count": cancelled_count, 
        "deleted_count": deleted_count
    })

# وب‌هوک خطای ورک‌فلوها با یکپارچه‌سازی سیستم نجات
@app.route('/api/webhook/fail', methods=['POST'])
def webhook_fail():
    data = request.get_json()
    if not data:
        print("⚠️ [وب‌هوک خطا] پارامترهای ناقص یا خالی ارسال شده است.", flush=True)
        return "Invalid request", 400
        
    run_id = data.get('run_id')
    error_msg = data.get('error', '')
    gh_run_id = data.get('github_run_id')

    print(f"⚠️ [گزارش شکست فرآیند در اکشنز] شناسه: {run_id} | خطا: {error_msg}", flush=True)
    
    if gh_run_id:
        print(f"🧹 [پاک‌سازی خودکار] ثبت فرآیند حذف اجرای ناموفق {gh_run_id} در پس‌زمینه...", flush=True)
        threading.Thread(target=delete_github_run, args=(gh_run_id,)).start()
        
    if not run_id:
        return "Missing run_id", 400
        
    res = trigger_retry(run_id, gh_run_id, error_msg)
    return res, 200

# استریم چت زنده (SSE) برای تحلیل پاسخ‌های متنی گما
@app.route('/api/stream_chat/<run_id>')
def stream_chat(run_id):
    def generate_stream():
        import json
        file_path = f"static/images/{run_id}.txt"
        waited = 0
        while waited < 120:
            if os.path.exists(file_path):
                try:
                    with open(file_path, "r", encoding="utf-8") as f:
                        content = f.read().strip()
                    yield f"data: {json.dumps({'text': content})}\n\n"
                    yield "data: DONE\n\n"
                    return
                except Exception:
                    pass
            time.sleep(1)
            waited += 1
        yield f"data: {json.dumps({'text': '⚠️ خطای زمان انتظار: سرور در زمان مقرر پاسخ نداد.'})}\n\n"
        yield "data: DONE\n\n"
        
    return Response(generate_stream(), mimetype='text/event-stream')

# --- API برای ساخت عکس و شبیه‌سازی صدا ---
@app.route('/api/generate', methods=['POST'])
def generate():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای امنیتی] درخواست متوقف شد: هیچ توکنی متصل نیست.", flush=True)
        return jsonify({"status": "error", "message": "Secret GITHUB_TOKENs are missing!"}), 500
        
    data = request.json
    persian_prompt = data.get('prompt', '')

    # بررسی جهت شناسایی و تفکیک درخواست‌های شبیه‌ساز صدا
    is_voice_config = persian_prompt.startswith("VOICECONFIG_")

    # بررسی کلمات نامناسب تنها برای درخواست‌های عادی (غیر از شبیه‌ساز صدا)
    if not is_voice_config and has_forbidden_words(persian_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت فیلتر شد: {persian_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400
    
    print(f"📝 [ثبت درخواست جدید] پرامپت خام: {persian_prompt[:100]}...", flush=True)
    
    if is_voice_config:
        # کدهای تنظیمات شبیه‌ساز صدا نباید ترجمه شوند تا ساختار کاراکترها بدون تغییر باقی بماند
        english_prompt = persian_prompt
        print("🎙️ [شبیه‌ساز صدا] دور زدن مترجم گوگل جهت جلوگیری از تغییر کاراکترهای پیکربندی صوتی.", flush=True)
    else:
        try: 
            english_prompt = GoogleTranslator(source='auto', target='en').translate(persian_prompt)
            print(f"🔄 [ترجمه با موفقیت انجام شد] فارسی: {persian_prompt} -> انگلیسی: {english_prompt}", flush=True)
        except Exception as e: 
            print(f"⚠️ [ترجمه ناموفق] مترجم گوگل با خطا مواجه شد: {e}. استفاده از پرامپت خام.", flush=True)
            english_prompt = persian_prompt

    run_id = str(uuid.uuid4())
    
    client_payload = {
        "prompt": english_prompt, 
        "width": data.get('width', 1024), 
        "height": data.get('height', 1024), 
        "run_id": run_id, 
        "space_url": SPACE_URL
    }
    r = dispatch_github_workflow(data.get('action_name', 'generate-flux'), client_payload)
    
    if r.status_code == 204:
        print(f"✅ [درخواست دیسپچ شد] فرآیند با شناسه {run_id} با موفقیت در صف گیت‌هاب قرار گرفت.", flush=True)
        return jsonify({
            "status": "success", 
            "run_id": run_id, 
            "translated_prompt": english_prompt, 
            "translated": english_prompt
        })
    else:
        print(f"❌ [توقف درخواست] گیت‌هاب پاسخ غیرمنتظره داد (کد {r.status_code}): {r.text}", flush=True)
        return jsonify({"status": "error", "message": f"GitHub dispatch error: {r.status_code}"}), 400

# --- API مخصوص ویرایش عکس (AI Photoshop) ---
@app.route('/api/edit', methods=['POST'])
def edit_image():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای ویرایش] توکنی برای ارتباط وجود ندارد.", flush=True)
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    if 'image' not in request.files: 
        print("❌ [خطای ویرایش] فایلی ارسال نشده است.", flush=True)
        return jsonify({"status": "error", "message": "Image required"}), 400
    
    file = request.files['image']
    persian_prompt = request.form.get('prompt', '')

    if has_forbidden_words(persian_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت ویرایش فیلتر شد: {persian_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400
    
    print(f"📝 [درخواست ویرایش عکس] دستور فارسی: {persian_prompt}", flush=True)
    try: 
        english_prompt = GoogleTranslator(source='auto', target='en').translate(persian_prompt)
        print(f"🔄 [ترجمه ویرایش با موفقیت انجام شد] فارسی: {persian_prompt} -> انگلیسی: {english_prompt}", flush=True)
    except Exception as e: 
        print(f"⚠️ [ترجمه ناموفق ویرایش] استفاده از پرامپت خام: {e}", flush=True)
        english_prompt = persian_prompt

    run_id = str(uuid.uuid4())
    
    input_filename = f"{run_id}_input.png"
    file.save(f"static/images/{input_filename}")
    image_public_url = f"{SPACE_URL}/static/images/{input_filename}"

    client_payload = {
        "prompt": english_prompt,
        "image_url": image_public_url,
        "run_id": run_id,
        "space_url": SPACE_URL
    }
    r = dispatch_github_workflow("generate-editor", client_payload)
    
    if r.status_code == 204:
        print(f"✅ [درخواست ویرایش عکس دیسپچ شد] فرآیند با شناسه {run_id} با موفقیت در صف گیت‌هاب قرار گرفت.", flush=True)
        return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
    else:
        print(f"❌ [توقف ویرایش عکس] گیت‌هاب پاسخ خطا داد (کد {r.status_code}): {r.text}", flush=True)
        return jsonify({"status": "error", "message": f"GitHub dispatch error: {r.status_code}"}), 400

# --- API مخصوص تولید تصویر FLUX.2 ---
@app.route('/api/generate-flux2', methods=['POST'])
def generate_flux2():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای FLUX2] حسابی یافت نشد.", flush=True)
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    
    persian_prompt = request.form.get('prompt', '')
    if has_forbidden_words(persian_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت Flux2 فیلتر شد: {persian_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400
    
    print(f"📝 [ثبت درخواست جدید Flux2] پرامپت فارسی: {persian_prompt}", flush=True)
    try:
        english_prompt = GoogleTranslator(source='auto', target='en').translate(persian_prompt)
        print(f"🔄 [ترجمه Flux2 انجام شد] فارسی: {persian_prompt} -> انگلیسی: {english_prompt}", flush=True)
    except Exception as e:
        print(f"⚠️ [ترجمه ناموفق Flux2] استفاده از پرامپت خام: {e}", flush=True)
        english_prompt = persian_prompt

    run_id = str(uuid.uuid4())
    
    image_urls = []
    files = request.files.getlist('images')
    for idx, file in enumerate(files):
        if file and file.filename:
            ext = file.filename.split('.')[-1] if '.' in file.filename else 'png'
            input_filename = f"{run_id}_input_{idx}.{ext}"
            file.save(f"static/images/{input_filename}")
            image_urls.append(f"{SPACE_URL}/static/images/{input_filename}")

    try: width = int(request.form.get('width', 1024))
    except ValueError: width = 1024
        
    try: height = int(request.form.get('height', 1024))
    except ValueError: height = 1024

    try: steps = int(request.form.get('steps', 30))
    except ValueError: steps = 30

    try: guidance = float(request.form.get('guidance', 4.0))
    except ValueError: guidance = 4.0

    try:
        seed_raw = request.form.get('seed', '0')
        seed = int(seed_raw) if seed_raw.strip() else 0
    except ValueError:
        seed = 0

    client_payload = {
        "data": {
            "prompt": english_prompt,
            "image_urls": image_urls,
            "width": width,
            "height": height,
            "steps": steps,
            "guidance": guidance,
            "seed": seed,
            "randomize_seed": request.form.get('randomizeSeed') == 'true',
            "prompt_upsampling": request.form.get('promptUpsampling') == 'true',
            "run_id": run_id,
            "space_url": SPACE_URL,
            "hf_token": request.form.get('hfToken', '')
        }
    }
    r = dispatch_github_workflow("generate-flux2", client_payload)
    
    if r.status_code == 204:
        print(f"✅ [درخواست Flux2 دیسپچ شد] فرآیند با شناسه {run_id} با موفقیت در صف گیت‌هاب قرار گرفت.", flush=True)
        return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
    else:
        print(f"❌ [توقف درخواست Flux2] خطا در ارسال درخواست دیسپچ: {r.status_code}", flush=True)
        return jsonify({"status": "error", "message": f"GitHub dispatch error: {r.status_code}"}), 400

# --- API مخصوص تبدیل عکس به ویدیو ---
@app.route('/api/generate-video', methods=['POST'])
def generate_video():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای ویدیو] حسابی جهت دیسپچ متصل نیست.", flush=True)
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    if 'image' not in request.files: 
        print("❌ [خطای ویدیو] تصویری به سرور ارسال نشده است.", flush=True)
        return jsonify({"status": "error", "message": "Image required"}), 400
    
    file = request.files['image']
    user_prompt = request.form.get('prompt', '').strip()
    duration = request.form.get('duration', 5.0)

    if has_forbidden_words(user_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت ویدیو فیلتر شد: {user_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400
    
    run_id = str(uuid.uuid4())
    
    input_filename = f"{run_id}_video_input.png"
    local_image_path = os.path.join("static/images", input_filename)
    file.save(local_image_path)
    image_public_url = f"{SPACE_URL}/static/images/{input_filename}"

    master_prompt = """You are an expert AI Animation Planner. Your absolute highest priority is to faithfully and creatively execute the user's specific request based on the provided image.

    1. If the user prompt is empty or generic (like "animate this"), add subtle, high-quality, believable cinematic motion (e.g., slow zoom, water flowing, wind in hair).
    2. If the user gives specific directions, focus ENTIRELY on executing that command perfectly. If the action is not visible in-frame, use cinematic camera movements to reveal it.
    3. You must output ONLY a highly detailed, descriptive animation prompt in ENGLISH. Do not translate literally; ENHANCE the prompt for a text-to-video AI model.
    4. MUST Include keywords at the end: cinematic, photorealistic, high detail, smooth motion, 8k.

    CRITICAL RULE: DO NOT say "Here is the prompt" or give any conversational explanations. DO NOT output JSON. Output ONLY the raw English animation prompt text and NOTHING ELSE."""

    english_prompt = ""
    gemma_success = False
    
    prompt_for_gemma = user_prompt if user_prompt else "لطفاً این تصویر را به یک ویدیوی سینمایی بسیار جذاب و واقع‌گرایانه متحرک کن."
    combined_prompt = f"{master_prompt}\n\nUser request: {prompt_for_gemma}"

    print(f"🎬 [متحرک‌سازی تصویر] آغاز هماهنگی سناریو با پلانر تصویری Gemma...", flush=True)
    
    for attempt in range(1):
        try:
            gemma_run_id = f"gemma_{run_id}_{attempt}"
            gemma_txt_path = os.path.join("static/images", f"{gemma_run_id}.txt")
            
            client_payload_gemma = {
                "prompt": combined_prompt,
                "file_url": image_public_url,
                "file_mime": "image/png",
                "run_id": gemma_run_id,
                "space_url": SPACE_URL
            }
            r_gemma = dispatch_github_workflow("chat-gemma", client_payload_gemma)
            
            if r_gemma.status_code == 204:
                waited = 0
                while waited < 50:
                    if os.path.exists(gemma_txt_path):
                        with open(gemma_txt_path, "r", encoding="utf-8") as f:
                            english_prompt = f.read().strip()
                        gemma_success = True
                        break
                    time.sleep(3)
                    waited += 3
            
            if gemma_success:
                if "```" in english_prompt:
                    english_prompt = english_prompt.replace("```text", "").replace("```", "").replace("```json", "").strip()

                if "پردازش متوقف شد" in english_prompt or "سهمیه سرور موقتاً پر شده" in english_prompt:
                    print(f"⚠️ پلانر تصویری Gemma با محدودیت سخت‌افزاری روبرو شد. تلاش مجدد...", flush=True)
                    gemma_success = False
                else:
                    break 
                
        except Exception as e:
            print(f"⚠️ خطای تلاش متحرک‌سازی در پلانر Gemma: {e}", flush=True)
        
        if not gemma_success:
            time.sleep(4) 

    if not gemma_success:
        print("🔄 [پلانر تصویر با خطا متوقف شد] ناتوانی در دریافت پرامپت بهینه. بازگشت به مترجم ساده...", flush=True)
        try: 
            if user_prompt:
                english_prompt = GoogleTranslator(source='auto', target='en').translate(user_prompt)
                english_prompt += ", cinematic motion, photorealistic, high detail, smooth animation, 8k"
            else:
                english_prompt = "cinematic motion, photorealistic, high detail, smooth animation, 8k"
        except: 
            english_prompt = "cinematic motion, photorealistic, high detail, smooth animation, 8k"

    print(f"🎥 [پرامپت ساخت نهایی ویدیو] {english_prompt}", flush=True)

    client_payload_video = {
        "prompt": english_prompt,
        "duration": duration,
        "image_url": image_public_url,
        "run_id": run_id,
        "space_url": SPACE_URL
    }
    r_vid = dispatch_github_workflow("generate-video", client_payload_video)
    
    if r_vid.status_code == 204:
        print(f"✅ [درخواست ساخت ویدیو دیسپچ شد] فرآیند با شناسه {run_id} با موفقیت در صف گیت‌هاب قرار گرفت.", flush=True)
        return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
    else:
        print(f"❌ [توقف متحرک‌سازی عکس] دیسپچ با کد {r_vid.status_code} رد شد.", flush=True)
        return jsonify({"status": "error", "message": f"GitHub dispatch error: {r_vid.status_code}"}), 400

# ==========================================
# API اختصاصی جهت پردازش فایل صوتی در گیت هاب
# ==========================================
@app.route('/api/generate-audio', methods=['POST'])
def generate_audio():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای صوتی] توکن‌های گیت‌هاب یافت نشدند.", flush=True)
        return jsonify({"status": "error", "message": "Tokens missing"}), 500
    
    audio_type = request.form.get('type', 'text')
    persian_prompt = request.form.get('prompt', '')
    persian_negative = request.form.get('negative_prompt', 'music')
    duration = request.form.get('duration', '8')
    seed = request.form.get('seed', '-1')
    
    if has_forbidden_words(persian_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت صوتی فیلتر شد: {persian_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400
        
    print(f"📝 [ثبت درخواست صوتی جدید] نوع: {audio_type} | پرامپت فارسی: {persian_prompt}", flush=True)
    try: 
        english_prompt = GoogleTranslator(source='auto', target='en').translate(persian_prompt) if persian_prompt else ""
        english_negative = GoogleTranslator(source='auto', target='en').translate(persian_negative) if persian_negative else "music"
        print(f"🔄 [ترجمه با موفقیت انجام شد] فارسی: {persian_prompt} -> انگلیسی: {english_prompt}", flush=True)
    except Exception as e: 
        print(f"⚠️ [ترجمه ناموفق صوتی] استفاده از پرامپت صوتی خام: {e}", flush=True)
        english_prompt = persian_prompt
        english_negative = persian_negative

    run_id = str(uuid.uuid4())
    file_url = ""
    
    if audio_type == 'video':
        file = request.files.get('file')
        if not file: 
            print("❌ [خطای صوتی] فایل ویدیویی دریافت نشد.", flush=True)
            return jsonify({"status": "error", "message": "Video file required"}), 400
        ext = file.filename.split('.')[-1] if '.' in file.filename else 'mp4'
        filename = f"{run_id}_input.{ext}"
        file.save(f"static/images/{filename}")
        file_url = f"{SPACE_URL}/static/images/{filename}"

    client_payload = {
        "data": {
            "prompt": english_prompt,
            "negative_prompt": english_negative,
            "duration": duration,
            "seed": seed,
            "audio_type": audio_type,
            "file_url": file_url,
            "run_id": run_id,
            "space_url": SPACE_URL
        }
    }
    
    r = dispatch_github_workflow("generate-audio", client_payload)
    if r.status_code == 204:
        print(f"✅ [درخواست صوتی دیسپچ شد] فرآیند با شناسه {run_id} با موفقیت در صف گیت‌هاب قرار گرفت.", flush=True)
        return jsonify({"status": "success", "run_id": run_id, "translated": english_prompt})
    else:
        print(f"❌ [توقف درخواست صوتی] دیسپچ با خطا مواجه شد: {r.status_code}", flush=True)
        return jsonify({"status": "error", "message": f"GitHub dispatch error: {r.status_code}"}), 400

# ==========================================
# سیستم جدید چت بات (ارسال مستقیم و بدون تاخیر به گیت‌هاب)
# ==========================================
@app.route('/api/chat', methods=['POST'])
def chat_api():
    if not GITHUB_ACCOUNTS: 
        print("❌ [خطای چت] توکن گیت‌هاب جهت چت یافت نشد.", flush=True)
        return jsonify({"status": "error", "message": "Tokens missing"}), 500

    text_prompt = request.form.get('prompt', '')
    if has_forbidden_words(text_prompt):
        print(f"⚠️ [محتوای غیرمجاز] پرامپت چت فیلتر شد: {text_prompt}", flush=True)
        return jsonify({"status": "error", "message": "متن ورودی شامل کلمات نامناسب است."}), 400

    file_obj = request.files.get('file')
    run_id = str(uuid.uuid4())
    file_url = ""
    mime_type = ""

    print(f"💬 [ثبت چت] پرامپت کاربر: {text_prompt[:100]}...", flush=True)
    if file_obj:
        ext = file_obj.filename.split('.')[-1] if '.' in file_obj.filename else 'bin'
        mime_type = file_obj.mimetype
        filename = f"{run_id}_chat_attach.{ext}"
        file_obj.save(f"static/images/{filename}")
        file_url = f"{SPACE_URL}/static/images/{filename}"
        print(f"   📎 پیوست چت ثبت شد در: {file_url}", flush=True)

    client_payload = {
        "prompt": text_prompt,
        "file_url": file_url,
        "file_mime": mime_type,
        "run_id": run_id,
        "space_url": SPACE_URL
    }
    r = dispatch_github_workflow("chat-gemma", client_payload)
    if r.status_code == 204:
         print(f"✅ [درخواست چت دیسپچ شد] چت با شناسه {run_id} ارسال شد.", flush=True)
         return jsonify({"status": "success", "run_id": run_id})
    else:
         print(f"❌ [توقف چت] گیت‌هاب به دیسپچ چت پاسخ خطا داد: {r.status_code}", flush=True)
         return jsonify({"status": "error", "message": f"GitHub dispatch error: {r.status_code}"}), 400

@app.route('/api/worker/poll', methods=['POST'])
def worker_poll():
    return jsonify({"status": "empty"})

# --- API میکس ویدیوها سمت سرور ---
@app.route('/api/merge-videos', methods=['POST'])
def merge_videos():
    if 'base_video' not in request.files or 'new_clip' not in request.files:
        return jsonify({"error": "هر دو فایل ویدیویی برای میکس لازم هستند."}), 400

    base_video_file = request.files['base_video']
    new_clip_file = request.files['new_clip']
    
    temp_files_to_clean = []

    try:
        base_video_path = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}.mp4")
        new_clip_path = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}.mp4")
        output_path = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}.mp4")

        temp_files_to_clean.extend([base_video_path, new_clip_path, output_path])

        base_video_file.save(base_video_path)
        new_clip_file.save(new_clip_path)

        probe = ffmpeg.probe(base_video_path)
        video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
        width = int(video_stream['width'])
        height = int(video_stream['height'])

        input1 = ffmpeg.input(base_video_path)
        input2 = ffmpeg.input(new_clip_path).filter('scale', width, height).filter('setsar', '1')

        merged_video = ffmpeg.concat(input1, input2, v=1, a=0).output(
            output_path,
            crf=18,
            preset='slow',
            pix_fmt='yuv420p'
        )
        
        merged_video.run(overwrite_output=True, quiet=True)
        
        @after_this_request
        def cleanup(response):
            try:
                for f_path in temp_files_to_clean:
                    if os.path.exists(f_path):
                        os.remove(f_path)
            except:
                pass
            return response

        return send_file(output_path, mimetype='video/mp4', as_attachment=True, download_name='merged_video.mp4')

    except ffmpeg.Error as e:
        for f_path in temp_files_to_clean:
            if os.path.exists(f_path):
                os.remove(f_path)
        return jsonify({"error": "خطا در پردازش و میکس ویدیو در سرور."}), 500
    except Exception as e:
        for f_path in temp_files_to_clean:
            if os.path.exists(f_path):
                os.remove(f_path)
        return jsonify({"error": "خطای پیش‌بینی نشده در سرور."}), 500

# --- مدیریت اعتبارها ---
@app.route('/api/check-credit', methods=['POST'])
def check_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    credits_remaining = USAGE_LIMIT
    limit_reached = False
    reset_timestamp = 0
    
    if user_record:
        if user_record['week_start'] < (now - ONE_WEEK_SECONDS):
            c.execute("UPDATE users SET count = 0, week_start = ? WHERE id = ?", (now, user_id))
            conn.commit()
            credits_remaining = USAGE_LIMIT
        else:
            credits_remaining = max(0, USAGE_LIMIT - user_record['count'])
            if credits_remaining == 0:
                limit_reached = True
                reset_timestamp = user_record['week_start'] + ONE_WEEK_SECONDS
                
    conn.close()
    return jsonify({
        "credits_remaining": credits_remaining, 
        "limit_reached": limit_reached, 
        "reset_timestamp": reset_timestamp
    })

@app.route('/api/use-credit', methods=['POST'])
def use_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    if user_record:
        if user_record['week_start'] < (now - ONE_WEEK_SECONDS):
            c.execute("UPDATE users SET count = 1, week_start = ? WHERE id = ?", (now, user_id))
        else:
            if user_record['count'] >= USAGE_LIMIT:
                conn.close()
                reset_timestamp = user_record['week_start'] + ONE_WEEK_SECONDS
                return jsonify({"status": "limit_reached", "credits_remaining": 0, "reset_timestamp": reset_timestamp}), 429
            c.execute("UPDATE users SET count = count + 1 WHERE id = ?", (user_id,))
    else:
        c.execute("INSERT INTO users (id, count, week_start) VALUES (?, 1, ?)", (user_id, now))
        
    conn.commit()
    c.execute("SELECT count FROM users WHERE id = ?", (user_id,))
    new_count = c.fetchone()['count']
    credits_remaining = USAGE_LIMIT - new_count
    
    conn.close()
    return jsonify({"status": "success", "credits_remaining": credits_remaining})

@app.route('/api/check-image-credit', methods=['POST'])
def check_image_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_image_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    credits_remaining = IMAGE_USAGE_LIMIT
    limit_reached = False
    reset_timestamp = 0
    
    if user_record:
        if user_record['week_start'] < (now - ONE_DAY_SECONDS):
            c.execute("UPDATE users SET count = 0, week_start = ? WHERE id = ?", (now, user_id))
            conn.commit()
            credits_remaining = IMAGE_USAGE_LIMIT
        else:
            credits_remaining = max(0, IMAGE_USAGE_LIMIT - user_record['count'])
            if credits_remaining == 0:
                limit_reached = True
                reset_timestamp = user_record['week_start'] + ONE_DAY_SECONDS
                
    conn.close()
    return jsonify({
        "credits_remaining": credits_remaining, 
        "limit_reached": limit_reached, 
        "reset_timestamp": reset_timestamp
    })

@app.route('/api/use-image-credit', methods=['POST'])
def use_image_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_image_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    if user_record:
        if user_record['week_start'] < (now - ONE_DAY_SECONDS):
            c.execute("UPDATE users SET count = 1, week_start = ? WHERE id = ?", (now, user_id))
        else:
            if user_record['count'] >= IMAGE_USAGE_LIMIT:
                conn.close()
                reset_timestamp = user_record['week_start'] + ONE_DAY_SECONDS
                return jsonify({"status": "limit_reached", "credits_remaining": 0, "reset_timestamp": reset_timestamp}), 429
            c.execute("UPDATE users SET count = count + 1 WHERE id = ?", (user_id,))
    else:
        c.execute("INSERT INTO users (id, count, week_start) VALUES (?, 1, ?)", (user_id, now))
        
    conn.commit()
    c.execute("SELECT count FROM users WHERE id = ?", (user_id,))
    new_count = c.fetchone()['count']
    credits_remaining = IMAGE_USAGE_LIMIT - new_count
    
    conn.close()
    return jsonify({"status": "success", "credits_remaining": credits_remaining})

@app.route('/api/check-edit-credit', methods=['POST'])
def check_edit_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_image_edit_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    credits_remaining = EDIT_USAGE_LIMIT
    limit_reached = False
    reset_timestamp = 0
    
    if user_record:
        if user_record['week_start'] < (now - ONE_WEEK_SECONDS):
            c.execute("UPDATE users SET count = 0, week_start = ? WHERE id = ?", (now, user_id))
            conn.commit()
            credits_remaining = EDIT_USAGE_LIMIT
        else:
            credits_remaining = max(0, EDIT_USAGE_LIMIT - user_record['count'])
            if credits_remaining == 0:
                limit_reached = True
                reset_timestamp = ONE_WEEK_SECONDS + user_record['week_start']
                
    conn.close()
    return jsonify({
        "credits_remaining": credits_remaining, 
        "limit_reached": limit_reached, 
        "reset_timestamp": reset_timestamp
    })

@app.route('/api/use-edit-credit', methods=['POST'])
def use_edit_credit():
    data = request.get_json()
    if not data: return jsonify({"error": "Invalid request"}), 400
    user_id = get_user_identifier(data)
    
    conn = get_image_edit_db_connection()
    c = conn.cursor()
    c.execute("SELECT * FROM users WHERE id = ?", (user_id,))
    user_record = c.fetchone()
    
    now = time.time()
    if user_record:
        if user_record['week_start'] < (now - ONE_WEEK_SECONDS):
            c.execute("UPDATE users SET count = 1, week_start = ? WHERE id = ?", (now, user_id))
        else:
            if user_record['count'] >= EDIT_USAGE_LIMIT:
                conn.close()
                reset_timestamp = user_record['week_start'] + ONE_WEEK_SECONDS
                return jsonify({"status": "limit_reached", "credits_remaining": 0, "reset_timestamp": reset_timestamp}), 429
            c.execute("UPDATE users SET count = count + 1 WHERE id = ?", (user_id,))
    else:
        c.execute("INSERT INTO users (id, count, week_start) VALUES (?, 1, ?)", (user_id, now))
        
    conn.commit()
    c.execute("SELECT count FROM users WHERE id = ?", (user_id,))
    new_count = c.fetchone()['count']
    credits_remaining = EDIT_USAGE_LIMIT - new_count
    
    conn.close()
    return jsonify({"status": "success", "credits_remaining": credits_remaining})


# --- دریافت و ارسال فایل ---
@app.route('/api/webhook/upload', methods=['POST'])
def webhook_upload():
    run_id = request.form.get('run_id')
    gh_run_id = request.form.get('github_run_id')
    ext = request.form.get('ext', 'webp')
    
    if 'file' not in request.files or not run_id:
        print("⚠️ [وب‌هوک آپلود] آپلود ناموفق به دلیل کسری پارامتر.", flush=True)
        return "Invalid request", 400
        
    file = request.files['file']
    file_path = f"static/images/{run_id}.{ext}"
    file.save(file_path)
    print(f"📥 [فایل دریافت شد] ذخیره‌سازی موفق خروجی در مسیر: {file_path}", flush=True)
    
    if gh_run_id: 
        print(f"🧹 [پاک‌سازی خودکار] ثبت فرآیند حذف اجرای {gh_run_id} در پس‌زمینه...", flush=True)
        threading.Thread(target=delete_github_run, args=(gh_run_id,)).start()
    return "OK", 200

@app.route('/api/status/<run_id>')
def check_status(run_id):
    # بررسی وجود فایل گزارش خطای نهایی
    error_path = f"static/images/{run_id}.txt"
    if os.path.exists(error_path):
        try:
            with open(error_path, "r", encoding="utf-8") as ef:
                error_msg = ef.read().strip()
            return jsonify({"status": "failed", "message": error_msg})
        except Exception:
            pass

    valid_extensions = [
        'png', 'jpg', 'jpeg', 'webp', 'gif',  
        'mp4', 'webm', 'mov', 'mkv', 'avi',   
        'mp3', 'wav', 'ogg', 'm4a'
    ]
    
    for ext in valid_extensions:
        file_path = f"static/images/{run_id}.{ext}"
        if os.path.exists(file_path):
            seed_val = None
            seed_path = f"static/images/{run_id}_seed.txt"
            if os.path.exists(seed_path):
                try:
                    with open(seed_path, 'r') as sf:
                        seed_val = sf.read().strip()
                except Exception:
                    pass
            
            resp = {"status": "ready", "url": f"/static/images/{run_id}.{ext}"}
            if seed_val:
                resp["seed"] = seed_val
            return jsonify(resp)

    # نظارت بر وضعیت جاری در گیت‌هاب در صورت نبود خروجی
    now = time.time()
    last_check = LAST_RUN_CHECK.get(run_id, 0)
    if now - last_check > 12:  
        LAST_RUN_CHECK[run_id] = now
        threading.Thread(target=check_github_run_status_and_log, args=(run_id,)).start()
            
    return jsonify({"status": "processing"})

@app.route('/static/images/<filename>')
def serve_file(filename):
    return send_from_directory('static/images', filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)