File size: 45,406 Bytes
6c4eb06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c298a02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c4eb06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# === FILE: app.py (Video Agent - FIXED: نص ثابت على شاشة متحركة) ===
# 
# IMPORTANT: For proper text rendering, make sure Roboto-Bold.ttf font is installed
# Install with: apt-get install -y fonts-roboto
# Or download from: https://fonts.google.com/specimen/Roboto
# 
# If font is not available, the system will fall back to Arial-Bold
#
# ✅ UPDATED: Video output dimensions set to YouTube Shorts (1080x1920 - 9:16 aspect ratio)
# ✅ UPDATED: Smart background color extraction from image for letterboxing

import os
import io
import json
import base64
import logging
import random
from typing import Optional, Dict, Any, Tuple, List
from datetime import datetime
import tempfile
from collections import Counter

import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFont

# Fix for Pillow 10.0.0+ compatibility with MoviePy
if not hasattr(Image, 'ANTIALIAS'):
    Image.ANTIALIAS = Image.LANCZOS

# استيراد مكتبات معالجة الصوت والفيديو
from kokoro_engine import KokoroEngine

KOKORO_AVAILABLE = True

try:
    from moviepy.editor import ImageClip, AudioFileClip, CompositeVideoClip, TextClip
    MOVIEPY_AVAILABLE = True
except ImportError:
    MOVIEPY_AVAILABLE = False
    logging.warning("⚠️ MoviePy not available. Install with: pip install moviepy")

# ---------------- Logging Setup ----------------
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
log = logging.getLogger("video_agent")

# ---------------- Environment Variables ----------------
VIDEO_HISTORY_DIR = os.getenv("VIDEO_HISTORY_DIR", "video_history")
MAX_HISTORY_COUNT = int(os.getenv("MAX_HISTORY_COUNT", "10"))
VOICE_STATE_FILE = os.getenv("VOICE_STATE_FILE", "voice_rotation_state.json")

# ---------------- YouTube Shorts Dimensions ----------------
YOUTUBE_SHORTS_WIDTH = 1080
YOUTUBE_SHORTS_HEIGHT = 1920

# ---------------- Kokoro Voices List ----------------
KOKORO_VOICES = [
    
    # British Male
    "bm_daniel",
    "bm_fable",
    "bm_george",
    "bm_lewis",
    # American Male
    "am_adam",
    "am_echo",
    "am_eric",
    "am_fenrir",
    "am_liam",
    "am_michael",
    "am_onyx",
    "am_puck",
    # British Female
    "bf_alice",
    "bf_emma",
    "bf_isabella",
    "bf_lily",
    # American Female
    "af_alloy",
    "af_aoede",
    "af_bella",
    "af_heart",
    "af_jessica",
    "af_kore",
    "af_nicole",
    "af_nova",
    "af_river",
    "af_sarah",
    "af_sky",
]

# ---------------- Initialization Check ----------------
IS_SERVICE_READY = True

# ---------------- Color Extraction Function ----------------
def get_dominant_color(image: Image.Image, sample_size: int = 100) -> Tuple[int, int, int]:
    """
    استخراج اللون السائد من الصورة باستخدام تحليل الألوان الأكثر شيوعاً.
    
    Args:
        image: صورة PIL
        sample_size: حجم العينة لتسريع المعالجة
    
    Returns:
        Tuple من (R, G, B) للون السائد
    """
    try:
        # تصغير الصورة لتسريع المعالجة
        img_small = image.copy()
        img_small.thumbnail((sample_size, sample_size))
        
        # تحويل إلى RGB إذا لزم الأمر
        if img_small.mode != 'RGB':
            img_small = img_small.convert('RGB')
        
        # الحصول على جميع الألوان
        pixels = list(img_small.getdata())
        
        # حساب اللون الأكثر شيوعاً
        color_counter = Counter(pixels)
        dominant_color = color_counter.most_common(1)[0][0]
        
        log.info(f"Dominant color extracted: RGB{dominant_color}")
        return dominant_color
        
    except Exception as e:
        log.warning(f"Failed to extract dominant color: {e}, using default (30, 30, 30)")
        return (30, 30, 30)  # لون رمادي غامق كخيار احتياطي

def get_edge_average_color(image: Image.Image, border_width: int = 50) -> Tuple[int, int, int]:
    """
    استخراج متوسط اللون من حواف الصورة (أكثر دقة للخلفية).
    
    Args:
        image: صورة PIL
        border_width: عرض الحدود للعينة
    
    Returns:
        Tuple من (R, G, B) لمتوسط لون الحواف
    """
    try:
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        width, height = image.size
        
        # استخراج عينات من الحواف
        edge_pixels = []
        
        # الحافة العلوية
        for x in range(width):
            for y in range(min(border_width, height)):
                edge_pixels.append(image.getpixel((x, y)))
        
        # الحافة السفلية
        for x in range(width):
            for y in range(max(0, height - border_width), height):
                edge_pixels.append(image.getpixel((x, y)))
        
        # الحافة اليسرى
        for y in range(height):
            for x in range(min(border_width, width)):
                edge_pixels.append(image.getpixel((x, y)))
        
        # الحافة اليمنى
        for y in range(height):
            for x in range(max(0, width - border_width), width):
                edge_pixels.append(image.getpixel((x, y)))
        
        # حساب المتوسط
        if edge_pixels:
            avg_r = int(sum(p[0] for p in edge_pixels) / len(edge_pixels))
            avg_g = int(sum(p[1] for p in edge_pixels) / len(edge_pixels))
            avg_b = int(sum(p[2] for p in edge_pixels) / len(edge_pixels))
            
            log.info(f"Edge average color: RGB({avg_r}, {avg_g}, {avg_b})")
            return (avg_r, avg_g, avg_b)
        else:
            return (30, 30, 30)
            
    except Exception as e:
        log.warning(f"Failed to extract edge color: {e}, using default")
        return (30, 30, 30)

def prepare_image_for_shorts(image: Image.Image) -> Image.Image:
    """
    تحضير الصورة لتناسب أبعاد YouTube Shorts (1080x1920) مع خلفية ملونة.
    
    Args:
        image: الصورة الأصلية
    
    Returns:
        صورة بأبعاد 1080x1920 مع خلفية ملونة مناسبة
    """
    try:
        # تحويل إلى RGB
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        # استخراج لون الخلفية المناسب (من حواف الصورة)
        bg_color = get_edge_average_color(image, border_width=30)
        
        # إنشاء canvas جديد بأبعاد YouTube Shorts
        canvas = Image.new('RGB', (YOUTUBE_SHORTS_WIDTH, YOUTUBE_SHORTS_HEIGHT), bg_color)
        
        # حساب نسبة القياس للصورة للحفاظ على النسب
        img_width, img_height = image.size
        target_ratio = YOUTUBE_SHORTS_WIDTH / YOUTUBE_SHORTS_HEIGHT
        img_ratio = img_width / img_height
        
        if img_ratio > target_ratio:
            # الصورة أعرض من النسبة المطلوبة
            new_width = YOUTUBE_SHORTS_WIDTH
            new_height = int(new_width / img_ratio)
        else:
            # الصورة أطول من النسبة المطلوبة
            new_height = YOUTUBE_SHORTS_HEIGHT
            new_width = int(new_height * img_ratio)
        
        # تغيير حجم الصورة
        resized_image = image.resize((new_width, new_height), Image.LANCZOS)
        
        # حساب موضع اللصق لتوسيط الصورة
        paste_x = (YOUTUBE_SHORTS_WIDTH - new_width) // 2
        paste_y = (YOUTUBE_SHORTS_HEIGHT - new_height) // 2
        
        # لصق الصورة على الـ canvas
        canvas.paste(resized_image, (paste_x, paste_y))
        
        log.info(f"✅ Image prepared for YouTube Shorts: {YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT} with background color {bg_color}")
        
        return canvas
        
    except Exception as e:
        log.error(f"Failed to prepare image for Shorts: {e}")
        raise

# ---------------- Voice Rotation Manager ----------------
class VoiceRotationManager:
    """إدارة تدوير الأصوات بشكل دوري"""
    
    def __init__(self, state_file: str, voices: List[str]):
        self.state_file = state_file
        self.voices = voices
        self.current_index = 0
        self.load_state()
    
    def load_state(self):
        """تحميل حالة التدوير من الملف"""
        if os.path.exists(self.state_file):
            try:
                with open(self.state_file, "r") as f:
                    data = json.load(f)
                    self.current_index = data.get("current_voice_index", 0)
                    # التأكد من أن المؤشر في النطاق الصحيح
                    if self.current_index >= len(self.voices):
                        self.current_index = 0
                log.info(f"Voice rotation state loaded: index={self.current_index}")
            except Exception as e:
                log.warning(f"Could not load voice rotation state: {e}")
                self.current_index = 0
        else:
            log.info("No voice rotation state file found, starting from index 0")
    
    def save_state(self):
        """حفظ حالة التدوير في الملف"""
        try:
            with open(self.state_file, "w") as f:
                json.dump({"current_voice_index": self.current_index}, f)
            log.info(f"Voice rotation state saved: index={self.current_index}")
        except Exception as e:
            log.error(f"Failed to save voice rotation state: {e}")
    
    def get_next_voice(self) -> str:
        """الحصول على الصوت التالي والانتقال للصوت الذي يليه"""
        voice = self.voices[self.current_index]
        log.info(f"Selected voice: {voice} (index: {self.current_index}/{len(self.voices)-1})")
        
        # الانتقال للصوت التالي
        self.current_index = (self.current_index + 1) % len(self.voices)
        self.save_state()
        
        return voice
    
    def get_current_voice(self) -> str:
        """الحصول على الصوت الحالي بدون تغيير المؤشر"""
        return self.voices[self.current_index]
    
    def reset(self):
        """إعادة تعيين التدوير إلى البداية"""
        self.current_index = 0
        self.save_state()
        log.info("Voice rotation reset to index 0")

# ---------------- Motion Effects Functions ----------------
def apply_zoom_in_effect(clip, duration):
    """تأثير التكبير التدريجي - من 100% إلى 120%"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        zoom_factor = 1.0 + (progress * 0.2)
        
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        left = (new_w - w) // 2
        top = (new_h - h) // 2
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_zoom_out_effect(clip, duration):
    """تأثير التصغير التدريجي - من 120% إلى 100%"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        zoom_factor = 1.2 - (progress * 0.2)
        
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        left = (new_w - w) // 2
        top = (new_h - h) // 2
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_pan_right_effect(clip, duration):
    """تأثير الانسحاب لليمين"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        
        zoom_factor = 1.2
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        max_offset = (new_w - w) // 2
        left = int(max_offset * (1 - progress))
        top = (new_h - h) // 2
        
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_pan_left_effect(clip, duration):
    """تأثير الانسحاب لليسار"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        
        zoom_factor = 1.2
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        max_offset = (new_w - w) // 2
        left = int(max_offset * progress)
        top = (new_h - h) // 2
        
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_pan_down_effect(clip, duration):
    """تأثير الانسحاب للأسفل"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        
        zoom_factor = 1.2
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        max_offset = (new_h - h) // 2
        left = (new_w - w) // 2
        top = int(max_offset * (1 - progress))
        
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_pan_up_effect(clip, duration):
    """تأثير الانسحاب للأعلى"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        
        zoom_factor = 1.2
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        max_offset = (new_h - h) // 2
        left = (new_w - w) // 2
        top = int(max_offset * progress)
        
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def apply_ken_burns_effect(clip, duration):
    """تأثير Ken Burns - تكبير وحركة قطرية"""
    w, h = clip.size
    
    def effect(gf, t):
        frame = gf(t)
        progress = min(t / duration, 1.0)
        
        zoom_factor = 1.0 + (progress * 0.3)
        new_w = int(w * zoom_factor)
        new_h = int(h * zoom_factor)
        
        from PIL import Image as PILImage
        img = PILImage.fromarray(frame.astype('uint8'))
        img_resized = img.resize((new_w, new_h), PILImage.LANCZOS)
        
        max_offset_w = (new_w - w) // 2
        max_offset_h = (new_h - h) // 2
        
        left = int(max_offset_w * (1 - progress * 0.5))
        top = int(max_offset_h * (1 - progress * 0.5))
        
        img_cropped = img_resized.crop((left, top, left + w, top + h))
        
        return np.array(img_cropped)
    
    return clip.fl(effect)

def get_random_motion_effect():
    """اختيار تأثير حركة عشوائي"""
    effects = [
        ('zoom_in', apply_zoom_in_effect),
        ('zoom_out', apply_zoom_out_effect),
        ('pan_right', apply_pan_right_effect),
        ('pan_left', apply_pan_left_effect),
        ('pan_down', apply_pan_down_effect),
        ('pan_up', apply_pan_up_effect),
        ('ken_burns', apply_ken_burns_effect)
    ]
    
    effect_name, effect_func = random.choice(effects)
    log.info(f"Selected motion effect: {effect_name}")
    return effect_name, effect_func

# ---------------- FIXED: Text Overlay Function ----------------
def create_text_overlay(text: str, video_size: Tuple[int, int], duration: float) -> Optional[ImageClip]:
    """
    إنشاء طبقة نص ثابتة شفافة فوق الفيديو.
    النص يبقى في نفس الموضع بالنسبة للشاشة، بينما الصورة تتحرك خلفه.
    
    Args:
        text: النص المراد عرضه
        video_size: حجم الفيديو (width, height)
        duration: المدة الكلية
    
    Returns:
        ImageClip شفاف مع النص أو None
    """
    # تنظيف النص من الرموز الخاصة
    clean_text = text.replace("...", "").replace("—", "-").strip()
    
    if not clean_text:
        log.warning("Empty text after cleaning, skipping text overlay")
        return None
    
    log.info(f"Creating fixed text overlay: '{clean_text[:50]}...'")
    
    try:
        width, height = video_size
        
        # إنشاء صورة شفافة بالكامل (RGBA)
        img = Image.new('RGBA', (width, height), (0, 0, 0, 0))
        draw = ImageDraw.Draw(img)
        
        # حساب حجم الخط (محسّن لأبعاد YouTube Shorts)
        fontsize = int(width * 0.07)  # زيادة حجم الخط قليلاً للشاشات العمودية
        stroke_width = max(3, int(fontsize / 18))
        
        # تحميل الخط
        font = None
        for font_name in ["Roboto-Bold.ttf", "DejaVuSans-Bold.ttf", "Arial.ttf", "LiberationSans-Bold.ttf"]:
            try:
                font = ImageFont.truetype(font_name, fontsize)
                log.info(f"✅ Font loaded: {font_name}")
                break
            except:
                pass
        
        if not font:
            font = ImageFont.load_default()
            log.warning("⚠️ Using default font")
        
        # تقسيم النص إلى أسطر
        def wrap_text(text, font, max_width):
            lines = []
            words = text.split()
            while words:
                line = ''
                while words and draw.textlength(line + words[0] + ' ', font=font) < max_width:
                    line += (words.pop(0) + ' ')
                if not line and words:
                    line = words.pop(0)
                lines.append(line.strip())
            return lines
        
        wrapped_lines = wrap_text(clean_text, font, width * 0.9)
        
        # حساب الموضع الثابت في منتصف الشاشة
        line_height = fontsize + 12
        total_height = len(wrapped_lines) * line_height
        
        # موضع ثابت في منتصف الشاشة
        start_y = (height - total_height) // 2
        
        # رسم كل سطر في موضع ثابت
        current_y = start_y
        
        for line in wrapped_lines:
            # حساب عرض السطر
            bbox = draw.textbbox((0, 0), line, font=font)
            line_width = bbox[2] - bbox[0]
            line_x = (width - line_width) // 2
            
            # رسم النص مع الحدود (موضع ثابت)
            draw.text(
                (line_x, current_y),
                line,
                font=font,
                fill=(255, 255, 255, 255),  # أبيض بالكامل
                stroke_width=stroke_width,
                stroke_fill=(0, 0, 0, 255)  # حدود سوداء
            )
            
            current_y += line_height
        
        # تحويل إلى numpy array مع الحفاظ على الشفافية
        img_array = np.array(img)
        
        # إنشاء ImageClip من الصورة الشفافة
        text_clip = ImageClip(img_array, duration=duration, ismask=False, transparent=True)
        
        log.info(f"✅ Fixed text overlay created successfully (transparent layer)")
        return text_clip
        
    except Exception as e:
        log.error(f"❌ Failed to create text overlay: {e}")
        import traceback
        traceback.print_exc()
        return None

# ---------------- Video Agent Class ----------------
class VideoAgent:
    def __init__(self):
        self.log = logging.getLogger("video_agent")
        self.history = []
        self._setup_history_dir()
        
        # تهيئة مدير تدوير الأصوات
        self.voice_manager = VoiceRotationManager(VOICE_STATE_FILE, KOKORO_VOICES)
        
        # تهيئة نموذج Kokoro TTS
        self.tts = None
        self.kokoro_available = False
        
        self.log.info("Initializing Kokoro TTS (ONNX - NeuML model)...")
        
        try:
            self.tts = KokoroEngine()
            self.kokoro_available = True
            self.log.info("✅ Kokoro-ONNX initialized successfully (NeuML model)")
        except Exception as e:
            self.tts = None
            self.kokoro_available = False
            self.log.error("❌ Kokoro TTS initialization failed")
            self.log.error(str(e))
            import traceback
            traceback.print_exc()
        
    def _setup_history_dir(self):
        """إنشاء مجلد السجل إذا لم يكن موجوداً"""
        if not os.path.exists(VIDEO_HISTORY_DIR):
            os.makedirs(VIDEO_HISTORY_DIR)
            self.log.info(f"Created video history directory: {VIDEO_HISTORY_DIR}")
    
    def generate_audio(self, text: str, output_path: str, voice: Optional[str] = None, speed: float = 0.8) -> Tuple[bool, str]:
        """توليد ملف صوتي من النص باستخدام Kokoro TTS."""
        if not self.kokoro_available or self.tts is None:
            self.log.error("Kokoro TTS not available.")
            return False, ""
        
        try:
            if voice is None:
                voice = self.voice_manager.get_next_voice()
            else:
                if voice not in KOKORO_VOICES:
                    self.log.warning(f"Voice '{voice}' not found, using next voice from rotation")
                    voice = self.voice_manager.get_next_voice()
            
            self.log.info(f"Generating audio with voice: {voice}, speed: {speed}")
            self.log.info(f"Text: {text[:50]}...")
            
            # تعيين الصوت
            self.tts.set_voice(voice)
            
            # توليد الصوت
            audio_data = self.tts.synthesize(text, speed=speed)
            
            # حفظ الملف
            import scipy.io.wavfile as wavfile
            sample_rate = 24000
            
            # التأكد من أن البيانات في النطاق الصحيح لـ int16
            audio_int16 = np.clip(audio_data * 32767, -32768, 32767).astype(np.int16)
            wavfile.write(output_path, sample_rate, audio_int16)
            
            self.log.info(f"✅ Audio generated successfully: {output_path}")
            return True, voice
            
        except Exception as e:
            self.log.error(f"Audio generation failed: {e}")
            import traceback
            traceback.print_exc()
            return False, ""
    
    def create_video(self, image_bytes: bytes, audio_path: str, output_path: str, display_text: str) -> bool:
        """
        إنشاء فيديو من صورة وملف صوتي مع نص ثابت على الشاشة.
        الفيديو الناتج بأبعاد YouTube Shorts (1080x1920).
        
        Args:
            image_bytes: بيانات الصورة
            audio_path: مسار الملف الصوتي
            output_path: مسار حفظ الفيديو
            display_text: النص المراد عرضه (ثابت على الشاشة)
        """
        if not MOVIEPY_AVAILABLE:
            self.log.error("MoviePy not available.")
            return False
        
        audio = None
        video = None
        base_clip = None
        animated_clip = None
        text_clip = None
        
        try:
            image = Image.open(io.BytesIO(image_bytes))
            
            # ✅ تحضير الصورة لأبعاد YouTube Shorts مع خلفية ملونة
            self.log.info(f"Preparing image for YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})...")
            shorts_image = prepare_image_for_shorts(image)
            img_array = np.array(shorts_image)
            
            if not os.path.exists(audio_path):
                raise FileNotFoundError(f"Audio file not found: {audio_path}")
            
            self.log.info(f"Loading audio from: {audio_path}")
            audio = AudioFileClip(audio_path)
            audio_duration = audio.duration
            
            self.log.info(f"Creating YouTube Shorts video ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) with duration: {audio_duration:.2f}s")
            
            # اختيار التأثير
            effect_name, effect_func = get_random_motion_effect()
            self.log.info(f"Applying effect: {effect_name}")
            
            # إنشاء clip أساسي
            base_clip = ImageClip(img_array, duration=audio_duration)
            
            # تطبيق التأثير على الصورة
            animated_clip = effect_func(base_clip, audio_duration)
            
            # إنشاء طبقة نص ثابتة شفافة
            self.log.info(f"Creating fixed text overlay for: '{display_text[:50]}...'")
            text_clip = create_text_overlay(display_text, animated_clip.size, audio_duration)
            
            if text_clip:
                # دمج الصورة المتحركة مع النص الثابت
                self.log.info("Compositing video: moving image + fixed text overlay...")
                video = CompositeVideoClip([animated_clip, text_clip])
                self.log.info("✅ Text overlay composited successfully (fixed position)")
            else:
                self.log.warning("⚠️ Text overlay creation failed, using video without text")
                video = animated_clip
            
            # إضافة الصوت
            video = video.set_audio(audio)
            
            # كتابة الفيديو
            self.log.info(f"Writing YouTube Shorts video to: {output_path}")
            video.write_videofile(
                output_path,
                fps=24,
                codec='libx264',
                audio_codec='aac',
                temp_audiofile='temp-audio.m4a',
                remove_temp=True,
                verbose=False,
                logger=None,
                preset='ultrafast',
                threads=4
            )
            
            self.log.info(f"✅ YouTube Shorts video created successfully ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) with {effect_name} effect + fixed text: {output_path}")
            return True
            
        except Exception as e:
            self.log.error(f"Video creation failed: {e}")
            import traceback
            traceback.print_exc()
            return False
            
        finally:
            # تنظيف الموارد
            try:
                if text_clip is not None:
                    text_clip.close()
                    self.log.debug("Text clip closed")
            except Exception as e:
                self.log.warning(f"Error closing text clip: {e}")
                
            try:
                if animated_clip is not None:
                    animated_clip.close()
                    self.log.debug("Animated clip closed")
            except Exception as e:
                self.log.warning(f"Error closing animated clip: {e}")
                
            try:
                if base_clip is not None:
                    base_clip.close()
                    self.log.debug("Base clip closed")
            except Exception as e:
                self.log.warning(f"Error closing base clip: {e}")
                
            try:
                if audio is not None:
                    audio.close()
                    self.log.debug("Audio closed")
            except Exception as e:
                self.log.warning(f"Error closing audio: {e}")
                
            try:
                if video is not None:
                    video.close()
                    self.log.debug("Video closed")
            except Exception as e:
                self.log.warning(f"Error closing video: {e}")
    
    def process_request_custom(
        self,
        image_base64: str,
        tts_text: str,
        voice: Optional[str] = None,
        speed: float = 0.8
    ) -> Dict[str, Any]:
        """
        معالجة طلب إنشاء فيديو مع نص صوتي مخصص.
        
        Args:
            image_base64: الصورة (بدون نص مكتوب عليها)
            tts_text: النص الصوتي المخصص من حقل tts_kokoro
            voice: الصوت (اختياري)
            speed: السرعة
        """
        if not self.kokoro_available or self.tts is None:
            raise RuntimeError("Kokoro TTS not available")
        
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        safe_text = tts_text[:30].replace(" ", "_").replace("/", "_").strip()
        
        temp_dir = tempfile.gettempdir()
        audio_path = os.path.join(temp_dir, f"audio_{timestamp}.wav")
        video_filename = f"{timestamp}_{safe_text}.mp4"
        video_path = os.path.join(VIDEO_HISTORY_DIR, video_filename)
        
        try:
            image_bytes = base64.b64decode(image_base64)
            
            # توليد الصوت
            success, voice_used = self.generate_audio(tts_text, audio_path, voice, speed)
            if not success:
                raise RuntimeError("Failed to generate audio.")
            
            # إنشاء الفيديو مع النص الثابت
            if not self.create_video(image_bytes, audio_path, video_path, tts_text):
                raise RuntimeError("Failed to create video.")
            
            with open(video_path, "rb") as f:
                video_bytes = f.read()
            video_base64 = base64.b64encode(video_bytes).decode('utf-8')
            
            entry = {
                "timestamp": timestamp,
                "tts_text": tts_text,
                "voice": voice_used,
                "video_path": video_path,
                "duration": self._get_video_duration(video_path)
            }
            
            self.history.insert(0, entry)
            self.history = self.history[:MAX_HISTORY_COUNT]
            
            if os.path.exists(audio_path):
                os.remove(audio_path)
            
            self.log.info(f"✅ Video processing completed: {video_filename}")
            
            return {
                "video_base64": video_base64,
                "video_path": video_path,
                "tts_text": tts_text,
                "voice_used": voice_used,
                "status": "success",
                "message": f"YouTube Shorts video ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) generated successfully with voice: {voice_used} at speed: {speed}"
            }
            
        except Exception as e:
            self.log.error(f"Video processing failed: {e}")
            if os.path.exists(audio_path):
                os.remove(audio_path)
            raise RuntimeError(f"Video generation failed: {str(e)}")
    
    def _get_video_duration(self, video_path: str) -> float:
        """الحصول على مدة الفيديو بالثواني"""
        try:
            if MOVIEPY_AVAILABLE:
                from moviepy.editor import VideoFileClip
                clip = VideoFileClip(video_path)
                duration = clip.duration
                clip.close()
                return duration
        except Exception as e:
            self.log.warning(f"Could not get video duration: {e}")
        return 0.0
    
    def get_history(self) -> List[Dict[str, Any]]:
        """الحصول على سجل الفيديوهات"""
        return self.history
    
    def get_voice_rotation_info(self) -> Dict[str, Any]:
        """الحصول على معلومات حالة تدوير الأصوات"""
        return {
            "current_voice": self.voice_manager.get_current_voice(),
            "current_index": self.voice_manager.current_index,
            "total_voices": len(KOKORO_VOICES),
            "all_voices": KOKORO_VOICES
        }

# ---------------- Global Agent Instance ----------------
agent = VideoAgent()

if IS_SERVICE_READY:
    if agent.tts is not None:
        current_voice = agent.voice_manager.get_current_voice()
        STATUS_MESSAGE = f"✅ Video Agent ready | Format: YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) | Voice: {current_voice} | Speed: 0.8"
    else:
        STATUS_MESSAGE = "⚠️ Video Agent ready but Kokoro-ONNX failed to initialize."
else:
    STATUS_MESSAGE = "❌ Service not ready"

# ---------------- Gradio Functions ----------------

def gradio_generate_video_custom(
    image_input,
    tts_text: str,
    voice_override: str,
    speed: float
) -> Tuple[Optional[str], str, str]:
    """دالة Gradio للواجهة التفاعلية مع نص مخصص."""
    if not IS_SERVICE_READY:
        return None, f"❌ Service not ready: {STATUS_MESSAGE}", ""
    
    if not image_input:
        return None, "❌ Please provide an image.", ""
    
    if not tts_text:
        return None, "❌ Please provide TTS text.", ""
    
    try:
        if isinstance(image_input, str):
            with open(image_input, "rb") as f:
                image_bytes = f.read()
        else:
            buffered = io.BytesIO()
            image_input.save(buffered, format="JPEG")
            image_bytes = buffered.getvalue()
        
        image_base64 = base64.b64encode(image_bytes).decode('utf-8')
        voice = None if voice_override == "Auto" or not voice_override else voice_override
        
        result = agent.process_request_custom(
            image_base64=image_base64,
            tts_text=tts_text,
            voice=voice,
            speed=speed
        )
        
        video_path = result["video_path"]
        voice_used = result["voice_used"]
        
        voice_info = agent.get_voice_rotation_info()
        next_voice = voice_info["current_voice"]
        current_index = voice_info["current_index"]
        total_voices = voice_info["total_voices"]
        
        status_msg = (
            f"✅ {result['message']}\n\n"
            f"**Format:** YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})\n"
            f"**TTS Text:** {result['tts_text'][:100]}...\n"
            f"**Duration:** {result.get('duration', 0):.2f}s\n"
            f"**Effect:** Moving image + fixed text overlay"
        )
        
        voice_rotation_msg = (
            f"**Voice Used:** {voice_used}\n"
            f"**Next Voice:** {next_voice}\n"
            f"**Progress:** {current_index}/{total_voices}"
        )
        
        return video_path, status_msg, voice_rotation_msg
        
    except Exception as e:
        log.error(f"Video generation failed: {e}")
        import traceback
        traceback.print_exc()
        return None, f"❌ Error: {str(e)}", ""

def gradio_api_endpoint_custom(
    image_base64: str,
    tts_text: str,
    voice: Optional[str] = None,
    speed: float = 0.8
) -> Dict[str, Any]:
    """نقطة النهاية للـ API مع نص مخصص (مع معاملات اختيارية)"""
    if not IS_SERVICE_READY:
        raise RuntimeError(f"Service not ready: {STATUS_MESSAGE}")
    
    log.info(f"API request received for TTS text: {tts_text[:50]}... with speed: {speed}")
    
    return agent.process_request_custom(
        image_base64=image_base64,
        tts_text=tts_text,
        voice=voice,
        speed=speed
    )

def gradio_api_simple(
    image_base64: str,
    tts_text: str
) -> Dict[str, Any]:
    """
    نقطة نهاية API مبسطة للاتصال من وكيل النشر.
    تستقبل فقط الصورة والنص الصوتي مع استخدام القيم الافتراضية للصوت والسرعة.
    """
    if not IS_SERVICE_READY:
        raise RuntimeError(f"Service not ready: {STATUS_MESSAGE}")
    
    log.info(f"🎬 Simple API request received for TTS text: {tts_text[:50]}...")
    
    return agent.process_request_custom(
        image_base64=image_base64,
        tts_text=tts_text,
        voice=None,  # استخدام التدوير التلقائي
        speed=0.8    # السرعة الافتراضية
    )

def format_history_for_gallery() -> List[Tuple[str, str]]:
    """تنسيق السجل لعرضه في Gallery"""
    formatted = []
    for entry in agent.get_history():
        if entry.get("video_path") and os.path.exists(entry["video_path"]):
            caption = (
                f'{entry["tts_text"][:80]}...\n'
                f'Voice: {entry.get("voice", "N/A")} | Duration: {entry.get("duration", 0):.1f}s'
            )
            formatted.append((entry["video_path"], caption))
    return formatted

def gradio_refresh_history():
    return format_history_for_gallery()

def gradio_reset_voice_rotation():
    agent.voice_manager.reset()
    voice_info = agent.get_voice_rotation_info()
    return f"✅ Voice rotation reset! Next voice: {voice_info['current_voice']}"

def gradio_get_voice_info():
    voice_info = agent.get_voice_rotation_info()
    return (
        f"**Current Voice:** {voice_info['current_voice']}\n"
        f"**Index:** {voice_info['current_index']}/{voice_info['total_voices']}\n"
        f"**Total Voices:** {len(voice_info['all_voices'])}"
    )

# ---------------- Gradio Interface ----------------

with gr.Blocks(title="Video Agent - YouTube Shorts") as demo:
    gr.Markdown("# 🎬 Video Agent - YouTube Shorts Format (1080x1920)")
    gr.Markdown(f"**Status:** {STATUS_MESSAGE}")
    
    if IS_SERVICE_READY:
        gr.Markdown(f"**Features:** YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) | {len(KOKORO_VOICES)} voices | Auto rotation | Smart background color | Fixed text overlay")
        gr.Markdown("✅ **NEW:** *Videos optimized for YouTube Shorts with intelligent background filling*")
    
    gr.Markdown("---")
    
    with gr.Tab("Generate Video"):
        with gr.Row():
            with gr.Column(scale=1):
                image_input = gr.Image(label="Upload Image (any size - will be adapted)", type="pil")
                tts_text_input = gr.Textbox(
                    label="TTS Text (will appear as fixed overlay)",
                    lines=4,
                    placeholder="Enter the text to display on screen..."
                )
                
                voice_dropdown = gr.Dropdown(
                    choices=["Auto"] + KOKORO_VOICES,
                    value="Auto",
                    label="Voice (Auto = rotation)"
                )
                
                speed_slider = gr.Slider(
                    minimum=0.5,
                    maximum=2.0,
                    value=0.8,
                    step=0.1,
                    label="Speech Speed"
                )
                
                generate_btn = gr.Button("🎬 Generate YouTube Shorts Video", variant="primary")
                status_output = gr.Textbox(label="Status", lines=5)
                voice_rotation_output = gr.Textbox(label="Voice Info", lines=3)
            
            with gr.Column(scale=1):
                video_output = gr.Video(label="Generated YouTube Shorts Video (1080x1920)")
        
        generate_btn.click(
            fn=gradio_generate_video_custom,
            inputs=[image_input, tts_text_input, voice_dropdown, speed_slider],
            outputs=[video_output, status_output, voice_rotation_output]
        )
    
    with gr.Tab("History"):
        refresh_btn = gr.Button("🔄 Refresh")
        history_gallery = gr.Gallery(label="Recent Videos", columns=2)
        refresh_btn.click(fn=gradio_refresh_history, outputs=[history_gallery])
    
    with gr.Tab("Voice Management"):
        with gr.Row():
            voice_info_btn = gr.Button("📊 Get Info")
            reset_btn = gr.Button("🔄 Reset Rotation")
        
        voice_mgmt_output = gr.Textbox(label="Voice Info", lines=5)
        
        voice_info_btn.click(fn=gradio_get_voice_info, outputs=[voice_mgmt_output])
        reset_btn.click(fn=gradio_reset_voice_rotation, outputs=[voice_mgmt_output])
        
        gr.Markdown(f"### {len(KOKORO_VOICES)} Voices Available")
        gr.Markdown("**British Female (4):** " + ", ".join([v for v in KOKORO_VOICES if v.startswith("bf_")]))
        gr.Markdown("**American Female (11):** " + ", ".join([v for v in KOKORO_VOICES if v.startswith("af_")]))
        gr.Markdown("**British Male (4):** " + ", ".join([v for v in KOKORO_VOICES if v.startswith("bm_")]))
        gr.Markdown("**American Male (8):** " + ", ".join([v for v in KOKORO_VOICES if v.startswith("am_")]))
    
    with gr.Tab("API Endpoints") as api_tab:
        gr.Markdown("### 🔌 API Endpoints for External Integration")
        gr.Markdown("Use these endpoints to integrate with other agents (e.g., Publisher Agent)")
        
        gr.Markdown("---")
        gr.Markdown("#### 📡 Endpoint 1: Simple API (Recommended for Publisher Agent)")
        gr.Markdown("- **API Name**: `generate_video_custom`")
        gr.Markdown("- **Parameters**: `image_base64` (str), `tts_text` (str)")
        gr.Markdown("- **Returns**: JSON with video_base64, video_path, status")
        gr.Markdown(f"- **Format**: YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})")
        gr.Markdown("- **Auto-settings**: Voice rotation enabled, Speed = 0.8, Smart background filling")
        
        gr.Markdown("---")
        gr.Markdown("#### 📡 Endpoint 2: Full API (Advanced)")
        gr.Markdown("- **API Name**: `generate_video_full`")
        gr.Markdown("- **Parameters**: `image_base64` (str), `tts_text` (str), `voice` (str, optional), `speed` (float, optional)")
        gr.Markdown("- **Returns**: JSON with video_base64, video_path, status")
        gr.Markdown(f"- **Format**: YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})")

# ✅ CRITICAL FIX: Define API endpoints as separate gr.Interface outside Blocks
# This is the CORRECT way to expose API endpoints in Gradio

# API Endpoint 1: Simple API (for Publisher Agent)
simple_api = gr.Interface(
    fn=gradio_api_simple,
    inputs=[
        gr.Textbox(label="image_base64", placeholder="Base64 encoded image (any size - will be adapted to 1080x1920)"),
        gr.Textbox(label="tts_text", placeholder="Text for TTS audio synthesis")
    ],
    outputs=gr.JSON(label="API Response"),
    title="Simple YouTube Shorts Video API",
    description=f"Generate YouTube Shorts video ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) with automatic voice rotation and speed=0.8",
    api_name="generate_video_custom"
)

# API Endpoint 2: Full API (with all options)
def gradio_api_full_wrapper(image_base64: str, tts_text: str, voice: str = "Auto", speed: float = 0.8):
    """Wrapper to handle optional parameters"""
    voice_to_use = None if voice == "Auto" or voice == "" else voice
    return gradio_api_endpoint_custom(image_base64, tts_text, voice_to_use, speed)

full_api = gr.Interface(
    fn=gradio_api_full_wrapper,
    inputs=[
        gr.Textbox(label="image_base64", placeholder="Base64 encoded image (any size)"),
        gr.Textbox(label="tts_text", placeholder="Text for TTS"),
        gr.Dropdown(choices=["Auto"] + KOKORO_VOICES, value="Auto", label="voice"),
        gr.Slider(minimum=0.5, maximum=2.0, value=0.8, step=0.1, label="speed")
    ],
    outputs=gr.JSON(label="API Response"),
    title="Full YouTube Shorts Video API",
    description=f"Generate YouTube Shorts video ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT}) with custom voice and speed settings",
    api_name="generate_video_full"
)

# Mount both APIs into the main demo using TabbedInterface
combined_demo = gr.TabbedInterface(
    [demo, simple_api, full_api],
    ["Main Interface", "API: Simple", "API: Full"],
    title=f"🎬 Video Agent - YouTube Shorts ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})"
)

if __name__ == "__main__":
    PORT = int(os.getenv("PORT", "7860"))
    log.info(f"Starting Video Agent with YouTube Shorts format ({YOUTUBE_SHORTS_WIDTH}x{YOUTUBE_SHORTS_HEIGHT})...")
    combined_demo.launch(server_name="0.0.0.0", server_port=PORT)