File size: 69,924 Bytes
3263f99
 
 
 
 
823995d
 
620c39a
823995d
0cb3f50
 
17509ae
 
 
 
 
 
 
 
0cb3f50
823995d
0cb3f50
823995d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c653f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823995d
 
 
 
 
 
 
 
 
 
 
524adf8
 
ac490ae
 
56d0d35
 
 
d9830ba
3870ab8
823995d
 
 
 
 
 
 
 
 
 
 
 
 
 
ae013d7
823995d
 
 
 
c3d93b2
c27967a
21410d4
a4ad051
 
823995d
 
 
 
 
 
 
 
 
 
dd55c75
 
9d2278b
823995d
5ed7f4f
 
 
 
 
 
 
 
 
 
823995d
 
 
 
6277fde
 
823995d
9dc14f3
823995d
 
faa0818
508dba9
 
773a66f
 
 
53690fe
773a66f
3cd4ac2
823995d
942ce2d
dd55c75
 
1af2cba
 
00d8c9c
cf01708
1af2cba
d9830ba
1af2cba
620c39a
123ce36
e136ba6
 
93d6e2a
c27967a
 
 
bf6dd9c
 
c178ebf
 
17aa0a6
 
c61a638
 
ebcbf92
76f08bf
a515c85
1eddcb1
ed19d20
 
 
1eddcb1
e1e29fa
1108a81
bcef903
6277fde
 
59268c1
 
 
 
 
 
524adf8
 
 
 
 
 
ac490ae
 
59268c1
 
823995d
 
 
 
 
 
d553264
 
 
 
 
 
 
 
 
 
823995d
 
 
9dc14f3
823995d
 
 
 
6277fde
 
 
823995d
 
 
 
 
 
 
 
 
bcef903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823995d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee830df
 
 
 
 
 
 
 
 
 
 
8aac039
156a410
 
 
ee830df
 
 
8aac039
 
 
 
 
 
ee830df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823995d
 
 
3870ab8
823995d
 
 
 
888567e
 
 
 
 
 
ba9e4dc
 
 
 
 
 
 
 
 
 
 
 
823995d
3870ab8
 
 
 
 
 
 
 
 
 
 
823995d
 
3469841
 
7312678
 
823995d
 
 
 
 
ac490ae
3f0189a
b120b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f0189a
 
c4aaf37
 
bef0468
c4aaf37
 
3f0189a
 
 
 
 
 
 
 
c4aaf37
 
b120b11
 
 
 
 
 
 
 
 
 
 
 
3f0189a
823995d
cd63a70
 
 
 
 
 
 
b120b11
 
 
 
 
 
 
 
 
 
 
cd63a70
3cd4ac2
bac5969
d8ac125
 
 
 
56d0d35
bac5969
bef0468
 
 
 
823995d
 
 
 
 
 
ee830df
 
 
 
 
 
 
 
 
 
 
 
8aac039
 
 
 
 
 
e21b437
 
 
 
 
8aac039
e21b437
ee830df
 
 
 
823995d
 
faa0818
 
 
 
ce6bce9
 
a4ad87e
 
482a617
 
faa0818
3c04db5
 
 
 
1e26539
3c04db5
 
 
 
 
 
 
 
 
 
 
4f3f11e
1e26539
 
3c04db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3589ddc
3c04db5
 
 
 
 
 
 
 
 
 
 
 
 
 
7738f48
 
 
3c04db5
 
faa0818
 
a8323ae
 
faa0818
0b4ac11
 
 
 
 
 
 
 
9cea1a9
0b4ac11
 
 
 
482a617
0b4ac11
 
 
 
 
 
 
 
9cea1a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b4ac11
 
 
 
 
 
 
 
9cea1a9
 
0b4ac11
 
9cea1a9
 
1bcca6b
 
 
 
 
 
 
 
 
 
 
 
 
9cea1a9
1bcca6b
 
 
 
 
 
 
 
 
 
 
0b4ac11
3c04db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97473f7
 
 
 
fda4f62
 
97473f7
 
 
 
 
 
 
 
8eab9a4
 
97473f7
 
 
 
 
 
8eab9a4
 
 
 
 
97473f7
 
 
 
 
 
 
 
 
 
 
 
 
8eab9a4
 
 
 
 
 
 
 
 
 
 
 
 
 
97473f7
 
 
9ecdeca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97473f7
 
 
8eab9a4
 
 
 
 
 
97473f7
 
 
8eab9a4
 
 
b655855
 
 
fe2105e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fda4f62
 
74c08b2
 
 
 
 
fe2105e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ecdeca
 
 
 
 
 
74c08b2
 
 
9ecdeca
 
 
 
 
 
74c08b2
 
 
9ecdeca
 
 
 
 
 
fe2105e
 
 
 
 
 
fda4f62
 
fe2105e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ecdeca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb3c912
 
 
 
 
 
 
 
 
 
 
 
 
 
0783b5b
 
 
 
 
bb3c912
 
 
 
 
 
 
 
 
 
 
 
 
 
ace569a
bb3c912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0783b5b
 
 
 
 
 
 
 
 
bb3c912
 
0783b5b
 
 
 
 
 
bb3c912
0783b5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b0e325
 
 
 
 
 
4bf5de8
1e26539
 
 
 
 
 
 
 
 
 
7b0e325
 
 
 
 
50ae26a
 
 
 
 
 
 
 
 
 
 
 
c27967a
 
 
 
 
 
 
 
 
508dba9
 
 
 
773a66f
 
 
 
1af2cba
 
00d8c9c
1af2cba
faa0818
a8323ae
cb61e26
123ce36
d9830ba
c27967a
 
17aa0a6
 
bf6dd9c
 
 
508dba9
773a66f
 
 
 
 
 
 
 
 
1af2cba
00d8c9c
1af2cba
 
faa0818
a8323ae
747e93d
cb61e26
d9830ba
c27967a
 
17aa0a6
 
bf6dd9c
773a66f
823995d
77bce56
 
 
 
 
620c39a
123ce36
77bce56
 
d9830ba
77bce56
 
 
 
 
 
 
 
cb61e26
bf6dd9c
17aa0a6
 
77bce56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9dc14f3
 
 
 
 
 
d9830ba
9dc14f3
 
 
 
 
 
 
 
d6d9180
620c39a
123ce36
cb61e26
c27967a
 
bf6dd9c
9dc14f3
 
 
 
 
 
 
d9830ba
9dc14f3
 
 
 
 
 
 
 
 
 
cb61e26
 
 
d6d9180
a8323ae
747e93d
 
 
c27967a
 
 
bf6dd9c
 
c27967a
9dc14f3
 
 
 
 
2bdd0f8
 
 
 
 
 
 
 
1af2cba
 
 
 
 
 
 
 
 
 
d437b1b
 
 
97da3cc
 
cf01708
97da3cc
 
 
 
 
 
 
 
823995d
 
 
 
 
 
bcef903
 
 
 
 
823995d
 
 
9d2278b
 
 
 
 
2401a70
 
 
823995d
 
 
942ce2d
 
823995d
 
 
 
942ce2d
 
 
 
 
 
 
fe7c6a8
 
 
05c0d5d
 
 
 
 
 
 
dcb21e8
 
 
 
 
942ce2d
 
 
 
 
 
7234d73
942ce2d
 
 
7234d73
 
 
 
 
 
 
 
 
 
 
 
6d3b450
 
 
 
 
 
93d6e2a
c27967a
 
bf6dd9c
17aa0a6
 
c61a638
 
ebcbf92
6d3b450
7234d73
 
492d482
 
 
 
 
 
 
 
 
 
 
 
 
 
d659d3a
 
492d482
 
 
 
 
 
 
 
 
 
 
 
 
d9830ba
492d482
 
 
 
 
 
e136ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d659d3a
 
 
 
 
 
 
 
 
d9830ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823995d
 
 
 
 
 
7234d73
d7919ff
 
 
 
 
 
 
 
 
 
 
 
 
 
105fe64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9740a0d
 
 
 
 
4967544
 
 
9740a0d
 
 
 
 
4967544
 
 
9740a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c600d0
 
 
 
a8dfb8d
 
 
 
 
 
 
7c600d0
 
a8dfb8d
 
7c600d0
 
a8dfb8d
 
5ed7f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf6dd9c
 
 
 
 
 
 
4967544
 
 
 
 
 
 
 
bf6dd9c
 
 
 
 
 
 
dcb21e8
 
 
 
 
bf6dd9c
 
682a86e
 
bf6dd9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
682a86e
bf6dd9c
682a86e
bf6dd9c
 
 
682a86e
 
 
 
 
 
5ed7f4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe2105e
 
 
 
 
 
 
76f08bf
 
 
449572b
fe2105e
76f08bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dec42d9
 
76f08bf
f985d98
 
76f08bf
 
dec42d9
 
 
 
 
 
f985d98
76f08bf
 
34737a6
76f08bf
 
 
 
 
 
 
942e9df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43c6b76
 
 
 
 
942e9df
43c6b76
 
 
 
942e9df
 
 
 
823995d
 
 
b6f2389
 
 
 
01fd7e2
c422beb
b6f2389
823995d
01fd7e2
 
 
 
 
 
 
942ce2d
 
 
 
 
 
 
bb9bf66
 
 
01fd7e2
dd55c75
942ce2d
 
 
 
 
01fd7e2
 
 
55c47d6
 
 
 
 
 
 
 
 
 
7143d05
55c47d6
e15c631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508dba9
 
e15c631
fae5def
 
 
 
 
 
 
 
 
 
 
 
 
 
823995d
 
 
 
 
 
a515c85
 
a4ad051
8722248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ad051
a515c85
3f88527
a515c85
 
c477b16
 
a515c85
 
 
 
 
 
 
a4ad051
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8722248
 
 
 
 
 
a515c85
 
 
 
 
 
 
482a617
a515c85
 
c477b16
 
 
 
92e8f98
c477b16
 
494db15
 
c9b34e9
fc41158
56d0d35
 
 
 
 
 
 
 
 
 
 
 
a515c85
 
 
 
c477b16
 
 
 
 
 
 
 
 
 
 
a515c85
 
 
 
 
c477b16
 
 
a515c85
 
 
 
 
 
 
 
 
 
 
 
c477b16
 
 
 
a515c85
 
 
 
482a617
a515c85
 
e1e29fa
 
a515c85
 
 
 
b959d2d
 
 
 
 
 
 
 
 
 
 
 
 
 
c477b16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59268c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c477b16
 
 
 
 
 
 
 
 
 
 
 
 
 
fc41158
 
c477b16
fc41158
c477b16
 
 
 
 
 
 
 
 
56d0d35
 
 
 
 
0ed74ae
 
 
 
 
 
 
 
 
1e6ba7c
0ed74ae
 
 
 
 
 
 
 
 
1e6ba7c
 
 
0ed74ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e6ba7c
0ed74ae
 
 
 
 
 
 
 
 
 
 
 
 
 
1e6ba7c
0ed74ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e6ba7c
0ed74ae
 
 
 
 
 
 
 
 
 
 
 
a43a939
 
 
 
 
 
 
 
 
 
 
0ed74ae
 
 
b7d99a3
 
 
 
 
823995d
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
export * from "./types";
export * from "./auth";
export * from "./billing";
export * from "./projects";
export * from "./enterprise";

import axios from "axios";
import type { VideoStyleId } from "../constants/videoStyles";

// In production, VITE_BACKEND_URL points to the Cloud Run backend.
// In local dev it's empty β€” Vite proxy handles /api and /media routing.
const viteEnv =
  typeof import.meta !== "undefined" ? import.meta.env : undefined;
const processEnv =
  typeof globalThis !== "undefined" && "process" in globalThis
    ? (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env
    : undefined;

export const BACKEND_URL = viteEnv?.VITE_BACKEND_URL || processEnv?.VITE_BACKEND_URL || "";

const api = axios.create({
  baseURL: `${BACKEND_URL}/api`,
  headers: {
    "Content-Type": "application/json",
  },
});

// ─── Auth interceptor ─────────────────────────────────────

api.interceptors.request.use((config) => {
  const token = localStorage.getItem("b2v_token");
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

api.interceptors.response.use(
  (res) => res,
  (err) => {
    if (err.response?.status === 401) {
      localStorage.removeItem("b2v_token");
      localStorage.removeItem("b2v_user");
      // Only redirect if not already on landing/login page
      if (
        window.location.pathname !== "/" &&
        window.location.pathname !== "/pricing"
      ) {
        window.location.href = "/";
      }
    }
    return Promise.reject(err);
  }
);

/** Public JSON client β€” no auth interceptor so 401 from gates (e.g. template studio) does not log the user out. */
const publicApi = axios.create({
  baseURL: `${BACKEND_URL}/api`,
  headers: {
    "Content-Type": "application/json",
  },
});

export interface TemplateStudioAuthStatusResponse {
  gated: boolean;
}

export interface TemplateStudioAuthVerifyResponse {
  ok: boolean;
  gated: boolean;
}

export async function getTemplateStudioAuthStatus(): Promise<TemplateStudioAuthStatusResponse> {
  const res = await publicApi.get<TemplateStudioAuthStatusResponse>("/template-studio/auth/status");
  return res.data;
}

export async function verifyTemplateStudioPassword(
  password: string
): Promise<TemplateStudioAuthVerifyResponse> {
  const res = await publicApi.post<TemplateStudioAuthVerifyResponse>("/template-studio/auth/verify", {
    password,
  });
  return res.data;
}

// ─── Types ────────────────────────────────────────────────

export interface UserInfo {
  id: number;
  email: string;
  name: string;
  picture: string | null;
  plan: string;
  videos_used_this_period: number;
  video_limit: number;
  can_create_video: boolean;
  /** Per-user, non-expirable pool of AI-assisted edits (+20 per purchased video). */
  ai_edit_credits: number;
  /** Remaining monthly AI-edit allowance for this plan (0 on FREE). Spent before ai_edit_credits. */
  ai_edit_allowance_remaining: number;
  custom_templates_created: number;
  custom_template_limit: number;
  can_create_custom_template: boolean;
  preferred_voice_emotion: string | null;
  survey_submitted: boolean;
}

export interface AuthResponse {
  access_token: string;
  token_type: string;
  user: UserInfo;
}

export interface Scene {
  id: number;
  project_id: number;
  order: number;
  title: string;
  narration_text: string;
  display_text?: string | null;
  visual_description: string;
  remotion_code: string | null;
  voiceover_path: string | null;
  duration_seconds: number;
  extra_hold_seconds?: number | null;
  bgm_volume?: number | null;
  preferred_layout?: string | null;
  /** "intro"|"content"|"outro"|"dataviz_chart"|"dataviz_table" (custom templates). */
  scene_type?: string | null;
  created_at: string;
}

export interface Asset {
  id: number;
  project_id: number;
  asset_type: string;
  original_url: string | null;
  local_path: string;
  filename: string;
  r2_key: string | null;
  r2_url: string | null;
  excluded: boolean;
  created_at: string;
  /** VIDEO assets (stock footage) only β€” null on image/audio rows. */
  duration_seconds?: number | null;
  width?: number | null;
  height?: number | null;
  source_provider?: string | null;
  source_id?: string | null;
  source_author?: string | null;
  source_page_url?: string | null;
  /** Sibling AAC file when the source had audio; null/absent = clip is silent. */
  audio_variant_filename?: string | null;
}

export interface Project {
  id: number;
  // Owner user id β€” lets the editor tell owners from collaborators.
  user_id: number;
  name: string;
  blog_url: string | null;
  blog_content: string | null;
  status: string;
  template?: string;
  voice_gender: string;
  voice_accent: string;
  accent_color: string;
  bg_color: string;
  text_color: string;
  font_family?: string | null;
  animation_instructions: string | null;
  studio_unlocked: boolean;
  studio_port: number | null;
  player_port: number | null;
  r2_video_key: string | null;
  r2_video_url: string | null;
  logo_r2_url: string | null;
  logo_position: string;
  logo_opacity: number;
  logo_size: number;
  custom_voice_id: string | null;
  voice_emotion: string | null;
  aspect_ratio: string;
  video_style?: VideoStyleId;
  video_length?: "auto" | "short" | "medium" | "detailed" | "more_detailed";
  /** ISO 639-1 content language ('en', 'es'). Null = auto-detected from the source. */
  content_language?: string | null;
  playback_speed?: number;
  bgm_track_id?: string | null;
  bgm_volume?: number;
  bgm_track_url?: string | null;
  /** Paid + Newscast: generation pauses for stock-footage review after scripting. */
  stock_footage_enabled?: boolean;
  /** Bulk-created: stock footage auto-approves (no review step). */
  is_bulk?: boolean;
  captions_enabled?: boolean;
  caption_position?: "bottom_center" | "top_center";
  caption_font_family?: string;
  caption_font_size?: string | number;
  caption_offset?: number;
  ai_assisted_editing_count?: number;
  custom_theme?: CustomTemplateTheme | null;
  custom_image_box_aspect_ratios?: {
    intro?: string | { landscape?: string; portrait?: string };
    content?: (string | { landscape?: string; portrait?: string })[];
    outro?: string | { landscape?: string; portrait?: string };
  } | null;
  custom_template_missing?: boolean;
  brand_logo_url?: string | null;
  review_state?: ReviewState | null;
  /** True when the project has β‰₯1 collaborator β€” gates the per-scene comment button. */
  is_shared?: boolean;
  /**
   * True when the project OWNER is on a paid plan. On a shared project the owner
   * pays, so collaborators gate Pro-only features (custom/crafted templates, paid
   * voices) on this rather than their own plan.
   */
  owner_is_pro?: boolean;
  /**
   * The project OWNER's remaining per-user purchased AI-edit credits. On a shared
   * project a FREE collaborator draws from the owner's pool once the free
   * per-project allowance is spent β€” the UI shows this balance for collaborators.
   */
  owner_ai_edit_credits?: number;
  /** The project OWNER's remaining monthly plan allowance (0 on FREE). */
  owner_ai_edit_allowance_remaining?: number;
  /** The project OWNER's display name β€” used to attribute owner-scoped templates/voices in a collaborator's UI. */
  owner_name?: string | null;
  created_at: string;
  updated_at: string;
  scenes: Scene[];
  assets: Asset[];
}

export interface EmbedProjectResponse extends Project {
  crafted_template?: CraftedTemplateDetail | null;
  custom_template_code?: {
    intro_code: string | null;
    outro_code: string | null;
    content_codes: string[] | null;
  } | null;
  layout_prop_schema?: Record<string, LayoutPropSchemaEntry> | null;
}

export interface ProjectListItem {
  id: number;
  name: string;
  blog_url: string | null;
  status: string;
  created_at: string;
  updated_at: string;
  scene_count: number;
  // Collaboration: acting user's role ("owner" | "editor") + owner display name.
  role?: string;
  owner_name?: string | null;
}

export interface ChatMessage {
  id: number;
  role: string;
  content: string;
  created_at: string;
}

export interface ReviewState {
  project_sequence: number;
  has_review_for_project: boolean;
  should_show_inline: boolean;
}

export interface Review {
  id: number;
  user_id: number;
  project_id: number;
  rating: number;
  suggestion: string | null;
  source: "first_project_popup" | "inline_row";
  trigger_event: "delayed_popup" | "manual";
  project_sequence: number;
  plan_at_submission: string;
  created_at: string;
  updated_at: string;
}

export interface SubmitProjectReviewPayload {
  rating: 1 | 2 | 3 | 4 | 5;
  suggestion?: string;
  source: "first_project_popup" | "inline_row";
  trigger_event: "delayed_popup" | "manual";
}

export interface SubmitProjectReviewResponse {
  review: Review;
  review_state: ReviewState;
}

export interface ChatResponse {
  reply: string;
  changes_made: string;
  updated_scenes: Scene[];
}

export interface StudioResponse {
  studio_url: string;
  port: number;
}

export interface BillingStatus {
  plan: string;
  videos_used: number;
  video_limit: number;
  can_create_video: boolean;
  stripe_subscription_id: string | null;
  is_active: boolean;
}

export interface SubscriptionDetail {
  id: number;
  plan_name: string;
  plan_slug: string;
  status: string;
  stripe_subscription_id: string | null;
  current_period_start: string | null;
  current_period_end: string | null;
  videos_used: number;
  amount_paid_cents: number;
  canceled_at: string | null;
  retention_offer_eligible: boolean;
  scheduled_plan_slug: string | null;
  scheduled_plan_name: string | null;
  scheduled_change_at: string | null;
  created_at: string;
}

export interface RetentionOfferImpressionResponse {
  recorded: boolean;
  shown_count: number;
  eligible: boolean;
}

export interface Invoice {
  id: string;
  number: string | null;
  status: string | null;
  amount_due: number;
  amount_paid: number;
  currency: string;
  created: string;
  hosted_invoice_url: string | null;
  invoice_pdf: string | null;
}

export interface DataSummary {
  total_projects: number;
  total_videos_rendered: number;
  total_assets: number;
  account_created: string;
  plan: string;
}

export interface PlanInfo {
  id: number;
  slug: string;
  name: string;
  description: string | null;
  price_cents: number;
  currency: string;
  billing_interval: string;
  video_limit: number;
  includes_studio: boolean;
  includes_chat_editor: boolean;
  includes_priority_support: boolean;
  sort_order: number;
}

export interface PublicConfig {
  google_client_id: string;
  stripe_publishable_key: string;
  survey_promo_code: string;
}

// ─── Auth API ─────────────────────────────────────────────

export const googleLogin = (credential: string, reactivate = false, refCode?: string | null) => {
  const params: Record<string, unknown> = { reactivate };
  if (refCode) params.ref_code = refCode;
  return api.post<AuthResponse>("/auth/google", { credential }, { params });
};

// Share B2V (referral/invite) disabled
// export const getAffiliateStats = () => api.get<AffiliateStats>("/affiliate/stats");
// export const sendAffiliateInvites = (emails: string[]) =>
//   api.post<{ sent: number; failed: number }>("/affiliate/invite", { emails });
//
// export interface AffiliateStats {
//   link: string;
//   signups_count: number;
//   bonus_earned: number;
//   max_signups: number;
//   bonus_per_signup: number;
// }

export interface SurveyPayload {
  rating?: string;
  use_case?: string;
  target_audience?: string;
  desired_feature?: string;
  heard_from?: string;
}

export const submitSurvey = (payload: SurveyPayload) =>
  api.post<{ submitted: boolean; promo_code: string | null }>("/affiliate/survey", payload);

export const getMe = () => api.get<UserInfo>("/auth/me");

export const logoutCleanup = () => api.post("/auth/logout");

export const deleteAccount = () => api.post("/auth/delete-account");

export const getPublicConfig = () =>
  api.get<PublicConfig>("/config/public");

// ─── Billing API ──────────────────────────────────────────

export type CheckoutPlan = "pro" | "standard" | "lite";

// Post-checkout win-back coupon gate. We want at most one follow-up scheduled
// per browser per day, BUT only count checkouts that actually succeed β€” a failed
// / unauthorized attempt must not "burn" the day. So we *read* the flag to decide
// whether to ask the backend to schedule, and only *write* it after a 2xx.
const COUPON_FOLLOWUP_KEY = "b2v_coupon_followup_date";

function couponFollowupAllowedToday(): boolean {
  try {
    const today = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
    return localStorage.getItem(COUPON_FOLLOWUP_KEY) !== today;
  } catch {
    return true; // storage blocked β†’ fall back to scheduling
  }
}

function markCouponFollowupScheduled(): void {
  try {
    localStorage.setItem(COUPON_FOLLOWUP_KEY, new Date().toISOString().slice(0, 10));
  } catch {
    /* storage blocked β†’ nothing to record */
  }
}

export const createCheckoutSession = (
  options:
    | {
        plan?: CheckoutPlan;
        billing_cycle?: "monthly" | "annual" | "lifetime";
        apply_third_video_offer?: boolean;
      }
    | "monthly"
    | "annual"
    = "monthly"
) => {
  const plan =
    typeof options === "string" ? "pro" : (options?.plan ?? "pro");
  const billing_cycle =
    typeof options === "string" ? options : (options?.billing_cycle ?? "monthly");
  const apply_third_video_offer =
    typeof options === "string" ? false : (options?.apply_third_video_offer ?? false);
  const schedule_coupon_followup = couponFollowupAllowedToday();
  return api
    .post<{ checkout_url: string }>("/billing/checkout", {
      plan,
      billing_cycle,
      apply_third_video_offer,
      schedule_coupon_followup,
    })
    .then((res) => {
      if (schedule_coupon_followup) markCouponFollowupScheduled();
      return res;
    });
};

export const createPerVideoCheckout = (
  options?: number | { projectId?: number; quantity?: number }
) => {
  const projectId =
    typeof options === "number" ? options : options?.projectId;
  const quantity =
    typeof options === "object" && options?.quantity ? options.quantity : 1;
  const schedule_coupon_followup = couponFollowupAllowedToday();
  return api
    .post<{ checkout_url: string }>("/billing/checkout-per-video", {
      project_id: projectId ?? null,
      quantity,
      schedule_coupon_followup,
    })
    .then((res) => {
      if (schedule_coupon_followup) markCouponFollowupScheduled();
      return res;
    });
};


export const createCustomTemplateCheckout = (quantity: number = 1) =>
  api.post<{ checkout_url: string }>("/billing/checkout-custom-template", {
    quantity,
  });


/** One-time $300 purchase of 500 never-expiring video credits. */
export const createBulkCreditsCheckout = () =>
  api.post<{ checkout_url: string }>("/billing/checkout-bulk-credits", {});

export const createPortalSession = () =>
  api.post<{ portal_url: string }>("/billing/portal");

export const getBillingStatus = () =>
  api.get<BillingStatus>("/billing/status");

export const getSubscriptionDetail = () =>
  api.get<SubscriptionDetail | null>("/billing/subscription");

export const getInvoices = () =>
  api.get<Invoice[]>("/billing/invoices");

export const getDataSummary = () =>
  api.get<DataSummary>("/billing/data-summary");

export const getPlans = () =>
  api.get<PlanInfo[]>("/billing/plans");

export const cancelSubscription = (body?: { declined_retention_offer?: boolean }) =>
  api.post("/billing/cancel", body ?? {});

export const recordRetentionOfferImpression = () =>
  api.post<RetentionOfferImpressionResponse>("/billing/retention-offer/impression");

export type RetentionOfferAcceptResponse = {
  status: "applied" | "already_applied";
  message: string;
};

export const acceptRetentionOffer = () =>
  api.post<RetentionOfferAcceptResponse>("/billing/retention-offer/accept");

export const resumeSubscription = () =>
  api.post("/billing/resume");

// ─── Project API ──────────────────────────────────────────

export interface TemplateMeta {
  id: string;
  name: string;
  description: string;
  /** When true, show a highlighted "New" tag on the template picker (step 2). */
  new_template?: boolean;
  /** When true, show an amber "Popular" tag on the template picker. */
  popular_template?: boolean;
  styles?: string[];  // DEPRECATED β€” was video_style filter; now replaced by `genres`. Kept for back-compat readers.
  genres?: string[];  // topical categorization, e.g. ["Finance", "Politics"] β€” drives the genre dropdown filter
  preview_colors?: { accent: string; bg: string; text: string };
  composition_id?: string;
  hero_layout?: string;
  fallback_layout?: string;
  valid_layouts?: string[];
  studio_only_layouts?: string[];
  layouts_without_image?: string[];
  layout_prop_schema?: Record<string, LayoutPropSchema>;
}

export type LayoutPropFieldType =
  | "string"
  | "text"
  | "number"
  | "color"
  | "select"
  | "string_array"
  | "object_array"
  | "chart_table"
  | "ticker_table";

export interface LayoutPropSubField {
  key: string;
  label: string;
  placeholder?: string;
}

export interface LayoutPropField {
  key: string;
  label: string;
  type: LayoutPropFieldType;
  responsive?: boolean;
  placeholder?: string;
  min?: number;
  max?: number;
  step?: number;
  maxItems?: number;
  minItems?: number;
  options?: Array<{ label: string; value: string }>;
  subFields?: LayoutPropSubField[];
}

export interface LayoutSceneDefaults {
  title?: string;
  narration?: string;
  durationSeconds?: number;
}

export interface LayoutPropSchema {
  label?: string;
  description?: string;
  defaults?: Record<string, unknown>;
  /** Studio-preview-only prop samples β€” merged over defaults in Template Studio,
      NEVER merged into real renders (remotion.py reads only `defaults`). */
  sample_props?: Record<string, unknown>;
  scene_defaults?: LayoutSceneDefaults;
  fields: LayoutPropField[];
}

export const getTemplates = (style?: string) =>
  api.get<TemplateMeta[]>(style ? `/templates?style=${encodeURIComponent(style)}` : "/templates");

export interface TemplateAvailabilitySignal {
  has_custom_templates: boolean;
  has_crafted_templates: boolean;
}

export const getTemplateAvailabilitySignal = () =>
  api.get<TemplateAvailabilitySignal>("/projects/template-availability");

export interface CraftedTemplateSummary {
  id: string;
  name: string;
  description?: string;
  styles?: string[];
  genres?: string[];
  preview_colors?: { accent: string; bg: string; text: string };
  composition_id?: string;
  hero_layout?: string;
  fallback_layout?: string;
  valid_layouts?: string[];
  layouts_without_image?: string[];
  layout_prop_schema?: Record<string, LayoutPropSchema>;
  theme?: CustomTemplateTheme;
  preview_image_url?: string | null;
  /** Source code for the marquee preview component (optional, compiled at runtime). */
  preview_file?: string | null;
  /** Bundle-relative path to the marquee preview source. */
  preview_file_rel?: string | null;
  /** Source for SceneEditModal field defs per layout (TS/JSON, compiled at runtime). */
  layout_fields?: string | null;
  /** Bundle-relative path to the layout_fields source. */
  layout_fields_rel?: string | null;
  logo_urls?: string[] | null;
  og_image?: string | null;
  template_type?: "crafted";
  crafted?: boolean;
}

export interface CraftedTemplateItem extends CraftedTemplateSummary {
  intro_code?: string | null;
  outro_code?: string | null;
  content_codes?: string[] | null;
  content_archetype_ids?: (string | { id: string; best_for?: string[] })[] | null;
  frontend_files?: Record<string, string> | null;
  frontend_entry_rel?: string | null;
  frontend_layout_index_rel?: string | null;
  frontend_mount_id?: string | null;
  /** R2/CDN URLs keyed like Remotion staticFile("fonts/foo.woff2") β†’ full URL */
  public_asset_urls?: Record<string, string> | null;
}

export interface CraftedTemplateDetail extends CraftedTemplateItem {}

export interface CraftedTemplateFetchOptions {
  /**
   * When true, ask the backend to bypass its in-process cache and re-read
   * `summary.json` (or the full bundle) directly from R2. Used on hard
   * refresh so a fresh upload to R2 is visible without a server restart.
   */
  forceFresh?: boolean;
}

function craftedRequestConfig(opts?: CraftedTemplateFetchOptions) {
  if (!opts?.forceFresh) return undefined;
  return { headers: { "Cache-Control": "no-cache" } } as const;
}

export const listCraftedTemplates = (opts?: CraftedTemplateFetchOptions) =>
  api.get<CraftedTemplateSummary[]>("/crafted-templates", craftedRequestConfig(opts));

export const getCraftedTemplateDetail = (
  templateId: string,
  opts?: CraftedTemplateFetchOptions,
) =>
  api.get<CraftedTemplateDetail>(
    `/crafted-templates/${encodeURIComponent(templateId)}`,
    craftedRequestConfig(opts),
  );

export interface AspectValue {
  portrait: number;
  landscape: number;
}

export interface SaveTemplateSourceRequest {
  template_id: string;
  layout_id: string;
  title_font_size?: AspectValue;
  description_font_size?: AspectValue;
}

export interface SaveTemplateSourceResponse {
  ok: boolean;
  updated_file: string;
  updated_files?: string[];
  updated_meta_file?: string | null;
  layout_id: string;
  template_id: string;
  changes_applied: number;
}

export const saveTemplateSourceDefaults = (payload: SaveTemplateSourceRequest) =>
  api.post<SaveTemplateSourceResponse>("/template-studio/save-source", payload);

export interface ProposeTemplateAiEditRequest {
  template_id: string;
  layout_id: string;
  instruction: string;
  image_base64?: string | null;
  image_mime_type?: string | null;
}

export interface StartTemplateAiPreviewResponse {
  ok: boolean;
  session_id: string;
  template_id: string;
  layout_id: string;
  preview_files: string[];
  versions?: string[];
  active_version_id?: string;
}

export interface TemplateAiPreviewSessionRequest {
  session_id: string;
}

export interface SwitchTemplateAiPreviewRequest {
  session_id: string;
  version: string;
}

export interface ApplyTemplateAiPreviewResponse {
  ok: boolean;
  session_id: string;
  template_id: string;
  layout_id: string;
  updated_files: string[];
}

export interface DiscardTemplateAiPreviewResponse {
  ok: boolean;
  session_id: string;
}

export interface ListTemplateAiVersionsRequest {
  template_id: string;
  layout_id: string;
}

export interface ListTemplateAiVersionsResponse {
  ok: boolean;
  session_id: string | null;
  template_id: string;
  layout_id: string;
  versions: string[];
  active_version_id: string | null;
}

export const startTemplateAiPreview = (payload: ProposeTemplateAiEditRequest) =>
  api.post<StartTemplateAiPreviewResponse>("/template-studio/ai-edit/preview", payload);

export const startTemplateAiPreviewFile = (payload: {
  template_id: string;
  layout_id: string;
  instruction: string;
  image: File;
}) => {
  const formData = new FormData();
  formData.append("template_id", payload.template_id);
  formData.append("layout_id", payload.layout_id);
  formData.append("instruction", payload.instruction);
  formData.append("image", payload.image);
  return api.post<StartTemplateAiPreviewResponse>("/template-studio/ai-edit/preview-file", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export const applyTemplateAiPreview = (payload: TemplateAiPreviewSessionRequest) =>
  api.post<ApplyTemplateAiPreviewResponse>("/template-studio/ai-edit/preview-apply", payload);

export const switchTemplateAiPreviewVersion = (payload: SwitchTemplateAiPreviewRequest) =>
  api.post<{ ok: boolean; session_id: string; version: string }>(
    "/template-studio/ai-edit/preview-switch",
    payload
  );

export const discardTemplateAiPreview = (payload: TemplateAiPreviewSessionRequest) =>
  api.post<DiscardTemplateAiPreviewResponse>("/template-studio/ai-edit/preview-discard", payload);

export const getTemplateAiVersions = (payload: ListTemplateAiVersionsRequest) =>
  api.post<ListTemplateAiVersionsResponse>("/template-studio/ai-edit/versions", payload);

export const getFeaturedPublicTemplates = (ids: number[]) =>
  api.get<any[]>(`/custom-templates/public/featured?ids=${ids.join(',')}`);

// ─── Layout rebuild / create ──────────────────────────────────────────────────

export interface PropDef {
  name: string;
  type: string;
  description: string;
  default?: string;
}

export const SUPPORTED_PROP_TYPES = [
  "string",
  "text",
  "number",
  "boolean",
  "color",
  "imageUrl",
  "string_array",
  "object_array",
] as const;
export type PropType = typeof SUPPORTED_PROP_TYPES[number];

export interface RebuildLayoutRequest {
  template_id: string;
  layout_id: string;
  instruction: string;
  extra_props: PropDef[];
  image_base64?: string | null;
  image_mime_type?: string | null;
  // Optional HTML / SVG mock to drive the redesign (paste or read from a file).
  html_example?: string | null;
  svg_example?: string | null;
  // How the SVG should be used (e.g. "use as a full-bleed background").
  svg_usage?: string | null;
}

export interface RebuildLayoutResponse {
  ok: boolean;
  session_id: string;
  template_id: string;
  layout_id: string;
  versions: string[];
  active_version_id: string;
  updated_files: string[];
  schema: object;
}

export const rebuildTemplateLayout = (payload: RebuildLayoutRequest) =>
  api.post<RebuildLayoutResponse>("/template-studio/ai-layout/rebuild", payload);

export const rebuildTemplateLayoutFile = (payload: {
  template_id: string;
  layout_id: string;
  instruction: string;
  extra_props: PropDef[];
  image: File;
  html_example?: string | null;
  svg_example?: string | null;
  svg_usage?: string | null;
}) => {
  const formData = new FormData();
  formData.append("template_id", payload.template_id);
  formData.append("layout_id", payload.layout_id);
  formData.append("instruction", payload.instruction);
  formData.append("extra_props_json", JSON.stringify(payload.extra_props || []));
  if (payload.html_example) formData.append("html_example", payload.html_example);
  if (payload.svg_example) formData.append("svg_example", payload.svg_example);
  if (payload.svg_usage) formData.append("svg_usage", payload.svg_usage);
  formData.append("image", payload.image);
  return api.post<RebuildLayoutResponse>("/template-studio/ai-layout/rebuild-file", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export interface CreateLayoutRequest {
  template_id: string;
  base_layout_id: string;
  new_layout_id: string;
  layout_description: string;
  props: PropDef[];
  image_base64?: string | null;
  image_mime_type?: string | null;
}

export interface CreateLayoutResponse {
  ok: boolean;
  session_id: string;
  template_id: string;
  new_layout_id: string;
  versions: string[];
  active_version_id: string;
  created_files: string[];
  schema: object;
}

export const createTemplateLayout = (payload: CreateLayoutRequest) =>
  api.post<CreateLayoutResponse>("/template-studio/ai-layout/create", payload);

export const createTemplateLayoutFile = (payload: {
  template_id: string;
  base_layout_id: string;
  new_layout_id: string;
  layout_description: string;
  props: PropDef[];
  image: File;
}) => {
  const formData = new FormData();
  formData.append("template_id", payload.template_id);
  formData.append("base_layout_id", payload.base_layout_id);
  formData.append("new_layout_id", payload.new_layout_id);
  formData.append("layout_description", payload.layout_description);
  formData.append("props_json", JSON.stringify(payload.props || []));
  formData.append("image", payload.image);
  return api.post<CreateLayoutResponse>("/template-studio/ai-layout/create-file", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

// The structured plan extracted from a (normalized) design doc. Passed
// opaquely from POST /template/plan back into POST /template/create.
export type TemplatePlan = Record<string, unknown>;

export interface TemplateVerification {
  ok: boolean;
  checked: string[];
  repaired: string[];
  stubbed: string[];
  errors: Record<string, string[]>;
}

// Phase 1: normalize the design doc + extract the plan (fast, no codegen).
export interface PlanTemplateRequest {
  template_id: string;
  design_doc: string;
  overwrite?: boolean;
}

export interface PlanTemplateResponse {
  ok: boolean;
  template_id: string;
  name: string;
  normalized_doc: string;
  plan: TemplatePlan;
  layout_ids: string[];
  hero_layout: string;
  fallback_layout: string;
  warnings: string[];
}

export const planTemplateFromDoc = (payload: PlanTemplateRequest) =>
  api.post<PlanTemplateResponse>("/template-studio/template/plan", payload, {
    timeout: 600000, // 10 min β€” two sequential Claude calls (normalize + extract) on a large doc
  });

// Phase 2: build the template. Supply `plan` (from planTemplateFromDoc) so
// what the user previewed is exactly what gets built; `design_doc` alone is
// the legacy path.
export interface CreateTemplateRequest {
  template_id: string;
  design_doc?: string;
  normalized_doc?: string;
  plan?: TemplatePlan;
  /** With plan: subset of layout ids to generate (Studio verify step). */
  keep_layout_ids?: string[];
  overwrite?: boolean;
}

export interface CreateTemplateResponse {
  ok: boolean;
  session_id: string;
  template_id: string;
  name: string;
  layout_ids: string[];
  hero_layout: string;
  fallback_layout: string;
  created_files: string[];
  normalized_doc?: string;
  verification?: TemplateVerification;
  warnings: string[];
  note?: string;
}

export const createTemplateFromDoc = (payload: CreateTemplateRequest) =>
  api.post<CreateTemplateResponse>("/template-studio/template/create", payload, {
    timeout: 900000, // 15 min β€” sequential codegen + full-template typechecks
  });

export interface ExtractDesignDocResponse {
  ok: boolean;
  filename: string;
  text: string;
}

export const extractDesignDocFile = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return api.post<ExtractDesignDocResponse>("/template-studio/template/extract-doc", formData, {
    headers: { "Content-Type": "multipart/form-data" },
    timeout: 120000,
  });
};

export const renderTemplateLayout = (payload: {
  template_id: string;
  layout_id: string;
  aspect_ratio?: string;
  duration_seconds?: number;
  layout_props?: Record<string, unknown>;
  resolution?: "1080p" | "720p";
  scenes?: Array<{
    id?: number;
    order?: number;
    title?: string;
    narration?: string;
    layout?: string;
    layoutProps?: Record<string, unknown>;
    durationSeconds?: number;
    imageUrl?: string;
  }>;
}) =>
  api.post<Blob>("/template-studio/render-layout", payload, {
    responseType: "blob",
  });

export interface VoicePreview {
  voice_id: string;
  name: string;
  preview_url: string | null;
  description: string;
  gender: string;
  accent: string;
}

export const getVoicePreviews = () =>
  api.get<Record<string, VoicePreview>>("/voices/previews");

export interface BgmTrack {
  track_id: string;
  display_name: string;
  mood: string;
  r2_url: string;
}

export const getBgmTracks = () => api.get<BgmTrack[]>("/background-music/tracks");

export const createProject = (
  blog_url: string,
  name?: string,
  voice_gender?: string,
  voice_accent?: string,
  accent_color?: string,
  bg_color?: string,
  text_color?: string,
  animation_instructions?: string,
  logo_position?: string,
  logo_opacity?: number,
  custom_voice_id?: string,
  aspect_ratio?: string,
  template?: string,
  video_style?: VideoStyleId,
  video_length?: "auto" | "short" | "medium" | "detailed" | "more_detailed",
  content_language?: string | null,
  voice_emotion?: string,
  bgm_track_id?: string | null,
  bgm_volume?: number,
  captions_enabled?: boolean,
  caption_position?: "bottom_center" | "top_center",
  /** Options that don't warrant another positional arg (already 22). */
  extra?: { stock_footage_enabled?: boolean }
) =>
  api.post<Project>("/projects", {
    blog_url,
    name,
    voice_gender,
    voice_accent,
    accent_color,
    bg_color,
    text_color,
    animation_instructions,
    logo_position,
    logo_opacity,
    custom_voice_id,
    aspect_ratio,
    template,
    video_style,
    video_length,
    content_language,
    voice_emotion,
    bgm_track_id,
    bgm_volume,
    captions_enabled,
    caption_position,
    stock_footage_enabled: extra?.stock_footage_enabled ?? false,
  });

/** One project config for bulk create (same shape as single create). */
export interface BulkProjectItem {
  blog_url: string;
  name?: string;
  template?: string;
  video_style?: VideoStyleId;
  video_length?: "auto" | "short" | "medium" | "detailed" | "more_detailed";
  voice_gender?: string;
  voice_accent?: string;
  voice_emotion?: string;
  accent_color?: string;
  bg_color?: string;
  text_color?: string;
  animation_instructions?: string;
  logo_position?: string;
  logo_opacity?: number;
  custom_voice_id?: string;
  aspect_ratio?: string;
  content_language?: string | null;
  stock_footage_enabled?: boolean;
  captions_enabled?: boolean;
  caption_position?: "bottom_center" | "top_center";
}

export interface BulkCreateResponse {
  project_ids: number[];
}

/** Per-project logos: indices into the projects array and corresponding files. */
export interface BulkLogoOptions {
  logoIndices: number[];
  logoFiles: File[];
}

export const createProjectsBulk = (
  projects: BulkProjectItem[],
  logoOptions?: BulkLogoOptions | null
) => {
  const formData = new FormData();
  formData.append("projects", JSON.stringify(projects));
  if (logoOptions && logoOptions.logoIndices.length > 0 && logoOptions.logoFiles.length === logoOptions.logoIndices.length) {
    formData.append("logo_indices", JSON.stringify(logoOptions.logoIndices));
    logoOptions.logoFiles.forEach((f) => formData.append("logos", f));
  }
  return api.post<BulkCreateResponse>("/projects/bulk", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export const createProjectFromDocs = (
  files: File[],
  config: {
    name?: string;
    voice_gender?: string;
    voice_accent?: string;
    voice_emotion?: string;
    accent_color?: string;
    bg_color?: string;
    text_color?: string;
    animation_instructions?: string;
    logo_position?: string;
    logo_opacity?: number;
    custom_voice_id?: string;
    aspect_ratio?: string;
    template?: string;
    video_style?: VideoStyleId;
    video_length?: "auto" | "short" | "medium" | "detailed" | "more_detailed";
    content_language?: string | null;
    bgm_track_id?: string | null;
    bgm_volume?: number;
    stock_footage_enabled?: boolean;
  } = {}
) => {
  const formData = new FormData();
  files.forEach((f) => formData.append("files", f));
  if (config.name) formData.append("name", config.name);
  if (config.voice_gender) formData.append("voice_gender", config.voice_gender);
  if (config.voice_accent) formData.append("voice_accent", config.voice_accent);
  if (config.voice_emotion) formData.append("voice_emotion", config.voice_emotion);
  if (config.accent_color) formData.append("accent_color", config.accent_color);
  if (config.bg_color) formData.append("bg_color", config.bg_color);
  if (config.text_color) formData.append("text_color", config.text_color);
  if (config.animation_instructions)
    formData.append("animation_instructions", config.animation_instructions);
  if (config.logo_position) formData.append("logo_position", config.logo_position);
  if (config.logo_opacity !== undefined)
    formData.append("logo_opacity", String(config.logo_opacity));
  if (config.custom_voice_id) formData.append("custom_voice_id", config.custom_voice_id);
  if (config.aspect_ratio) formData.append("aspect_ratio", config.aspect_ratio);
  if (config.content_language !== undefined && config.content_language !== null) {
    formData.append("content_language", config.content_language);
  }
  if (config.template) formData.append("template", config.template);
  if (config.video_style) formData.append("video_style", config.video_style);
  if (config.video_length !== undefined && config.video_length !== null) {
    formData.append("video_length", config.video_length);
  }
  if (config.bgm_track_id) formData.append("bgm_track_id", config.bgm_track_id);
  if (config.bgm_volume !== undefined && config.bgm_volume !== null) {
    formData.append("bgm_volume", String(config.bgm_volume));
  if (config.stock_footage_enabled)
    formData.append("stock_footage_enabled", "true");
  }
  return api.post<Project>("/projects/upload", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export const uploadProjectDocuments = (projectId: number, files: File[]) => {
  const formData = new FormData();
  files.forEach((f) => formData.append("files", f));
  return api.post<Project>(`/projects/${projectId}/upload-documents`, formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });
};

export const uploadLogo = (projectId: number, file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return api.post<{ logo_url: string; logo_position: string }>(
    `/projects/${projectId}/logo`,
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

export const deleteLogo = (projectId: number) =>
  api.delete<{ detail: string }>(`/projects/${projectId}/logo`);

export interface ProjectLogoUpdate {
  logo_position?: string;
  logo_size?: number;
  logo_opacity?: number;
}

export const updateProjectLogo = (
  projectId: number,
  data: ProjectLogoUpdate
) => api.patch<Project>(`/projects/${projectId}`, data);

export const listProjects = () =>
  api.get<ProjectListItem[]>("/projects");

export const getProject = (id: number) =>
  api.get<Project>(`/projects/${id}`);

export const submitProjectReview = (
  projectId: number,
  data: SubmitProjectReviewPayload
) => api.post<SubmitProjectReviewResponse>(`/projects/${projectId}/review`, data);

export const deleteProject = (id: number) =>
  api.delete(`/projects/${id}`);

export const toggleAssetExclusion = (projectId: number, assetId: number) =>
  api.patch<{ id: number; excluded: boolean }>(
    `/projects/${projectId}/assets/${assetId}/exclude`
  );

export const deleteAsset = (projectId: number, assetId: number) =>
  api.delete(`/projects/${projectId}/assets/${assetId}`);

export const scrapeProject = (id: number) =>
  api.post<Project>(`/projects/${id}/scrape`);

export const generateScript = (id: number) =>
  api.post<Project>(`/projects/${id}/generate-script`);

export const generateScenes = (id: number) =>
  api.post<Project>(`/projects/${id}/generate-scenes`);

// ─── Async pipeline ──────────────────────────────────────

export interface PipelineStatus {
  status: string;
  step: number;
  running: boolean;
  error: string | null;
  error_code?: string | null;
  /** True when the server removed the project after a failed generation (quota reverted). */
  project_removed?: boolean;
  notice?: {
    code: string;
    message?: string;
    requested_video_length?: string;
    effective_video_length?: string;
    video_style?: string;
  } | null;
  stock_footage?: {
    current: number;
    total: number;
    scene_id: number;
  } | null;
  studio_port: number | null;
}

export const startGeneration = (id: number) =>
  api.post(`/projects/${id}/generate`);


export const getPipelineStatus = (id: number) =>
  api.get<PipelineStatus>(`/projects/${id}/status`);


export const bulkUpdateSceneTypography = (
  projectId: number,
  data: { title_font_size?: number; description_font_size?: number }
) =>
  api.put<Scene[]>(
    `/projects/${projectId}/bulk-update-scenes`,
    data
  );

export const updateProject = (
  projectId: number,
  data: {
    accent_color?: string;
    bg_color?: string;
    text_color?: string;
    font_family?: string | null;
    aspect_ratio?: string;
    playback_speed?: number;
    bgm_track_id?: string | null;
    bgm_volume?: number;
    stock_footage_enabled?: boolean;
    captions_enabled?: boolean;
    caption_position?: "bottom_center" | "top_center";
    caption_font_family?: string;
    caption_font_size?: number;
    caption_offset?: number;
  }
) => api.patch<Project>(`/projects/${projectId}/update-project`, data);

export interface VoiceChangeStartResponse {
  started: boolean;
  total: number;
}

export interface VoiceChangeStatus {
  active: boolean;
  done: boolean;
  error: string | null;
  total: number;
  completed: number;
  progress: number;
  status: string;
  r2_video_url: string | null;
  /** Which operation is running: "voice_change" (add/change) or "delete". */
  kind?: string;
}

/**
 * Change the project voice and regenerate every scene's voiceover (verbatim, in
 * the new voice). Counts as a new video (deducts one credit). Regeneration runs
 * in the background β€” poll getVoiceChangeStatus for scene-by-scene progress.
 */
export const changeProjectVoice = (
  projectId: number,
  data: {
    voice_gender?: string;
    voice_accent?: string;
    custom_voice_id?: string;
    voice_emotion?: string;
  }
) => api.post<VoiceChangeStartResponse>(`/projects/${projectId}/change-voice`, data);

export const getVoiceChangeStatus = (projectId: number) =>
  api.get<VoiceChangeStatus>(`/projects/${projectId}/voice-change-status`);

export interface LanguageChangeStartResponse {
  started: boolean;
  /** Scene count x 2 (translate pass + voiceover pass). */
  total: number;
  content_language: string;
}

export interface LanguageChangeStatus {
  active: boolean;
  done: boolean;
  error: string | null;
  total: number;
  completed: number;
  progress: number;
  /** Which pass is running: "translating" then "voiceover". */
  phase?: "translating" | "voiceover";
  status: string;
  r2_video_url: string | null;
  kind?: string;
  content_language: string | null;
}

/**
 * Translate the whole project into a new language and regenerate every voiceover.
 * Layouts, image assignments, colors, chart data and links are preserved β€” only the
 * prose changes language.
 *
 * Any editor may trigger this, but it counts as a new video and deducts one credit
 * from the project OWNER. Runs in the background β€” poll getLanguageChangeStatus.
 */
export const changeProjectLanguage = (projectId: number, contentLanguage: string) =>
  api.post<LanguageChangeStartResponse>(`/projects/${projectId}/change-language`, {
    content_language: contentLanguage,
  });

export const getLanguageChangeStatus = (projectId: number) =>
  api.get<LanguageChangeStatus>(`/projects/${projectId}/language-change-status`);

/**
 * Remove the project's voiceover and make the video mute. Does NOT deduct a video
 * credit. Runs in the background as a job β€” poll getVoiceChangeStatus for progress
 * (it reports kind="delete"). The existing render is kept; re-rendering to apply the
 * mute is a normal (paid) re-render.
 */
export const deleteProjectVoiceover = (projectId: number) =>
  api.post<VoiceChangeStartResponse>(`/projects/${projectId}/delete-voiceover`);

/**
 * Synthesize a short on-demand voice preview with the given voice + tuning (Advanced Options).
 * Returns an object URL for playback β€” the caller must revoke it. Paid + rate-limited server-side.
 */
export const previewVoice = async (params: {
  voice_gender?: string | null;
  voice_accent?: string | null;
  custom_voice_id?: string | null;
  voice_emotion?: string | null;
  video_style?: string | null;
}): Promise<string> => {
  const res = await api.post("/voice/preview", params, { responseType: "blob" });
  const blob = res.data as Blob;
  if (!blob || blob.size === 0) throw new Error("Empty audio");
  return window.URL.createObjectURL(blob);
};

export const updateScene = (
  projectId: number,
  sceneId: number,
  data: Partial<Scene>
) => api.put<Scene>(`/projects/${projectId}/scenes/${sceneId}`, data);


export const updateSceneImage = (
  projectId: number,
  sceneId: number,
  imageFile: File
) => {
  const formData = new FormData();
  formData.append("image", imageFile);
  return api.post<Scene>(
    `/projects/${projectId}/scenes/${sceneId}/image`,
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

/**
 * Upload a user-recorded voiceover for a scene. The backend transcodes the
 * recording (WebM/MP4) to MP3, uploads to R2, and replaces the scene's existing
 * voiceover (updating voiceover_path and duration). Voice settings are unchanged.
 */
export const updateSceneVoiceover = (
  projectId: number,
  sceneId: number,
  audioBlob: Blob
) => {
  const formData = new FormData();
  const ext = audioBlob.type.includes("mp4")
    ? "mp4"
    : audioBlob.type.includes("ogg")
      ? "ogg"
      : "webm";
  formData.append("audio", audioBlob, `recording.${ext}`);
  return api.post<Scene>(
    `/projects/${projectId}/scenes/${sceneId}/voiceover`,
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

export const updateSceneImageFocus = (
  projectId: number,
  sceneId: number,
  imageFocusX: number,
  imageFocusY: number,
  imageZoom?: number,
  /** Clip trim offset; only applied when the scene carries a stock clip. */
  videoStartSeconds?: number
) =>
  api.patch<Scene>(`/projects/${projectId}/scenes/${sceneId}/image-focus`, {
    image_focus_x: imageFocusX,
    image_focus_y: imageFocusY,
    ...(imageZoom !== undefined ? { image_zoom: imageZoom } : {}),
    ...(videoStartSeconds !== undefined
      ? { video_start_seconds: videoStartSeconds }
      : {}),
  });

export const moveSceneImage = (
  projectId: number,
  fromSceneId: number,
  toSceneId: number
) =>
  api.post<{ detail: string }>(`/projects/${projectId}/images/move`, {
    from_scene_id: fromSceneId,
    to_scene_id: toSceneId,
  });

export const swapSceneImages = (
  projectId: number,
  firstSceneId: number,
  secondSceneId: number
) =>
  api.post<{ detail: string }>(`/projects/${projectId}/images/swap`, {
    first_scene_id: firstSceneId,
    second_scene_id: secondSceneId,
  });

export const duplicateSceneImage = (
  projectId: number,
  sourceSceneId: number,
  targetSceneId: number
) =>
  api.post<{ detail: string }>(`/projects/${projectId}/images/duplicate`, {
    source_scene_id: sourceSceneId,
    target_scene_id: targetSceneId,
  });

export const assignExistingImageToScene = (
  projectId: number,
  sceneId: number,
  assetId: number
) =>
  api.post<{ detail: string }>(`/projects/${projectId}/images/assign-existing`, {
    scene_id: sceneId,
    asset_id: assetId,
  });

export interface GenerateSceneImageRequest {
  image_description: string;
}

export interface GenerateSceneImageResponse {
  image_base64: string;
  refined_prompt: string;
}

export const generateSceneImage = (
  projectId: number,
  sceneId: number,
  body: GenerateSceneImageRequest
) =>
  api.post<GenerateSceneImageResponse>(
    `/projects/${projectId}/scenes/${sceneId}/generate-image`,
    body
  );

/** A stock-footage search result, normalised across Pexels and Pixabay. */
export interface StockClip {
  provider: string;
  id: string;
  preview_url: string;
  thumbnail_url: string;
  download_url: string;
  width: number;
  height: number;
  duration: number;
  fps: number | null;
  author: string;
  page_url: string;
}

export const searchStockFootage = (
  projectId: number,
  params: {
    q: string;
    provider?: string;
    page?: number;
    per_page?: number;
    /** Scene image box in px on the 1080p canvas β€” steers orientation + rendition. */
    box_w?: number;
    box_h?: number;
  }
) =>
  api.get<{ clips: StockClip[] }>(`/projects/${projectId}/stock-footage/search`, {
    params,
  });

/** One scene awaiting stock-footage review at the generation gate. */
export interface PendingFootageScene {
  scene_id: number;
  order: number;
  title: string;
  scene_type: string | null;
  layout: string | null;
  /** Scene length + saved framing, used to seed the review gate's clip editor. */
  duration_seconds?: number | null;
  image_focus_x?: number | null;
  image_focus_y?: number | null;
  image_zoom?: number | null;
  video_start_seconds?: number | null;
  /** Set for custom templates, where the box ratio can't be derived from layout. */
  image_box_aspect_ratio?: string | null;
  clip: {
    filename: string;
    url: string;
    duration_seconds: number | null;
    author: string | null;
    provider: string | null;
  } | null;
  /** Scraped still used when auto stock search found no clip for this scene. */
  fallback_image?: {
    filename: string;
    url: string;
  } | null;
}

/** Scenes to review while a project sits at `awaiting_stock_footage_review`
 *  (or, for a legacy project, the old `awaiting_footage` gate). */
export const getPendingStockFootage = (projectId: number) =>
  api.get<{ status: string; awaiting: boolean; scenes: PendingFootageScene[] }>(
    `/projects/${projectId}/stock-footage/pending`
  );

/**
 * Point a scene at an already-uploaded clip. The upload endpoint creates the
 * asset but does not touch the scene, so the review gate calls this right after
 * swapping β€” otherwise the new clip is orphaned and the scene keeps the old one.
 */
export const linkStockFootage = (
  projectId: number,
  sceneId: number,
  filename: string
) =>
  api.post<{ detail: string; scene_id: number; filename: string }>(
    `/projects/${projectId}/stock-footage/link`,
    { scene_id: sceneId, filename }
  );

/** Accept the auto-picked clips as-is and finalize the video. */
export const approveStockFootage = (projectId: number) =>
  api.post<{ detail: string; status: string }>(
    `/projects/${projectId}/stock-footage/approve`
  );

/** Discard every auto-picked clip: fall back to images (or hide), then finalize. */
export const rejectStockFootage = (projectId: number) =>
  api.post<{ detail: string; status: string }>(
    `/projects/${projectId}/stock-footage/reject`
  );

/** The processed VIDEO asset returned after uploading a chosen clip. */
export interface UploadedStockClip {
  asset_id: number;
  filename: string;
  video_url: string;
  audio_variant_url: string | null;
  has_audio: boolean;
  duration_seconds: number | null;
  width: number | null;
  height: number | null;
  source_author: string;
  source_provider: string;
}

/**
 * Download + normalise a chosen clip into a VIDEO asset, WITHOUT linking it to
 * the scene. The backend transcodes it to CFR 30 fps (so it stays in sync with
 * the 30 fps composition) β€” expect a few seconds. The returned URLs let the
 * editor stage and preview the clip (audio included); the scene link is written
 * later by the normal scene Save (via `assignedVideo` in the descriptor).
 */
export const uploadStockFootage = (
  projectId: number,
  sceneId: number,
  clip: StockClip
) =>
  api.post<UploadedStockClip>(
    `/projects/${projectId}/scenes/${sceneId}/stock-footage`,
    {
      provider: clip.provider,
      clip_id: clip.id,
      download_url: clip.download_url,
      width: clip.width,
      height: clip.height,
      duration: clip.duration,
      author: clip.author,
      page_url: clip.page_url,
    }
  );

export interface LayoutPropSchemaEntry {
  label?: string;
  defaults?: Record<string, unknown>;
  fields?: Array<{ key: string; label?: string; type?: string }>;
  scene_defaults?: { title?: string; narration?: string };
}

export interface LayoutInfo {
  layouts: string[];
  layout_names: Record<string, string>;
  layouts_without_image?: string[];
  layout_prop_schema?: Record<string, LayoutPropSchemaEntry>;
}

export const getValidLayouts = (projectId: number) =>
  api.get<LayoutInfo>(`/projects/${projectId}/layouts`);

export interface SceneOrderItem {
  scene_id: number;
  order: number;
}

export const reorderScenes = (
  projectId: number,
  sceneOrders: SceneOrderItem[]
) =>
  api.post<Scene[]>(`/projects/${projectId}/scenes/reorder`, {
    scene_orders: sceneOrders,
  });

export const regenerateScene = (
  projectId: number,
  sceneId: number,
  description: string,
  narrationText: string,
  regenerateVoiceover: boolean,
  layout?: string,
  imageFile?: File,
  voiceoverVerbatim: boolean = true
) => {
  const formData = new FormData();
  // Only append description if it has a value
  if (description && description.trim()) {
    formData.append("description", description);
  }
  formData.append("narration_text", narrationText);
  formData.append("regenerate_voiceover", regenerateVoiceover ? "true" : "false");
  formData.append("voiceover_verbatim", voiceoverVerbatim ? "true" : "false");
  if (layout) formData.append("layout", layout);
  if (imageFile) formData.append("image", imageFile);
  
  return api.post<Scene>(
    `/projects/${projectId}/scenes/${sceneId}/regenerate`,
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

// A background add-scene generation job (polled via getAddSceneStatus).
export interface AddSceneJob {
  id: number;
  status: "queued" | "running" | "completed" | "failed";
  current_step: string;
  error_message?: string | null;
  // Set on success so the client can locate the newly inserted scene row.
  new_scene_id?: number | null;
  // 1-indexed insert position among active scenes (null = appended at end).
  position?: number | null;
}

// Enqueue background generation of a new AI scene. `position` is 1-indexed among
// active scenes (the new scene takes that slot, everything after shifts down); omit
// to append. Returns a job immediately β€” poll getAddSceneStatus for completion.
export const addScene = (
  projectId: number,
  prompt: string,
  position?: number
) =>
  api.post<AddSceneJob>(`/projects/${projectId}/scenes/add`, {
    prompt,
    position: position ?? null,
  });

// Latest add-scene job for a project (null if one has never been run).
export const getAddSceneStatus = (projectId: number) =>
  api.get<AddSceneJob | null>(`/projects/${projectId}/scenes/add-status`);

export const launchStudio = (id: number) =>
  api.post<StudioResponse>(`/projects/${id}/launch-studio`);

export const renderVideo = (
  id: number,
  forceReRender = false
) =>
  api.post<RenderStartResponse>(
    `/projects/${id}/render?force_render=${forceReRender}`
  );

export interface RenderStartResponse {
  detail: string;
  progress: number;
  resolution?: string;
  render_run_id?: string | null;
}

export interface RenderStatus {
  progress: number;
  rendered_frames: number;
  total_frames: number;
  done: boolean;
  error: string | null;
  time_remaining: string | null;
  eta_seconds: number | null;
  progress_unknown?: boolean;
  render_attempt?: number | null;
  render_run_id?: string | null;
  r2_video_url: string | null;
}

export const getRenderStatus = (id: number) =>
  api.get<RenderStatus>(`/projects/${id}/render-status`);

export const cancelRender = (id: number) =>
  api.post<{ detail: string; cancelled: boolean }>(`/projects/${id}/cancel-render`);

/** Fetch video as blob for playback. Returns object URL; caller must revoke it. */
export const fetchVideoBlob = async (id: number): Promise<string> => {
  const res = await api.get(`/projects/${id}/download`, {
    responseType: "blob",
  });
  if (res.status !== 200) throw new Error(`Download failed (${res.status})`);
  const blob = res.data as Blob;
  if (!blob || blob.size === 0) throw new Error("Empty video");
  return window.URL.createObjectURL(blob);
};

export const downloadVideo = async (id: number, filename?: string) => {
  try {
  
    const res = await api.get(`/projects/${id}/download`);

    const finalR2Url = res.request.responseURL;

    const link = document.createElement("a");
    link.href = finalR2Url;
    link.setAttribute("download", filename || "video.mp4");
    
    document.body.appendChild(link);
    link.click();
    link.remove();
    
  } catch (err) {
    // Fallback: If the API call fails, just try a direct browser navigation
    console.error("Link trigger failed, trying direct window location", err);
  }
};


export const downloadStudioZip = async (id: number, filename?: string) => {
  const res = await api.get(`/projects/${id}/download-studio`, {
    responseType: "blob",
  });
  const url = window.URL.createObjectURL(new Blob([res.data]));
  const a = document.createElement("a");
  a.href = url;
  a.download = filename || "studio_project.zip";
  document.body.appendChild(a);
  a.click();
  a.remove();
  window.URL.revokeObjectURL(url);
};

export const sendChatMessage = (id: number, message: string) =>
  api.post<ChatResponse>(`/projects/${id}/chat`, { message });

export const getChatHistory = (id: number) =>
  api.get<ChatMessage[]>(`/projects/${id}/chat/history`);

// ─── Custom Templates API (Pro only) ─────────────────────

/** Scene-exit transition styles (must match GeneratedTransition registry). */
export type TransitionStyle =
  | "fade"
  | "accent_wash"
  | "rule_sweep"
  | "ink_wash"
  | "whip_blur"
  | "push_slide"
  | "cover_wipe"
  | "page_flip"
  | "clock_sweep"
  // richer custom presentations (palette-driven; ported from economist/chronicle)
  | "parallax_push"
  | "whip_pan"
  | "accent_bar"
  | "page_fold"
  | "ink_bleed";

export interface CustomTemplateTheme {
  colors: { accent: string; bg: string; text: string; surface: string; muted: string; bg2?: string };
  fonts: { heading: string; body: string; mono: string };
  borderRadius: number;
  style: string;
  animationPreset: string;
  category: string;
  patterns: {
    cards: { corners: string; shadowDepth: string; borderStyle: string };
    spacing: { density: string; gridGap: number };
    images: { treatment: string; overlay: string; captionStyle: string };
    layout: { direction: string; decorativeElements: string[] };
  };
  /** Motion personality β€” drives kit animation timing + scene-exit transitions. */
  motion?: {
    energy?: "calm" | "smooth" | "energetic";
    easing?: string;
    transitionFamily?: TransitionStyle[];
  };
  /** Data-viz styling cues for the craft kit's CustomChart. */
  charts?: {
    style?: "clean" | "precise" | "editorial";
    gridStyle?: "dashed" | "horizontal" | "none";
    palette?: string[];
  };
  /** Background decoration system for the kit's <Decor>. */
  decor?: {
    system?: "none" | "dots" | "grid" | "orbs" | "starfield" | "rules" | "vignette";
    intensity?: number;
  };
  /** Preferred content archetypes for this brand (scene-type variety hint). */
  sceneBias?: string[];
  /**
   * The user's raw prompt / uploaded-doc text (prompt & doc creation paths only).
   * Round-trips extract β†’ create β†’ DB so scene generation can honor explicit
   * scene requests ("add a testimonial scene"). Absent for URL-scraped themes.
   */
  brief?: string;
}

export interface CustomTemplateItem {
  id: number;
  name: string;
  source_url: string | null;
  category: string;
  genres?: string[];
  theme: CustomTemplateTheme;
  preview_colors: { accent: string; bg: string; text: string };
  component_code: string | null;
  intro_code: string | null;
  outro_code: string | null;
  content_codes: string[] | null;
  content_archetype_ids: (string | { id: string; best_for?: string[] })[] | null;
  current_version_id: number | null;
  preview_image_url: string | null;
  logo_urls?: string[];
  og_image?: string;
  generation_failed: boolean;
  is_regenerating: boolean;
  my_rating?: number | null;
  my_rating_comment?: string | null;
  created_at: string;
  updated_at: string;
}

export interface TemplateRating {
  id: number;
  user_id: number;
  custom_template_id: number;
  rating: number;
  suggestion: string | null;
  created_at: string;
  updated_at: string;
}

export interface TemplateVersionItem {
  id: number;
  label: string;
  created_at: string;
}

export interface TemplateVersionsResponse {
  current_version_id: number | null;
  versions: TemplateVersionItem[];
}

export interface ExtractThemeResponse {
  extractable: boolean;
  reason: string;
  theme: CustomTemplateTheme | null;
  template_name: string;
  logo_urls: string[];
  og_image: string;
  screenshot_url: string;
}

export const listCustomTemplates = () =>
  api.get<CustomTemplateItem[]>("/custom-templates");

export const getCustomTemplate = (id: number) =>
  api.get<CustomTemplateItem>(`/custom-templates/${id}`);

export const createCustomTemplate = (data: {
  name: string;
  source_url?: string;
  theme: CustomTemplateTheme;
  logo_urls?: string[];
  og_image?: string;
  screenshot_url?: string;
  reason?: string;
}) => api.post<CustomTemplateItem>("/custom-templates", data);

export const updateCustomTemplate = (
  id: number,
  data: { name?: string; theme?: CustomTemplateTheme }
) => api.put<CustomTemplateItem>(`/custom-templates/${id}`, data);

export const deleteCustomTemplate = (id: number, force = false) =>
  api.delete(`/custom-templates/${id}${force ? "?force=true" : ""}`);

export const extractTheme = (url: string) =>
  api.post<ExtractThemeResponse>("/custom-templates/extract-theme", { url });

export const extractThemeFromPrompt = (prompt: string, name?: string) =>
  api.post<ExtractThemeResponse>("/custom-templates/extract-theme-from-prompt", { prompt, name });

export const extractThemeFromDoc = (file: File, name?: string) => {
  const formData = new FormData();
  formData.append("file", file);
  if (name) formData.append("name", name);
  return api.post<ExtractThemeResponse>(
    "/custom-templates/extract-theme-from-doc",
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

export const generateTemplateCode = (templateId: number) =>
  api.post<{ detail: string; template_id: number }>(`/custom-templates/${templateId}/generate-code`);

export interface CodeGenStatus {
  status: "generating" | "complete" | "error" | "unknown";
  step: string;
  running: boolean;
  error: string | null;
}

export const getCodeGenerationStatus = (templateId: number) =>
  api.get<CodeGenStatus>(`/custom-templates/${templateId}/generation-status`);

export const getTemplateCode = (templateId: number) =>
  api.get<{ component_code: string | null; intro_code: string | null; outro_code: string | null; content_codes: string[] | null }>(
    `/custom-templates/${templateId}/code`
  );

// ─── Project-scoped (owner-resolved) reads for shared projects ─────
// On a shared project, a collaborator must see the OWNER's templates/voices,
// not their own. These authorize via project membership on the backend and
// resolve against the project owner. The frontend routes through them only
// when the viewer is a collaborator (project.user_id !== user.id).

export const listProjectCustomTemplates = (projectId: number) =>
  api.get<CustomTemplateItem[]>(`/projects/${projectId}/custom-templates`);

export const getProjectTemplateCode = (projectId: number, templateId: number) =>
  api.get<{ component_code: string | null; intro_code: string | null; outro_code: string | null; content_codes: string[] | null }>(
    `/projects/${projectId}/custom-templates/${templateId}/code`
  );

export const listProjectCraftedTemplates = (
  projectId: number,
  opts?: CraftedTemplateFetchOptions,
) =>
  api.get<CraftedTemplateSummary[]>(
    `/projects/${projectId}/crafted-templates`,
    craftedRequestConfig(opts),
  );

export const getProjectCraftedTemplateDetail = (
  projectId: number,
  templateId: string,
  opts?: CraftedTemplateFetchOptions,
) =>
  api.get<CraftedTemplateDetail>(
    `/projects/${projectId}/crafted-templates/${encodeURIComponent(templateId)}`,
    craftedRequestConfig(opts),
  );

export const getProjectVoices = (projectId: number) =>
  api.get<SavedVoiceFromAPI[]>(`/projects/${projectId}/voices`);

// ─── Brand asset uploads ────────────────────────────────────

export const uploadTemplateLogo = (templateId: number, file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return api.post<{ logo_url: string; template: CustomTemplateItem }>(
    `/custom-templates/${templateId}/upload-logo`,
    formData,
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};

// ─── Template versioning & regeneration ─────────────────────

// Fire-and-poll, like generateTemplateCode: returns 202 immediately and the
// caller polls getCodeGenerationStatus / re-fetches the template list.
export const regenerateTemplateCode = (templateId: number) =>
  api.post<{ detail: string; template_id: number }>(`/custom-templates/${templateId}/regenerate-code`);

export const getTemplateVersions = (templateId: number) =>
  api.get<TemplateVersionsResponse>(`/custom-templates/${templateId}/versions`);

export const rollbackTemplateVersion = (templateId: number, versionId: number) =>
  api.post<CustomTemplateItem>(
    `/custom-templates/${templateId}/versions/${versionId}/rollback`
  );

export const submitTemplateRating = (
  templateId: number,
  data: { rating: 1 | 2 | 3 | 4 | 5; suggestion?: string }
) => api.post<TemplateRating>(`/custom-templates/${templateId}/rating`, data);

// ─── ElevenLabs voices (default / available) ─────────────────

export interface ElevenLabsVoice {
  voice_id: string;
  name: string;
  preview_url: string | null;
  labels: Record<string, string>;
  category?: string;
  description?: string;
  plan?: "free" | "paid";
}

export interface ListVoicesResponse {
  voices: ElevenLabsVoice[];
  has_more: boolean;
}

export const getVoices = () => api.get<ListVoicesResponse>("/voices");

export const getPrebuiltVoices = () =>
  api.get<{ voices: ElevenLabsVoice[]; has_more: boolean }>("/voices/prebuilt");

// ─── Voice design (preset + custom prompt) ───────────────────

export interface VoiceDesignPreview {
  generated_voice_id: string;
  audio_base_64: string;
  media_type?: string;
  duration_secs?: number;
}

export interface VoiceDesignResponse {
  previews: VoiceDesignPreview[];
  text?: string;
}

export interface DesignFromPresetPayload {
  gender?: string;
  age?: string;
  persona?: string;
  speed?: string;
  accent?: string;
}

export const designVoiceFromPreset = (payload: DesignFromPresetPayload) =>
  api.post<VoiceDesignResponse>("/voices/design-from-preset", payload);

export const designVoiceFromPrompt = (payload: { prompt: string }) =>
  api.post<VoiceDesignResponse>("/voices/design-from-prompt", payload);

// ─── Saved voices (user's My Voices, persisted in DB) ─────────

export interface SavedVoiceFromAPI {
  id: number;
  voice_id: string;
  name: string;
  preview_url?: string | null;
  source: string;
  plan?: string | null;  // "free" | "paid" for prebuilt (ElevenLabs)
  gender?: string | null;
  accent?: string | null;
  description?: string | null;
  created_at: string;
  custom_voice_id?: number | null;
}

export const getMyVoices = () => api.get<SavedVoiceFromAPI[]>("/voices/saved");

export const saveVoice = (payload: {
  voice_id: string;
  name: string;
  preview_url?: string;
  source?: string;
  plan?: string;  // "free" | "paid" for prebuilt
  gender?: string;
  accent?: string;
  description?: string;
  custom_voice_id?: number;
}) => api.post<SavedVoiceFromAPI>("/voices/saved", payload);

// ─── Custom voices (creation records: prompt/response/form) ─────

export interface CustomVoiceFromAPI {
  id: number;
  name: string;
  voice_id: string;
  source: string;
  prompt_text?: string | null;
  form_gender?: string | null;
  form_age?: string | null;
  form_persona?: string | null;
  form_speed?: string | null;
  form_accent?: string | null;
  preview_url?: string | null;
  created_at: string;
}

export const createCustomVoice = (payload: {
  voice_id: string;
  source: "prompt" | "form";
  name?: string;
  prompt_text?: string;
  response?: Record<string, unknown>;
  form_gender?: string;
  form_age?: string;
  form_persona?: string;
  form_speed?: string;
  form_accent?: string;
  preview_url?: string;
}) => api.post<CustomVoiceFromAPI>("/voices/custom", payload);

export const getCustomVoices = () => api.get<CustomVoiceFromAPI[]>("/voices/custom");

export const getCustomVoicePreview = (customVoiceId: number) =>
  api.get<{ preview_url: string | null; ready: boolean }>(`/voices/custom/${customVoiceId}/preview`);

export const deleteCustomVoice = (id: number) =>
  api.delete<{ ok: boolean }>(`/voices/custom/${id}`);

export const createCustomVoiceClone = (formData: FormData) =>
  api.post<CustomVoiceFromAPI>("/voices/clone", formData, {
    headers: { "Content-Type": "multipart/form-data" },
  });

export const deleteSavedVoice = (id: number) =>
  api.delete<{ ok: boolean }>(`/voices/saved/${id}`);

// ─── Embed API ────────────────────────────────────────────

export const generateEmbedToken = (projectId: number) =>
  api.post<{ embed_token: string; preview_url: string }>(`/embed/token/${projectId}`);

export default api;