File size: 108,447 Bytes
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
 
 
c1fd40d
 
 
ee38251
c1fd40d
 
 
ee38251
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
c1fd40d
 
ee38251
 
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
 
ee38251
c1fd40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee38251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1fd40d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
import gradio as gr
import os
import json
import requests
from datetime import datetime
import time
from typing import List, Dict, Any, Generator, Tuple, Optional, Set
import logging
import re
import tempfile
from pathlib import Path
import sqlite3
import hashlib
import threading
from contextlib import contextmanager
from dataclasses import dataclass, field, asdict
from collections import defaultdict
import random
from huggingface_hub import HfApi, upload_file, hf_hub_download

# --- Logging setup ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# --- Document export imports ---
try:
   from docx import Document
   from docx.shared import Inches, Pt, RGBColor, Mm
   from docx.enum.text import WD_ALIGN_PARAGRAPH
   from docx.enum.style import WD_STYLE_TYPE
   from docx.oxml.ns import qn
   from docx.oxml import OxmlElement
   DOCX_AVAILABLE = True
except ImportError:
   DOCX_AVAILABLE = False
   logger.warning("python-docx not installed. DOCX export will be disabled.")

# --- Environment variables and constants ---
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN", "")
BRAVE_SEARCH_API_KEY = os.getenv("BRAVE_SEARCH_API_KEY", "")
API_URL = "https://api.friendli.ai/dedicated/v1/chat/completions"
MODEL_ID = "dep86pjolcjjnv8"
DB_PATH = "screenplay_sessions_v1.db"

# Screenplay length settings
SCREENPLAY_LENGTHS = {
   "movie": {"pages": 110, "description": "Feature Film (90-120 pages)"},
   "tv_drama": {"pages": 55, "description": "TV Drama Episode (50-60 pages)"},
   "ott_series": {"pages": 45, "description": "OTT Series Episode (30-60 pages)"},
   "short_film": {"pages": 15, "description": "Short Film (10-20 pages)"}
}

# --- Environment validation ---
if not FRIENDLI_TOKEN:
   logger.error("FRIENDLI_TOKEN not set. Application will not work properly.")
   FRIENDLI_TOKEN = "dummy_token_for_testing"

if not BRAVE_SEARCH_API_KEY:
   logger.warning("BRAVE_SEARCH_API_KEY not set. Web search features will be disabled.")

# --- Global variables ---
db_lock = threading.Lock()

# Genre templates
GENRE_TEMPLATES = {
   "action": {
       "pacing": "fast",
       "scene_length": "short",
       "dialogue_ratio": 0.3,
       "key_elements": ["set pieces", "physical conflict", "urgency", "stakes escalation"],
       "structure_beats": ["explosive opening", "pursuit/chase", "confrontation", "climactic battle"]
   },
   "thriller": {
       "pacing": "fast",
       "scene_length": "short",
       "dialogue_ratio": 0.35,
       "key_elements": ["suspense", "twists", "paranoia", "time pressure"],
       "structure_beats": ["hook", "mystery deepens", "false victory", "revelation", "final confrontation"]
   },
   "drama": {
       "pacing": "moderate",
       "scene_length": "medium",
       "dialogue_ratio": 0.5,
       "key_elements": ["character depth", "emotional truth", "relationships", "internal conflict"],
       "structure_beats": ["status quo", "catalyst", "debate", "commitment", "complications", "crisis", "resolution"]
   },
   "comedy": {
       "pacing": "fast",
       "scene_length": "short",
       "dialogue_ratio": 0.6,
       "key_elements": ["setup/payoff", "timing", "character comedy", "escalation"],
       "structure_beats": ["funny opening", "complication", "misunderstandings multiply", "chaos peak", "resolution with callback"]
   },
   "horror": {
       "pacing": "variable",
       "scene_length": "mixed",
       "dialogue_ratio": 0.3,
       "key_elements": ["atmosphere", "dread", "jump scares", "gore/psychological"],
       "structure_beats": ["normal world", "first sign", "investigation", "first attack", "survival", "final girl/boy"]
   },
   "sci-fi": {
       "pacing": "moderate",
       "scene_length": "medium",
       "dialogue_ratio": 0.4,
       "key_elements": ["world building", "technology", "concepts", "visual spectacle"],
       "structure_beats": ["ordinary world", "discovery", "new world", "complications", "understanding", "choice", "new normal"]
   },
   "romance": {
       "pacing": "moderate",
       "scene_length": "medium",
       "dialogue_ratio": 0.55,
       "key_elements": ["chemistry", "obstacles", "emotional moments", "intimacy"],
       "structure_beats": ["meet cute", "attraction", "first conflict", "deepening", "crisis/breakup", "grand gesture", "together"]
   }
}

# Screenplay stages definition
SCREENPLAY_STAGES = [
   ("producer", "๐ŸŽฌ Producer: Concept Development & Market Analysis"),
   ("story_developer", "๐Ÿ“– Story Developer: Synopsis & Three-Act Structure"),
   ("character_designer", "๐Ÿ‘ฅ Character Designer: Cast & Relationships"),
   ("critic_structure", "๐Ÿ” Structure Critic: Story & Character Review"),
   ("scene_planner", "๐ŸŽฏ Scene Planner: Detailed Scene Breakdown"),
   ("screenwriter", "โœ๏ธ Screenwriter: Act 1 - Setup (25%)"),
   ("script_doctor", "๐Ÿ”ง Script Doctor: Act 1 Review & Polish"),
   ("screenwriter", "โœ๏ธ Screenwriter: Act 2A - Rising Action (25%)"),
   ("script_doctor", "๐Ÿ”ง Script Doctor: Act 2A Review & Polish"),
   ("screenwriter", "โœ๏ธ Screenwriter: Act 2B - Complications (25%)"),
   ("script_doctor", "๐Ÿ”ง Script Doctor: Act 2B Review & Polish"),
   ("screenwriter", "โœ๏ธ Screenwriter: Act 3 - Resolution (25%)"),
   ("final_reviewer", "๐ŸŽญ Final Review: Complete Screenplay Analysis"),
]

# Save the Cat Beat Sheet
SAVE_THE_CAT_BEATS = {
   1: "Opening Image (0-1%)",
   2: "Setup (1-10%)",
   3: "Theme Stated (5%)",
   4: "Catalyst (10%)",
   5: "Debate (10-20%)",
   6: "Break into Two (20%)",
   7: "B Story (22%)",
   8: "Fun and Games (20-50%)",
   9: "Midpoint (50%)",
   10: "Bad Guys Close In (50-75%)",
   11: "All Is Lost (75%)",
   12: "Dark Night of the Soul (75-80%)",
   13: "Break into Three (80%)",
   14: "Finale (80-99%)",
   15: "Final Image (99-100%)"
}

# --- Data classes ---
@dataclass
class ScreenplayBible:
   """Screenplay bible for maintaining consistency"""
   title: str = ""
   logline: str = ""
   genre: str = ""
   subgenre: str = ""
   tone: str = ""
   themes: List[str] = field(default_factory=list)
   
   # Characters
   protagonist: Dict[str, Any] = field(default_factory=dict)
   antagonist: Dict[str, Any] = field(default_factory=dict)
   supporting_cast: Dict[str, Dict[str, Any]] = field(default_factory=dict)
   
   # Structure
   three_act_structure: Dict[str, str] = field(default_factory=dict)
   save_the_cat_beats: Dict[int, str] = field(default_factory=dict)
   
   # World
   time_period: str = ""
   primary_locations: List[Dict[str, str]] = field(default_factory=list)
   world_rules: List[str] = field(default_factory=list)
   
   # Visual style
   visual_style: str = ""
   key_imagery: List[str] = field(default_factory=list)

@dataclass
class SceneBreakdown:
   """Individual scene information"""
   scene_number: int
   act: int
   location: str
   time_of_day: str
   characters: List[str]
   purpose: str
   conflict: str
   page_count: float
   beat: str = ""
   transition: str = "CUT TO:"

@dataclass
class CharacterProfile:
   """Detailed character profile"""
   name: str
   role: str  # protagonist, antagonist, supporting, etc.
   archetype: str
   want: str  # External goal
   need: str  # Internal need
   backstory: str
   personality: List[str]
   speech_pattern: str
   character_arc: str
   relationships: Dict[str, str] = field(default_factory=dict)
   first_appearance: str = ""

# --- Core logic classes ---
class ScreenplayTracker:
    """Unified screenplay tracker"""
    def __init__(self):
        self.screenplay_bible = ScreenplayBible()
        self.scenes: List[SceneBreakdown] = []
        self.characters: Dict[str, CharacterProfile] = {}
        self.page_count = 0
        self.act_pages = {"1": 0, "2A": 0, "2B": 0, "3": 0}
        self.dialogue_action_ratio = 0.0
        
    def add_scene(self, scene: SceneBreakdown):
        """Add scene to tracker"""
        self.scenes.append(scene)
        self.page_count += scene.page_count
        
    def add_character(self, character: CharacterProfile):
        """Add character to tracker"""
        self.characters[character.name] = character
        # Update bible with main characters
        if character.role == "protagonist":
            self.screenplay_bible.protagonist = asdict(character)
        elif character.role == "antagonist":
            self.screenplay_bible.antagonist = asdict(character)
        elif character.role == "supporting":
            self.screenplay_bible.supporting_cast[character.name] = asdict(character)
        
    def update_bible(self, key: str, value: Any):
        """Update screenplay bible"""
        if hasattr(self.screenplay_bible, key):
            setattr(self.screenplay_bible, key, value)
            
    def get_act_page_target(self, act: str, total_pages: int) -> int:
        """Get target pages for each act"""
        if act == "1":
            return int(total_pages * 0.25)
        elif act in ["2A", "2B"]:
            return int(total_pages * 0.25)
        elif act == "3":
            return int(total_pages * 0.25)
        return 0

class ScreenplayDatabase:
    """Database management for screenplay sessions"""
    @staticmethod
    def init_db():
        with sqlite3.connect(DB_PATH) as conn:
            conn.execute("PRAGMA journal_mode=WAL")
            cursor = conn.cursor()
            
            # Main screenplay sessions table
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS screenplay_sessions (
                    session_id TEXT PRIMARY KEY,
                    user_query TEXT NOT NULL,
                    screenplay_type TEXT NOT NULL,
                    genre TEXT NOT NULL,
                    subgenre TEXT,
                    target_pages INTEGER,
                    language TEXT NOT NULL,
                    title TEXT,
                    logline TEXT,
                    synopsis TEXT,
                    three_act_structure TEXT,
                    character_profiles TEXT,
                    scene_breakdown TEXT,
                    screenplay_bible TEXT,
                    final_screenplay TEXT,
                    pdf_path TEXT,
                    created_at TEXT DEFAULT (datetime('now')),
                    updated_at TEXT DEFAULT (datetime('now')),
                    status TEXT DEFAULT 'active',
                    current_stage INTEGER DEFAULT 0,
                    total_pages REAL DEFAULT 0
                )
            ''')
            
            # Stages table
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS screenplay_stages (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    session_id TEXT NOT NULL,
                    stage_number INTEGER NOT NULL,
                    stage_name TEXT NOT NULL,
                    role TEXT NOT NULL,
                    content TEXT,
                    page_count REAL DEFAULT 0,
                    status TEXT DEFAULT 'pending',
                    created_at TEXT DEFAULT (datetime('now')),
                    updated_at TEXT DEFAULT (datetime('now')),
                    FOREIGN KEY (session_id) REFERENCES screenplay_sessions(session_id),
                    UNIQUE(session_id, stage_number)
                )
            ''')
            
            # Scenes table
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS scenes (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    session_id TEXT NOT NULL,
                    act_number INTEGER NOT NULL,
                    scene_number INTEGER NOT NULL,
                    location TEXT NOT NULL,
                    time_of_day TEXT NOT NULL,
                    characters TEXT,
                    purpose TEXT,
                    content TEXT,
                    page_count REAL,
                    created_at TEXT DEFAULT (datetime('now')),
                    FOREIGN KEY (session_id) REFERENCES screenplay_sessions(session_id)
                )
            ''')
            
            # Characters table
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS characters (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    session_id TEXT NOT NULL,
                    character_name TEXT NOT NULL,
                    character_data TEXT,
                    created_at TEXT DEFAULT (datetime('now')),
                    FOREIGN KEY (session_id) REFERENCES screenplay_sessions(session_id),
                    UNIQUE(session_id, character_name)
                )
            ''')
            
            # Screenplay themes library
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS screenplay_themes_library (
                    theme_id TEXT PRIMARY KEY,
                    theme_text TEXT NOT NULL,
                    screenplay_type TEXT NOT NULL,
                    genre TEXT NOT NULL,
                    language TEXT NOT NULL,
                    title TEXT,
                    logline TEXT,
                    protagonist_desc TEXT,
                    conflict_desc TEXT,
                    generated_at TEXT DEFAULT (datetime('now')),
                    view_count INTEGER DEFAULT 0,
                    used_count INTEGER DEFAULT 0,
                    tags TEXT,
                    metadata TEXT
                )
            ''')
            
            conn.commit()

    @staticmethod
    @contextmanager
    def get_db():
        with db_lock:
            conn = sqlite3.connect(DB_PATH, timeout=30.0)
            conn.row_factory = sqlite3.Row
            try:
                yield conn
            finally:
                conn.close()

    @staticmethod
    def create_session(user_query: str, screenplay_type: str, genre: str, language: str) -> str:
        session_id = hashlib.md5(f"{user_query}{screenplay_type}{datetime.now()}".encode()).hexdigest()
        target_pages = SCREENPLAY_LENGTHS[screenplay_type]["pages"]
        
        with ScreenplayDatabase.get_db() as conn:
            conn.cursor().execute(
                '''INSERT INTO screenplay_sessions 
                   (session_id, user_query, screenplay_type, genre, target_pages, language) 
                   VALUES (?, ?, ?, ?, ?, ?)''',
                (session_id, user_query, screenplay_type, genre, target_pages, language)
            )
            conn.commit()
        return session_id

    @staticmethod
    def save_stage(session_id: str, stage_number: int, stage_name: str,
                   role: str, content: str, status: str = 'complete'):
        page_count = 0
        if role == "screenwriter" and content:
            # Estimate pages based on screenplay format (rough estimate)
            page_count = len(content.split('\n')) / 55  # ~55 lines per page
        
        with ScreenplayDatabase.get_db() as conn:
            cursor = conn.cursor()
            cursor.execute('''
                INSERT INTO screenplay_stages 
                (session_id, stage_number, stage_name, role, content, page_count, status)
                VALUES (?, ?, ?, ?, ?, ?, ?)
                ON CONFLICT(session_id, stage_number)
                DO UPDATE SET content=?, page_count=?, status=?, updated_at=datetime('now')
            ''', (session_id, stage_number, stage_name, role, content, page_count, status,
                  content, page_count, status))
            
            # Update session info
            cursor.execute('''
                UPDATE screenplay_sessions 
                SET current_stage = ?, updated_at = datetime('now')
                WHERE session_id = ?
            ''', (stage_number, session_id))
            
            conn.commit()

    @staticmethod
    def save_screenplay_bible(session_id: str, bible: ScreenplayBible):
        """Save screenplay bible"""
        with ScreenplayDatabase.get_db() as conn:
            bible_json = json.dumps(asdict(bible))
            conn.cursor().execute(
                'UPDATE screenplay_sessions SET screenplay_bible = ? WHERE session_id = ?',
                (bible_json, session_id)
            )
            conn.commit()

    @staticmethod
    def save_character(session_id: str, character: CharacterProfile):
        """Save character profile"""
        with ScreenplayDatabase.get_db() as conn:
            char_json = json.dumps(asdict(character))
            conn.cursor().execute(
                '''INSERT INTO characters (session_id, character_name, character_data)
                   VALUES (?, ?, ?)
                   ON CONFLICT(session_id, character_name)
                   DO UPDATE SET character_data = ?''',
                (session_id, character.name, char_json, char_json)
            )
            conn.commit()

    @staticmethod
    def save_scene(session_id: str, scene: SceneBreakdown):
        """Save scene breakdown"""
        with ScreenplayDatabase.get_db() as conn:
            conn.cursor().execute(
                '''INSERT INTO scenes 
                   (session_id, act_number, scene_number, location, time_of_day, 
                    characters, purpose, page_count)
                   VALUES (?, ?, ?, ?, ?, ?, ?, ?)''',
                (session_id, scene.act, scene.scene_number, scene.location,
                 scene.time_of_day, json.dumps(scene.characters), scene.purpose,
                 scene.page_count)
            )
            conn.commit()

    @staticmethod
    def get_screenplay_content(session_id: str) -> str:
        """Get complete screenplay content"""
        with ScreenplayDatabase.get_db() as conn:
            rows = conn.cursor().execute('''
                SELECT content FROM screenplay_stages 
                WHERE session_id = ? AND role = 'screenwriter' 
                ORDER BY stage_number
            ''', (session_id,)).fetchall()
            
            if rows:
                return '\n\n'.join(row['content'] for row in rows if row['content'])
        return ""

    @staticmethod
    def update_final_screenplay(session_id: str, final_screenplay: str, title: str, logline: str):
        """Update final screenplay"""
        with ScreenplayDatabase.get_db() as conn:
            total_pages = len(final_screenplay.split('\n')) / 55
            conn.cursor().execute(
                '''UPDATE screenplay_sessions 
                   SET final_screenplay = ?, title = ?, logline = ?, 
                       total_pages = ?, status = 'complete', updated_at = datetime('now')
                   WHERE session_id = ?''',
                (final_screenplay, title, logline, total_pages, session_id)
            )
            conn.commit()

    @staticmethod
    def get_session(session_id: str) -> Optional[Dict]:
        with ScreenplayDatabase.get_db() as conn:
            row = conn.cursor().execute(
                'SELECT * FROM screenplay_sessions WHERE session_id = ?', 
                (session_id,)
            ).fetchone()
            return dict(row) if row else None

    @staticmethod
    def get_stages(session_id: str) -> List[Dict]:
        """Get all stages for a session"""
        with ScreenplayDatabase.get_db() as conn:
            rows = conn.cursor().execute(
                '''SELECT * FROM screenplay_stages 
                   WHERE session_id = ? 
                   ORDER BY stage_number''', 
                (session_id,)
            ).fetchall()
            return [dict(row) for row in rows]

    @staticmethod
    def get_active_sessions() -> List[Dict]:
        with ScreenplayDatabase.get_db() as conn:
            rows = conn.cursor().execute(
                '''SELECT session_id, title, user_query, screenplay_type, genre, 
                          created_at, current_stage, total_pages 
                   FROM screenplay_sessions 
                   WHERE status = 'active' 
                   ORDER BY updated_at DESC 
                   LIMIT 10'''
            ).fetchall()
            return [dict(row) for row in rows]

    @staticmethod
    def save_random_theme(theme_text: str, screenplay_type: str, genre: str, 
                         language: str, metadata: Dict[str, Any]) -> str:
        """Save randomly generated screenplay theme"""
        theme_id = hashlib.md5(f"{theme_text}{datetime.now()}".encode()).hexdigest()[:12]
        
        title = metadata.get('title', '')
        logline = metadata.get('logline', '')
        protagonist_desc = metadata.get('protagonist', '')
        conflict_desc = metadata.get('conflict', '')
        tags = json.dumps(metadata.get('tags', []))
        
        with ScreenplayDatabase.get_db() as conn:
            conn.cursor().execute('''
                INSERT INTO screenplay_themes_library 
                (theme_id, theme_text, screenplay_type, genre, language, title, logline,
                 protagonist_desc, conflict_desc, tags, metadata)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ''', (theme_id, theme_text, screenplay_type, genre, language, title, logline,
                  protagonist_desc, conflict_desc, tags, json.dumps(metadata)))
            conn.commit()
        
        return theme_id

class WebSearchIntegration:
   """Web search functionality for screenplay research"""
   def __init__(self):
       self.brave_api_key = BRAVE_SEARCH_API_KEY
       self.search_url = "https://api.search.brave.com/res/v1/web/search"
       self.enabled = bool(self.brave_api_key)

   def search(self, query: str, count: int = 3, language: str = "en") -> List[Dict]:
       if not self.enabled:
           return []
       headers = {
           "Accept": "application/json",
           "X-Subscription-Token": self.brave_api_key
       }
       params = {
           "q": query,
           "count": count,
           "search_lang": "ko" if language == "Korean" else "en",
           "text_decorations": False,
           "safesearch": "moderate"
       }
       try:
           response = requests.get(self.search_url, headers=headers, params=params, timeout=10)
           response.raise_for_status()
           results = response.json().get("web", {}).get("results", [])
           return results
       except requests.exceptions.RequestException as e:
           logger.error(f"Web search API error: {e}")
           return []

   def extract_relevant_info(self, results: List[Dict], max_chars: int = 1500) -> str:
       if not results:
           return ""
       extracted = []
       total_chars = 0
       for i, result in enumerate(results[:3], 1):
           title = result.get("title", "")
           description = result.get("description", "")
           info = f"[{i}] {title}: {description}"
           if total_chars + len(info) < max_chars:
               extracted.append(info)
               total_chars += len(info)
           else:
               break
       return "\n".join(extracted)

class ScreenplayGenerationSystem:
    """Professional screenplay generation system"""
    def __init__(self):
        self.token = FRIENDLI_TOKEN
        self.api_url = API_URL
        self.model_id = MODEL_ID
        self.screenplay_tracker = ScreenplayTracker()
        self.web_search = WebSearchIntegration()
        self.current_session_id = None
        ScreenplayDatabase.init_db()

    def create_headers(self):
        """Create headers for API request with proper token"""
        if not self.token or self.token == "dummy_token_for_testing":
            raise ValueError("Valid FRIENDLI_TOKEN is required")
        
        return {
            "Authorization": f"Bearer {self.token}", 
            "Content-Type": "application/json"
        }

    # --- Prompt generation functions ---
    def create_producer_prompt(self, user_query: str, screenplay_type: str, 
                              genre: str, language: str) -> str:
        """Producer initial concept development"""
        
        # Web search for market trends if enabled
        search_results = ""
        if self.web_search.enabled:
            queries = [
                f"box office success {genre} films 2024 2025",
                f"popular {screenplay_type} {genre} trends",
                f"audience demographics {genre} movies"
            ]
            for q in queries[:2]:
                results = self.web_search.search(q, count=2, language=language)
                if results:
                    search_results += self.web_search.extract_relevant_info(results) + "\n"
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ํ• ๋ฆฌ์šฐ๋“œ ํ”„๋กœ๋“€์„œ์ž…๋‹ˆ๋‹ค. ์ƒ์—…์ ์œผ๋กœ ์„ฑ๊ณต ๊ฐ€๋Šฅํ•œ {screenplay_type} ์ปจ์…‰์„ ๊ฐœ๋ฐœํ•˜์„ธ์š”.

**์š”์ฒญ์‚ฌํ•ญ:** {user_query}
**ํƒ€์ž…:** {SCREENPLAY_LENGTHS[screenplay_type]['description']}
**์žฅ๋ฅด:** {genre}

**์‹œ์žฅ ์กฐ์‚ฌ:**
{search_results if search_results else "N/A"}

**ํ•„์ˆ˜ ์ œ๊ณต ํ•ญ๋ชฉ:**

1. **์ œ๋ชฉ (TITLE)**
   - ๊ธฐ์–ตํ•˜๊ธฐ ์‰ฝ๊ณ  ๋งˆ์ผ€ํŒ… ๊ฐ€๋Šฅํ•œ ์ œ๋ชฉ
   - ์žฅ๋ฅด์™€ ํ†ค์„ ์•”์‹œํ•˜๋Š” ์ œ๋ชฉ

2. **๋กœ๊ทธ๋ผ์ธ (LOGLINE)**
   - 25๋‹จ์–ด ์ด๋‚ด ํ•œ ๋ฌธ์žฅ
   - ํ˜•์‹: "[์‚ฌ๊ฑด]์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ, [์ฃผ์ธ๊ณต]์€ [๋ชฉํ‘œ]๋ฅผ ์ด๋ฃจ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด [๊ฒฐ๊ณผ]"
   - ๊ฐˆ๋“ฑ๊ณผ stakes๊ฐ€ ๋ช…ํ™•ํ•ด์•ผ ํ•จ

3. **์žฅ๋ฅด ๋ถ„์„**
   - ์ฃผ ์žฅ๋ฅด: {genre}
   - ์„œ๋ธŒ ์žฅ๋ฅด:
   - ํ†ค & ๋ถ„์œ„๊ธฐ:

4. **ํƒ€๊ฒŸ ๊ด€๊ฐ**
   - ์ฃผ์š” ์—ฐ๋ น๋Œ€:
   - ์„ฑ๋ณ„ ๋ถ„ํฌ:
   - ๊ด€์‹ฌ์‚ฌ:
   - ์œ ์‚ฌ ์ž‘ํ’ˆ ํŒฌ์ธต:

5. **๋น„๊ต ์ž‘ํ’ˆ (COMPS)**
   - 3๊ฐœ์˜ ์„ฑ๊ณตํ•œ ์œ ์‚ฌ ํ”„๋กœ์ ํŠธ
   - ๊ฐ๊ฐ์˜ ๋ฐ•์Šค์˜คํ”ผ์Šค/์‹œ์ฒญ๋ฅ  ์„ฑ๊ณผ
   - ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์™€์˜ ์ฐจ๋ณ„์ 

6. **๊ณ ์œ  ํŒ๋งค ํฌ์ธํŠธ (USP)**
   - ์ด ์ด์•ผ๊ธฐ๋งŒ์˜ ๋…ํŠนํ•œ ์ 
   - ํ˜„์žฌ ์‹œ์žฅ์—์„œ์˜ ํ•„์š”์„ฑ
   - ์ œ์ž‘ ๊ฐ€๋Šฅ์„ฑ

7. **๋น„์ฃผ์–ผ ์ปจ์…‰**
   - ํ•ต์‹ฌ ๋น„์ฃผ์–ผ ์ด๋ฏธ์ง€ 3๊ฐœ
   - ์ „์ฒด์ ์ธ ๋ฃฉ & ํ•„

๊ตฌ์ฒด์ ์ด๊ณ  ์‹œ์žฅ์„ฑ ์žˆ๋Š” ์ปจ์…‰์„ ์ œ์‹œํ•˜์„ธ์š”.""",

            "English": f"""You are a Hollywood producer. Develop a commercially viable {screenplay_type} concept.

**Request:** {user_query}
**Type:** {SCREENPLAY_LENGTHS[screenplay_type]['description']}
**Genre:** {genre}

**Market Research:**
{search_results if search_results else "N/A"}

**Required Elements:**

1. **TITLE**
   - Memorable and marketable
   - Hints at genre and tone

2. **LOGLINE**
   - One sentence, 25 words max
   - Format: "When [inciting incident], a [protagonist] must [objective] or else [stakes]"
   - Clear conflict and stakes

3. **GENRE ANALYSIS**
   - Primary Genre: {genre}
   - Sub-genre:
   - Tone & Mood:

4. **TARGET AUDIENCE**
   - Primary Age Range:
   - Gender Distribution:
   - Interests:
   - Similar Works Fanbase:

5. **COMPARABLE FILMS (COMPS)**
   - 3 successful similar projects
   - Box office/viewership performance
   - How ours differs

6. **UNIQUE SELLING POINT (USP)**
   - What makes this story unique
   - Why now in the market
   - Production feasibility

7. **VISUAL CONCEPT**
   - 3 key visual images
   - Overall look & feel

Provide specific, marketable concept."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_story_developer_prompt(self, producer_concept: str, user_query: str,
                                     screenplay_type: str, genre: str, language: str) -> str:
        """Story developer for synopsis and structure"""
        
        genre_template = GENRE_TEMPLATES.get(genre, GENRE_TEMPLATES["drama"])
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ์Šคํ† ๋ฆฌ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ํ”„๋กœ๋“€์„œ์˜ ์ปจ์…‰์„ ๋ฐ”ํƒ•์œผ๋กœ {screenplay_type}์˜ ์‹œ๋†‰์‹œ์Šค์™€ 3๋ง‰ ๊ตฌ์กฐ๋ฅผ ๊ฐœ๋ฐœํ•˜์„ธ์š”.

**ํ”„๋กœ๋“€์„œ ์ปจ์…‰:**
{producer_concept}

**์žฅ๋ฅด ํŠน์„ฑ:** {genre}
- ํŽ˜์ด์‹ฑ: {genre_template['pacing']}
- ํ•ต์‹ฌ ์š”์†Œ: {', '.join(genre_template['key_elements'])}
- ๊ตฌ์กฐ ๋น„ํŠธ: {', '.join(genre_template['structure_beats'])}

**ํ•„์ˆ˜ ์ž‘์„ฑ ํ•ญ๋ชฉ:**

1. **์‹œ๋†‰์‹œ์Šค (SYNOPSIS)**
   - 300-500๋‹จ์–ด
   - 3๋ง‰ ๊ตฌ์กฐ๊ฐ€ ๋ช…ํ™•ํžˆ ๋“œ๋Ÿฌ๋‚˜๋„๋ก
   - ์ฃผ์ธ๊ณต์˜ ๋ณ€ํ™” arc ํฌํ•จ
   - ์ฃผ์š” ์ „ํ™˜์  ๋ช…์‹œ
   - ๊ฒฐ๋ง ํฌํ•จ (์Šคํฌ์ผ๋Ÿฌ OK)

2. **3๋ง‰ ๊ตฌ์กฐ (THREE-ACT STRUCTURE)**
   
   **์ œ1๋ง‰ - ์„ค์ • (Setup) [25%]**
   - ์ผ์ƒ ์„ธ๊ณ„ (Ordinary World):
   - ๊ทผ์นœ์ƒ๊ฐ„ ์‚ฌ๊ฑด (Inciting Incident):
   - ์ฃผ์ธ๊ณต ์†Œ๊ฐœ ๋ฐ ๊ฒฐํ•จ:
   - 1๋ง‰ ์ „ํ™˜์  (Plot Point 1):
   
   **์ œ2๋ง‰A - ์ƒ์Šน ์•ก์…˜ (Rising Action) [25%]**
   - ์ƒˆ๋กœ์šด ์„ธ๊ณ„ ์ง„์ž…:
   - ์žฌ๋ฏธ์™€ ๊ฒŒ์ž„ (Fun and Games):
   - B ์Šคํ† ๋ฆฌ (๊ด€๊ณ„/ํ…Œ๋งˆ):
   - ์ค‘๊ฐ„์  (Midpoint) - ๊ฐ€์งœ ์Šน๋ฆฌ/ํŒจ๋ฐฐ:
   
   **์ œ2๋ง‰B - ๋ณต์žกํ™” (Complications) [25%]**
   - ์•…๋‹น์˜ ๋ฐ˜๊ฒฉ:
   - ํŒ€ ํ•ด์ฒด/์œ„๊ธฐ:
   - ๋ชจ๋“  ๊ฒƒ์„ ์žƒ์Œ (All Is Lost):
   - ์˜ํ˜ผ์˜ ์–ด๋‘” ๋ฐค:
   
   **์ œ3๋ง‰ - ํ•ด๊ฒฐ (Resolution) [25%]**
   - 2๋ง‰ ์ „ํ™˜์  (Plot Point 2):
   - ์ตœ์ข… ์ „ํˆฌ ์ค€๋น„:
   - ํด๋ผ์ด๋งฅ์Šค:
   - ์ƒˆ๋กœ์šด ์ผ์ƒ:

3. **Save the Cat ๋น„ํŠธ ์‹œํŠธ**
   15๊ฐœ ๋น„ํŠธ๋ฅผ {SCREENPLAY_LENGTHS[screenplay_type]['pages']}ํŽ˜์ด์ง€์— ๋งž์ถฐ ๋ฐฐ์น˜

4. **์ฃผ์ œ (THEME)**
   - ์ค‘์‹ฌ ์ฃผ์ œ:
   - ์ฃผ์ œ๊ฐ€ ๋“œ๋Ÿฌ๋‚˜๋Š” ์ˆœ๊ฐ„:
   - ์ฃผ์ œ์˜ ์‹œ๊ฐ์  ํ‘œํ˜„:

5. **ํ†ค & ์Šคํƒ€์ผ**
   - ์ „์ฒด์ ์ธ ํ†ค:
   - ์œ ๋จธ ์‚ฌ์šฉ ์—ฌ๋ถ€:
   - ๋น„์ฃผ์–ผ ์Šคํƒ€์ผ:

๊ตฌ์ฒด์ ์ด๊ณ  ๊ฐ์ •์ ์œผ๋กœ ๊ณต๊ฐ๊ฐ€๋Š” ์Šคํ† ๋ฆฌ๋ฅผ ๋งŒ๋“œ์„ธ์š”.""",

            "English": f"""You are a story developer. Based on the producer's concept, develop the synopsis and three-act structure for this {screenplay_type}.

**Producer Concept:**
{producer_concept}

**Genre Characteristics:** {genre}
- Pacing: {genre_template['pacing']}
- Key Elements: {', '.join(genre_template['key_elements'])}
- Structure Beats: {', '.join(genre_template['structure_beats'])}

**Required Elements:**

1. **SYNOPSIS**
   - 300-500 words
   - Clear three-act structure
   - Protagonist's change arc
   - Major turning points
   - Include ending (spoilers OK)

2. **THREE-ACT STRUCTURE**
   
   **ACT 1 - Setup [25%]**
   - Ordinary World:
   - Inciting Incident:
   - Protagonist Introduction & Flaw:
   - Plot Point 1:
   
   **ACT 2A - Rising Action [25%]**
   - Entering New World:
   - Fun and Games:
   - B Story (Relationship/Theme):
   - Midpoint - False Victory/Defeat:
   
   **ACT 2B - Complications [25%]**
   - Bad Guys Close In:
   - Team Breaks Down/Crisis:
   - All Is Lost:
   - Dark Night of the Soul:
   
   **ACT 3 - Resolution [25%]**
   - Plot Point 2:
   - Final Battle Preparation:
   - Climax:
   - New Normal:

3. **SAVE THE CAT BEAT SHEET**
   Place 15 beats across {SCREENPLAY_LENGTHS[screenplay_type]['pages']} pages

4. **THEME**
   - Central Theme:
   - Theme Stated Moment:
   - Visual Theme Expression:

5. **TONE & STYLE**
   - Overall Tone:
   - Use of Humor:
   - Visual Style:

Create specific, emotionally resonant story."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_character_designer_prompt(self, producer_concept: str, story_structure: str,
                                        genre: str, language: str) -> str:
        """Character designer prompt"""
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ์บ๋ฆญํ„ฐ ๋””์ž์ด๋„ˆ์ž…๋‹ˆ๋‹ค. ๋‹ค์ธต์ ์ด๊ณ  ๋งค๋ ฅ์ ์ธ ์บ๋ฆญํ„ฐ๋“ค์„ ์ฐฝ์กฐํ•˜์„ธ์š”.

**ํ”„๋กœ๋“€์„œ ์ปจ์…‰:**
{producer_concept}

**์Šคํ† ๋ฆฌ ๊ตฌ์กฐ:**
{story_structure}

**ํ•„์ˆ˜ ์บ๋ฆญํ„ฐ ํ”„๋กœํ•„:**

1. **์ฃผ์ธ๊ณต (PROTAGONIST)**
   - ์ด๋ฆ„:
   - ์ง์—…/์—ญํ• :
   - ์บ๋ฆญํ„ฐ ์•„ํฌํƒ€์ž…:
   - WANT (์™ธ์  ๋ชฉํ‘œ):
   - NEED (๋‚ด์  ํ•„์š”):
   - ์น˜๋ช…์  ๊ฒฐํ•จ (Fatal Flaw):
   - ๋ฐฑ์Šคํ† ๋ฆฌ (ํ•ต์‹ฌ ์ƒ์ฒ˜):
   - ์„ฑ๊ฒฉ ํŠน์„ฑ (3-5๊ฐœ):
   - ๋งํˆฌ & ์–ธ์–ด ํŒจํ„ด:
   - ์‹œ๊ฐ์  ํŠน์ง•:
   - ์บ๋ฆญํ„ฐ ์•„ํฌ (Aโ†’B):

2. **์ ๋Œ€์ž (ANTAGONIST)**
   - ์ด๋ฆ„:
   - ์ง์—…/์—ญํ• :
   - ์•…์—ญ ์•„ํฌํƒ€์ž…:
   - ๋ชฉํ‘œ & ๋™๊ธฐ:
   - ์ฃผ์ธ๊ณต๊ณผ์˜ ์—ฐ๊ฒฐ์ :
   - ์ •๋‹น์„ฑ ์žˆ๋Š” ์ด์œ :
   - ์•ฝ์ :
   - ํŠน์ง•์  ํ–‰๋™:

3. **์กฐ๋ ฅ์ž๋“ค (SUPPORTING CAST)**
   ์ตœ์†Œ 3๋ช…, ๊ฐ๊ฐ:
   - ์ด๋ฆ„ & ์—ญํ• :
   - ์ฃผ์ธ๊ณต๊ณผ์˜ ๊ด€๊ณ„:
   - ์Šคํ† ๋ฆฌ ๊ธฐ๋Šฅ:
   - ๋…ํŠนํ•œ ํŠน์„ฑ:
   - ๊ธฐ์—ฌํ•˜๋Š” ๋ฐ”:

4. **์บ๋ฆญํ„ฐ ๊ด€๊ณ„๋„**
   - ์ฃผ์š” ๊ด€๊ณ„ ์—ญํ•™:
   - ๊ฐˆ๋“ฑ ๊ตฌ์กฐ:
   - ๊ฐ์ •์  ์—ฐ๊ฒฐ:
   - ํŒŒ์›Œ ๋‹ค์ด๋‚˜๋ฏน:

5. **์บ์ŠคํŒ… ์ œ์•ˆ**
   - ๊ฐ ์ฃผ์š” ์บ๋ฆญํ„ฐ๋ณ„ ์ด์ƒ์ ์ธ ๋ฐฐ์šฐ ํƒ€์ž…
   - ์™ธ๋ชจ, ์—ฐ๊ธฐ ์Šคํƒ€์ผ

6. **๋Œ€ํ™” ์ƒ˜ํ”Œ**
   - ๊ฐ ์ฃผ์š” ์บ๋ฆญํ„ฐ์˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜ ๋Œ€์‚ฌ 2-3๊ฐœ
   - ์บ๋ฆญํ„ฐ์˜ ๋ณธ์งˆ์„ ๋“œ๋Ÿฌ๋‚ด๋Š” ๋Œ€ํ™”

๊ฐ ์บ๋ฆญํ„ฐ๊ฐ€ ํ…Œ๋งˆ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์Šคํ† ๋ฆฌ๋ฅผ ์ถ”์ง„ํ•˜๋„๋ก ๋””์ž์ธํ•˜์„ธ์š”.""",

            "English": f"""You are a character designer. Create multi-dimensional, compelling characters.

**Producer Concept:**
{producer_concept}

**Story Structure:**
{story_structure}

**Required Character Profiles:**

1. **PROTAGONIST**
   - Name:
   - Occupation/Role:
   - Character Archetype:
   - WANT (External Goal):
   - NEED (Internal Need):
   - Fatal Flaw:
   - Backstory (Core Wound):
   - Personality Traits (3-5):
   - Speech Pattern:
   - Visual Characteristics:
   - Character Arc (Aโ†’B):

2. **ANTAGONIST**
   - Name:
   - Occupation/Role:
   - Villain Archetype:
   - Goal & Motivation:
   - Connection to Protagonist:
   - Justifiable Reason:
   - Weakness:
   - Signature Behaviors:

3. **SUPPORTING CAST**
   Minimum 3, each with:
   - Name & Role:
   - Relationship to Protagonist:
   - Story Function:
   - Unique Traits:
   - What They Contribute:

4. **CHARACTER RELATIONSHIPS**
   - Key Relationship Dynamics:
   - Conflict Structure:
   - Emotional Connections:
   - Power Dynamics:

5. **CASTING SUGGESTIONS**
   - Ideal actor type for each major character
   - Appearance, acting style

6. **DIALOGUE SAMPLES**
   - 2-3 signature lines per major character
   - Dialogue revealing character essence

Design each character to embody theme and drive story."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_scene_planner_prompt(self, story_structure: str, characters: str,
                                   screenplay_type: str, genre: str, language: str) -> str:
        """Scene breakdown planner"""
        
        total_pages = SCREENPLAY_LENGTHS[screenplay_type]['pages']
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ์”ฌ ํ”Œ๋ž˜๋„ˆ์ž…๋‹ˆ๋‹ค. {total_pages}ํŽ˜์ด์ง€ {screenplay_type}์˜ ์ƒ์„ธํ•œ ์”ฌ ๋ธŒ๋ ˆ์ดํฌ๋‹ค์šด์„ ์ž‘์„ฑํ•˜์„ธ์š”.

**์Šคํ† ๋ฆฌ ๊ตฌ์กฐ:**
{story_structure}

**์บ๋ฆญํ„ฐ:**
{characters}

**์”ฌ ๋ธŒ๋ ˆ์ดํฌ๋‹ค์šด ์š”๊ตฌ์‚ฌํ•ญ:**

๊ฐ ์”ฌ๋งˆ๋‹ค ๋‹ค์Œ ์ •๋ณด ์ œ๊ณต:
- ์”ฌ ๋ฒˆํ˜ธ
- ์žฅ์†Œ (INT./EXT. LOCATION)
- ์‹œ๊ฐ„ (DAY/NIGHT/DAWN/DUSK)
- ๋“ฑ์žฅ์ธ๋ฌผ
- ์”ฌ์˜ ๋ชฉ์  (์Šคํ† ๋ฆฌ/์บ๋ฆญํ„ฐ/ํ…Œ๋งˆ)
- ํ•ต์‹ฌ ๊ฐˆ๋“ฑ
- ์˜ˆ์ƒ ํŽ˜์ด์ง€ ์ˆ˜
- Save the Cat ๋น„ํŠธ (ํ•ด๋‹น์‹œ)

**๋ง‰๋ณ„ ๋ฐฐ๋ถ„:**
- 1๋ง‰: ~{int(total_pages * 0.25)}ํŽ˜์ด์ง€ (10-12์”ฌ)
- 2๋ง‰A: ~{int(total_pages * 0.25)}ํŽ˜์ด์ง€ (12-15์”ฌ)
- 2๋ง‰B: ~{int(total_pages * 0.25)}ํŽ˜์ด์ง€ (12-15์”ฌ)  
- 3๋ง‰: ~{int(total_pages * 0.25)}ํŽ˜์ด์ง€ (8-10์”ฌ)

**์žฅ๋ฅด๋ณ„ ๊ณ ๋ ค์‚ฌํ•ญ:** {genre}
{self._get_genre_scene_guidelines(genre, "Korean")}

**์”ฌ ์ „ํ™˜ ์Šคํƒ€์ผ:**
- CUT TO:
- FADE IN/OUT:
- MATCH CUT:
- SMASH CUT:
- DISSOLVE TO:

๊ฐ ์”ฌ์ด ์Šคํ† ๋ฆฌ๋ฅผ ์ „์ง„์‹œํ‚ค๊ณ  ์บ๋ฆญํ„ฐ๋ฅผ ๋ฐœ์ „์‹œํ‚ค๋„๋ก ๊ณ„ํšํ•˜์„ธ์š”.""",

            "English": f"""You are a scene planner. Create detailed scene breakdown for {total_pages}-page {screenplay_type}.

**Story Structure:**
{story_structure}

**Characters:**
{characters}

**Scene Breakdown Requirements:**

For each scene provide:
- Scene Number
- Location (INT./EXT. LOCATION)
- Time (DAY/NIGHT/DAWN/DUSK)
- Characters Present
- Scene Purpose (Story/Character/Theme)
- Core Conflict
- Estimated Page Count
- Save the Cat Beat (if applicable)

**Act Distribution:**
- Act 1: ~{int(total_pages * 0.25)} pages (10-12 scenes)
- Act 2A: ~{int(total_pages * 0.25)} pages (12-15 scenes)
- Act 2B: ~{int(total_pages * 0.25)} pages (12-15 scenes)
- Act 3: ~{int(total_pages * 0.25)} pages (8-10 scenes)

**Genre Considerations:** {genre}
{self._get_genre_scene_guidelines(genre, "English")}

**Scene Transitions:**
- CUT TO:
- FADE IN/OUT:
- MATCH CUT:
- SMASH CUT:
- DISSOLVE TO:

Plan each scene to advance story and develop character."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_screenwriter_prompt(self, act: str, scene_breakdown: str, 
                                  characters: str, previous_acts: str,
                                  screenplay_type: str, genre: str, language: str) -> str:
        """Screenwriter prompt for actual screenplay pages"""
        
        act_pages = int(SCREENPLAY_LENGTHS[screenplay_type]['pages'] * 0.25)
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ํ”„๋กœ ์‹œ๋‚˜๋ฆฌ์˜ค ์ž‘๊ฐ€์ž…๋‹ˆ๋‹ค. {act}์„ ํ‘œ์ค€ ์‹œ๋‚˜๋ฆฌ์˜ค ํฌ๋งท์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.

**ํƒ€๊ฒŸ ๋ถ„๋Ÿ‰:** {act_pages}ํŽ˜์ด์ง€

**์”ฌ ๋ธŒ๋ ˆ์ดํฌ๋‹ค์šด:**
{self._extract_act_scenes(scene_breakdown, act)}

**์บ๋ฆญํ„ฐ ์ •๋ณด:**
{characters}

**์ด์ „ ๋‚ด์šฉ:**
{previous_acts if previous_acts else "์ฒซ ๋ง‰์ž…๋‹ˆ๋‹ค."}

**์‹œ๋‚˜๋ฆฌ์˜ค ํฌ๋งท ๊ทœ์น™:**

1. **์”ฌ ํ—ค๋”ฉ**
   INT. ์žฅ์†Œ - ์‹œ๊ฐ„
   EXT. ์žฅ์†Œ - ์‹œ๊ฐ„

2. **์•ก์…˜ ๋ผ์ธ**
   - ํ˜„์žฌ ์‹œ์ œ ์‚ฌ์šฉ
   - ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด์ด๋Š” ๊ฒƒ๋งŒ ๋ฌ˜์‚ฌ
   - 4์ค„ ์ดํ•˜๋กœ ์œ ์ง€
   - ๊ฐ์ •์€ ํ–‰๋™์œผ๋กœ ํ‘œํ˜„

3. **์บ๋ฆญํ„ฐ ์†Œ๊ฐœ**
   ์ฒซ ๋“ฑ์žฅ์‹œ: ์ด๋ฆ„๊ณผ ๊ฐ„๋‹จํ•œ ๋ฌ˜์‚ฌ

4. **๋Œ€ํ™”**
   ์บ๋ฆญํ„ฐ๋ช…
   (์ง€๋ฌธ)
   ๋Œ€์‚ฌ

5. **์ค‘์š” ์›์น™**
   - Show, don't tell
   - ์„œ๋ธŒํ…์ŠคํŠธ ํ™œ์šฉ
   - ์ž์—ฐ์Šค๋Ÿฌ์šด ๋Œ€ํ™”
   - ์‹œ๊ฐ์  ์Šคํ† ๋ฆฌํ…”๋ง
   - ํŽ˜์ด์ง€๋‹น 1๋ถ„ ๊ทœ์น™

**{genre} ์žฅ๋ฅด ํŠน์„ฑ:**
- ๋Œ€ํ™” ๋น„์œจ: {GENRE_TEMPLATES[genre]['dialogue_ratio']*100}%
- ์”ฌ ๊ธธ์ด: {GENRE_TEMPLATES[genre]['scene_length']}
- ํ•ต์‹ฌ ์š”์†Œ: {', '.join(GENRE_TEMPLATES[genre]['key_elements'][:2])}

์ •ํ™•ํ•œ ํฌ๋งท๊ณผ ๋ชฐ์ž…๊ฐ ์žˆ๋Š” ์Šคํ† ๋ฆฌํ…”๋ง์œผ๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.""",

            "English": f"""You are a professional screenwriter. Write {act} in standard screenplay format.

**Target Length:** {act_pages} pages

**Scene Breakdown:**
{self._extract_act_scenes(scene_breakdown, act)}

**Character Info:**
{characters}

**Previous Content:**
{previous_acts if previous_acts else "This is the first act."}

**Screenplay Format Rules:**

1. **Scene Headings**
   INT. LOCATION - TIME
   EXT. LOCATION - TIME

2. **Action Lines**
   - Present tense
   - Only what's visually seen
   - Keep under 4 lines
   - Emotions through actions

3. **Character Intros**
   First appearance: NAME with brief description

4. **Dialogue**
   CHARACTER NAME
   (parenthetical)
   Dialogue

5. **Key Principles**
   - Show, don't tell
   - Use subtext
   - Natural dialogue
   - Visual storytelling
   - One page = one minute

**{genre} Genre Characteristics:**
- Dialogue Ratio: {GENRE_TEMPLATES[genre]['dialogue_ratio']*100}%
- Scene Length: {GENRE_TEMPLATES[genre]['scene_length']}
- Key Elements: {', '.join(GENRE_TEMPLATES[genre]['key_elements'][:2])}

Write with proper format and engaging storytelling."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_script_doctor_prompt(self, act_content: str, act: str,
                                   genre: str, language: str) -> str:
        """Script doctor review and polish"""
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ์Šคํฌ๋ฆฝํŠธ ๋‹ฅํ„ฐ์ž…๋‹ˆ๋‹ค. {act}๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ  ๊ฐœ์„  ์‚ฌํ•ญ์„ ์ œ์‹œํ•˜์„ธ์š”.

**์ž‘์„ฑ๋œ ๋‚ด์šฉ:**
{act_content}

**๊ฒ€ํ†  ํ•ญ๋ชฉ:**

1. **ํฌ๋งท ์ •ํ™•์„ฑ**
   - ์”ฌ ํ—ค๋”ฉ ํ˜•์‹
   - ์•ก์…˜ ๋ผ์ธ ๊ธธ์ด
   - ๋Œ€ํ™” ํฌ๋งท
   - ์ „ํ™˜ ํ‘œ์‹œ

2. **์Šคํ† ๋ฆฌํ…”๋ง**
   - ์‹œ๊ฐ์  ๋ช…ํ™•์„ฑ
   - ํŽ˜์ด์‹ฑ
   - ๊ธด์žฅ๊ฐ ๊ตฌ์ถ•
   - ์”ฌ ๋ชฉ์  ๋‹ฌ์„ฑ

3. **๋Œ€ํ™” ํ’ˆ์งˆ**
   - ์ž์—ฐ์Šค๋Ÿฌ์›€
   - ์บ๋ฆญํ„ฐ ๊ณ ์œ ์„ฑ
   - ์„œ๋ธŒํ…์ŠคํŠธ
   - ๋ถˆํ•„์š”ํ•œ ์„ค๋ช… ์ œ๊ฑฐ

4. **{genre} ์žฅ๋ฅด ์ ํ•ฉ์„ฑ**
   - ์žฅ๋ฅด ๊ด€์Šต ์ค€์ˆ˜
   - ํ†ค ์ผ๊ด€์„ฑ
   - ๊ธฐ๋Œ€ ์ถฉ์กฑ

5. **๊ธฐ์ˆ ์  ์ธก๋ฉด**
   - ํŽ˜์ด์ง€ ์ˆ˜ ์ ์ •์„ฑ
   - ์ œ์ž‘ ๊ฐ€๋Šฅ์„ฑ
   - ์˜ˆ์‚ฐ ๊ณ ๋ ค์‚ฌํ•ญ

**ํ•„์ˆ˜ ๊ฐœ์„ ์‚ฌํ•ญ:**
๊ตฌ์ฒด์ ์ธ ์ˆ˜์ • ์ œ์•ˆ๊ณผ ๊ฐœ์„ ๋œ ์˜ˆ์‹œ๋ฅผ ์ œ๊ณตํ•˜์„ธ์š”.""",

            "English": f"""You are a script doctor. Review and provide improvements for {act}.

**Written Content:**
{act_content}

**Review Areas:**

1. **Format Accuracy**
   - Scene heading format
   - Action line length
   - Dialogue format
   - Transitions

2. **Storytelling**
   - Visual clarity
   - Pacing
   - Tension building
   - Scene purpose achievement

3. **Dialogue Quality**
   - Naturalness
   - Character uniqueness
   - Subtext
   - Remove exposition

4. **{genre} Genre Fit**
   - Genre conventions
   - Tone consistency
   - Meeting expectations

5. **Technical Aspects**
   - Page count appropriateness
   - Production feasibility
   - Budget considerations

**Required Improvements:**
Provide specific revision suggestions with improved examples."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_final_reviewer_prompt(self, complete_screenplay: str, 
                                   screenplay_type: str, genre: str, language: str) -> str:
        """Final comprehensive review"""
        
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ์ตœ์ข… ๋ฆฌ๋ทฐ์–ด์ž…๋‹ˆ๋‹ค. ์™„์„ฑ๋œ {screenplay_type} ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ข…ํ•ฉ ํ‰๊ฐ€ํ•˜์„ธ์š”.

**ํ‰๊ฐ€ ๊ธฐ์ค€:**

1. **์ƒ์—…์„ฑ (25์ )**
   - ์‹œ์žฅ์„ฑ
   - ํƒ€๊ฒŸ ๊ด€๊ฐ ์–ดํ•„
   - ์ œ์ž‘ ๊ฐ€๋Šฅ์„ฑ
   - ๋ฐฐ๊ธ‰ ์ž ์žฌ๋ ฅ

2. **์Šคํ† ๋ฆฌ (25์ )**
   - 3๋ง‰ ๊ตฌ์กฐ ํšจ๊ณผ์„ฑ
   - ์บ๋ฆญํ„ฐ ์•„ํฌ
   - ํ”Œ๋กฏ ์ผ๊ด€์„ฑ
   - ํ…Œ๋งˆ ์ „๋‹ฌ

3. **๊ธฐ์ˆ ์  ์™„์„ฑ๋„ (25์ )**
   - ํฌ๋งท ์ •ํ™•์„ฑ
   - ํŽ˜์ด์ง€ ์ˆ˜ ์ ์ •์„ฑ
   - ์”ฌ ๊ตฌ์„ฑ
   - ์‹œ๊ฐ์  ์Šคํ† ๋ฆฌํ…”๋ง

4. **๋Œ€ํ™” & ์บ๋ฆญํ„ฐ (25์ )**
   - ๋Œ€ํ™” ์ž์—ฐ์Šค๋Ÿฌ์›€
   - ์บ๋ฆญํ„ฐ ๊ณ ์œ ์„ฑ
   - ๊ด€๊ณ„ ์—ญํ•™
   - ๊ฐ์ •์  ์ง„์ •์„ฑ

**์ข…ํ•ฉ ํ‰๊ฐ€:**
- ๊ฐ•์  (3-5๊ฐœ)
- ๊ฐœ์„  ํ•„์š” ์‚ฌํ•ญ (3-5๊ฐœ)
- ์‹œ์žฅ ์ž ์žฌ๋ ฅ
- ์ถ”์ฒœ ์‚ฌํ•ญ

**๋“ฑ๊ธ‰:** A+ ~ F

๊ตฌ์ฒด์ ์ด๊ณ  ๊ฑด์„ค์ ์ธ ํ”ผ๋“œ๋ฐฑ์„ ์ œ๊ณตํ•˜์„ธ์š”.""",

            "English": f"""You are the final reviewer. Comprehensively evaluate the completed {screenplay_type} screenplay.

**Evaluation Criteria:**

1. **Commercial Viability (25 points)**
   - Marketability
   - Target audience appeal
   - Production feasibility
   - Distribution potential

2. **Story (25 points)**
   - Three-act structure effectiveness
   - Character arcs
   - Plot consistency
   - Theme delivery

3. **Technical Excellence (25 points)**
   - Format accuracy
   - Page count appropriateness
   - Scene construction
   - Visual storytelling

4. **Dialogue & Character (25 points)**
   - Dialogue naturalness
   - Character uniqueness
   - Relationship dynamics
   - Emotional authenticity

**Overall Assessment:**
- Strengths (3-5)
- Areas for Improvement (3-5)
- Market Potential
- Recommendations

**Grade:** A+ to F

Provide specific, constructive feedback."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def create_critic_structure_prompt(self, story_structure: str, characters: str,
                                     screenplay_type: str, genre: str, language: str) -> str:
        """Structure critic prompt"""
        lang_prompts = {
            "Korean": f"""๋‹น์‹ ์€ ๊ตฌ์กฐ ๋น„ํ‰๊ฐ€์ž…๋‹ˆ๋‹ค. ์Šคํ† ๋ฆฌ ๊ตฌ์กฐ์™€ ์บ๋ฆญํ„ฐ ์„ค์ •์„ ์‹ฌ์ธต ๋ถ„์„ํ•˜์„ธ์š”.

**์Šคํ† ๋ฆฌ ๊ตฌ์กฐ:**
{story_structure}

**์บ๋ฆญํ„ฐ ์„ค์ •:**
{characters}

**๋ถ„์„ ํ•ญ๋ชฉ:**

1. **3๋ง‰ ๊ตฌ์กฐ ํšจ๊ณผ์„ฑ**
   - ๊ฐ ๋ง‰์˜ ๊ท ํ˜•
   - ์ „ํ™˜์ ์˜ ๊ฐ•๋„
   - ํ”Œ๋กฏ ํฌ์ธํŠธ์˜ ๋ช…ํ™•์„ฑ
   - ํด๋ผ์ด๋งฅ์Šค ์œ„์น˜

2. **์บ๋ฆญํ„ฐ ์•„ํฌ ํƒ€๋‹น์„ฑ**
   - ๋ณ€ํ™”์˜ ์‹ ๋น™์„ฑ
   - ๋™๊ธฐ์˜ ๋ช…ํ™•์„ฑ
   - ๋‚ด์ /์™ธ์  ๋ชฉํ‘œ ์ผ์น˜
   - ๊ด€๊ณ„ ์—ญํ•™

3. **ํ…Œ๋งˆ ํ†ตํ•ฉ**
   - ํ…Œ๋งˆ์˜ ์ผ๊ด€์„ฑ
   - ํ”Œ๋กฏ๊ณผ ํ…Œ๋งˆ ์—ฐ๊ฒฐ
   - ์บ๋ฆญํ„ฐ์™€ ํ…Œ๋งˆ ์—ฐ๊ฒฐ
   - ์‹œ๊ฐ์  ํ…Œ๋งˆ ํ‘œํ˜„

4. **์žฅ๋ฅด ๊ธฐ๋Œ€์น˜**
   - {genre} ๊ด€์Šต ์ถฉ์กฑ
   - ๋…์ฐฝ์„ฑ๊ณผ ์นœ์ˆ™ํ•จ ๊ท ํ˜•
   - ํƒ€๊ฒŸ ๊ด€๊ฐ ๋งŒ์กฑ๋„

5. **์ œ์ž‘ ํ˜„์‹ค์„ฑ**
   - ์˜ˆ์‚ฐ ๊ทœ๋ชจ ์ ์ •์„ฑ
   - ๋กœ์ผ€์ด์…˜ ์‹คํ˜„ ๊ฐ€๋Šฅ์„ฑ
   - ํŠน์ˆ˜ํšจ๊ณผ ์š”๊ตฌ์‚ฌํ•ญ

**ํ•„์ˆ˜ ๊ฐœ์„  ์ œ์•ˆ:**
๊ฐ ๋ฌธ์ œ์ ์— ๋Œ€ํ•œ ๊ตฌ์ฒด์  ํ•ด๊ฒฐ์ฑ…์„ ์ œ์‹œํ•˜์„ธ์š”.""",

            "English": f"""You are a structure critic. Deeply analyze story structure and character setup.

**Story Structure:**
{story_structure}

**Character Setup:**
{characters}

**Analysis Items:**

1. **Three-Act Structure Effectiveness**
   - Balance of each act
   - Strength of transitions
   - Clarity of plot points
   - Climax positioning

2. **Character Arc Validity**
   - Credibility of change
   - Clarity of motivation
   - Internal/external goal alignment
   - Relationship dynamics

3. **Theme Integration**
   - Theme consistency
   - Plot-theme connection
   - Character-theme connection
   - Visual theme expression

4. **Genre Expectations**
   - Meeting {genre} conventions
   - Balance of originality and familiarity
   - Target audience satisfaction

5. **Production Reality**
   - Budget scale appropriateness
   - Location feasibility
   - Special effects requirements

**Required Improvement Suggestions:**
Provide specific solutions for each issue."""
        }
        
        return lang_prompts.get(language, lang_prompts["English"])

    def _get_genre_scene_guidelines(self, genre: str, language: str) -> str:
        """Get genre-specific scene guidelines"""
        guidelines = {
            "action": {
                "Korean": "- ์งง๊ณ  ํŽ€์น˜๊ฐ ์žˆ๋Š” ์”ฌ\n- ์•ก์…˜ ์‹œํ€€์Šค ์ƒ์„ธ ๊ณ„ํš\n- ๊ธด์žฅ๊ฐ ์ง€์†",
                "English": "- Short, punchy scenes\n- Detailed action sequences\n- Maintain tension"
            },
            "thriller": {
                "Korean": "- ์„œ์ŠคํŽœ์Šค ๊ตฌ์ถ•\n- ์ •๋ณด ์ ์ง„์  ๊ณต๊ฐœ\n- ๋ฐ˜์ „ ๋ฐฐ์น˜",
                "English": "- Build suspense\n- Gradual information reveal\n- Place twists"
            },
            "drama": {
                "Korean": "- ๊ฐ์ •์  ๋น„ํŠธ ๊ฐ•์กฐ\n- ์บ๋ฆญํ„ฐ ์ค‘์‹ฌ ์”ฌ\n- ๋Œ€ํ™” ๊ณต๊ฐ„ ํ™•๋ณด",
                "English": "- Emphasize emotional beats\n- Character-driven scenes\n- Allow dialogue space"
            },
            "comedy": {
                "Korean": "- ์…‹์—…๊ณผ ํŽ˜์ด์˜คํ”„\n- ์ฝ”๋ฏน ํƒ€์ด๋ฐ\n- ์‹œ๊ฐ์  ๊ฐœ๊ทธ",
                "English": "- Setup and payoff\n- Comic timing\n- Visual gags"
            },
            "horror": {
                "Korean": "- ๋ถ„์œ„๊ธฐ ์กฐ์„ฑ\n- ์ ํ”„ ์Šค์ผ€์–ด ๋ฐฐ์น˜\n- ๊ธด์žฅ๊ณผ ์ด์™„",
                "English": "- Atmosphere building\n- Jump scare placement\n- Tension and release"
            },
            "sci-fi": {
                "Korean": "- ์„ธ๊ณ„๊ด€ ์„ค๋ช…\n- ์‹œ๊ฐ์  ์ŠคํŽ™ํ„ฐํด\n- ๊ฐœ๋… ์†Œ๊ฐœ",
                "English": "- World building\n- Visual spectacle\n- Concept introduction"
            },
            "romance": {
                "Korean": "- ๊ฐ์ •์  ์นœ๋ฐ€๊ฐ\n- ๊ด€๊ณ„ ๋ฐœ์ „\n- ๋กœ๋งจํ‹ฑ ๋น„ํŠธ",
                "English": "- Emotional intimacy\n- Relationship progression\n- Romantic beats"
            }
        }
        
        return guidelines.get(genre, guidelines["drama"]).get(language, "")

    def _extract_act_scenes(self, scene_breakdown: str, act: str) -> str:
        """Extract scenes for specific act"""
        # This would parse the scene breakdown and return only scenes for the requested act
        # For now, returning a placeholder
        return f"Scenes for {act} from the breakdown"

    # --- LLM call functions ---
    def call_llm_sync(self, messages: List[Dict[str, str]], role: str, language: str) -> str:
        full_content = ""
        for chunk in self.call_llm_streaming(messages, role, language):
            full_content += chunk
        if full_content.startswith("โŒ"):
            raise Exception(f"LLM Call Failed: {full_content}")
        return full_content

    def call_llm_streaming(self, messages: List[Dict[str, str]], role: str, 
                          language: str) -> Generator[str, None, None]:
        try:
            # Debug logging
            logger.info(f"Calling LLM for role: {role}, language: {language}")
            
            system_prompts = self.get_system_prompts(language)
            system_content = system_prompts.get(role, "")
            
            if not system_content:
                logger.warning(f"No system prompt found for role: {role}")
                system_content = "You are a helpful assistant."
            
            full_messages = [
                {"role": "system", "content": system_content}, 
                *messages
            ]
            
            max_tokens = 15000 if role == "screenwriter" else 8000
            
            payload = {
                "model": self.model_id,
                "messages": full_messages,
                "max_tokens": max_tokens,
                "temperature": 0.7 if role in ["screenwriter", "script_doctor"] else 0.8,
                "top_p": 0.9,
                "presence_penalty": 0.3,
                "frequency_penalty": 0.3,
                "stream": True
            }
            
            # Debug logging
            logger.debug(f"API URL: {self.api_url}")
            logger.debug(f"Model ID: {self.model_id}")
            
            try:
                headers = self.create_headers()
            except ValueError as e:
                logger.error(f"Header creation failed: {e}")
                yield f"โŒ API configuration error: {e}"
                return
            
            response = requests.post(
                self.api_url, 
                headers=headers, 
                json=payload, 
                stream=True, 
                timeout=180
            )
            
            logger.info(f"API Response Status: {response.status_code}")
            
            if response.status_code != 200:
                error_msg = f"API Error (Status Code: {response.status_code})"
                try:
                    error_data = response.json()
                    logger.error(f"API Error Response: {error_data}")
                    if isinstance(error_data, dict):
                        if 'error' in error_data:
                            error_msg += f" - {error_data['error']}"
                        elif 'message' in error_data:
                            error_msg += f" - {error_data['message']}"
                except Exception as e:
                    logger.error(f"Error parsing error response: {e}")
                    error_msg += f" - {response.text[:200]}"
                
                yield f"โŒ {error_msg}"
                return
            
            buffer = ""
            line_count = 0
            
            for line in response.iter_lines():
                if not line:
                    continue
                
                line_count += 1
                
                try:
                    line_str = line.decode('utf-8').strip()
                    
                    # Skip non-SSE lines
                    if not line_str.startswith("data: "):
                        logger.debug(f"Skipping non-SSE line: {line_str[:50]}")
                        continue
                    
                    data_str = line_str[6:]  # Remove "data: " prefix
                    
                    if data_str == "[DONE]":
                        logger.info(f"Stream completed. Total lines: {line_count}")
                        break
                    
                    if not data_str:
                        continue
                    
                    # Parse JSON data
                    try:
                        data = json.loads(data_str)
                    except json.JSONDecodeError as e:
                        logger.warning(f"JSON decode error on line {line_count}: {e}")
                        logger.debug(f"Problematic data: {data_str[:100]}")
                        continue
                    
                    # Extract content from response
                    if isinstance(data, dict) and "choices" in data:
                        choices = data["choices"]
                        if isinstance(choices, list) and len(choices) > 0:
                            choice = choices[0]
                            if isinstance(choice, dict) and "delta" in choice:
                                delta = choice["delta"]
                                if isinstance(delta, dict) and "content" in delta:
                                    content = delta["content"]
                                    if content:
                                        buffer += content
                                        
                                        # Yield when buffer is large enough
                                        if len(buffer) >= 50 or '\n' in buffer:
                                            yield buffer
                                            buffer = ""
                                            time.sleep(0.01)
                except Exception as e:
                    logger.error(f"Error processing line {line_count}: {str(e)}")
                    logger.debug(f"Problematic line: {line_str[:100] if 'line_str' in locals() else 'N/A'}")
                    continue
            
            # Yield any remaining buffer content
            if buffer:
                yield buffer
            
            # Check if we got any content
            if line_count == 0:
                logger.error("No lines received from API")
                yield "โŒ No response from API"
                
        except requests.exceptions.Timeout:
            logger.error("API request timed out")
            yield "โŒ Request timed out. Please try again."
        except requests.exceptions.ConnectionError as e:
            logger.error(f"Connection error: {e}")
            yield "โŒ Connection error. Please check your internet connection."
        except requests.exceptions.RequestException as e:
            logger.error(f"Request error: {type(e).__name__}: {str(e)}")
            yield f"โŒ Network error: {str(e)}"
        except Exception as e:
            logger.error(f"Unexpected error in streaming: {type(e).__name__}: {str(e)}")
            import traceback
            logger.error(traceback.format_exc())
            yield f"โŒ Unexpected error: {str(e)}"

    def get_system_prompts(self, language: str) -> Dict[str, str]:
        """Role-specific system prompts"""
        
        base_prompts = {
            "Korean": {
                "producer": """๋‹น์‹ ์€ 20๋…„ ๊ฒฝ๋ ฅ์˜ ํ• ๋ฆฌ์šฐ๋“œ ํ”„๋กœ๋“€์„œ์ž…๋‹ˆ๋‹ค.
์ƒ์—…์  ์„ฑ๊ณต๊ณผ ์˜ˆ์ˆ ์  ๊ฐ€์น˜๋ฅผ ๋ชจ๋‘ ์ถ”๊ตฌํ•ฉ๋‹ˆ๋‹ค.
์‹œ์žฅ ํŠธ๋ Œ๋“œ์™€ ๊ด€๊ฐ ์‹ฌ๋ฆฌ๋ฅผ ์ •ํ™•ํžˆ ํŒŒ์•…ํ•ฉ๋‹ˆ๋‹ค.
์‹คํ˜„ ๊ฐ€๋Šฅํ•˜๊ณ  ๋งค๋ ฅ์ ์ธ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค.""",
                
                "story_developer": """๋‹น์‹ ์€ ์ˆ˜์ƒ ๊ฒฝ๋ ฅ์ด ์žˆ๋Š” ์Šคํ† ๋ฆฌ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.
๊ฐ์ •์ ์œผ๋กœ ๊ณต๊ฐ๊ฐ€๊ณ  ๊ตฌ์กฐ์ ์œผ๋กœ ํƒ„ํƒ„ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
์บ๋ฆญํ„ฐ์˜ ๋‚ด์  ์—ฌ์ •๊ณผ ์™ธ์  ํ”Œ๋กฏ์„ ์กฐํ™”๋กญ๊ฒŒ ์—ฎ์Šต๋‹ˆ๋‹ค.
๋ณดํŽธ์  ์ฃผ์ œ๋ฅผ ๋…ํŠนํ•œ ๋ฐฉ์‹์œผ๋กœ ํƒ๊ตฌํ•ฉ๋‹ˆ๋‹ค.""",
                
                "character_designer": """๋‹น์‹ ์€ ์‹ฌ๋ฆฌํ•™์„ ๊ณต๋ถ€ํ•œ ์บ๋ฆญํ„ฐ ๋””์ž์ด๋„ˆ์ž…๋‹ˆ๋‹ค.
์ง„์งœ ๊ฐ™์€ ์ธ๋ฌผ๋“ค์„ ์ฐฝ์กฐํ•˜๋Š” ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
๊ฐ ์บ๋ฆญํ„ฐ์—๊ฒŒ ๊ณ ์œ ํ•œ ๋ชฉ์†Œ๋ฆฌ์™€ ๊ด€์ ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.
๋ณต์žกํ•˜๊ณ  ๋ชจ์ˆœ์ ์ธ ์ธ๊ฐ„์„ฑ์„ ํฌ์ฐฉํ•ฉ๋‹ˆ๋‹ค.""",
                
                "scene_planner": """๋‹น์‹ ์€ ์ •๋ฐ€ํ•œ ์”ฌ ๊ตฌ์„ฑ์˜ ๋Œ€๊ฐ€์ž…๋‹ˆ๋‹ค.
๊ฐ ์”ฌ์ด ์Šคํ† ๋ฆฌ์™€ ์บ๋ฆญํ„ฐ๋ฅผ ์ „์ง„์‹œํ‚ค๋„๋ก ์„ค๊ณ„ํ•ฉ๋‹ˆ๋‹ค.
๋ฆฌ๋“ฌ๊ณผ ํŽ˜์ด์‹ฑ์„ ์™„๋ฒฝํ•˜๊ฒŒ ์กฐ์ ˆํ•ฉ๋‹ˆ๋‹ค.
์‹œ๊ฐ์  ์Šคํ† ๋ฆฌํ…”๋ง์„ ๊ทน๋Œ€ํ™”ํ•ฉ๋‹ˆ๋‹ค.""",
                
                "screenwriter": """๋‹น์‹ ์€ ๋‹ค์ž‘์˜ ์‹œ๋‚˜๋ฆฌ์˜ค ์ž‘๊ฐ€์ž…๋‹ˆ๋‹ค.
'๋ณด์—ฌ์ฃผ๊ธฐ'์˜ ๋Œ€๊ฐ€์ด๋ฉฐ ์„œ๋ธŒํ…์ŠคํŠธ๋ฅผ ๋Šฅ์ˆ™ํ•˜๊ฒŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค.
์ƒ์ƒํ•˜๊ณ  ์ž์—ฐ์Šค๋Ÿฌ์šด ๋Œ€ํ™”๋ฅผ ์“ฐ๋Š” ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
์ œ์ž‘ ํ˜„์‹ค์„ ๊ณ ๋ คํ•˜๋ฉด์„œ๋„ ์ฐฝ์˜์ ์ธ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์Šต๋‹ˆ๋‹ค.""",
                
                "script_doctor": """๋‹น์‹ ์€ ๊นŒ๋‹ค๋กœ์šด ์Šคํฌ๋ฆฝํŠธ ๋‹ฅํ„ฐ์ž…๋‹ˆ๋‹ค.
์ž‘์€ ๋””ํ…Œ์ผ๋„ ๋†“์น˜์ง€ ์•Š๋Š” ์™„๋ฒฝ์ฃผ์˜์ž์ž…๋‹ˆ๋‹ค.
์Šคํ† ๋ฆฌ์˜ ์ž ์žฌ๋ ฅ์„ ์ตœ๋Œ€ํ•œ ๋Œ์–ด๋ƒ…๋‹ˆ๋‹ค.
๊ฑด์„ค์ ์ด๊ณ  ๊ตฌ์ฒด์ ์ธ ๊ฐœ์„ ์•ˆ์„ ์ œ์‹œํ•ฉ๋‹ˆ๋‹ค.""",
                
                "critic_structure": """๋‹น์‹ ์€ ๊ตฌ์กฐ ๋ถ„์„ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
์Šคํ† ๋ฆฌ์˜ ๋ผˆ๋Œ€์™€ ๊ทผ์œก์„ ๊ฟฐ๋šซ์–ด ๋ด…๋‹ˆ๋‹ค.
๋…ผ๋ฆฌ์  ํ—ˆ์ ๊ณผ ๊ฐ์ •์  ๊ณต๋ฐฑ์„ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค.
๋” ๋‚˜์€ ๊ตฌ์กฐ๋ฅผ ์œ„ํ•œ ๊ตฌ์ฒด์  ์ œ์•ˆ์„ ํ•ฉ๋‹ˆ๋‹ค.""",
                
                "final_reviewer": """๋‹น์‹ ์€ ์—…๊ณ„ ๋ฒ ํ…Œ๋ž‘ ์ตœ์ข… ๋ฆฌ๋ทฐ์–ด์ž…๋‹ˆ๋‹ค.
์ƒ์—…์„ฑ๊ณผ ์˜ˆ์ˆ ์„ฑ์„ ๊ท ํ˜•์žˆ๊ฒŒ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
์ œ์ž‘์‚ฌ, ๋ฐฐ์šฐ, ๊ด€๊ฐ ๋ชจ๋“  ๊ด€์ ์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค.
๋ƒ‰์ •ํ•˜์ง€๋งŒ ๊ฒฉ๋ คํ•˜๋Š” ํ”ผ๋“œ๋ฐฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค."""
            },
            "English": {
                "producer": """You are a Hollywood producer with 20 years experience.
You pursue both commercial success and artistic value.
You accurately grasp market trends and audience psychology.
You develop feasible and attractive projects.""",
                
                "story_developer": """You are an award-winning story developer.
You create emotionally resonant and structurally sound stories.
You harmoniously weave internal journeys with external plots.
You explore universal themes in unique ways.""",
                
                "character_designer": """You are a character designer who studied psychology.
You're an expert at creating lifelike characters.
You give each character a unique voice and perspective.
You capture complex and contradictory humanity.""",
                
                "scene_planner": """You are a master of precise scene construction.
You design each scene to advance story and character.
You perfectly control rhythm and pacing.
You maximize visual storytelling.""",
                
                "screenwriter": """You are a prolific screenwriter.
You're a master of 'showing' and skilled with subtext.
You're an expert at writing vivid, natural dialogue.
You find creative solutions while considering production reality.""",
                
                "script_doctor": """You are a demanding script doctor.
You're a perfectionist who misses no small detail.
You maximize the story's potential.
You provide constructive and specific improvements.""",
                
                "critic_structure": """You are a structure analysis expert.
You see through the story's skeleton and muscles.
You find logical gaps and emotional voids.
You make specific suggestions for better structure.""",
                
                "final_reviewer": """You are an industry veteran final reviewer.
You evaluate commercial and artistic value in balance.
You consider all perspectives: producers, actors, audience.
You provide feedback that's critical yet encouraging."""
            }
        }
        
        return base_prompts.get(language, base_prompts["English"])

    # --- Main process ---
    def process_screenplay_stream(self, query: str, screenplay_type: str, genre: str,
                                language: str, session_id: Optional[str] = None
                                ) -> Generator[Tuple[str, List[Dict[str, Any]], str], None, None]:
        """Main screenplay generation process"""
        try:
            resume_from_stage = 0
            if session_id:
                self.current_session_id = session_id
                session = ScreenplayDatabase.get_session(session_id)
                if session:
                    query = session['user_query']
                    screenplay_type = session['screenplay_type']
                    genre = session['genre']
                    language = session['language']
                    resume_from_stage = session['current_stage'] + 1
            else:
                self.current_session_id = ScreenplayDatabase.create_session(
                    query, screenplay_type, genre, language
                )
                logger.info(f"Created new screenplay session: {self.current_session_id}")

            stages = []
            if resume_from_stage > 0:
                # Get existing stages from database
                db_stages = ScreenplayDatabase.get_stages(self.current_session_id)
                stages = [{
                    "name": s['stage_name'], 
                    "status": s['status'], 
                    "content": s.get('content', ''),
                    "page_count": s.get('page_count', 0)
                } for s in db_stages]

            for stage_idx in range(resume_from_stage, len(SCREENPLAY_STAGES)):
                role, stage_name = SCREENPLAY_STAGES[stage_idx]
                
                if stage_idx >= len(stages):
                    stages.append({
                        "name": stage_name, 
                        "status": "active", 
                        "content": "", 
                        "page_count": 0
                    })
                else:
                    stages[stage_idx]["status"] = "active"

                yield f"๐Ÿ”„ Processing {stage_name}...", stages, self.current_session_id
                
                prompt = self.get_stage_prompt(stage_idx, role, query, screenplay_type, 
                                             genre, language, stages)
                stage_content = ""
                
                for chunk in self.call_llm_streaming([{"role": "user", "content": prompt}], 
                                                   role, language):
                    stage_content += chunk
                    stages[stage_idx]["content"] = stage_content
                    if role == "screenwriter":
                        stages[stage_idx]["page_count"] = len(stage_content.split('\n')) / 55
                    yield f"๐Ÿ”„ {stage_name} in progress...", stages, self.current_session_id

                # Process content based on role
                if role == "producer":
                    self._process_producer_content(stage_content)
                elif role == "story_developer":
                    self._process_story_content(stage_content)
                elif role == "character_designer":
                    self._process_character_content(stage_content)
                elif role == "scene_planner":
                    self._process_scene_content(stage_content)

                stages[stage_idx]["status"] = "complete"
                ScreenplayDatabase.save_stage(
                    self.current_session_id, stage_idx, stage_name, role,
                    stage_content, "complete"
                )
                
                yield f"โœ… {stage_name} completed", stages, self.current_session_id

            # Final processing
            final_screenplay = ScreenplayDatabase.get_screenplay_content(self.current_session_id)
            title = self.screenplay_tracker.screenplay_bible.title
            logline = self.screenplay_tracker.screenplay_bible.logline
            
            ScreenplayDatabase.update_final_screenplay(
                self.current_session_id, final_screenplay, title, logline
            )
            
            yield f"โœ… Screenplay completed! {title}", stages, self.current_session_id

        except Exception as e:
            logger.error(f"Screenplay generation error: {e}", exc_info=True)
            yield f"โŒ Error occurred: {e}", stages if 'stages' in locals() else [], self.current_session_id

    def get_stage_prompt(self, stage_idx: int, role: str, query: str, 
                        screenplay_type: str, genre: str, language: str, 
                        stages: List[Dict]) -> str:
        """Generate stage-specific prompt"""
        if stage_idx == 0:  # Producer
            return self.create_producer_prompt(query, screenplay_type, genre, language)
        
        if stage_idx == 1:  # Story Developer
            return self.create_story_developer_prompt(
                stages[0]["content"], query, screenplay_type, genre, language
            )
        
        if stage_idx == 2:  # Character Designer
            return self.create_character_designer_prompt(
                stages[0]["content"], stages[1]["content"], genre, language
            )
        
        if stage_idx == 3:  # Structure Critic
            return self.create_critic_structure_prompt(
                stages[1]["content"], stages[2]["content"], screenplay_type, genre, language
            )
        
        if stage_idx == 4:  # Scene Planner
            return self.create_scene_planner_prompt(
                stages[1]["content"], stages[2]["content"], screenplay_type, genre, language
            )
        
        # Screenwriter acts
        if role == "screenwriter":
            act_mapping = {5: "Act 1", 7: "Act 2A", 9: "Act 2B", 11: "Act 3"}
            if stage_idx in act_mapping:
                act = act_mapping[stage_idx]
                previous_acts = self._get_previous_acts(stages, stage_idx)
                return self.create_screenwriter_prompt(
                    act, stages[4]["content"], stages[2]["content"], 
                    previous_acts, screenplay_type, genre, language
                )
        
        # Script doctor reviews
        if role == "script_doctor":
            act_mapping = {6: "Act 1", 8: "Act 2A", 10: "Act 2B"}
            if stage_idx in act_mapping:
                act = act_mapping[stage_idx]
                act_content = stages[stage_idx-1]["content"]
                return self.create_script_doctor_prompt(act_content, act, genre, language)
        
        # Final reviewer
        if role == "final_reviewer":
            complete_screenplay = ScreenplayDatabase.get_screenplay_content(self.current_session_id)
            return self.create_final_reviewer_prompt(
                complete_screenplay, screenplay_type, genre, language
            )
        
        return ""

    def _get_previous_acts(self, stages: List[Dict], current_idx: int) -> str:
        """Get previous acts content"""
        previous = []
        act_indices = {5: [], 7: [5], 9: [5, 7], 11: [5, 7, 9]}
        
        if current_idx in act_indices:
            for idx in act_indices[current_idx]:
                if idx < len(stages) and stages[idx]["content"]:
                    previous.append(stages[idx]["content"])
        
        return "\n\n---\n\n".join(previous) if previous else ""


    def _extract_field(self, content: str, field_pattern: str) -> Optional[str]:
            """Extract field value from content with improved parsing"""
            pattern = rf'{field_pattern}[:\s]*([^\n]+?)(?=\n[A-Z๊ฐ€-ํžฃ]|$)'
            match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
            if match and match.group(1):
                value = match.group(1).strip()
                value = re.sub(r'\*\*', '', value)           # **bold** ์ œ๊ฑฐ
                value = re.sub(r'^\s*[-โ€ข]\s*', '', value)    # ๊ธ€๋จธ๋ฆฌํ‘œ ์ œ๊ฑฐ
                value = re.sub(r'[,.:;]+$', '', value)       # ํ–‰ ๋ ๊ตฌ๋‘์  ์ œ๊ฑฐ
                return value.strip() if value else None
            return None

    


    def _parse_character_profile(self, content: str, role: str) -> CharacterProfile:
        """Parse character profile from content"""
        logger.debug(f"Parsing character profile for role: {role}")
        logger.debug(f"Content preview: {content[:200]}...")
    
        # 1) ์ด๋ฆ„ ์ถ”์ถœ โ”€ ํŒจํ„ด 3์ข…
        name = f"Character_{role}"  # fallback
        name_patterns = [
            r'(?:์ด๋ฆ„|Name)[:\s]*([^\n,(]+)',         # "์ด๋ฆ„: ํ™๊ธธ๋™"
            r'^\s*[-*โ€ข]\s*([^\n,(]+)',               # "- ํ™๊ธธ๋™"
            r'^([^\n,(]+)'                           # ๋ฌธ๋‹จ ์ฒซ ๋‹จ์–ด
        ]
        for pat in name_patterns:
            m = re.search(pat, content, re.IGNORECASE | re.MULTILINE)
            if m and m.group(1).strip():
                name = re.sub(r'[\*:\s]+', '', m.group(1).strip())  # ๋ถˆํ•„์š” ๊ธฐํ˜ธ ์ œ๊ฑฐ
                break
    
        # 2) ํ•„๋“œ ์ถ”์ถœ์šฉ ํ—ฌํผ
        def extract_clean_field(pats):
            pats = [pats] if isinstance(pats, str) else pats
            for p in pats:
                m = re.search(rf'{p}[:\s]*([^\n*]+?)(?=\n|$)', content,
                              re.IGNORECASE | re.DOTALL)
                if m and m.group(1).strip():
                    v = m.group(1).strip()
                    v = re.sub(r'^[-*โ€ข:\s]+', '', v)   # ๋ฆฌ์ŠคํŠธยท๊ธฐํ˜ธ ์ œ๊ฑฐ
                    v = v.replace('*', '').strip()
                    return v
            return ""
    
        # 3) Personality(์—ฌ๋Ÿฌ ์ค„) ๋”ฐ๋กœ ํŒŒ์‹ฑ
        def extract_traits():
            section = re.search(r'(?:Personality|์„ฑ๊ฒฉ[^\n]*)\n((?:[-*โ€ข].+\n?)+)',
                                content, re.IGNORECASE)
            if not section:
                return []
            traits = [
                re.sub(r'^[-*โ€ข]\s*', '', line.strip())
                for line in section.group(1).splitlines() if line.strip()
            ]
            return traits[:5]
    
        # 4) CharacterProfile ์ƒ์„ฑ
        return CharacterProfile(
            name=name,
            role=role,
            archetype=extract_clean_field(
                [r"์บ๋ฆญํ„ฐ ์•„ํฌํƒ€์ž…", r"Character Archetype", r"Archetype", r"์•„ํฌํƒ€์ž…"]
            ),
            want=extract_clean_field(
                [r"WANT\s*\(์™ธ์  ๋ชฉํ‘œ\)", r"WANT", r"์™ธ์  ๋ชฉํ‘œ", r"External Goal"]
            ),
            need=extract_clean_field(
                [r"NEED\s*\(๋‚ด์  ํ•„์š”\)", r"NEED", r"๋‚ด์  ํ•„์š”", r"Internal Need"]
            ),
            backstory=extract_clean_field(
                [r"๋ฐฑ์Šคํ† ๋ฆฌ", r"Backstory", r"ํ•ต์‹ฌ ์ƒ์ฒ˜", r"Core Wound"]
            ),
            personality=extract_traits(),
            speech_pattern=extract_clean_field(
                [r"๋งํˆฌ.*?ํŒจํ„ด", r"Speech Pattern", r"์–ธ์–ด ํŒจํ„ด", r"๋งํˆฌ"]
            ),
            character_arc=extract_clean_field(
                [r"์บ๋ฆญํ„ฐ ์•„ํฌ", r"Character Arc", r"Arc", r"๋ณ€ํ™”"]
            ),
        )
    
 

    def _extract_personality_traits(self, content: str) -> List[str]:
        """Extract personality traits from content"""
        traits = []
        # Look for personality section with multiple pattern options
        personality_patterns = [
            r"(?:Personality|์„ฑ๊ฒฉ ํŠน์„ฑ|์„ฑ๊ฒฉ)[:\s]*([^\n]+(?:\n(?![\w๊ฐ€-ํžฃ]+:)[^\n]+)*)",
            r"์„ฑ๊ฒฉ[:\s]*(?:\n?[-โ€ข*]\s*[^\n]+)+"
        ]
        
        for pattern in personality_patterns:
            match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
            if match and match.group(1):
                personality_section = match.group(1)
                # Extract individual traits (usually listed)
                trait_lines = personality_section.split('\n')
                for line in trait_lines:
                    line = line.strip()
                    if line and not line.endswith(':'):
                        # Remove list markers
                        trait = re.sub(r'^\s*[-โ€ข*]\s*', '', line)
                        trait = re.sub(r'^\d+\.\s*', '', trait)  # Remove numbered lists
                        if trait and len(trait) > 2:  # Skip very short entries
                            traits.append(trait)
                if traits:  # If we found traits, stop looking
                    break
        
        return traits[:5]  # Limit to 5 traits

    def _process_character_content(self, content: str):
        """Process character designer output with better error handling"""
        try:
            # Extract protagonist
            protagonist_section = self._extract_section(content, r"(?:PROTAGONIST|์ฃผ์ธ๊ณต)")
            if protagonist_section:
                try:
                    protagonist = self._parse_character_profile(protagonist_section, "protagonist")
                    self.screenplay_tracker.add_character(protagonist)
                    ScreenplayDatabase.save_character(self.current_session_id, protagonist)
                except Exception as e:
                    logger.error(f"Error parsing protagonist: {e}")
                    # Create a default protagonist to continue
                    protagonist = CharacterProfile(
                        name="Protagonist",
                        role="protagonist",
                        archetype="Hero",
                        want="To achieve goal",
                        need="To grow",
                        backstory="Unknown",
                        personality=["Determined"],
                        speech_pattern="Normal",
                        character_arc="Growth"
                    )
                    self.screenplay_tracker.add_character(protagonist)
            
            # Extract antagonist
            antagonist_section = self._extract_section(content, r"(?:ANTAGONIST|์ ๋Œ€์ž)")
            if antagonist_section:
                try:
                    antagonist = self._parse_character_profile(antagonist_section, "antagonist")
                    self.screenplay_tracker.add_character(antagonist)
                    ScreenplayDatabase.save_character(self.current_session_id, antagonist)
                except Exception as e:
                    logger.error(f"Error parsing antagonist: {e}")
                    # Create a default antagonist to continue
                    antagonist = CharacterProfile(
                        name="Antagonist",
                        role="antagonist",
                        archetype="Villain",
                        want="To stop protagonist",
                        need="Power",
                        backstory="Unknown",
                        personality=["Ruthless"],
                        speech_pattern="Menacing",
                        character_arc="Downfall"
                    )
                    self.screenplay_tracker.add_character(antagonist)
            
            # Extract supporting characters
            supporting_section = self._extract_section(content, r"(?:SUPPORTING CAST|์กฐ๋ ฅ์ž๋“ค)")
            if supporting_section:
                # Parse multiple supporting characters
                self._parse_supporting_characters(supporting_section)
                
        except Exception as e:
            logger.error(f"Error processing character content: {e}")
            # Continue with default values rather than failing

    def _extract_section(self, content: str, section_pattern: str) -> str:
        """Extract section from content with improved pattern matching"""
        # More flexible section extraction
        patterns = [
            # Pattern 1: Section header followed by content until next major section
            rf'{section_pattern}[:\s]*\n?(.*?)(?=\n\n[A-Z๊ฐ€-ํžฃ]{{2,}}[:\s]|\n\n\d+\.|$)',
            # Pattern 2: Section header with content until next section (alternative)
            rf'{section_pattern}.*?\n((?:.*\n)*?)(?=\n[A-Z๊ฐ€-ํžฃ]{{2,}}:|$)',
            # Pattern 3: More flexible pattern for Korean text
            rf'{section_pattern}[:\s]*\n?((?:[^\n]+\n?)*?)(?=\n\n|\Z)'
        ]
        
        for pattern in patterns:
            match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
            if match and match.group(1):
                section_content = match.group(1).strip()
                if section_content:  # Only return if we got actual content
                    return section_content
        
        return ""

    def _parse_supporting_characters(self, content: str):
        """Parse supporting characters from content"""
        # Split by character markers (numbers or bullets)
        char_sections = re.split(r'\n(?:\d+\.|[-โ€ข*])\s*', content)
        
        for i, section in enumerate(char_sections[1:], 1):  # Skip first empty split
            if section.strip():
                try:
                    # Try multiple name extraction patterns
                    name = None
                    name_patterns = [
                        r"(?:์ด๋ฆ„|Name)[:\s]*([^,\n]+)",
                        r"^([^:\n]+?)(?:\s*[-โ€“]\s*|:\s*)",  # Name at start before dash or colon
                        r"^([๊ฐ€-ํžฃA-Za-z\s]+?)(?:\s*\(|$)"  # Korean/English name before parenthesis
                    ]
                    
                    for pattern in name_patterns:
                        name_match = re.search(pattern, section.strip(), re.IGNORECASE)
                        if name_match and name_match.group(1):
                            name = name_match.group(1).strip()
                            if name and len(name) > 1:
                                break
                    
                    if not name:
                        name = f"Supporting_{i}"
                    
                    role_desc = self._extract_field(section, r"(?:Role|์—ญํ• )[:\s]*") or "supporting"
                    
                    character = CharacterProfile(
                        name=name,
                        role="supporting",
                        archetype=role_desc,
                        want="",
                        need="",
                        backstory=self._extract_field(section, r"(?:Backstory|๋ฐฑ์Šคํ† ๋ฆฌ)[:\s]*") or "",
                        personality=[],
                        speech_pattern="",
                        character_arc=""
                    )
                    
                    self.screenplay_tracker.add_character(character)
                    ScreenplayDatabase.save_character(self.current_session_id, character)
                    
                except Exception as e:
                    logger.warning(f"Error parsing supporting character {i}: {e}")
                    continue

    def _process_producer_content(self, content: str):
        """Process producer output with better extraction"""
        try:
            # Extract title with various formats
            title_patterns = [
                r'(?:TITLE|์ œ๋ชฉ)[:\s]*\*?\*?([^\n*]+)\*?\*?',
                r'\*\*(?:TITLE|์ œ๋ชฉ)\*\*[:\s]*([^\n]+)',
                r'Title[:\s]*([^\n]+)'
            ]
            
            for pattern in title_patterns:
                title_match = re.search(pattern, content, re.IGNORECASE)
                if title_match:
                    self.screenplay_tracker.screenplay_bible.title = title_match.group(1).strip()
                    break
            
            # Extract logline with various formats
            logline_patterns = [
                r'(?:LOGLINE|๋กœ๊ทธ๋ผ์ธ)[:\s]*\*?\*?([^\n]+)',
                r'\*\*(?:LOGLINE|๋กœ๊ทธ๋ผ์ธ)\*\*[:\s]*([^\n]+)',
                r'Logline[:\s]*([^\n]+)'
            ]
            
            for pattern in logline_patterns:
                logline_match = re.search(pattern, content, re.IGNORECASE | re.DOTALL)
                if logline_match:
                    # Get full logline (might be multi-line)
                    logline_text = logline_match.group(1).strip()
                    # Continue reading if it's incomplete
                    if not logline_text.endswith('.'):
                        next_lines = content[logline_match.end():].split('\n')
                        for line in next_lines[:3]:  # Check next 3 lines
                            if line.strip() and not re.match(r'^[A-Z๊ฐ€-ํžฃ\d]', line.strip()):
                                logline_text += ' ' + line.strip()
                            else:
                                break
                    self.screenplay_tracker.screenplay_bible.logline = logline_text
                    break
            
            # Extract genre
            genre_match = re.search(r'(?:Primary Genre|์ฃผ ์žฅ๋ฅด)[:\s]*([^\n]+)', content, re.IGNORECASE)
            if genre_match:
                self.screenplay_tracker.screenplay_bible.genre = genre_match.group(1).strip()
            
            # Save to database
            ScreenplayDatabase.save_screenplay_bible(self.current_session_id, 
                                                   self.screenplay_tracker.screenplay_bible)
                                                   
        except Exception as e:
            logger.error(f"Error processing producer content: {e}")

    def _process_story_content(self, content: str):
        """Process story developer output"""
        # Extract three-act structure
        self.screenplay_tracker.screenplay_bible.three_act_structure = {
            "act1": self._extract_section(content, "ACT 1|์ œ1๋ง‰"),
            "act2a": self._extract_section(content, "ACT 2A|์ œ2๋ง‰A"),
            "act2b": self._extract_section(content, "ACT 2B|์ œ2๋ง‰B"),
            "act3": self._extract_section(content, "ACT 3|์ œ3๋ง‰")
        }
        
        ScreenplayDatabase.save_screenplay_bible(self.current_session_id,
                                               self.screenplay_tracker.screenplay_bible)

    def _process_scene_content(self, content: str):
        """Process scene planner output"""
        # Parse scene breakdown
        scene_pattern = r'(?:Scene|์”ฌ)\s*(\d+).*?(?:INT\.|EXT\.)\s*(.+?)\s*-\s*(\w+)'
        scenes = re.finditer(scene_pattern, content, re.IGNORECASE | re.MULTILINE)
        
        for match in scenes:
            scene_num = int(match.group(1))
            location = match.group(2).strip()
            time_of_day = match.group(3).strip()
            
            # Determine act based on scene number
            act = 1 if scene_num <= 12 else 2 if scene_num <= 35 else 3
            
            scene = SceneBreakdown(
                scene_number=scene_num,
                act=act,
                location=location,
                time_of_day=time_of_day,
                characters=[],  # Would be extracted from content
                purpose="",  # Would be extracted from content
                conflict="",  # Would be extracted from content
                page_count=1.5  # Default estimate
            )
            
            self.screenplay_tracker.add_scene(scene)
            ScreenplayDatabase.save_scene(self.current_session_id, scene)

# --- Utility functions ---
def generate_random_screenplay_theme(screenplay_type: str, genre: str, language: str) -> str:
    """Generate random screenplay theme"""
    try:
        # Log the attempt
        logger.info(f"Generating random theme - Type: {screenplay_type}, Genre: {genre}, Language: {language}")
        
        # Load themes data
        themes_data = load_screenplay_themes_data()
        
        # Select random elements
        import secrets
        situations = themes_data['situations'].get(genre, themes_data['situations']['drama'])
        protagonists = themes_data['protagonists'].get(genre, themes_data['protagonists']['drama'])
        conflicts = themes_data['conflicts'].get(genre, themes_data['conflicts']['drama'])
        
        if not situations or not protagonists or not conflicts:
            logger.error(f"No theme data available for genre {genre}")
            return f"Error: No theme data available for genre {genre}"
        
        situation = secrets.choice(situations)
        protagonist = secrets.choice(protagonists)
        conflict = secrets.choice(conflicts)
        
        logger.info(f"Selected elements - Situation: {situation}, Protagonist: {protagonist}, Conflict: {conflict}")
        
        # Check if API token is valid
        if not FRIENDLI_TOKEN or FRIENDLI_TOKEN == "dummy_token_for_testing":
            logger.warning("No valid API token, returning fallback theme")
            return get_fallback_theme(screenplay_type, genre, language, situation, protagonist, conflict)
        
        # Generate theme using LLM
        system = ScreenplayGenerationSystem()
        
        if language == "Korean":
            prompt = f"""๋‹ค์Œ ์š”์†Œ๋“ค๋กœ {screenplay_type}์šฉ ๋งค๋ ฅ์ ์ธ ์ปจ์…‰์„ ์ƒ์„ฑํ•˜์„ธ์š”:

์ƒํ™ฉ: {situation}
์ฃผ์ธ๊ณต: {protagonist}
๊ฐˆ๋“ฑ: {conflict}
์žฅ๋ฅด: {genre}

๋‹ค์Œ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ:

**์ œ๋ชฉ:** [๋งค๋ ฅ์ ์ธ ์ œ๋ชฉ]

**๋กœ๊ทธ๋ผ์ธ:** [25๋‹จ์–ด ์ด๋‚ด ํ•œ ๋ฌธ์žฅ]

**์ปจ์…‰:** [์ฃผ์ธ๊ณต]์ด(๊ฐ€) [์ƒํ™ฉ]์—์„œ [๊ฐˆ๋“ฑ]์„ ๊ฒช์œผ๋ฉฐ [๋ชฉํ‘œ]๋ฅผ ์ถ”๊ตฌํ•˜๋Š” ์ด์•ผ๊ธฐ.

**๋…ํŠนํ•œ ์š”์†Œ:** [์ด ์ด์•ผ๊ธฐ๋งŒ์˜ ํŠน๋ณ„ํ•œ ์ ]"""
        else:
            prompt = f"""Generate an attractive concept for {screenplay_type} using these elements:

Situation: {situation}
Protagonist: {protagonist}  
Conflict: {conflict}
Genre: {genre}

Format as:

**Title:** [Compelling title]

**Logline:** [One sentence, 25 words max]

**Concept:** A story about [protagonist] who faces [conflict] in [situation] while pursuing [goal].

**Unique Element:** [What makes this story special]"""

        messages = [{"role": "user", "content": prompt}]
        
        # Call LLM with error handling
        logger.info("Calling LLM for theme generation...")
        
        generated_theme = ""
        error_occurred = False
        
        # Use streaming to get the response
        for chunk in system.call_llm_streaming(messages, "producer", language):
            if chunk.startswith("โŒ"):
                logger.error(f"LLM streaming error: {chunk}")
                error_occurred = True
                break
            generated_theme += chunk
        
        # If error occurred or no content generated, use fallback
        if error_occurred or not generated_theme.strip():
            logger.warning("LLM call failed or empty response, using fallback theme")
            return get_fallback_theme(screenplay_type, genre, language, situation, protagonist, conflict)
        
        logger.info(f"Successfully generated theme of length: {len(generated_theme)}")
        
        # Extract metadata
        metadata = {
            'title': extract_title_from_theme(generated_theme),
            'logline': extract_logline_from_theme(generated_theme),
            'protagonist': protagonist,
            'conflict': conflict,
            'situation': situation,
            'tags': [genre, screenplay_type]
        }
        
        # Save to database
        try:
            theme_id = ScreenplayDatabase.save_random_theme(
                generated_theme, screenplay_type, genre, language, metadata
            )
            logger.info(f"Saved theme with ID: {theme_id}")
        except Exception as e:
            logger.error(f"Failed to save theme to database: {e}")
        
        return generated_theme
        
    except Exception as e:
        logger.error(f"Theme generation error: {str(e)}")
        import traceback
        logger.error(traceback.format_exc())
        return f"Error generating theme: {str(e)}"

def get_fallback_theme(screenplay_type: str, genre: str, language: str, 
                      situation: str, protagonist: str, conflict: str) -> str:
    """Generate fallback theme without LLM"""
    if language == "Korean":
        return f"""**์ œ๋ชฉ:** {protagonist}์˜ ์„ ํƒ

**๋กœ๊ทธ๋ผ์ธ:** {situation}์— ๊ฐ‡ํžŒ {protagonist}๊ฐ€ {conflict}์— ๋งž์„œ๋ฉฐ ์ƒ์กด์„ ์œ„ํ•ด ์‹ธ์šด๋‹ค.

**์ปจ์…‰:** {protagonist}๊ฐ€ {situation}์—์„œ {conflict}์„ ๊ฒช์œผ๋ฉฐ ์ž์‹ ์˜ ํ•œ๊ณ„๋ฅผ ๊ทน๋ณตํ•˜๋Š” ์ด์•ผ๊ธฐ.

**๋…ํŠนํ•œ ์š”์†Œ:** {genre} ์žฅ๋ฅด์˜ ์ „ํ†ต์  ์š”์†Œ๋ฅผ ํ˜„๋Œ€์ ์œผ๋กœ ์žฌํ•ด์„ํ•œ ์ž‘ํ’ˆ."""
    else:
        return f"""**Title:** The {protagonist.title()}'s Choice

**Logline:** When trapped in {situation}, a {protagonist} must face {conflict} to survive.

**Concept:** A story about a {protagonist} who faces {conflict} in {situation} while discovering their true strength.

**Unique Element:** A fresh take on {genre} genre conventions with contemporary relevance."""

def load_screenplay_themes_data() -> Dict:
   """Load screenplay themes data"""
   return {
       'situations': {
           'action': ['hostage crisis', 'heist gone wrong', 'revenge mission', 'race against time'],
           'thriller': ['false accusation', 'witness protection', 'conspiracy uncovered', 'identity theft'],
           'drama': ['family reunion', 'terminal diagnosis', 'divorce proceedings', 'career crossroads'],
           'comedy': ['mistaken identity', 'wedding disaster', 'workplace chaos', 'odd couple roommates'],
           'horror': ['isolated location', 'ancient curse', 'home invasion', 'supernatural investigation'],
           'sci-fi': ['first contact', 'time loop', 'AI awakening', 'space colony crisis'],
           'romance': ['second chance', 'enemies to lovers', 'long distance', 'forbidden love']
       },
       'protagonists': {
           'action': ['ex-soldier', 'undercover cop', 'skilled thief', 'reluctant hero'],
           'thriller': ['investigative journalist', 'wrongly accused person', 'FBI agent', 'whistleblower'],
           'drama': ['single parent', 'recovering addict', 'immigrant', 'caregiver'],
           'comedy': ['uptight professional', 'slacker', 'fish out of water', 'eccentric artist'],
           'horror': ['skeptical scientist', 'final girl', 'paranormal investigator', 'grieving parent'],
           'sci-fi': ['astronaut', 'AI researcher', 'time traveler', 'colony leader'],
           'romance': ['workaholic', 'hopeless romantic', 'cynical divorce lawyer', 'small town newcomer']
       },
       'conflicts': {
           'action': ['stop the villain', 'save the hostages', 'prevent disaster', 'survive pursuit'],
           'thriller': ['prove innocence', 'expose truth', 'stay alive', 'protect loved ones'],
           'drama': ['reconcile past', 'find purpose', 'heal relationships', 'accept change'],
           'comedy': ['save the business', 'win the competition', 'fool everyone', 'find love'],
           'horror': ['survive the night', 'break the curse', 'escape the monster', 'save the town'],
           'sci-fi': ['save humanity', 'prevent paradox', 'stop the invasion', 'preserve identity'],
           'romance': ['overcome differences', 'choose between options', 'trust again', 'follow heart']
       }
   }

def extract_title_from_theme(theme_text: str) -> str:
   """Extract title from generated theme"""
   match = re.search(r'\*\*(?:Title|์ œ๋ชฉ):\*\*\s*(.+)', theme_text, re.IGNORECASE)
   return match.group(1).strip() if match else ""

def extract_logline_from_theme(theme_text: str) -> str:
   """Extract logline from generated theme"""
   match = re.search(r'\*\*(?:Logline|๋กœ๊ทธ๋ผ์ธ):\*\*\s*(.+)', theme_text, re.IGNORECASE)
   return match.group(1).strip() if match else ""

import re

def format_screenplay_display(screenplay_text: str) -> str:
    """Convert raw screenplay text to a nicely formatted Markdown preview."""

    if not screenplay_text:
        return "No screenplay content yet."

    # 1) ์ œ๋ชฉ ์˜์—ญ
    formatted = "# ๐ŸŽฌ Screenplay\n\n"

    # 2) ์”ฌ ํ—ค๋”ฉ(INT./EXT. ๋ผ์ธ) ๋ณผ๋“œ ์ฒ˜๋ฆฌ
    #    - ^  : ํ–‰์˜ ์‹œ์ž‘
    #    - .* : ํ–‰ ์ „์ฒด
    #    - re.MULTILINE : ๊ฐ ์ค„๋งˆ๋‹ค ^ $๊ฐ€ ๋™์ž‘
    formatted_text = re.sub(
        r'^(INT\.|EXT\.).*$',             # ์บก์ฒ˜: INT. ๋˜๋Š” EXT.์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ•œ ์ค„
        r'**\g<0>**',                     # ์ „์ฒด ํ–‰์„ ๊ตต๊ฒŒ
        screenplay_text,
        flags=re.MULTILINE
    )

    # 3) ๋Œ€๋ฌธ์ž ์ „์›(์ธ๋ฌผ ์ด๋ฆ„) ๋ณผ๋“œ ์ฒ˜๋ฆฌ
    #    - [A-Z][A-Z\s]+$ : ALL-CAPS ๊ธ€์ž์™€ ๊ณต๋ฐฑ๋งŒ์œผ๋กœ ์ด๋ค„์ง„ ํ–‰
    formatted_text = re.sub(
        r'^([A-Z][A-Z\s]+)$',
        r'**\g<0>**',
        formatted_text,
        flags=re.MULTILINE
    )

    # 4) ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด INT./EXT. ๋’ค์— ๋นˆ ์ค„ ์‚ฝ์ž…
    lines = formatted_text.splitlines()
    pretty_lines = []
    for line in lines:
        pretty_lines.append(line)
        if line.startswith("**INT.") or line.startswith("**EXT."):
            pretty_lines.append("")        # ๋นˆ ์ค„ ์ถ”๊ฐ€

    formatted += "\n".join(pretty_lines)

    # 5) ํŽ˜์ด์ง€ ์ˆ˜(์Šคํฌ๋ฆฝํŠธ ๊ทœ์น™: 1 ํŽ˜์ด์ง€ โ‰ˆ 55 ๋ผ์ธ) ๊ณ„์‚ฐ
    page_count = len(screenplay_text.splitlines()) / 55
    formatted = f"**Total Pages: {page_count:.1f}**\n\n" + formatted
    return formatted


def format_stages_display(stages: List[Dict]) -> str:
   """Format stages display for screenplay"""
   markdown = "## ๐ŸŽฌ Production Progress\n\n"
   
   # Progress summary
   completed = sum(1 for s in stages if s.get('status') == 'complete')
   total = len(stages)
   markdown += f"**Progress: {completed}/{total} stages complete**\n\n"
   
   # Page count if available
   total_pages = sum(s.get('page_count', 0) for s in stages if s.get('page_count'))
   if total_pages > 0:
       markdown += f"**Current Page Count: {total_pages:.1f} pages**\n\n"
   
   markdown += "---\n\n"
   
   # Stage details
   current_act = None
   for i, stage in enumerate(stages):
       status_icon = "โœ…" if stage['status'] == 'complete' else "๐Ÿ”„" if stage['status'] == 'active' else "โณ"
       
       # Group by acts
       if 'Act' in stage.get('name', ''):
           act_match = re.search(r'Act (\w+)', stage['name'])
           if act_match and act_match.group(1) != current_act:
               current_act = act_match.group(1)
               markdown += f"\n### ๐Ÿ“„ Act {current_act}\n\n"
       
       markdown += f"{status_icon} **{stage['name']}**"
       
       if stage.get('page_count', 0) > 0:
           markdown += f" ({stage['page_count']:.1f} pages)"
       
       markdown += "\n"
       
       if stage['content'] and stage['status'] == 'complete':
           preview_length = 200
           preview = stage['content'][:preview_length] + "..." if len(stage['content']) > preview_length else stage['content']
           markdown += f"> {preview}\n\n"
       elif stage['status'] == 'active':
           markdown += "> *In progress...*\n\n"
   
   return markdown

def process_query(query: str, screenplay_type: str, genre: str, language: str, 
                session_id: Optional[str] = None) -> Generator[Tuple[str, str, str, str], None, None]:
   """Main query processing function"""
   if not query.strip():
       yield "", "", "โŒ Please enter a screenplay concept.", session_id
       return
   
   system = ScreenplayGenerationSystem()
   stages_markdown = ""
   screenplay_display = ""
   
   for status, stages, current_session_id in system.process_screenplay_stream(
       query, screenplay_type, genre, language, session_id
   ):
       stages_markdown = format_stages_display(stages)
       
       # Get screenplay content when available
       if stages and all(s.get("status") == "complete" for s in stages[-4:]):
           screenplay_text = ScreenplayDatabase.get_screenplay_content(current_session_id)
           screenplay_display = format_screenplay_display(screenplay_text)
       
       yield stages_markdown, screenplay_display, status or "๐Ÿ”„ Processing...", current_session_id

def get_active_sessions() -> List[str]:
   """Get active screenplay sessions"""
   sessions = ScreenplayDatabase.get_active_sessions()
   return [
       f"{s['session_id'][:8]}... - {s.get('title', s['user_query'][:30])}... "
       f"({s['screenplay_type']}/{s['genre']}) [{s['total_pages']:.1f} pages]"
       for s in sessions
   ]

def export_screenplay_pdf(screenplay_text: str, title: str, session_id: str) -> str:
   """Export screenplay to PDF format"""
   # This would use a library like reportlab to create industry-standard PDF
   # For now, returning a placeholder
   pdf_path = f"screenplay_{session_id[:8]}.pdf"
   # PDF generation logic would go here
   return pdf_path

def export_screenplay_fdx(screenplay_text: str, title: str, session_id: str) -> str:
   """Export to Final Draft format"""
   # This would create .fdx XML format
   fdx_path = f"screenplay_{session_id[:8]}.fdx"
   # FDX generation logic would go here
   return fdx_path

def download_screenplay(screenplay_text: str, format_type: str, title: str, 
                      session_id: str) -> Optional[str]:
   """Generate screenplay download file"""
   if not screenplay_text or not session_id:
       return None
   
   timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
   
   try:
       if format_type == "PDF":
           return export_screenplay_pdf(screenplay_text, title, session_id)
       elif format_type == "FDX":
           return export_screenplay_fdx(screenplay_text, title, session_id)
       elif format_type == "FOUNTAIN":
           filepath = f"screenplay_{session_id[:8]}_{timestamp}.fountain"
           with open(filepath, 'w', encoding='utf-8') as f:
               f.write(screenplay_text)
           return filepath
       else:  # TXT
           filepath = f"screenplay_{session_id[:8]}_{timestamp}.txt"
           with open(filepath, 'w', encoding='utf-8') as f:
               f.write(f"Title: {title}\n")
               f.write("=" * 50 + "\n\n")
               f.write(screenplay_text)
           return filepath
   except Exception as e:
       logger.error(f"Download generation failed: {e}")
       return None

# Create Gradio interface
def create_interface():
   """Create Gradio interface for screenplay generation"""
   
   css = """
   .main-header {
       text-align: center;
       margin-bottom: 2rem;
       padding: 2rem;
       background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
       border-radius: 10px;
       color: white;
   }
   
   .header-title {
       font-size: 3rem;
       margin-bottom: 1rem;
       background: linear-gradient(45deg, #f39c12, #e74c3c);
       -webkit-background-clip: text;
       -webkit-text-fill-color: transparent;
   }
   
   .header-description {
       font-size: 1.1rem;
       opacity: 0.9;
       line-height: 1.6;
   }
   
   .type-selector {
       display: flex;
       gap: 1rem;
       margin: 1rem 0;
   }
   
   .type-card {
       flex: 1;
       padding: 1rem;
       border: 2px solid #ddd;
       border-radius: 8px;
       cursor: pointer;
       transition: all 0.3s;
   }
   
   .type-card:hover {
       border-color: #f39c12;
       transform: translateY(-2px);
   }
   
   .type-card.selected {
       border-color: #e74c3c;
       background: #fff5f5;
   }
   
   #stages-display {
       max-height: 600px;
       overflow-y: auto;
       padding: 1rem;
       background: #f8f9fa;
       border-radius: 8px;
   }
   
   #screenplay-output {
       font-family: 'Courier New', monospace;
       white-space: pre-wrap;
       background: white;
       padding: 2rem;
       border: 1px solid #ddd;
       border-radius: 8px;
       max-height: 800px;
       overflow-y: auto;
   }
   
   .genre-grid {
       display: grid;
       grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
       gap: 0.5rem;
       margin: 1rem 0;
   }
   
   .genre-btn {
       padding: 0.75rem;
       border: 2px solid #e0e0e0;
       background: white;
       border-radius: 8px;
       cursor: pointer;
       transition: all 0.3s;
       text-align: center;
   }
   
   .genre-btn:hover {
       border-color: #f39c12;
       background: #fffbf0;
   }
   
   .genre-btn.selected {
       border-color: #e74c3c;
       background: #fff5f5;
       font-weight: bold;
   }
   """
   
   with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Screenplay Generator") as interface:
       gr.HTML("""
       <div class="main-header">
           <h1 class="header-title">๐ŸŽฌ AI Screenplay Generator</h1>
           <p class="header-description">
               Transform your ideas into professional screenplays for films, TV shows, and streaming series.
               Using industry-standard format and story structure to create compelling, producible scripts.
           </p>
       </div>
       """)
       
       # State management
       current_session_id = gr.State(None)
       
       with gr.Tabs():
           # Main Writing Tab
           with gr.Tab("โœ๏ธ Write Screenplay"):
               with gr.Row():
                   with gr.Column(scale=3):
                       query_input = gr.Textbox(
                           label="Screenplay Concept",
                           placeholder="""Describe your screenplay idea. For example:
- A detective with memory loss must solve their own attempted murder
- Two rival food truck owners forced to work together to save the city food festival  
- A space station AI develops consciousness during a critical mission
- A family reunion turns into a murder mystery during a hurricane

The more specific your concept, the better the screenplay will be tailored to your vision.""",
                           lines=6
                       )
                   
                   with gr.Column(scale=1):
                       screenplay_type = gr.Radio(
                           choices=list(SCREENPLAY_LENGTHS.keys()),
                           value="movie",
                           label="Screenplay Type",
                           info="Choose your format"
                       )
                       
                       genre_select = gr.Dropdown(
                           choices=list(GENRE_TEMPLATES.keys()),
                           value="drama",
                           label="Primary Genre",
                           info="Select main genre"
                       )
                       
                       language_select = gr.Radio(
                           choices=["English", "Korean"],
                           value="English",
                           label="Language"
                       )
               
               with gr.Row():
                   random_btn = gr.Button("๐ŸŽฒ Random Concept", scale=1)
                   clear_btn = gr.Button("๐Ÿ—‘๏ธ Clear", scale=1)
                   submit_btn = gr.Button("๐ŸŽฌ Start Writing", variant="primary", scale=2)
               
               status_text = gr.Textbox(
                   label="Status",
                   interactive=False,
                   value="Ready to create your screenplay"
               )
               
               # Session management
               with gr.Group():
                   gr.Markdown("### ๐Ÿ“ Saved Projects")
                   with gr.Row():
                       session_dropdown = gr.Dropdown(
                           label="Active Sessions",
                           choices=[],
                           interactive=True,
                           scale=3
                       )
                       refresh_btn = gr.Button("๐Ÿ”„", scale=1)
                       resume_btn = gr.Button("๐Ÿ“‚ Load", scale=1)
               
               # Output displays
               with gr.Row():
                   with gr.Column():
                       with gr.Tab("๐ŸŽญ Writing Progress"):
                           stages_display = gr.Markdown(
                               value="*Your screenplay journey will unfold here...*",
                               elem_id="stages-display"
                           )
                       
                       with gr.Tab("๐Ÿ“„ Screenplay"):
                           screenplay_output = gr.Markdown(
                               value="*Your formatted screenplay will appear here...*",
                               elem_id="screenplay-output"
                           )
                           
                           with gr.Row():
                               format_select = gr.Radio(
                                   choices=["PDF", "FDX", "FOUNTAIN", "TXT"],
                                   value="PDF",
                                   label="Export Format"
                               )
                               download_btn = gr.Button("๐Ÿ“ฅ Download Screenplay", variant="secondary")
                           
                           download_file = gr.File(
                               label="Download",
                               visible=False
                           )
               
               # Examples
               gr.Examples(
                   examples=[
                       ["A burned-out teacher discovers her students are being replaced by AI duplicates"],
                       ["Two funeral home employees accidentally release a ghost who helps them solve murders"],
                       ["A time-loop forces a wedding planner to relive the worst wedding until they find true love"],
                       ["An astronaut returns to Earth to find everyone has forgotten space exists"],
                       ["A support group for reformed villains must save the city when heroes disappear"],
                       ["A food critic loses their sense of taste and teams up with a street food vendor"]
                   ],
                   inputs=query_input,
                   label="๐Ÿ’ก Example Concepts"
               )
           
           # Screenplay Library Tab
           with gr.Tab("๐Ÿ“š Concept Library"):
               gr.Markdown("""
               ### ๐ŸŽฒ Random Screenplay Concepts
               
               Browse through AI-generated screenplay concepts. Each concept includes a title, logline, and brief setup.
               """)
               
               library_display = gr.HTML(
                   value="<p>Library feature coming soon...</p>"
               )
       
       # Event handlers
       def handle_submit(query, s_type, genre, lang, session_id):
           if not query:
               yield "", "", "โŒ Please enter a concept", session_id
               return
           
           yield from process_query(query, s_type, genre, lang, session_id)
       
       def handle_random(s_type, genre, lang):
           return generate_random_screenplay_theme(s_type, genre, lang)
       
       def handle_download(screenplay_text, format_type, session_id):
           if not screenplay_text or not session_id:
               return gr.update(visible=False)
           
           # Get title from database
           session = ScreenplayDatabase.get_session(session_id)
           title = session.get('title', 'Untitled') if session else 'Untitled'
           
           file_path = download_screenplay(screenplay_text, format_type, title, session_id)
           if file_path and os.path.exists(file_path):
               return gr.update(value=file_path, visible=True)
           return gr.update(visible=False)
       
       # Connect events
       submit_btn.click(
           fn=handle_submit,
           inputs=[query_input, screenplay_type, genre_select, language_select, current_session_id],
           outputs=[stages_display, screenplay_output, status_text, current_session_id]
       )
       
       random_btn.click(
           fn=handle_random,
           inputs=[screenplay_type, genre_select, language_select],
           outputs=[query_input]
       )
       
       clear_btn.click(
           fn=lambda: ("", "", "Ready to create your screenplay", None),
           outputs=[stages_display, screenplay_output, status_text, current_session_id]
       )
       
       refresh_btn.click(
           fn=get_active_sessions,
           outputs=[session_dropdown]
       )
       
       download_btn.click(
           fn=handle_download,
           inputs=[screenplay_output, format_select, current_session_id],
           outputs=[download_file]
       )
       
       # Load sessions on start
       interface.load(
           fn=get_active_sessions,
           outputs=[session_dropdown]
       )
   
   return interface

# Main function
if __name__ == "__main__":
   logger.info("Screenplay Generator Starting...")
   logger.info("=" * 60)
   
   # Environment check
   logger.info(f"API Endpoint: {API_URL}")
   logger.info("Screenplay Types Available:")
   for s_type, info in SCREENPLAY_LENGTHS.items():
       logger.info(f"  - {s_type}: {info['description']}")
   logger.info(f"Genres: {', '.join(GENRE_TEMPLATES.keys())}")
   
   if BRAVE_SEARCH_API_KEY:
       logger.info("Web search enabled for market research.")
   else:
       logger.warning("Web search disabled.")
   
   logger.info("=" * 60)
   
   # Initialize database
   logger.info("Initializing database...")
   ScreenplayDatabase.init_db()
   logger.info("Database initialization complete.")
   
   # Create and launch interface
   interface = create_interface()
   
   interface.launch(
       server_name="0.0.0.0",
       server_port=7860,
       share=False,
       debug=True
   )