File size: 135,211 Bytes
b5bea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
b5bea7f
8dcb1c7
 
b5bea7f
8dcb1c7
4202680
9daf18e
8dcb1c7
 
 
 
 
 
9daf18e
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
4202680
8dcb1c7
9daf18e
 
 
 
 
 
 
 
8dcb1c7
 
 
 
b5bea7f
8dcb1c7
 
 
 
 
b5bea7f
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
 
 
4202680
8dcb1c7
 
4202680
9daf18e
 
4202680
 
 
 
 
9daf18e
 
4202680
 
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9daf18e
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
9daf18e
8dcb1c7
 
4202680
 
 
 
 
 
b5bea7f
 
4202680
 
 
 
 
 
 
b5bea7f
4202680
 
 
 
 
8dcb1c7
 
 
 
4202680
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
4202680
 
b5bea7f
4202680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
 
 
 
9daf18e
b5bea7f
9daf18e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
9daf18e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
 
 
8dcb1c7
 
 
 
 
4202680
8dcb1c7
4202680
8dcb1c7
 
 
 
4202680
8dcb1c7
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
4202680
8dcb1c7
b5bea7f
 
8dcb1c7
 
4202680
 
 
 
b5bea7f
 
4202680
b5bea7f
4202680
 
 
b5bea7f
 
64072e5
b5bea7f
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
 
 
 
4202680
8dcb1c7
4202680
8dcb1c7
b5bea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
 
 
 
 
4202680
8dcb1c7
 
 
4202680
8dcb1c7
 
 
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
4202680
8dcb1c7
 
 
 
4202680
8dcb1c7
 
 
4202680
8dcb1c7
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
b5bea7f
 
 
 
 
 
 
9daf18e
 
b5bea7f
 
 
 
8dcb1c7
 
 
 
 
b5bea7f
f046ed3
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
b5bea7f
8dcb1c7
 
 
 
 
 
4202680
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
 
 
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
 
 
 
9daf18e
 
b5bea7f
9daf18e
 
 
 
b5bea7f
9daf18e
 
8dcb1c7
 
 
 
4202680
99cddfb
 
 
 
4202680
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
4202680
 
 
b5bea7f
4202680
 
 
 
64072e5
b5bea7f
4202680
b5bea7f
 
4202680
 
 
b5bea7f
4202680
 
 
99cddfb
4202680
b5bea7f
 
 
 
4202680
 
 
 
 
 
b5bea7f
 
 
 
 
 
 
 
4202680
b5bea7f
9ae4e14
b5bea7f
9ae4e14
b5bea7f
 
 
 
64072e5
8dcb1c7
b5bea7f
8dcb1c7
 
 
 
 
b5bea7f
8dcb1c7
b5bea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
b5bea7f
 
a992ada
f39b364
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
f39b364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
 
8dcb1c7
b5bea7f
8dcb1c7
 
b5bea7f
 
 
 
 
 
 
 
 
 
64072e5
b5bea7f
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
b5bea7f
 
 
 
 
 
 
8dcb1c7
 
b5bea7f
 
 
 
8dcb1c7
 
b5bea7f
 
 
 
 
 
 
 
 
3bfdb8d
b5bea7f
 
64072e5
b5bea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
8dcb1c7
 
 
 
 
 
b5bea7f
 
8dcb1c7
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
b5bea7f
8dcb1c7
 
 
b5bea7f
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
b5bea7f
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
 
8dcb1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bea7f
8dcb1c7
b5bea7f
8dcb1c7
 
 
 
b5bea7f
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
# import os
# import json
# import time
# from datetime import datetime
# from typing import List, Dict, Any, Optional, Union
# from pydantic import BaseModel, Field, EmailStr, field_validator
# from fastapi import FastAPI, HTTPException, Query, Depends, Request
# from fastapi.responses import JSONResponse, Response
# from fastapi.middleware.cors import CORSMiddleware
# from fastapi.openapi.utils import get_openapi
# import httpx
# from dotenv import load_dotenv
# import pandas as pd
# import psycopg2
# from sqlalchemy import create_engine, inspect, text

# # LangChain and OpenAI imports
# try:
#     from langchain_openai import ChatOpenAI
#     from langchain.prompts import ChatPromptTemplate
#     LANGCHAIN_AVAILABLE = True
# except ImportError:
#     LANGCHAIN_AVAILABLE = False
#     print("Warning: LangChain not available. Install with: pip install langchain langchain-openai")

# load_dotenv(override=True)

# # Configuration
# SMARTLEAD_API_KEY = os.getenv("SMARTLEAD_API_KEY", "your-api-key-here")
# SMARTLEAD_BASE_URL = "https://server.smartlead.ai/api/v1"
# DB_PARAMS = {
#     'dbname': os.getenv("DB_NAME"),
#     'user': os.getenv("DB_USER"),
#     'password': os.getenv("DB_PASSWORD"),
#     'host': os.getenv("DB_HOST"),
#     'port': os.getenv("DB_PORT")
# }
# DATABASE_URL = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}"

# # Initialize FastAPI app
# app = FastAPI(
#     title="Smartlead API - Complete Integration",
#     version="2.0.0",
#     description="Comprehensive FastAPI wrapper for Smartlead email automation platform",
#     docs_url="/docs",
#     redoc_url="/redoc"
# )

# # Add CORS middleware
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],
#     allow_credentials=True,
#     allow_methods=["*"],
#     allow_headers=["*"],
# )

# # ============================================================================
# # DATA MODELS
# # ============================================================================

# class CreateCampaignRequest(BaseModel):
#     name: str = Field(..., description="Campaign name")
#     client_id: Optional[int] = Field(None, description="Client ID (leave null if no client)")

# class CampaignScheduleRequest(BaseModel):
#     timezone: str = Field(..., description="Timezone for the campaign schedule (e.g., 'America/Los_Angeles')")
#     days_of_the_week: List[int] = Field(..., description="Days of the week for scheduling [0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday]")
#     start_hour: str = Field(..., description="Start hour for sending emails in HH:MM format (e.g., '09:00')")
#     end_hour: str = Field(..., description="End hour for sending emails in HH:MM format (e.g., '18:00')")
#     min_time_btw_emails: int = Field(..., description="Minimum time in minutes between sending emails")
#     max_new_leads_per_day: int = Field(..., description="Maximum number of new leads to process per day")
#     schedule_start_time: str = Field(..., description="Schedule start time in ISO 8601 format (e.g., '2023-04-25T07:29:25.978Z')")

# class CampaignSettingsRequest(BaseModel):
#     track_settings: List[str] = Field(..., description="Tracking settings array (allowed values: DONT_TRACK_EMAIL_OPEN, DONT_TRACK_LINK_CLICK, DONT_TRACK_REPLY_TO_AN_EMAIL)")
#     stop_lead_settings: str = Field(..., description="Settings for stopping leads (allowed values: CLICK_ON_A_LINK, OPEN_AN_EMAIL)")
#     unsubscribe_text: str = Field(..., description="Text for the unsubscribe link")
#     send_as_plain_text: bool = Field(..., description="Whether emails should be sent as plain text")
#     follow_up_percentage: int = Field(ge=0, le=100, description="Follow-up percentage (max 100, min 0)")
#     client_id: Optional[int] = Field(None, description="Client ID (leave as null if not needed)")
#     enable_ai_esp_matching: bool = Field(False, description="Enable AI ESP matching (by default is false)")

# class LeadInput(BaseModel):
#     first_name: Optional[str] = Field(None, description="Lead's first name")
#     last_name: Optional[str] = Field(None, description="Lead's last name")
#     email: str = Field(..., description="Lead's email address")
#     phone_number: Optional[Union[str, int]] = Field(None, description="Lead's phone number (can be string or integer)")
#     company_name: Optional[str] = Field(None, description="Lead's company name")
#     website: Optional[str] = Field(None, description="Lead's website")
#     location: Optional[str] = Field(None, description="Lead's location")
#     custom_fields: Optional[Dict[str, str]] = Field(None, description="Custom fields as key-value pairs (max 20 fields)")
#     linkedin_profile: Optional[str] = Field(None, description="Lead's LinkedIn profile URL")
#     company_url: Optional[str] = Field(None, description="Company website URL")
    
#     @field_validator('custom_fields')
#     @classmethod
#     def validate_custom_fields(cls, v):
#         if v is not None and len(v) > 20:
#             raise ValueError('Custom fields cannot exceed 20 fields')
#         return v
    
#     @field_validator('phone_number')
#     @classmethod
#     def validate_phone_number(cls, v):
#         if v is not None:
#             # Convert to string if it's an integer
#             return str(v)
#         return v

# class LeadSettings(BaseModel):
#     ignore_global_block_list: bool = Field(True, description="Ignore leads if they are in the global block list")
#     ignore_unsubscribe_list: bool = Field(True, description="Ignore leads if they are in the unsubscribe list")
#     ignore_duplicate_leads_in_other_campaign: bool = Field(False, description="Allow leads to be added even if they are duplicates in other campaigns")

# class AddLeadsRequest(BaseModel):
#     lead_list: List[LeadInput] = Field(..., max_items=100, description="List of leads to add (maximum 100 leads)")
#     settings: Optional[LeadSettings] = Field(None, description="Settings for lead processing")

# class AddLeadsResponse(BaseModel):
#     ok: bool = Field(..., description="Indicates if the operation was successful")
#     upload_count: int = Field(..., description="Number of leads successfully uploaded")
#     total_leads: int = Field(..., description="Total number of leads attempted to upload")
#     already_added_to_campaign: int = Field(..., description="Number of leads already present in the campaign")
#     duplicate_count: int = Field(..., description="Number of duplicate emails found")
#     invalid_email_count: int = Field(..., description="Number of leads with invalid email format")
#     unsubscribed_leads: Any = Field(..., description="Number of leads that had previously unsubscribed (can be int or empty list)")

# class SeqDelayDetails(BaseModel):
#     delay_in_days: int = Field(..., description="Delay in days before sending this sequence")

# class SeqVariant(BaseModel):
#     subject: str = Field(..., description="Email subject line")
#     email_body: str = Field(..., description="Email body content (HTML format)")
#     variant_label: str = Field(..., description="Variant label (A, B, C, etc.)")
#     id: Optional[int] = Field(None, description="Variant ID (only for updating, not for creating)")

# class CampaignSequence(BaseModel):
#     id: Optional[int] = Field(None, description="Sequence ID (only for updating, not for creating)")
#     seq_number: int = Field(..., description="Sequence number (1, 2, 3, etc.)")
#     seq_delay_details: SeqDelayDetails = Field(..., description="Delay details for this sequence")
#     seq_variants: Optional[List[SeqVariant]] = Field(None, description="Email variants for A/B testing")
#     subject: Optional[str] = Field("", description="Subject line (blank for follow-up in same thread)")
#     email_body: Optional[str] = Field(None, description="Email body content (HTML format)")

# class SaveSequencesRequest(BaseModel):
#     sequences: List[CampaignSequence] = Field(..., description="List of campaign sequences")

# class Job(BaseModel):
#     id: str
#     company_name: Optional[str] = None
#     job_title: Optional[str] = None
#     job_location: Optional[str] = None
#     company_blurb: Optional[str] = None
#     company_culture: Optional[str] = None
#     description: Optional[str] = None
#     company_size: Optional[str] = None
#     requirements: Optional[str] = None
#     salary: Optional[str] = None

# class GenerateSequencesRequest(BaseModel):
#     job_id: str = Field(..., description="Job ID to fetch from database and generate sequences for")

# class Campaign(BaseModel):
#     id: int
#     user_id: int
#     created_at: datetime
#     updated_at: datetime
#     status: str
#     name: str
#     track_settings: Union[str, List[Any]] # FIX: Accept string or list
#     scheduler_cron_value: Optional[Union[str, Dict[str, Any]]] = None # FIX: Accept string or dict
#     min_time_btwn_emails: int
#     max_leads_per_day: int
#     stop_lead_settings: str
#     unsubscribe_text: Optional[str] = None
#     client_id: Optional[int] = None
#     enable_ai_esp_matching: bool
#     send_as_plain_text: bool
#     follow_up_percentage: Optional[Union[str, int]] = None # FIX: Accept string or int

# class CampaignListResponse(BaseModel):
#     campaigns: List[Campaign]
#     total: int
#     source: str

# class Lead(BaseModel):
#     id: int
#     email: EmailStr
#     first_name: Optional[str] = None
#     last_name: Optional[str] = None
#     company: Optional[str] = None
#     position: Optional[str] = None
#     phone_number: Optional[str] = None
#     linkedin_url: Optional[str] = None
#     status: Optional[str] = None

# class WarmupDetails(BaseModel):
#     status: str
#     total_sent_count: int
#     total_spam_count: int
#     warmup_reputation: Union[str, int]
#     warmup_key_id: Optional[str] = None
#     warmup_created_at: Optional[datetime] = None
#     reply_rate: Optional[int] = None
#     blocked_reason: Optional[str] = None

# class EmailAccount(BaseModel):
#     id: int
#     created_at: datetime
#     updated_at: datetime
#     user_id: int
#     from_name: str
#     from_email: str
#     username: str
#     password: Optional[str] = None
#     smtp_host: Optional[str] = None
#     smtp_port: Optional[int] = None
#     smtp_port_type: Optional[str] = None
#     message_per_day: int
#     different_reply_to_address: Optional[str] = None
#     is_different_imap_account: bool
#     imap_username: Optional[str] = None
#     imap_password: Optional[str] = None
#     imap_host: Optional[str] = None
#     imap_port: Optional[int] = None
#     imap_port_type: Optional[str] = None
#     signature: Optional[str] = None
#     custom_tracking_domain: Optional[str] = None
#     bcc_email: Optional[str] = None
#     is_smtp_success: bool
#     is_imap_success: bool
#     smtp_failure_error: Optional[str] = None
#     imap_failure_error: Optional[str] = None
#     type: str
#     daily_sent_count: int
#     client_id: Optional[int] = None
#     campaign_count: Optional[int] = None
#     warmup_details: Optional[WarmupDetails] = None

# class WarmupSettingsRequest(BaseModel):
#     warmup_enabled: bool
#     total_warmup_per_day: Optional[int] = None
#     daily_rampup: Optional[int] = None
#     reply_rate_percentage: Optional[int] = None
#     warmup_key_id: Optional[str] = Field(None, description="String value if passed will update the custom warmup-key identifier")

# class LeadCategoryUpdateRequest(BaseModel):
#     category_id: int = Field(..., description="Category ID to assign to the lead")
#     pause_lead: bool = Field(False, description="Whether to pause the lead after category update")

# class CampaignStatusUpdateRequest(BaseModel):
#     status: str = Field(..., description="New campaign status (PAUSED, STOPPED, START)")

# class ResumeLeadRequest(BaseModel):
#     resume_lead_with_delay_days: Optional[int] = Field(None, description="Delay in days before resuming (defaults to 0)")

# class DomainBlockListRequest(BaseModel):
#     domain_block_list: List[str] = Field(..., description="List of domains/emails to block")
#     client_id: Optional[int] = Field(None, description="Client ID if blocking is client-specific")

# class WebhookRequest(BaseModel):
#     id: Optional[int] = Field(None, description="Webhook ID (null for creating new)")
#     name: str = Field(..., description="Webhook name")
#     webhook_url: str = Field(..., description="Webhook URL")
#     event_types: List[str] = Field(..., description="List of event types to listen for")
#     categories: Optional[List[str]] = Field(None, description="List of categories to filter by")

# class WebhookDeleteRequest(BaseModel):
#     id: int = Field(..., description="Webhook ID to delete")

# class ClientRequest(BaseModel):
#     name: str = Field(..., description="Client name")
#     email: str = Field(..., description="Client email")
#     permission: List[str] = Field(..., description="List of permissions")
#     logo: Optional[str] = Field(None, description="Client logo text")
#     logo_url: Optional[str] = Field(None, description="Client logo URL")
#     password: str = Field(..., description="Client password")

# class MessageHistoryRequest(BaseModel):
#     email_stats_id: str = Field(..., description="Email stats ID for the specific email")
#     email_body: str = Field(..., description="Reply message email body")
#     reply_message_id: str = Field(..., description="Message ID to reply to")
#     reply_email_time: str = Field(..., description="Time of the message being replied to")
#     reply_email_body: str = Field(..., description="Body of the message being replied to")
#     cc: Optional[str] = Field(None, description="CC recipients")
#     bcc: Optional[str] = Field(None, description="BCC recipients")
#     add_signature: bool = Field(True, description="Whether to add signature")

# # ============================================================================
# # DATABASE HELPER FUNCTIONS
# # ============================================================================

# def get_database_connection():
#     """Get database connection"""
#     try:
#         conn_string = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}"
#         return create_engine(conn_string)
#     except Exception as e:
#         raise HTTPException(status_code=500, detail=f"Database connection failed: {str(e)}")

# def fetch_job_by_id(job_id: str) -> Job:
#     """Fetch job details from database by ID using pandas DataFrame"""
#     try:
#         conn = get_database_connection()
#         df = pd.read_sql_table("jobs", con=conn)
        
#         # Filter the DataFrame to find the job with the specified ID
#         job_row = df[df['job_id'] == job_id]
        
#         if job_row.empty:
#             raise HTTPException(status_code=404, detail=f"Job with ID {job_id} not found")
        
#         # Get the first (and should be only) row
#         return Job(
#             id=str(job_row.iloc[0]['job_id']),
#             company_name=str(job_row.iloc[0]['company_name']) if pd.notna(job_row.iloc[0]['company_name']) else None,
#             job_title=str(job_row.iloc[0]['job_title']) if pd.notna(job_row.iloc[0]['job_title']) else None,
#             job_location=str(job_row.iloc[0]['job_location']) if pd.notna(job_row.iloc[0]['job_location']) else None,
#             company_blurb=str(job_row.iloc[0]['company_blurb']) if pd.notna(job_row.iloc[0]['company_blurb']) else None,
#             company_culture=str(job_row.iloc[0]['company_culture']) if pd.notna(job_row.iloc[0]['company_culture']) else None,
#             description=str(job_row.iloc[0]['description']) if pd.notna(job_row.iloc[0]['description']) else None,
#             company_size=str(job_row.iloc[0]['company_size']) if pd.notna(job_row.iloc[0]['company_size']) else None,
#             requirements=str(job_row.iloc[0]['requirements']) if pd.notna(job_row.iloc[0]['requirements']) else None,
#             salary=str(job_row.iloc[0]['salary']) if pd.notna(job_row.iloc[0]['salary']) else None
#         )
        
#     except HTTPException:
#         raise
#     except Exception as e:
#         raise HTTPException(status_code=500, detail=f"Error fetching job: {str(e)}")

# def list_available_jobs(limit: int = 10) -> List[Dict[str, Any]]:
#     """List available jobs from database using pandas DataFrame"""
#     try:
#         conn = get_database_connection()
#         df = pd.read_sql_table("jobs", con=conn)
        
#         # Select only the required columns and limit the results
#         selected_columns = ['job_id', 'company_name', 'job_title', 'job_location', 'company_size', 'salary']
#         df_subset = df[selected_columns].head(limit)
        
#         # Convert DataFrame to list of dictionaries
#         jobs_list = []
#         for _, row in df_subset.iterrows():
#             jobs_list.append({
#                 "id": str(row['job_id']),  # Ensure ID is returned as string
#                 "company_name": str(row['company_name']) if pd.notna(row['company_name']) else None,
#                 "job_title": str(row['job_title']) if pd.notna(row['job_title']) else None,
#                 "job_location": str(row['job_location']) if pd.notna(row['job_location']) else None,
#                 "company_size": str(row['company_size']) if pd.notna(row['company_size']) else None,
#                 "salary": str(row['salary']) if pd.notna(row['salary']) else None
#             })
        
#         return jobs_list
        
#     except Exception as e:
#         raise HTTPException(status_code=500, detail=f"Error fetching jobs: {str(e)}")

# def build_job_description(job: Job) -> str:
#     """Build a comprehensive job description from job details"""
#     parts = []
    
#     if job.company_name:
#         parts.append(f"Company: {job.company_name}")
    
#     if job.job_title:
#         parts.append(f"Position: {job.job_title}")
    
#     if job.job_location:
#         parts.append(f"Location: {job.job_location}")
    
#     if job.company_size:
#         parts.append(f"Company Size: {job.company_size}")
    
#     if job.salary:
#         parts.append(f"Salary: {job.salary}")
    
#     if job.company_blurb:
#         parts.append(f"About the Company: {job.company_blurb}")
    
#     if job.company_culture:
#         parts.append(f"Company Culture: {job.company_culture}")
    
#     if job.description:
#         parts.append(f"Job Description: {job.description}")
    
#     if job.requirements:
#         parts.append(f"Requirements: {job.requirements}")
    
#     return "\n\n".join(parts)

# # ============================================================================
# # HELPER FUNCTIONS
# # ============================================================================

# def _get_smartlead_url(endpoint: str) -> str:
#     return f"{SMARTLEAD_BASE_URL}/{endpoint.lstrip('/')}"

# async def call_smartlead_api(method: str, endpoint: str, data: Any = None, params: Dict[str, Any] = None) -> Any:
#     if SMARTLEAD_API_KEY == "your-api-key-here":
#         raise HTTPException(status_code=400, detail="Smartlead API key not configured")
#     if params is None:
#         params = {}
#     params['api_key'] = SMARTLEAD_API_KEY
#     url = _get_smartlead_url(endpoint)
    
#     try:
#         async with httpx.AsyncClient(timeout=30.0) as client:
#             request_kwargs = {"params": params}
#             if data is not None:
#                 request_kwargs["json"] = data
            
#             resp = await client.request(method, url, **request_kwargs)
        
#         if resp.status_code >= 400:
#             try:
#                 error_data = resp.json()
#                 error_message = error_data.get('message', error_data.get('error', 'Unknown error'))
#                 raise HTTPException(status_code=resp.status_code, detail=error_message)
#             except (ValueError, KeyError):
#                 raise HTTPException(status_code=resp.status_code, detail=resp.text)
        
#         return resp.json()
        
#     except httpx.TimeoutException:
#         raise HTTPException(status_code=408, detail="Request to Smartlead API timed out")
#     except httpx.RequestError as e:
#         raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}")

# # ============================================================================
# # CAMPAIGN ENDPOINTS
# # ============================================================================

# @app.post("/campaigns/create", response_model=Dict[str, Any], tags=["Campaigns"])
# async def create_campaign(campaign: CreateCampaignRequest):
#     """Create a new campaign in Smartlead"""
#     return await call_smartlead_api("POST", "campaigns/create", data=campaign.dict())

# @app.get("/campaigns", response_model=CampaignListResponse, tags=["Campaigns"])
# async def list_campaigns():
#     """Fetch all campaigns from Smartlead API"""
#     campaigns = await call_smartlead_api("GET", "campaigns")
#     return {"campaigns": campaigns, "total": len(campaigns), "source": "smartlead"}

# @app.get("/campaigns/{campaign_id}", response_model=Campaign, tags=["Campaigns"])
# async def get_campaign(campaign_id: int):
#     """Get Campaign By Id"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}")

# @app.post("/campaigns/{campaign_id}/settings", response_model=Dict[str, Any], tags=["Campaigns"])
# async def update_campaign_settings(campaign_id: int, settings: CampaignSettingsRequest):
#     """Update Campaign General Settings"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/settings", data=settings.dict())

# @app.post("/campaigns/{campaign_id}/schedule", response_model=Dict[str, Any], tags=["Campaigns"])
# async def schedule_campaign(campaign_id: int, schedule: CampaignScheduleRequest):
#     """Update Campaign Schedule"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/schedule", data=schedule.dict())

# @app.delete("/campaigns/{campaign_id}", response_model=Dict[str, Any], tags=["Campaigns"])
# async def delete_campaign(campaign_id: int):
#     """Delete Campaign"""
#     return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}")

# @app.post("/campaigns/{campaign_id}/status", response_model=Dict[str, Any], tags=["Campaigns"])
# async def patch_campaign_status(campaign_id: int, request: CampaignStatusUpdateRequest):
#     """Patch campaign status"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/status", data=request.dict())

# @app.get("/campaigns/{campaign_id}/analytics", response_model=Any, tags=["Analytics"])
# async def campaign_analytics(campaign_id: int):
#     """Fetch analytics for a campaign"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics")

# @app.get("/campaigns/{campaign_id}/statistics", response_model=Dict[str, Any], tags=["Analytics"])
# async def fetch_campaign_statistics_by_campaign_id(
#     campaign_id: int, 
#     offset: int = 0, 
#     limit: int = 100,
#     email_sequence_number: Optional[int] = None,
#     email_status: Optional[str] = None
# ):
#     """Fetch Campaign Statistics By Campaign Id"""
#     params = {"offset": offset, "limit": limit}
#     if email_sequence_number:
#         params["email_sequence_number"] = email_sequence_number
#     if email_status:
#         params["email_status"] = email_status
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/statistics", params=params)

# @app.get("/campaigns/{campaign_id}/analytics-by-date", response_model=Dict[str, Any], tags=["Analytics"])
# async def fetch_campaign_statistics_by_date_range(
#     campaign_id: int, 
#     start_date: str, 
#     end_date: str
# ):
#     """Fetch Campaign Statistics By Campaign Id And Date Range"""
#     params = {"start_date": start_date, "end_date": end_date}
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics-by-date", params=params)

# # ============================================================================
# # LEAD MANAGEMENT ENDPOINTS
# # ============================================================================

# @app.get("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"])
# async def get_campaign_leads(campaign_id: int, offset: int = 0, limit: int = 100):
#     """List all leads by campaign id"""
#     params = {"offset": offset, "limit": limit}
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads", params=params)

# @app.post("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"])
# async def add_leads_to_campaign(campaign_id: int, request: AddLeadsRequest):
#     """Add leads to a campaign by ID with personalized welcome and closing messages"""
#     request_data = request.dict()
    
#     # Process each lead to generate personalized messages and clean up data
#     for lead in request_data.get("lead_list", []):
#         lead_cleaned = {k: v for k, v in lead.items() if v is not None and v != ""}
        
#         # Generate personalized welcome and closing messages using LLM
#         try:
#             personalized_messages = await generate_welcome_closing_messages(lead_cleaned)
            
#             # Initialize custom_fields if it doesn't exist
#             if "custom_fields" not in lead_cleaned:
#                 lead_cleaned["custom_fields"] = {}
            
#             # Add the generated messages to custom_fields
#             if personalized_messages.get("welcome_message"):
#                 lead_cleaned["custom_fields"]["Welcome_Message"] = personalized_messages["welcome_message"]
#             if personalized_messages.get("closing_message"):
#                 lead_cleaned["custom_fields"]["Closing_Message"] = personalized_messages["closing_message"]
                
#         except Exception as e:
#             print(f"Error generating personalized messages for lead {lead_cleaned.get('email', 'unknown')}: {str(e)}")
#             # Continue with template messages if LLM fails
#             template_messages = generate_template_welcome_closing_messages(lead_cleaned)
#             if "custom_fields" not in lead_cleaned:
#                 lead_cleaned["custom_fields"] = {}
#             if template_messages.get("welcome_message"):
#                 lead_cleaned["custom_fields"]["Welcome_Message"] = template_messages["welcome_message"]
#             if template_messages.get("closing_message"):
#                 lead_cleaned["custom_fields"]["Closing_Message"] = template_messages["closing_message"]
        
#         # Clean up custom_fields - remove None values and empty strings
#         if "custom_fields" in lead_cleaned:
#             custom_fields = lead_cleaned["custom_fields"]
#             if custom_fields:
#                 custom_fields_cleaned = {k: v for k, v in custom_fields.items() if v is not None and v != ""}
#                 if custom_fields_cleaned:
#                     lead_cleaned["custom_fields"] = custom_fields_cleaned
#                 else:
#                     lead_cleaned.pop("custom_fields", None)
#             else:
#                 lead_cleaned.pop("custom_fields", None)
        
#         lead.clear()
#         lead.update(lead_cleaned)
    
#     request_data["lead_list"] = [lead for lead in request_data["lead_list"] if lead]
    
#     if not request_data["lead_list"]:
#         raise HTTPException(status_code=400, detail="No valid leads to add.")
    
#     if "settings" not in request_data or request_data["settings"] is None:
#         request_data["settings"] = LeadSettings().dict()
    
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads", data=request_data)

# @app.post("/campaigns/{campaign_id}/leads/bulk", response_model=Dict[str, Any], tags=["Leads"])
# async def add_bulk_leads(campaign_id: int, leads: List[LeadInput]):
#     """Add multiple leads to a Smartlead campaign with personalized messages (legacy endpoint)"""
#     request = AddLeadsRequest(lead_list=leads)
#     return await add_leads_to_campaign(campaign_id, request)

# @app.post("/campaigns/{campaign_id}/leads/{lead_id}/resume", response_model=Dict[str, Any], tags=["Leads"])
# async def resume_lead_by_campaign_id(campaign_id: int, lead_id: int, request: ResumeLeadRequest):
#     """Resume Lead By Campaign ID"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/resume", data=request.dict())

# @app.post("/campaigns/{campaign_id}/leads/{lead_id}/pause", response_model=Dict[str, Any], tags=["Leads"])
# async def pause_lead_by_campaign_id(campaign_id: int, lead_id: int):
#     """Pause Lead By Campaign ID"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/pause")

# @app.delete("/campaigns/{campaign_id}/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"])
# async def delete_lead_by_campaign_id(campaign_id: int, lead_id: int):
#     """Delete Lead By Campaign ID"""
#     return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/leads/{lead_id}")

# @app.post("/campaigns/{campaign_id}/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"])
# async def unsubscribe_lead_from_campaign(campaign_id: int, lead_id: int):
#     """Unsubscribe/Pause Lead From Campaign"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/unsubscribe")

# @app.post("/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"])
# async def unsubscribe_lead_from_all_campaigns(lead_id: int):
#     """Unsubscribe Lead From All Campaigns"""
#     return await call_smartlead_api("POST", f"leads/{lead_id}/unsubscribe")

# @app.post("/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"])
# async def update_lead(lead_id: int, lead_data: Dict[str, Any]):
#     """Update lead using the Lead ID"""
#     return await call_smartlead_api("POST", f"leads/{lead_id}", data=lead_data)

# @app.post("/campaigns/{campaign_id}/leads/{lead_id}/category", response_model=Dict[str, Any], tags=["Leads"])
# async def update_lead_category_by_campaign(campaign_id: int, lead_id: int, request: LeadCategoryUpdateRequest):
#     """Update a lead's category based on their campaign"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/category", data=request.dict())

# @app.post("/leads/add-domain-block-list", response_model=Dict[str, Any], tags=["Leads"])
# async def add_domain_to_global_block_list(request: DomainBlockListRequest):
#     """Add Lead/Domain to Global Block List"""
#     return await call_smartlead_api("POST", "leads/add-domain-block-list", data=request.dict())

# @app.get("/leads/fetch-categories", response_model=List[Dict[str, Any]], tags=["Leads"])
# async def fetch_lead_categories():
#     """Fetch lead categories"""
#     return await call_smartlead_api("GET", "leads/fetch-categories")

# @app.get("/leads", response_model=Dict[str, Any], tags=["Leads"])
# async def fetch_lead_by_email_address(email: str):
#     """Fetch lead by email address"""
#     return await call_smartlead_api("GET", "leads", params={"email": email})

# @app.get("/leads/{lead_id}/campaigns", response_model=List[Dict[str, Any]], tags=["Leads"])
# async def campaigns_for_lead(lead_id: int):
#     """Fetch all campaigns that a lead belongs to"""
#     return await call_smartlead_api("GET", f"leads/{lead_id}/campaigns")

# @app.get("/campaigns/{campaign_id}/leads/check", response_model=Dict[str, Any], tags=["Leads"])
# async def check_lead_in_campaign(campaign_id: int, email: str):
#     """Check if a lead exists in a campaign using efficient indexed lookups"""
#     try:
#         lead_response = await call_smartlead_api("GET", "leads", params={"email": email})
        
#         if not lead_response or "id" not in lead_response:
#             return {"exists": False, "message": "Lead not found"}
        
#         lead_id = lead_response["id"]
#         campaigns_response = await call_smartlead_api("GET", f"leads/{lead_id}/campaigns")
        
#         if not campaigns_response:
#             return {"exists": False, "message": "No campaigns found for lead"}
        
#         campaign_exists = any(campaign.get("id") == campaign_id for campaign in campaigns_response)
        
#         return {"exists": campaign_exists, "message": "Lead found in campaign" if campaign_exists else "Lead not found in campaign"}
        
#     except HTTPException as e:
#         if e.status_code == 404:
#             return {"exists": False, "message": "Lead not found"}
#         raise e
#     except Exception as e:
#         raise HTTPException(status_code=500, detail=f"Error checking lead in campaign: {str(e)}")

# @app.get("/campaigns/{campaign_id}/leads-export", tags=["Leads"])
# async def export_data_from_campaign(campaign_id: int):
#     """Export data from a campaign as CSV"""
#     if SMARTLEAD_API_KEY == "your-api-key-here":
#         raise HTTPException(status_code=400, detail="Smartlead API key not configured")
    
#     url = _get_smartlead_url(f"campaigns/{campaign_id}/leads-export")
#     params = {"api_key": SMARTLEAD_API_KEY}
    
#     try:
#         async with httpx.AsyncClient(timeout=30.0) as client:
#             resp = await client.get(url, params=params)
        
#         if resp.status_code >= 400:
#             try:
#                 error_data = resp.json()
#                 error_message = error_data.get('message', error_data.get('error', 'Unknown error'))
#                 raise HTTPException(status_code=resp.status_code, detail=error_message)
#             except (ValueError, KeyError):
#                 raise HTTPException(status_code=resp.status_code, detail=resp.text)
        
#         return Response(
#             content=resp.text,
#             media_type="text/csv",
#             headers={"Content-Disposition": f"attachment; filename=campaign_{campaign_id}_leads.csv"}
#         )
        
#     except httpx.TimeoutException:
#         raise HTTPException(status_code=408, detail="Request to Smartlead API timed out")
#     except httpx.RequestError as e:
#         raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}")

# # ============================================================================
# # SEQUENCE ENDPOINTS
# # ============================================================================

# @app.get("/campaigns/{campaign_id}/sequences", response_model=Any, tags=["Sequences"])
# async def get_campaign_sequences(campaign_id: int):
#     """Fetch email sequences for a campaign"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/sequences")

# @app.post("/campaigns/{campaign_id}/sequences", response_model=Dict[str, Any], tags=["Sequences"])
# async def save_campaign_sequences(campaign_id: int, request: SaveSequencesRequest):
#     """Save Campaign Sequence"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=request.dict())

# @app.post("/campaigns/{campaign_id}/sequences/generate", response_model=Dict[str, Any], tags=["Sequences"])
# async def generate_campaign_sequences(campaign_id: int, request: GenerateSequencesRequest):
#     """Generate Campaign Sequences using LLM"""
#     # Fetch job details from database using job_id
#     job = fetch_job_by_id(request.job_id)
#     job_description = build_job_description(job)
    
#     generated_sequences = await generate_sequences_with_llm(job_description)
#     save_request = SaveSequencesRequest(sequences=generated_sequences)
#     result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict())
    
#     return {
#         "ok": True,
#         "message": "Sequences generated and saved successfully",
#         "job_details": {
#             "id": job.id,
#             "company_name": job.company_name,
#             "job_title": job.job_title,
#             "job_location": job.job_location
#         },
#         "generated_sequences": [seq for seq in generated_sequences],
#         "save_result": result
#     }

# # ============================================================================
# # WEBHOOK ENDPOINTS
# # ============================================================================

# @app.get("/campaigns/{campaign_id}/webhooks", response_model=List[Dict[str, Any]], tags=["Webhooks"])
# async def fetch_webhooks_by_campaign_id(campaign_id: int):
#     """Fetch Webhooks By Campaign ID"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/webhooks")

# @app.post("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"])
# async def add_update_campaign_webhook(campaign_id: int, request: WebhookRequest):
#     """Add / Update Campaign Webhook"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/webhooks", data=request.dict())

# @app.delete("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"])
# async def delete_campaign_webhook(campaign_id: int, request: WebhookDeleteRequest):
#     """Delete Campaign Webhook"""
#     return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/webhooks", data=request.dict())

# # ============================================================================
# # CLIENT MANAGEMENT ENDPOINTS
# # ============================================================================

# @app.post("/client/save", response_model=Dict[str, Any], tags=["Clients"])
# async def add_client_to_system(request: ClientRequest):
#     """Add Client To System (Whitelabel or not)"""
#     return await call_smartlead_api("POST", "client/save", data=request.dict())

# @app.get("/client", response_model=List[Dict[str, Any]], tags=["Clients"])
# async def fetch_all_clients():
#     """Fetch all clients"""
#     return await call_smartlead_api("GET", "client")

# # ============================================================================
# # MESSAGE HISTORY AND REPLY ENDPOINTS
# # ============================================================================

# @app.get("/campaigns/{campaign_id}/leads/{lead_id}/message-history", response_model=Dict[str, Any], tags=["Messages"])
# async def fetch_lead_message_history_based_on_campaign(campaign_id: int, lead_id: int):
#     """Fetch Lead Message History Based On Campaign"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads/{lead_id}/message-history")

# @app.post("/campaigns/{campaign_id}/reply-email-thread", response_model=Dict[str, Any], tags=["Messages"])
# async def reply_to_lead_from_master_inbox(campaign_id: int, request: MessageHistoryRequest):
#     """Reply To Lead From Master Inbox via API"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/reply-email-thread", data=request.dict())

# # ============================================================================
# # EMAIL ACCOUNT ENDPOINTS
# # ============================================================================

# @app.get("/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"])
# async def list_email_accounts(offset: int = 0, limit: int = 100):
#     """List all email accounts with optional pagination"""
#     params = {"offset": offset, "limit": limit}
#     return await call_smartlead_api("GET", "email-accounts", params=params)

# @app.post("/email-accounts/save", response_model=Any, tags=["Email Accounts"])
# async def save_email_account(account: Dict[str, Any]):
#     """Create an Email Account"""
#     return await call_smartlead_api("POST", "email-accounts/save", data=account)

# @app.post("/email-accounts/reconnect-failed-email-accounts", response_model=Dict[str, Any], tags=["Email Accounts"])
# async def reconnect_failed_email_accounts(body: Optional[Dict] = {}):
#     """Reconnect failed email accounts"""
#     return await call_smartlead_api("POST", "email-accounts/reconnect-failed-email-accounts", data={})

# @app.get("/email-accounts/{account_id}", response_model=EmailAccount, tags=["Email Accounts"])
# async def get_email_account(account_id: int):
#     """Fetch Email Account By ID"""
#     return await call_smartlead_api("GET", f"email-accounts/{account_id}")

# @app.post("/email-accounts/{account_id}", response_model=Any, tags=["Email Accounts"])
# async def update_email_account(account_id: int, payload: Dict[str, Any]):
#     """Update Email Account"""
#     return await call_smartlead_api("POST", f"email-accounts/{account_id}", data=payload)

# @app.post("/email-accounts/{account_id}/warmup", response_model=Any, tags=["Email Accounts"])
# async def set_warmup(account_id: int, payload: WarmupSettingsRequest):
#     """Add/Update Warmup To Email Account"""
#     return await call_smartlead_api("POST", f"email-accounts/{account_id}/warmup", data=payload.dict(exclude_none=True))

# @app.get("/email-accounts/{account_id}/warmup-stats", response_model=Any, tags=["Email Accounts"])
# async def get_warmup_stats(account_id: int):
#     """Fetch Warmup Stats By Email Account ID"""
#     return await call_smartlead_api("GET", f"email-accounts/{account_id}/warmup-stats")

# @app.get("/campaigns/{campaign_id}/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"])
# async def list_campaign_email_accounts(campaign_id: int):
#     """List all email accounts per campaign"""
#     return await call_smartlead_api("GET", f"campaigns/{campaign_id}/email-accounts")

# @app.post("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"])
# async def add_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]):
#     """Add Email Account To A Campaign"""
#     return await call_smartlead_api("POST", f"campaigns/{campaign_id}/email-accounts", data=payload)

# @app.delete("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"])
# async def remove_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]):
#     """Remove Email Account From A Campaign"""
#     return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/email-accounts", data=payload)

# # ============================================================================
# # UTILITY ENDPOINTS
# # ============================================================================

# @app.get("/health", response_model=Dict[str, Any], tags=["Utilities"])
# async def health_check():
#     """Health check endpoint to verify API connectivity"""
#     try:
#         campaigns = await call_smartlead_api("GET", "campaigns")
#         return {
#             "status": "healthy",
#             "message": "Smartlead API is accessible",
#             "campaigns_count": len(campaigns) if isinstance(campaigns, list) else 0,
#             "timestamp": datetime.now().isoformat()
#         }
#     except Exception as e:
#         return {
#             "status": "unhealthy",
#             "message": f"Smartlead API connection failed: {str(e)}",
#             "timestamp": datetime.now().isoformat()
#         }

# @app.get("/api-info", response_model=Dict[str, Any], tags=["Utilities"])
# async def api_info():
#     """Get information about the API and available endpoints"""
#     return {
#         "name": "Smartlead API - Complete Integration",
#         "version": "2.0.0",
#         "description": "Comprehensive FastAPI wrapper for Smartlead email automation platform",
#         "base_url": SMARTLEAD_BASE_URL,
#         "available_endpoints": [
#             "Campaign Management",
#             "Lead Management", 
#             "Sequence Management",
#             "Webhook Management",
#             "Client Management",
#             "Message History & Reply",
#             "Analytics",
#             "Email Account Management",
#             "Job Management"
#         ],
#         "documentation": "Based on Smartlead API documentation",
#         "timestamp": datetime.now().isoformat()
#     }

# # ============================================================================
# # JOB MANAGEMENT ENDPOINTS
# # ============================================================================

# @app.get("/jobs", response_model=List[Dict[str, Any]], tags=["Jobs"])
# async def get_available_jobs(limit: int = Query(10, ge=1, le=100, description="Number of jobs to return")):
#     """Get list of available jobs from database"""
#     return list_available_jobs(limit)

# @app.get("/jobs/{job_id}", response_model=Job, tags=["Jobs"])
# async def get_job_by_id(job_id: str):
#     """Get job details by ID from database"""
#     return fetch_job_by_id(job_id)

# # ============================================================================
# # AI SEQUENCE GENERATION FUNCTIONS
# # ============================================================================

# async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
#     class structure(BaseModel):
#         welcome_message: str = Field(description="Welcome message for the candidate")
#         closing_message: str = Field(description="Closing message for the candidate")

#     """Generate personalized welcome and closing messages using LLM based on candidate details"""
    
#     if not LANGCHAIN_AVAILABLE:
#         return generate_template_welcome_closing_messages(lead_data)
    
#     try:
#         openai_api_key = os.getenv("OPENAI_API_KEY")
#         if not openai_api_key:
#             print("Warning: OPENAI_API_KEY not set. Using template messages.")
#             return generate_template_welcome_closing_messages(lead_data)
        
#         llm = ChatOpenAI(
#             model="gpt-4o-mini",
#             temperature=0.7,
#             openai_api_key=openai_api_key
#         )
#         str_llm = llm.with_structured_output(structure, method="function_calling")
        
#         # Extract relevant information from lead data
#         first_name = lead_data.get("first_name", "")
#         last_name = lead_data.get("last_name", "")
#         company_name = lead_data.get("company_name", "")
#         location = lead_data.get("location", "")
#         title = lead_data.get("custom_fields", {}).get("Title", "")
#         linkedin_profile = lead_data.get("linkedin_profile", "")
        
#         # Create a summary of the candidate's background
#         candidate_info = f"""
#         Name: {first_name}
#         Company: {company_name}
#         Location: {location}
#         Title: {title}
#         LinkedIn: {linkedin_profile}
#         """
        
#         system_prompt = """You are an expert recruiter who creates personalized welcome and closing messages for email campaigns.
        
#         Based on the candidate's information, generate:
#         1. A personalized welcome message (2-3 sentences) starting with "Hi first_name then change the line using <br> tag and continue with a friendly introduction about their background/company/role.
#         2. A personalized closing message (1-2 sentences)
        
#         Requirements:
#         - Professional but friendly tone
#         - Reference their specific background/company/role when possible
#         - Keep messages concise and engaging
#         - Make them feel valued and understood
         
#         IMPORTANT: Respond with ONLY valid JSON. No additional text."""
        
#         prompt_template = ChatPromptTemplate.from_messages([
#             ("system", system_prompt),
#             ("human", "Generate personalized messages for this candidate: {candidate_info}")
#         ])
        
#         messages = prompt_template.format_messages(candidate_info=candidate_info)
#         response = await str_llm.ainvoke(messages)
        
#         try:
            
#             return {
#                 "welcome_message": response.welcome_message,
#                 "closing_message": response.closing_message
#             }
            
#         except Exception as parse_error:
#             print(f"JSON parsing failed for welcome/closing messages: {parse_error}")
#             return generate_template_welcome_closing_messages(lead_data)
        
#     except Exception as e:
#         print(f"Error generating welcome/closing messages with LLM: {str(e)}")
#         return generate_template_welcome_closing_messages(lead_data)

# def generate_template_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
#     """Generate template-based welcome and closing messages as fallback"""
    
#     first_name = lead_data.get("first_name", "")
#     company_name = lead_data.get("company_name", "")
#     title = lead_data.get("custom_fields", {}).get("Title", "")
    
#     # Personalized welcome message
#     if first_name and company_name:
#         welcome_message = f"Hi {first_name}, I came across your profile and was impressed by your work at {company_name}."
#     elif first_name:
#         welcome_message = f"Hi {first_name}, I came across your profile and was impressed by your background."
#     elif company_name:
#         welcome_message = f"Hi there, I came across your profile and was impressed by your work at {company_name}."
#     else:
#         welcome_message = "Hi there, I came across your profile and was impressed by your background."
    
#     # Personalized closing message
#     if first_name:
#         closing_message = f"Looking forward to connecting with you, {first_name}!"
#     else:
#         closing_message = "Looking forward to connecting with you!"
    
#     return {
#         "welcome_message": welcome_message,
#         "closing_message": closing_message
#     }

# async def generate_sequences_with_llm(job_description: str) -> List[CampaignSequence]:
#     class email_seq(BaseModel):
#         subject: str = Field(description="Subject line for the email")
#         body: str = Field(description="Body of the email")
#     class structure(BaseModel):
#         introduction: email_seq = Field(description="Email sequence for sequence 1 asking for consent and interest in the role")
#         email_sequence_2: email_seq = Field(description="Email sequence for sequence 2 following up on updates and next steps")
#         email_sequence_3: email_seq = Field(description="Email sequence for sequence 3 Another variant on following up on updates and next steps")



#     """Generate email sequences using LangChain and OpenAI based on job description"""
    
#     if not LANGCHAIN_AVAILABLE:
#         return await generate_template_sequences(job_description)
    
#     try:
#         openai_api_key = os.getenv("OPENAI_API_KEY")
#         if not openai_api_key:
#             print("Warning: OPENAI_API_KEY not set. Using template sequences.")
#             return await generate_template_sequences(job_description)
        
#         llm = ChatOpenAI(
#             model="gpt-4o-mini",
#             temperature=0.7,
#             openai_api_key=openai_api_key
#         )
#         str_llm = llm.with_structured_output(structure, method="function_calling")
        
#         system_prompt = """You are an expert email sequence template generator for recruitment campaigns.

#         Generate ONLY the subject lines and email body content for 3 professional email sequences.
#         Write the email on behalf of Ali Taghikhani, CEO SRN.

#         Use the following placeholders in your templates, EXACTLY as writtenm, AlWAYS use double braces for placeholders:
#         - `{{first_name}}`
#         - `{{company_name}}`
#         - `{{Title}}`
#         - `{{Welcome_Message}}`
#         - `{{Closing_Message}}`

#         Email Sequence Structure:
#         1. INTRODUCTION (Day 1): Start the email with `{{Welcome_Message}}`. Ask for consent and interest in the role. End the email with `{{Closing_Message}}` followed by the sender's name and title.
#         2. OUTREACH (Day 3): Provide detailed job information. This is a follow-up, so it should not have a subject line.
#         3. FOLLOW-UP (Day 5): Another follow-up on updates and next steps. Also no subject line.

#         Requirements:
#         - The first sequence must start with `{{Welcome_Message}}` and end with `{{Closing_Message}}`.
#         - Always end the email with Best regards
#         - The second and third sequences are follow-ups and should not have a subject line.
#         - All emails should be HTML formatted with proper `<br>` tags.
#         - Professional but friendly tone.
#         - Include clear call-to-actions.
#         - Focus on building consent and trust.

#         IMPORTANT: Respond with ONLY valid JSON. No additional text."""
        
#         prompt_template = ChatPromptTemplate.from_messages([
#             ("system", system_prompt),
#             ("human", "Generate email content for this job description: {job_description}")
#         ])
        
#         messages = prompt_template.format_messages(job_description=job_description)
#         response = await str_llm.ainvoke(messages)
        
#         try:
#             # Handle the response structure correctly
#             sequences = []
            
#             # Sequence 1: Introduction with A/B variants
#             if hasattr(response, 'introduction') and response.introduction:
#                 # Check if body is a string or dict
#                 intro_body = response.introduction.body
#                 if isinstance(intro_body, dict):
#                     # If it's a dict, extract the content
#                     intro_body = str(intro_body)
                
#                 sequences.append(CampaignSequence(
#                     seq_number=1,
#                     seq_delay_details=SeqDelayDetails(delay_in_days=1),
#                     seq_variants=[
#                         SeqVariant(
#                             subject=response.introduction.subject,
#                             email_body=intro_body,
#                             variant_label="A"
#                         )
#                     ]
#                 ))
            
#             # Sequence 2: Outreach
#             if hasattr(response, 'email_sequence_2') and response.email_sequence_2:
#                 seq2_body = response.email_sequence_2.body
#                 if isinstance(seq2_body, dict):
#                     seq2_body = str(seq2_body)
                
#                 sequences.append(CampaignSequence(
#                     seq_number=2,
#                     seq_delay_details=SeqDelayDetails(delay_in_days=3),
#                     subject="",
#                     email_body=seq2_body
#                 ))
            
#             # Sequence 3: Follow-up
#             if hasattr(response, 'email_sequence_3') and response.email_sequence_3:
#                 seq3_body = response.email_sequence_3.body
#                 if isinstance(seq3_body, dict):
#                     seq3_body = str(seq3_body)
                
#                 sequences.append(CampaignSequence(
#                     seq_number=3,
#                     seq_delay_details=SeqDelayDetails(delay_in_days=5),
#                     subject="",
#                     email_body=seq3_body
#                 ))
            
#             # Fill with templates if needed
#             while len(sequences) < 3:
#                 if len(sequences) == 0:
#                     sequences.append(CampaignSequence(
#                         seq_number=1,
#                         seq_delay_details=SeqDelayDetails(delay_in_days=1),
#                         seq_variants=[
#                             SeqVariant(
#                                 subject=f"Quick question about {job_description}",
#                                 email_body=f"""<p>Hi there,<br><br>
# I came across your profile and noticed your experience in {job_description}. 
# I'm reaching out because we have some exciting opportunities that might be a great fit for your background.<br><br>
# Before I share more details, I wanted to ask: Are you currently open to exploring new opportunities in this space?<br><br>
# Would you be interested in hearing more about the roles we have available?<br><br>
# Best regards,<br>
# [Your Name]</p>""",
#                                 variant_label="A"
#                             ),
#                             SeqVariant(
#                                 subject=f"Interested in {job_description} opportunities?",
#                                 email_body=f"""<p>Hello,<br><br>
# I hope this message finds you well. I'm a recruiter specializing in {job_description} positions.<br><br>
# I'd love to connect and share some opportunities that align with your expertise. 
# Are you currently open to exploring new roles in this space?<br><br>
# If so, I can send you specific details about the positions we have available.<br><br>
# Thanks,<br>
# [Your Name]</p>""",
#                                 variant_label="B"
#                             )
#                         ]
#                     ))
#                 elif len(sequences) == 1:
#                     sequences.append(CampaignSequence(
#                         seq_number=2,
#                         seq_delay_details=SeqDelayDetails(delay_in_days=3),
#                         subject="",
#                         email_body=f"""<p>Hi,<br><br>
# Thanks for your interest! Here are more details about the {job_description} opportunities:<br><br>
# <strong>Role Details:</strong><br>
# • [Specific responsibilities]<br>
# • [Required skills and experience]<br>
# • [Team and company information]<br><br>
# <strong>Benefits:</strong><br>
# • [Compensation and benefits]<br>
# • [Growth opportunities]<br>
# • [Work environment]<br><br>
# Would you be interested in a quick call to discuss this role in more detail?<br><br>
# Best regards,<br>
# [Your Name]</p>"""
#                     ))
#                 elif len(sequences) == 2:
#                     sequences.append(CampaignSequence(
#                         seq_number=3,
#                         seq_delay_details=SeqDelayDetails(delay_in_days=5),
#                         subject="",
#                         email_body=f"""<p>Hi,<br><br>
# Just wanted to follow up on the {job_description} opportunity I shared.<br><br>
# Have you had a chance to review the information? I'd love to hear your thoughts and answer any questions.<br><br>
# If you're interested, I can help schedule next steps. If not, no worries at all!<br><br>
# Thanks for your time!<br>
# [Your Name]</p>"""
#                     ))
            
#             return sequences
            
#         except Exception as parse_error:
#             print(f"JSON parsing failed: {parse_error}")
#             return await generate_template_sequences(job_description)
        
#     except Exception as e:
#         print(f"Error generating sequences with LLM: {str(e)}")
#         return await generate_template_sequences(job_description)

# def create_sequences_from_content(content: dict, job_description: str) -> List[CampaignSequence]:
#     """Create CampaignSequence objects from parsed LLM content"""
    
#     sequences = []
    
#     # Sequence 1: Introduction with A/B variants
#     if "sequence1_variant_a" in content and "sequence1_variant_b" in content:
#         variants = []
        
#         if "sequence1_variant_a" in content:
#             var_a = content["sequence1_variant_a"]
#             variants.append(SeqVariant(
#                 subject=var_a.get("subject", f"Quick question about {job_description}"),
#                 email_body=var_a.get("body", ""),
#                 variant_label="A"
#             ))
        
#         if "sequence1_variant_b" in content:
#             var_b = content["sequence1_variant_b"]
#             variants.append(SeqVariant(
#                 subject=var_b.get("subject", f"Interested in {job_description} opportunities?"),
#                 email_body=var_b.get("body", ""),
#                 variant_label="B"
#             ))
        
#         sequences.append(CampaignSequence(
#             seq_number=1,
#             seq_delay_details=SeqDelayDetails(delay_in_days=1),
#             seq_variants=variants
#         ))
    
#     # Sequence 2: Outreach
#     if "sequence2" in content:
#         seq2_body = content["sequence2"].get("body", "")
#         sequences.append(CampaignSequence(
#             seq_number=2,
#             seq_delay_details=SeqDelayDetails(delay_in_days=3),
#             subject="",
#             email_body=seq2_body
#         ))
    
#     # Sequence 3: Follow-up
#     if "sequence3" in content:
#         seq3_body = content["sequence3"].get("body", "")
#         sequences.append(CampaignSequence(
#             seq_number=3,
#             seq_delay_details=SeqDelayDetails(delay_in_days=5),
#             subject="",
#             email_body=seq3_body
#         ))
    
#     # Fill with templates if needed
#     while len(sequences) < 3:
#         if len(sequences) == 0:
#             sequences.append(CampaignSequence(
#                 seq_number=1,
#                 seq_delay_details=SeqDelayDetails(delay_in_days=1),
#                 seq_variants=[
#                     SeqVariant(
#                         subject=f"Quick question about {job_description}",
#                         email_body=f"""<p>Hi there,<br><br>
# I came across your profile and noticed your experience in {job_description}. 
# I'm reaching out because we have some exciting opportunities that might be a great fit for your background.<br><br>
# Before I share more details, I wanted to ask: Are you currently open to exploring new opportunities in this space?<br><br>
# Would you be interested in hearing more about the roles we have available?<br><br>
# Best regards,<br>
# [Your Name]</p>""",
#                         variant_label="A"
#                     ),
#                     SeqVariant(
#                         subject=f"Interested in {job_description} opportunities?",
#                         email_body=f"""<p>Hello,<br><br>
# I hope this message finds you well. I'm a recruiter specializing in {job_description} positions.<br><br>
# I'd love to connect and share some opportunities that align with your expertise. 
# Are you currently open to exploring new roles in this space?<br><br>
# If so, I can send you specific details about the positions we have available.<br><br>
# Thanks,<br>
# [Your Name]</p>""",
#                         variant_label="B"
#                     )
#                 ]
#             ))
#         elif len(sequences) == 1:
#             sequences.append(CampaignSequence(
#                 seq_number=2,
#                 seq_delay_details=SeqDelayDetails(delay_in_days=3),
#                 subject="",
#                 email_body=f"""<p>Hi,<br><br>
# Thanks for your interest! Here are more details about the {job_description} opportunities:<br><br>
# <strong>Role Details:</strong><br>
# • [Specific responsibilities]<br>
# • [Required skills and experience]<br>
# • [Team and company information]<br><br>
# <strong>Benefits:</strong><br>
# • [Compensation and benefits]<br>
# • [Growth opportunities]<br>
# • [Work environment]<br><br>
# Would you be interested in a quick call to discuss this role in more detail?<br><br>
# Best regards,<br>
# [Your Name]</p>"""
#             ))
#         elif len(sequences) == 2:
#             sequences.append(CampaignSequence(
#                 seq_number=3,
#                 seq_delay_details=SeqDelayDetails(delay_in_days=5),
#                 subject="",
#                 email_body=f"""<p>Hi,<br><br>
# Just wanted to follow up on the {job_description} opportunity I shared.<br><br>
# Have you had a chance to review the information? I'd love to hear your thoughts and answer any questions.<br><br>
# If you're interested, I can help schedule next steps. If not, no worries at all!<br><br>
# Thanks for your time!<br>
# [Your Name]</p>"""
#             ))
    
#     return sequences

# async def generate_template_sequences(job_description: str) -> List[CampaignSequence]:
#     """Generate template-based sequences as fallback"""
    
#     sequences = [
#         CampaignSequence(
#             seq_number=1,
#             seq_delay_details=SeqDelayDetails(delay_in_days=1),
#             seq_variants=[
#                 SeqVariant(
#                     subject=f"Quick question about {job_description}",
#                     email_body=f"""<p>Hi there,<br><br>
# I came across your profile and noticed your experience in {job_description}. 
# I'm reaching out because we have some exciting opportunities that might be a great fit for your background.<br><br>
# Before I share more details, I wanted to ask: Are you currently open to exploring new opportunities in this space?<br><br>
# Would you be interested in hearing more about the roles we have available?<br><br>
# Best regards,<br>
# [Your Name]</p>""",
#                     variant_label="A"
#                 ),
#                 SeqVariant(
#                     subject=f"Interested in {job_description} opportunities?",
#                     email_body=f"""<p>Hello,<br><br>
# I hope this message finds you well. I'm a recruiter specializing in {job_description} positions.<br><br>
# I'd love to connect and share some opportunities that align with your expertise. 
# Are you currently open to exploring new roles in this space?<br><br>
# If so, I can send you specific details about the positions we have available.<br><br>
# Thanks,<br>
# [Your Name]</p>""",
#                     variant_label="B"
#                 )
#             ]
#         ),
#         CampaignSequence(
#             seq_number=2,
#             seq_delay_details=SeqDelayDetails(delay_in_days=3),
#             subject="",
#             email_body=f"""<p>Hi,<br><br>
# Thanks for your interest! Here are more details about the {job_description} opportunities:<br><br>
# <strong>Role Details:</strong><br>
# • [Specific responsibilities]<br>
# • [Required skills and experience]<br>
# • [Team and company information]<br><br>
# <strong>Benefits:</strong><br>
# • [Compensation and benefits]<br>
# • [Growth opportunities]<br>
# • [Work environment]<br><br>
# Would you be interested in a quick call to discuss this role in more detail?<br><br>
# Best regards,<br>
# [Your Name]</p>"""
#         ),
#                 CampaignSequence(
#             seq_number=3,
#             seq_delay_details=SeqDelayDetails(delay_in_days=5),
#             subject="",
#             email_body=f"""<p>Hi,<br><br>
# Just wanted to follow up on the {job_description} opportunity I shared.<br><br>
# Have you had a chance to review the information? I'd love to hear your thoughts and answer any questions.<br><br>
# If you're interested, I can help schedule next steps. If not, no worries at all!<br><br>
# Thanks for your time!<br>
# [Your Name]</p>"""
#         )
#     ]
    
#     return sequences

# # ============================================================================
# # RATE LIMITING MIDDLEWARE
# # ============================================================================

# class RateLimiter:
#     def __init__(self, max_requests: int = 10, window_seconds: int = 2):
#         self.max_requests = max_requests
#         self.window_seconds = window_seconds
#         self.requests = []
    
#     def is_allowed(self) -> bool:
#         now = time.time()
#         # Remove old requests outside the window
#         self.requests = [req_time for req_time in self.requests if now - req_time < self.window_seconds]
        
#         if len(self.requests) >= self.max_requests:
#             return False
        
#         self.requests.append(now)
#         return True

# # Global rate limiter instance
# rate_limiter = RateLimiter(max_requests=10, window_seconds=2)

# @app.middleware("http")
# async def rate_limit_middleware(request: Request, call_next):
#     """Rate limiting middleware to respect Smartlead's API limits"""
#     if not rate_limiter.is_allowed():
#         return JSONResponse(
#             status_code=429,
#             content={
#                 "error": "Rate limit exceeded",
#                 "message": "Too many requests. Please wait before making another request.",
#                 "retry_after": 2
#             }
#         )
    
#     response = await call_next(request)
#     return response

# # ============================================================================
# # ERROR HANDLING
# # ============================================================================

# @app.exception_handler(HTTPException)
# async def http_exception_handler(request: Request, exc: HTTPException):
#     """Custom HTTP exception handler"""
#     return JSONResponse(
#         status_code=exc.status_code,
#         content={
#             "error": True,
#             "message": exc.detail,
#             "status_code": exc.status_code,
#             "timestamp": datetime.now().isoformat()
#         }
#     )

# @app.exception_handler(Exception)
# async def general_exception_handler(request: Request, exc: Exception):
#     """General exception handler"""
#     return JSONResponse(
#         status_code=500,
#         content={
#             "error": True,
#             "message": "Internal server error",
#             "detail": str(exc) if os.getenv("DEBUG", "false").lower() == "true" else "An unexpected error occurred",
#             "timestamp": datetime.now().isoformat()
#         }
#     )

# # ============================================================================
# # CUSTOM OPENAPI SCHEMA
# # ============================================================================

# def custom_openapi():
#     if app.openapi_schema:
#         return app.openapi_schema
    
#     openapi_schema = get_openapi(
#         title="Smartlead API - Complete Integration",
#         version="2.0.0",
#         description="""
#         # Smartlead API - Complete Integration
        
#         A comprehensive FastAPI wrapper for the Smartlead email automation platform.
        
#         ## Features
#         - **Campaign Management**: Create, update, and manage email campaigns
#         - **Lead Management**: Add, update, and manage leads across campaigns with AI-powered personalization
#         - **Sequence Management**: Create and manage email sequences with AI generation using job_id from database
#         - **Webhook Management**: Set up webhooks for real-time notifications
#         - **Analytics**: Get detailed campaign analytics and statistics
#         - **Email Account Management**: Manage email accounts and warmup
#         - **Client Management**: Handle client accounts and permissions
#         - **Job Management**: Fetch job details from database for sequence generation
        
#         ## AI-Powered Personalization
#         When adding leads to campaigns, the API automatically generates personalized welcome and closing messages using LLM (Language Model) based on candidate details. These messages are added to the custom_fields as:
#         - `Welcome_Message`: Personalized greeting based on candidate's background
#         - `Closing_Message`: Personalized closing statement
        
#         ## Sequence Generation with Job ID
#         The sequence generation endpoint now supports using `job_id` to fetch job details from the database and automatically build comprehensive job descriptions for AI-powered sequence generation. This eliminates the need to manually provide job descriptions.
        
#         ## Lead Schema
#         The lead schema supports the following structure:
#         ```json
#         {
#           "lead_list": [
#             {
#               "first_name": "Cristiano",
#               "last_name": "Ronaldo", 
#               "email": "[email protected]",
#               "phone_number": "0239392029",
#               "company_name": "Manchester United",
#               "website": "mufc.com",
#               "location": "London",
#               "custom_fields": {
#                 "Title": "Regional Manager",
#                 "First_Line": "Loved your recent post about remote work on Linkedin"
#               },
#               "linkedin_profile": "http://www.linkedin.com/in/cristianoronaldo",
#               "company_url": "mufc.com"
#             }
#           ],
#           "settings": {
#             "ignore_global_block_list": true,
#             "ignore_unsubscribe_list": true,
#             "ignore_duplicate_leads_in_other_campaign": false
#           }
#         }
#         ```
        
#         ## Authentication
#         All requests require a Smartlead API key passed as a query parameter: `?api_key=YOUR_API_KEY`
        
#         ## Rate Limits
#         - 10 requests per 2 seconds (enforced automatically)
        
#         ## Base URL
#         - Smartlead API: `https://server.smartlead.ai/api/v1`
#         """,
#         routes=app.routes,
#     )
    
#     # Add custom tags
#     openapi_schema["tags"] = [
#         {"name": "Campaigns", "description": "Campaign management operations"},
#         {"name": "Leads", "description": "Lead management operations"},
#         {"name": "Sequences", "description": "Email sequence management"},
#         {"name": "Webhooks", "description": "Webhook management"},
#         {"name": "Clients", "description": "Client account management"},
#         {"name": "Messages", "description": "Message history and reply operations"},
#         {"name": "Analytics", "description": "Campaign analytics and statistics"},
#         {"name": "Email Accounts", "description": "Email account management"},
#         {"name": "Jobs", "description": "Job management operations"},
#         {"name": "Utilities", "description": "Utility endpoints"}
#     ]
    
#     app.openapi_schema = openapi_schema
#     return app.openapi_schema

# app.openapi = custom_openapi

# # ============================================================================
# # MAIN APPLICATION ENTRY POINT
# # ============================================================================

# if __name__ == "__main__":
#     import uvicorn
    
#     print("�� Starting Smartlead API - Complete Integration")
#     print(f"�� API Documentation: http://localhost:8000/docs")
#     print(f"📖 ReDoc Documentation: http://localhost:8000/redoc")
#     print(f"�� Smartlead Base URL: {SMARTLEAD_BASE_URL}")
#     print(f"⚡ Rate Limit: 10 requests per 2 seconds")
    
#     uvicorn.run(
#         "updated-final:app",
#         host="0.0.0.0",
#         port=8000,
#         reload=True,
#         log_level="info"
#     )


import os
import re
import json
import time
import asyncio
from datetime import datetime
from typing import List, Dict, Any, Optional, Union
from pydantic import BaseModel, Field, EmailStr, field_validator
from fastapi import FastAPI, HTTPException, Query, Depends, Request
from fastapi.responses import JSONResponse, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
import httpx
from dotenv import load_dotenv
import pandas as pd
import psycopg2
from sqlalchemy import create_engine, inspect, text

# LangChain and OpenAI imports
try:
    from langchain_openai import ChatOpenAI
    from langchain.prompts import ChatPromptTemplate
    LANGCHAIN_AVAILABLE = True
except ImportError:
    LANGCHAIN_AVAILABLE = False
    print("Warning: LangChain not available. Install with: pip install langchain langchain-openai")

load_dotenv()

# Configuration
SMARTLEAD_API_KEY = os.getenv("SMARTLEAD_API_KEY", "your-api-key-here")
SMARTLEAD_BASE_URL = "https://server.smartlead.ai/api/v1"
DB_PARAMS = {
    'dbname': os.getenv("DB_NAME"),
    'user': os.getenv("DB_USER"),
    'password': os.getenv("DB_PASSWORD"),
    'host': os.getenv("DB_HOST"),
    'port': os.getenv("DB_PORT")
}
DATABASE_URL = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}"

# Initialize FastAPI app
app = FastAPI(
    title="Smartlead API - Complete Integration",
    version="2.1.0",
    description="Comprehensive FastAPI wrapper for Smartlead email automation platform",
    docs_url="/docs",
    redoc_url="/redoc"
)


# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================================================
# DATA MODELS
# ============================================================================

class CreateCampaignRequest(BaseModel):
    name: str = Field(..., description="Campaign name")
    client_id: Optional[int] = Field(None, description="Client ID (leave null if no client)")

class CampaignScheduleRequest(BaseModel):
    timezone: str = Field(..., description="Timezone for the campaign schedule (e.g., 'America/Los_Angeles')")
    days_of_the_week: List[int] = Field(..., description="Days of the week for scheduling [0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday]")
    start_hour: str = Field(..., description="Start hour for sending emails in HH:MM format (e.g., '09:00')")
    end_hour: str = Field(..., description="End hour for sending emails in HH:MM format (e.g., '18:00')")
    min_time_btw_emails: int = Field(..., description="Minimum time in minutes between sending emails")
    max_new_leads_per_day: int = Field(..., description="Maximum number of new leads to process per day")
    schedule_start_time: str = Field(..., description="Schedule start time in ISO 8601 format (e.g., '2023-04-25T07:29:25.978Z')")

class CampaignSettingsRequest(BaseModel):
    track_settings: List[str] = Field(..., description="Tracking settings array (allowed values: DONT_TRACK_EMAIL_OPEN, DONT_TRACK_LINK_CLICK, DONT_TRACK_REPLY_TO_AN_EMAIL)")
    stop_lead_settings: str = Field(..., description="Settings for stopping leads (allowed values: CLICK_ON_A_LINK, OPEN_AN_EMAIL)")
    unsubscribe_text: str = Field(..., description="Text for the unsubscribe link")
    send_as_plain_text: bool = Field(..., description="Whether emails should be sent as plain text")
    follow_up_percentage: int = Field(ge=0, le=100, description="Follow-up percentage (max 100, min 0)")
    client_id: Optional[int] = Field(None, description="Client ID (leave as null if not needed)")
    enable_ai_esp_matching: bool = Field(False, description="Enable AI ESP matching (by default is false)")

class LeadInput(BaseModel):
    first_name: Optional[str] = Field(None, description="Lead's first name")
    last_name: Optional[str] = Field(None, description="Lead's last name")
    email: str = Field(..., description="Lead's email address")
    phone_number: Optional[Union[str, int]] = Field(None, description="Lead's phone number (can be string or integer)")
    company_name: Optional[str] = Field(None, description="Lead's company name")
    website: Optional[str] = Field(None, description="Lead's website")
    location: Optional[str] = Field(None, description="Lead's location")
    custom_fields: Optional[Dict[str, str]] = Field(None, description="Custom fields as key-value pairs (max 20 fields)")
    linkedin_profile: Optional[str] = Field(None, description="Lead's LinkedIn profile URL")
    company_url: Optional[str] = Field(None, description="Company website URL")
    
    @field_validator('custom_fields')
    @classmethod
    def validate_custom_fields(cls, v):
        if v is not None and len(v) > 20:
            raise ValueError('Custom fields cannot exceed 20 fields')
        return v
    
    @field_validator('phone_number')
    @classmethod
    def validate_phone_number(cls, v):
        if v is not None:
            return str(v)
        return v

class LeadSettings(BaseModel):
    ignore_global_block_list: bool = Field(True, description="Ignore leads if they are in the global block list")
    ignore_unsubscribe_list: bool = Field(True, description="Ignore leads if they are in the unsubscribe list")
    ignore_duplicate_leads_in_other_campaign: bool = Field(False, description="Allow leads to be added even if they are duplicates in other campaigns")

class AddLeadsRequest(BaseModel):
    lead_list: List[LeadInput] = Field(..., max_items=100, description="List of leads to add (maximum 100 leads)")
    settings: Optional[LeadSettings] = Field(None, description="Settings for lead processing")

class AddLeadsResponse(BaseModel):
    ok: bool = Field(..., description="Indicates if the operation was successful")
    upload_count: int = Field(..., description="Number of leads successfully uploaded")
    total_leads: int = Field(..., description="Total number of leads attempted to upload")
    already_added_to_campaign: int = Field(..., description="Number of leads already present in the campaign")
    duplicate_count: int = Field(..., description="Number of duplicate emails found")
    invalid_email_count: int = Field(..., description="Number of leads with invalid email format")
    unsubscribed_leads: Any = Field(..., description="Number of leads that had previously unsubscribed (can be int or empty list)")

class SeqDelayDetails(BaseModel):
    delay_in_days: int = Field(..., description="Delay in days before sending this sequence")

class SeqVariant(BaseModel):
    subject: str = Field(..., description="Email subject line")
    email_body: str = Field(..., description="Email body content (HTML format)")
    variant_label: str = Field(..., description="Variant label (A, B, C, etc.)")
    id: Optional[int] = Field(None, description="Variant ID (only for updating, not for creating)")

class CampaignSequence(BaseModel):
    id: Optional[int] = Field(None, description="Sequence ID (only for updating, not for creating)")
    seq_number: int = Field(..., description="Sequence number (1, 2, 3, etc.)")
    seq_delay_details: SeqDelayDetails = Field(..., description="Delay details for this sequence")
    seq_variants: Optional[List[SeqVariant]] = Field(None, description="Email variants for A/B testing")
    subject: Optional[str] = Field("", description="Subject line (blank for follow-up in same thread)")
    email_body: Optional[str] = Field(None, description="Email body content (HTML format)")

class SaveSequencesRequest(BaseModel):
    sequences: List[CampaignSequence] = Field(..., description="List of campaign sequences")

class Job(BaseModel):
    id: str
    company_name: Optional[str] = None
    job_title: Optional[str] = None
    job_location: Optional[str] = None
    company_blurb: Optional[str] = None
    company_culture: Optional[str] = None
    description: Optional[str] = None
    company_size: Optional[str] = None
    requirements: Optional[str] = None
    salary: Optional[str] = None

class GenerateSequencesRequest(BaseModel):
    job_id: str = Field(..., description="Job ID to fetch from database and generate sequences for")

class Campaign(BaseModel):
    id: int
    user_id: int
    created_at: datetime
    updated_at: datetime
    status: str
    name: str
    track_settings: Union[str, List[Any]]
    scheduler_cron_value: Optional[Union[str, Dict[str, Any]]] = None
    min_time_btwn_emails: int
    max_leads_per_day: int
    stop_lead_settings: str
    unsubscribe_text: Optional[str] = None
    client_id: Optional[int] = None
    enable_ai_esp_matching: bool
    send_as_plain_text: bool
    follow_up_percentage: Optional[Union[str, int]] = None

class CampaignListResponse(BaseModel):
    campaigns: List[Campaign]
    total: int
    source: str

class Lead(BaseModel):
    id: int
    email: EmailStr
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    company: Optional[str] = None
    position: Optional[str] = None
    phone_number: Optional[str] = None
    linkedin_url: Optional[str] = None
    status: Optional[str] = None

class WarmupDetails(BaseModel):
    status: str
    total_sent_count: int
    total_spam_count: int
    warmup_reputation: str
    warmup_key_id: Optional[str] = None
    warmup_created_at: Optional[datetime] = None
    reply_rate: int
    blocked_reason: Optional[str] = None

class EmailAccount(BaseModel):
    id: int
    created_at: datetime
    updated_at: datetime
    user_id: int
    from_name: str
    from_email: str
    username: str
    password: Optional[str] = None
    smtp_host: Optional[str] = None
    smtp_port: Optional[int] = None
    smtp_port_type: Optional[str] = None
    message_per_day: int
    different_reply_to_address: Optional[str] = None
    is_different_imap_account: bool
    imap_username: Optional[str] = None
    imap_password: Optional[str] = None
    imap_host: Optional[str] = None
    imap_port: Optional[int] = None
    imap_port_type: Optional[str] = None
    signature: Optional[str] = None
    custom_tracking_domain: Optional[str] = None
    bcc_email: Optional[str] = None
    is_smtp_success: bool
    is_imap_success: bool
    smtp_failure_error: Optional[str] = None
    imap_failure_error: Optional[str] = None
    type: str
    daily_sent_count: int
    client_id: Optional[int] = None
    campaign_count: Optional[int] = None
    warmup_details: Optional[WarmupDetails] = None

class LeadCategoryUpdateRequest(BaseModel):
    category_id: int = Field(..., description="Category ID to assign to the lead")
    pause_lead: bool = Field(False, description="Whether to pause the lead after category update")

class CampaignStatusUpdateRequest(BaseModel):
    status: str = Field(..., description="New campaign status (PAUSED, STOPPED, START)")

class ResumeLeadRequest(BaseModel):
    resume_lead_with_delay_days: Optional[int] = Field(None, description="Delay in days before resuming (defaults to 0)")

class DomainBlockListRequest(BaseModel):
    domain_block_list: List[str] = Field(..., description="List of domains/emails to block")
    client_id: Optional[int] = Field(None, description="Client ID if blocking is client-specific")

class WebhookRequest(BaseModel):
    id: Optional[int] = Field(None, description="Webhook ID (null for creating new)")
    name: str = Field(..., description="Webhook name")
    webhook_url: str = Field(..., description="Webhook URL")
    event_types: List[str] = Field(..., description="List of event types to listen for")
    categories: Optional[List[str]] = Field(None, description="List of categories to filter by")

class WebhookDeleteRequest(BaseModel):
    id: int = Field(..., description="Webhook ID to delete")

class ClientRequest(BaseModel):
    name: str = Field(..., description="Client name")
    email: str = Field(..., description="Client email")
    permission: List[str] = Field(..., description="List of permissions")
    logo: Optional[str] = Field(None, description="Client logo text")
    logo_url: Optional[str] = Field(None, description="Client logo URL")
    password: str = Field(..., description="Client password")

class MessageHistoryRequest(BaseModel):
    email_stats_id: str = Field(..., description="Email stats ID for the specific email")
    email_body: str = Field(..., description="Reply message email body")
    reply_message_id: str = Field(..., description="Message ID to reply to")
    reply_email_time: str = Field(..., description="Time of the message being replied to")
    reply_email_body: str = Field(..., description="Body of the message being replied to")
    cc: Optional[str] = Field(None, description="CC recipients")
    bcc: Optional[str] = Field(None, description="BCC recipients")
    add_signature: bool = Field(True, description="Whether to add signature")

class AddLeadsAndSequencesRequest(BaseModel):
    lead_list: List[LeadInput] = Field(..., max_items=100, description="List of leads to add (maximum 100 leads)")
    settings: Optional[LeadSettings] = Field(None, description="Settings for lead processing")
    job_id: str = Field(..., description="Job ID to fetch from database and generate sequences for")

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

def get_database_connection():
    """Get database connection"""
    try:
        conn_string = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}"
        return create_engine(conn_string)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Database connection failed: {str(e)}")

def fetch_job_by_id(job_id: str) -> Job:
    """Fetch job details from database by ID using pandas DataFrame"""
    try:
        conn = get_database_connection()
        df = pd.read_sql_table("jobs", con=conn)
        
        # Filter the DataFrame to find the job with the specified ID
        job_row = df[df['job_id'] == job_id]
        
        if job_row.empty:
            raise HTTPException(status_code=404, detail=f"Job with ID {job_id} not found")
        
        # Get the first (and should be only) row
        
        return Job(
            id=str(job_row.iloc[0]['job_id']),
            company_name=str(job_row.iloc[0]['company_name']) if pd.notna(job_row.iloc[0]['company_name']) else None,
            job_title=str(job_row.iloc[0]['job_title']) if pd.notna(job_row.iloc[0]['job_title']) else None,
            job_location=str(job_row.iloc[0]['job_location']) if pd.notna(job_row.iloc[0]['job_location']) else None,
            company_blurb=str(job_row.iloc[0]['company_blurb']) if pd.notna(job_row.iloc[0]['company_blurb']) else None,
            company_culture=str(job_row.iloc[0]['company_culture']) if pd.notna(job_row.iloc[0]['company_culture']) else None,
            description=str(job_row.iloc[0]['description']) if pd.notna(job_row.iloc[0]['description']) else None,
            company_size=str(job_row.iloc[0]['company_size']) if pd.notna(job_row.iloc[0]['company_size']) else None,
            requirements=str(job_row.iloc[0]['requirements']) if pd.notna(job_row.iloc[0]['requirements']) else None,
            salary=str(job_row.iloc[0]['salary']) if pd.notna(job_row.iloc[0]['salary']) else None
        )
        
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching job: {str(e)}")

def list_available_jobs(limit: int = 10) -> List[Dict[str, Any]]:
    """List available jobs from database using pandas DataFrame"""
    try:
        conn = get_database_connection()
        df = pd.read_sql_table("jobs", con=conn)
        
        # Select only the required columns and limit the results
        selected_columns = ['job_id', 'company_name', 'job_title', 'job_location', 'company_size', 'salary']
        df_subset = df[selected_columns].head(limit)
        
        # Convert DataFrame to list of dictionaries
        jobs_list = []
        for _, row in df_subset.iterrows():
            jobs_list.append({
                "id": str(row['job_id']),  # Ensure ID is returned as string
                "company_name": str(row['company_name']) if pd.notna(row['company_name']) else None,
                "job_title": str(row['job_title']) if pd.notna(row['job_title']) else None,
                "job_location": str(row['job_location']) if pd.notna(row['job_location']) else None,
                "company_size": str(row['company_size']) if pd.notna(row['company_size']) else None,
                "salary": str(row['salary']) if pd.notna(row['salary']) else None
            })
        
        return jobs_list
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error fetching jobs: {str(e)}")

def build_job_description(job: Job) -> str:
    """Build a comprehensive job description from job details"""
    parts = []
    
    if job.company_name:
        parts.append(f"Company: {job.company_name}")
    
    if job.job_title:
        parts.append(f"Position: {job.job_title}")
    
    if job.job_location:
        parts.append(f"Location: {job.job_location}")
    
    if job.company_size:
        parts.append(f"Company Size: {job.company_size}")
    
    if job.salary:
        parts.append(f"Salary: {job.salary}")
    
    if job.company_blurb:
        parts.append(f"About the Company: {job.company_blurb}")
    
    if job.company_culture:
        parts.append(f"Company Culture: {job.company_culture}")
    
    if job.description:
        parts.append(f"Job Description: {job.description}")
    
    if job.requirements:
        parts.append(f"Requirements: {job.requirements}")
    
    return "\n\n".join(parts)

def _get_smartlead_url(endpoint: str) -> str:
    return f"{SMARTLEAD_BASE_URL}/{endpoint.lstrip('/')}"

async def call_smartlead_api(method: str, endpoint: str, data: Any = None, params: Dict[str, Any] = None) -> Any:
    if SMARTLEAD_API_KEY == "your-api-key-here":
        raise HTTPException(status_code=400, detail="Smartlead API key not configured")
    if params is None:
        params = {}
    params['api_key'] = SMARTLEAD_API_KEY
    url = _get_smartlead_url(endpoint)
    
    try:
        async with httpx.AsyncClient(timeout=30.0) as client:
            if method.upper() in ("GET", "DELETE"):
                resp = await client.request(method, url, params=params)
            else:
                resp = await client.request(method, url, params=params, json=data)
        
        if resp.status_code >= 400:
            try:
                error_data = resp.json()
                error_message = error_data.get('message', error_data.get('error', 'Unknown error'))
                raise HTTPException(status_code=resp.status_code, detail=error_message)
            except (ValueError, KeyError):
                raise HTTPException(status_code=resp.status_code, detail=resp.text)
        
        return resp.json()
        
    except httpx.TimeoutException:
        raise HTTPException(status_code=408, detail="Request to Smartlead API timed out")
    except httpx.RequestError as e:
        raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}")

# ============================================================================
# CAMPAIGN ENDPOINTS
# ============================================================================

@app.post("/campaigns/create", response_model=Dict[str, Any], tags=["Campaigns"])
async def create_campaign(campaign: CreateCampaignRequest):
    """Create a new campaign in Smartlead"""
    return await call_smartlead_api("POST", "campaigns/create", data=campaign.dict())

@app.get("/campaigns", response_model=CampaignListResponse, tags=["Campaigns"])
async def list_campaigns():
    """Fetch all campaigns from Smartlead API"""
    campaigns = await call_smartlead_api("GET", "campaigns")
    return {"campaigns": campaigns, "total": len(campaigns), "source": "smartlead"}

@app.get("/campaigns/{campaign_id}", response_model=Campaign, tags=["Campaigns"])
async def get_campaign(campaign_id: int):
    """Get Campaign By Id"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}")

@app.post("/campaigns/{campaign_id}/settings", response_model=Dict[str, Any], tags=["Campaigns"])
async def update_campaign_settings(campaign_id: int, settings: CampaignSettingsRequest):
    """Update Campaign General Settings"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/settings", data=settings.dict())

@app.post("/campaigns/{campaign_id}/schedule", response_model=Dict[str, Any], tags=["Campaigns"])
async def schedule_campaign(campaign_id: int, schedule: CampaignScheduleRequest):
    """Update Campaign Schedule"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/schedule", data=schedule.dict())

@app.delete("/campaigns/{campaign_id}", response_model=Dict[str, Any], tags=["Campaigns"])
async def delete_campaign(campaign_id: int):
    """Delete Campaign"""
    return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}")

@app.post("/campaigns/{campaign_id}/status", response_model=Dict[str, Any], tags=["Campaigns"])
async def patch_campaign_status(campaign_id: int, request: CampaignStatusUpdateRequest):
    """Patch campaign status"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/status", data=request.dict())

@app.get("/campaigns/{campaign_id}/analytics", response_model=Any, tags=["Analytics"])
async def campaign_analytics(campaign_id: int):
    """Fetch analytics for a campaign"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics")

@app.get("/campaigns/{campaign_id}/statistics", response_model=Dict[str, Any], tags=["Analytics"])
async def fetch_campaign_statistics_by_campaign_id(
    campaign_id: int, 
    offset: int = 0, 
    limit: int = 100,
    email_sequence_number: Optional[int] = None,
    email_status: Optional[str] = None
):
    """Fetch Campaign Statistics By Campaign Id"""
    params = {"offset": offset, "limit": limit}
    if email_sequence_number:
        params["email_sequence_number"] = email_sequence_number
    if email_status:
        params["email_status"] = email_status
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/statistics", params=params)

@app.get("/campaigns/{campaign_id}/analytics-by-date", response_model=Dict[str, Any], tags=["Analytics"])
async def fetch_campaign_statistics_by_date_range(
    campaign_id: int, 
    start_date: str, 
    end_date: str
):
    """Fetch Campaign Statistics By Campaign Id And Date Range"""
    params = {"start_date": start_date, "end_date": end_date}
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics-by-date", params=params)

# ============================================================================
# LEAD MANAGEMENT ENDPOINTS
# ============================================================================

@app.get("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"])
async def get_campaign_leads(campaign_id: int, offset: int = 0, limit: int = 100):
    """List all leads by campaign id"""
    params = {"offset": offset, "limit": limit}
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads", params=params)

# *** MODIFIED: add_leads_to_campaign now uses asyncio.gather for performance ***
@app.post("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"])
async def add_leads_to_campaign(campaign_id: int, request: AddLeadsRequest):
    """Add leads to a campaign by ID with personalized welcome and closing messages"""
    
    async def process_lead(lead: Dict[str, Any]) -> Dict[str, Any]:
        """Inner function to process a single lead."""
        lead_cleaned = {k: v for k, v in lead.items() if v is not None and v != ""}
        
        try:
            personalized_messages = await generate_welcome_closing_messages(lead_cleaned)
            if "custom_fields" not in lead_cleaned:
                lead_cleaned["custom_fields"] = {}
            lead_cleaned["custom_fields"]["Welcome_Message"] = personalized_messages.get("welcome_message", "")
            lead_cleaned["custom_fields"]["Closing_Message"] = personalized_messages.get("closing_message", "")
        except Exception as e:
            print(f"Error generating AI messages for {lead.get('email')}: {e}. Falling back to template.")
            template_messages = generate_template_welcome_closing_messages(lead_cleaned)
            if "custom_fields" not in lead_cleaned:
                lead_cleaned["custom_fields"] = {}
            lead_cleaned["custom_fields"]["Welcome_Message"] = template_messages["welcome_message"]
            lead_cleaned["custom_fields"]["Closing_Message"] = template_messages["closing_message"]
        
        return lead_cleaned

    # Create a list of concurrent tasks for AI processing
    tasks = [process_lead(lead.dict()) for lead in request.lead_list]
    processed_leads = await asyncio.gather(*tasks)

    # Prepare the final request data for Smartlead
    request_data = {
        "lead_list": processed_leads,
        "settings": request.settings.dict() if request.settings else LeadSettings().dict()
    }
    
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads", data=request_data)

@app.post("/campaigns/{campaign_id}/leads/bulk", response_model=Dict[str, Any], tags=["Leads"])
async def add_bulk_leads(campaign_id: int, leads: List[LeadInput]):
    """Add multiple leads to a Smartlead campaign with personalized messages (legacy endpoint)"""
    request = AddLeadsRequest(lead_list=leads)
    return await add_leads_to_campaign(campaign_id, request)

@app.post("/campaigns/{campaign_id}/leads-and-sequences", response_model=Dict[str, Any], tags=["Leads"])
async def add_leads_and_generate_sequences(campaign_id: int, request: AddLeadsAndSequencesRequest):
    """Add leads to campaign and immediately generate informed sequences using their data"""
    
    # Step 1: Add leads with personalized messages
    leads_request = AddLeadsRequest(lead_list=request.lead_list, settings=request.settings)
    leads_result = await add_leads_to_campaign(campaign_id, leads_request)
    
    # Step 2: Fetch job details and generate informed sequences
    try:
        job = fetch_job_by_id(request.job_id)
        job_description = build_job_description(job)
        generated_sequences = await generate_sequences_with_llm(job_description, campaign_id)
        save_request = SaveSequencesRequest(sequences=generated_sequences)
        sequences_result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict())
        
    except Exception as e:
        print(f"Error generating sequences after adding leads: {str(e)}")
        # Fallback to generic sequences
        job = fetch_job_by_id(request.job_id)
        job_description = build_job_description(job)
        generated_sequences = await generate_sequences_with_llm(job_description)
        save_request = SaveSequencesRequest(sequences=generated_sequences)
        sequences_result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict())
    
    return {
        "ok": True,
        "message": "Leads added and informed sequences generated successfully",
        "leads_result": leads_result,
        "sequences_result": sequences_result,
        "generated_sequences": [seq for seq in generated_sequences]
    }

@app.post("/campaigns/{campaign_id}/leads/{lead_id}/resume", response_model=Dict[str, Any], tags=["Leads"])
async def resume_lead_by_campaign_id(campaign_id: int, lead_id: int, request: ResumeLeadRequest):
    """Resume Lead By Campaign ID"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/resume", data=request.dict())

@app.post("/campaigns/{campaign_id}/leads/{lead_id}/pause", response_model=Dict[str, Any], tags=["Leads"])
async def pause_lead_by_campaign_id(campaign_id: int, lead_id: int):
    """Pause Lead By Campaign ID"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/pause")

@app.delete("/campaigns/{campaign_id}/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"])
async def delete_lead_by_campaign_id(campaign_id: int, lead_id: int):
    """Delete Lead By Campaign ID"""
    return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/leads/{lead_id}")

@app.post("/campaigns/{campaign_id}/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"])
async def unsubscribe_lead_from_campaign(campaign_id: int, lead_id: int):
    """Unsubscribe/Pause Lead From Campaign"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/unsubscribe")

@app.post("/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"])
async def unsubscribe_lead_from_all_campaigns(lead_id: int):
    """Unsubscribe Lead From All Campaigns"""
    return await call_smartlead_api("POST", f"leads/{lead_id}/unsubscribe")

@app.post("/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"])
async def update_lead(lead_id: int, lead_data: Dict[str, Any]):
    """Update lead using the Lead ID"""
    return await call_smartlead_api("POST", f"leads/{lead_id}", data=lead_data)

@app.post("/campaigns/{campaign_id}/leads/{lead_id}/category", response_model=Dict[str, Any], tags=["Leads"])
async def update_lead_category_by_campaign(campaign_id: int, lead_id: int, request: LeadCategoryUpdateRequest):
    """Update a lead's category based on their campaign"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/category", data=request.dict())

@app.post("/leads/add-domain-block-list", response_model=Dict[str, Any], tags=["Leads"])
async def add_domain_to_global_block_list(request: DomainBlockListRequest):
    """Add Lead/Domain to Global Block List"""
    return await call_smartlead_api("POST", "leads/add-domain-block-list", data=request.dict())

@app.get("/leads/fetch-categories", response_model=List[Dict[str, Any]], tags=["Leads"])
async def fetch_lead_categories():
    """Fetch lead categories"""
    return await call_smartlead_api("GET", "leads/fetch-categories")

@app.get("/leads", response_model=Dict[str, Any], tags=["Leads"])
async def fetch_lead_by_email_address(email: str):
    """Fetch lead by email address"""
    return await call_smartlead_api("GET", "leads", params={"email": email})

@app.get("/leads/{lead_id}/campaigns", response_model=List[Dict[str, Any]], tags=["Leads"])
async def campaigns_for_lead(lead_id: int):
    """Fetch all campaigns that a lead belongs to"""
    return await call_smartlead_api("GET", f"leads/{lead_id}/campaigns")

@app.get("/campaigns/{campaign_id}/leads/check", response_model=Dict[str, Any], tags=["Leads"])
async def check_lead_in_campaign(campaign_id: int, email: str):
    """Check if a lead exists in a campaign using efficient indexed lookups"""
    try:
        lead_response = await call_smartlead_api("GET", "leads", params={"email": email})
        
        if not lead_response or "id" not in lead_response:
            return {"exists": False, "message": "Lead not found"}
        
        lead_id = lead_response["id"]
        campaigns_response = await call_smartlead_api("GET", f"leads/{lead_id}/campaigns")
        
        if not campaigns_response:
            return {"exists": False, "message": "No campaigns found for lead"}
        
        campaign_exists = any(campaign.get("id") == campaign_id for campaign in campaigns_response)
        
        return {"exists": campaign_exists, "message": "Lead found in campaign" if campaign_exists else "Lead not found in campaign"}
        
    except HTTPException as e:
        if e.status_code == 404:
            return {"exists": False, "message": "Lead not found"}
        raise e
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error checking lead in campaign: {str(e)}")

@app.get("/campaigns/{campaign_id}/leads-export", tags=["Leads"])
async def export_data_from_campaign(campaign_id: int):
    """Export data from a campaign as CSV"""
    if SMARTLEAD_API_KEY == "your-api-key-here":
        raise HTTPException(status_code=400, detail="Smartlead API key not configured")
    
    url = _get_smartlead_url(f"campaigns/{campaign_id}/leads-export")
    params = {"api_key": SMARTLEAD_API_KEY}
    
    try:
        async with httpx.AsyncClient(timeout=30.0) as client:
            resp = await client.get(url, params=params)
        
        if resp.status_code >= 400:
            try:
                error_data = resp.json()
                error_message = error_data.get('message', error_data.get('error', 'Unknown error'))
                raise HTTPException(status_code=resp.status_code, detail=error_message)
            except (ValueError, KeyError):
                raise HTTPException(status_code=resp.status_code, detail=resp.text)
        
        return Response(
            content=resp.text,
            media_type="text/csv",
            headers={"Content-Disposition": f"attachment; filename=campaign_{campaign_id}_leads.csv"}
        )
        
    except httpx.TimeoutException:
        raise HTTPException(status_code=408, detail="Request to Smartlead API timed out")
    except httpx.RequestError as e:
        raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}")

# ============================================================================
# SEQUENCE ENDPOINTS
# ============================================================================

@app.get("/campaigns/{campaign_id}/sequences", response_model=Any, tags=["Sequences"])
async def get_campaign_sequences(campaign_id: int):
    """Fetch email sequences for a campaign"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/sequences")

@app.post("/campaigns/{campaign_id}/sequences", response_model=Dict[str, Any], tags=["Sequences"])
async def save_campaign_sequences(campaign_id: int, request: SaveSequencesRequest):
    """Save Campaign Sequence"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=request.dict())

# *** MODIFIED: generate_campaign_sequences now uses the corrected AI function ***
@app.post("/campaigns/{campaign_id}/sequences/generate", response_model=Dict[str, Any], tags=["Sequences"])
async def generate_campaign_sequences(campaign_id: int, request: GenerateSequencesRequest):
    """Generate a campaign sequence template using AI that leverages personalized custom fields."""
    job_id = request.job_id
    
    # Fetch job details from database
    job = fetch_job_by_id(job_id)
    
    # Build comprehensive job description from job details
    job_description = build_job_description(job)
    
    # Generate the smart template
    generated_sequences = await generate_sequences_with_llm(job_description, campaign_id)
    
    # Save the template to the campaign
    save_request = SaveSequencesRequest(sequences=generated_sequences)
    result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict())
    
    return {
        "ok": True,
        "message": "Sequence template generated and saved successfully. It will use personalized fields for each lead.",
        "generated_sequences": [seq for seq in generated_sequences],
        "save_result": result
    }

# ============================================================================
# WEBHOOK ENDPOINTS
# ============================================================================

@app.get("/campaigns/{campaign_id}/webhooks", response_model=List[Dict[str, Any]], tags=["Webhooks"])
async def fetch_webhooks_by_campaign_id(campaign_id: int):
    """Fetch Webhooks By Campaign ID"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/webhooks")

@app.post("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"])
async def add_update_campaign_webhook(campaign_id: int, request: WebhookRequest):
    """Add / Update Campaign Webhook"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/webhooks", data=request.dict())

@app.delete("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"])
async def delete_campaign_webhook(campaign_id: int, request: WebhookDeleteRequest):
    """Delete Campaign Webhook"""
    return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/webhooks", data=request.dict())

# ============================================================================
# CLIENT MANAGEMENT ENDPOINTS
# ============================================================================

@app.post("/client/save", response_model=Dict[str, Any], tags=["Clients"])
async def add_client_to_system(request: ClientRequest):
    """Add Client To System (Whitelabel or not)"""
    return await call_smartlead_api("POST", "client/save", data=request.dict())

@app.get("/client", response_model=List[Dict[str, Any]], tags=["Clients"])
async def fetch_all_clients():
    """Fetch all clients"""
    return await call_smartlead_api("GET", "client")

# ============================================================================
# MESSAGE HISTORY AND REPLY ENDPOINTS
# ============================================================================

@app.get("/campaigns/{campaign_id}/leads/{lead_id}/message-history", response_model=Dict[str, Any], tags=["Messages"])
async def fetch_lead_message_history_based_on_campaign(campaign_id: int, lead_id: int):
    """Fetch Lead Message History Based On Campaign"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads/{lead_id}/message-history")

@app.post("/campaigns/{campaign_id}/reply-email-thread", response_model=Dict[str, Any], tags=["Messages"])
async def reply_to_lead_from_master_inbox(campaign_id: int, request: MessageHistoryRequest):
    """Reply To Lead From Master Inbox via API"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/reply-email-thread", data=request.dict())

# ============================================================================
# EMAIL ACCOUNT ENDPOINTS
# ============================================================================

@app.get("/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"])
async def list_email_accounts(offset: int = 0, limit: int = 100):
    """List all email accounts with optional pagination"""
    params = {"offset": offset, "limit": limit}
    return await call_smartlead_api("GET", "email-accounts", params=params)

@app.post("/email-accounts/save", response_model=Any, tags=["Email Accounts"])
async def save_email_account(account: Dict[str, Any]):
    """Create an Email Account"""
    return await call_smartlead_api("POST", "email-accounts/save", data=account)

@app.get("/email-accounts/{account_id}", response_model=EmailAccount, tags=["Email Accounts"])
async def get_email_account(account_id: int):
    """Fetch Email Account By ID"""
    return await call_smartlead_api("GET", f"email-accounts/{account_id}")

@app.post("/email-accounts/{account_id}", response_model=Any, tags=["Email Accounts"])
async def update_email_account(account_id: int, payload: Dict[str, Any]):
    """Update Email Account"""
    return await call_smartlead_api("POST", f"email-accounts/{account_id}", data=payload)

@app.post("/email-accounts/{account_id}/warmup", response_model=Any, tags=["Email Accounts"])
async def set_warmup(account_id: int, payload: Dict[str, Any]):
    """Add/Update Warmup To Email Account"""
    return await call_smartlead_api("POST", f"email-accounts/{account_id}/warmup", data=payload)

@app.get("/email-accounts/{account_id}/warmup-stats", response_model=Any, tags=["Email Accounts"])
async def get_warmup_stats(account_id: int):
    """Fetch Warmup Stats By Email Account ID"""
    return await call_smartlead_api("GET", f"email-accounts/{account_id}/warmup-stats")

@app.get("/campaigns/{campaign_id}/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"])
async def list_campaign_email_accounts(campaign_id: int):
    """List all email accounts per campaign"""
    return await call_smartlead_api("GET", f"campaigns/{campaign_id}/email-accounts")

@app.post("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"])
async def add_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]):
    """Add Email Account To A Campaign"""
    return await call_smartlead_api("POST", f"campaigns/{campaign_id}/email-accounts", data=payload)

@app.delete("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"])
async def remove_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]):
    """Remove Email Account From A Campaign"""
    return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/email-accounts", data=payload)

@app.post("/email-accounts/reconnect-failed-email-accounts", response_model=Dict[str, Any], tags=["Email Accounts"])
async def reconnect_failed_email_accounts():
    """Reconnect failed email accounts"""
    return await call_smartlead_api("POST", "email-accounts/reconnect-failed-email-accounts")

# ============================================================================
# UTILITY ENDPOINTS
# ============================================================================

@app.get("/health", response_model=Dict[str, Any], tags=["Utilities"])
async def health_check():
    """Health check endpoint to verify API connectivity"""
    try:
        campaigns = await call_smartlead_api("GET", "campaigns")
        return {
            "status": "healthy",
            "message": "Smartlead API is accessible",
            "campaigns_count": len(campaigns) if isinstance(campaigns, list) else 0,
            "timestamp": datetime.now().isoformat()
        }
    except Exception as e:
        return {
            "status": "unhealthy",
            "message": f"Smartlead API connection failed: {str(e)}",
            "timestamp": datetime.now().isoformat()
        }

@app.get("/api-info", response_model=Dict[str, Any], tags=["Utilities"])
async def api_info():
    """Get information about the API and available endpoints"""
    return {
        "name": "Smartlead API - Complete Integration",
        "version": "2.0.0",
        "description": "Comprehensive FastAPI wrapper for Smartlead email automation platform",
        "base_url": SMARTLEAD_BASE_URL,
        "available_endpoints": [
            "Campaign Management",
            "Lead Management", 
            "Sequence Management",
            "Webhook Management",
            "Client Management",
            "Message History & Reply",
            "Analytics",
            "Email Account Management"
        ],
        "documentation": "Based on Smartlead API documentation",
        "timestamp": datetime.now().isoformat()
    }

@app.get("/jobs", response_model=List[Dict[str, Any]], tags=["Jobs"])
async def get_available_jobs(limit: int = Query(10, ge=1, le=100, description="Number of jobs to return")):
    """List available jobs from the database"""
    return list_available_jobs(limit)

@app.get("/jobs/{job_id}", response_model=Job, tags=["Jobs"])
async def get_job_by_id(job_id: str):
    """Get job details by ID"""
    return fetch_job_by_id(job_id)

# ============================================================================
# AI SEQUENCE GENERATION FUNCTIONS
# ============================================================================

async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
    class structure(BaseModel):
        welcome_message: str = Field(description="Welcome message for the candidate")
        closing_message: str = Field(description="Closing message for the candidate")

    """Generate personalized welcome and closing messages using LLM based on candidate details"""
    
    if not LANGCHAIN_AVAILABLE:
        return generate_template_welcome_closing_messages(lead_data)
    
    try:
        openai_api_key = os.getenv("OPENAI_API_KEY")
        if not openai_api_key:
            print("Warning: OPENAI_API_KEY not set. Using template messages.")
            return generate_template_welcome_closing_messages(lead_data)
        
        llm = ChatOpenAI(
            model="gpt-4o",
            temperature=0.7,
            openai_api_key=openai_api_key
        )
        str_llm = llm.with_structured_output(structure)
        
        first_name = lead_data.get("first_name", "")
        company_name = lead_data.get("company_name", "")
        title = lead_data.get("custom_fields", {}).get("Title", "")
        
        candidate_info = f"Name: {first_name}, Company: {company_name}, Title: {title}"
        
        system_prompt = """You are an expert recruiter creating personalized messages. Generate a 2-sentence welcome message and a 1-sentence closing message. Be professional and friendly and sound like real human recruitor. Reference their background. Respond with ONLY valid JSON.
        Start with Hi then first name and then from next line write the message."""
        
        prompt_template = ChatPromptTemplate.from_messages([
            ("system", system_prompt),
            ("human", "Generate messages for this candidate: {candidate_info}")
        ])
        
        messages = prompt_template.format_messages(candidate_info=candidate_info)
        response = await str_llm.ainvoke(messages)
        
        return {
            "welcome_message": response.welcome_message,
            "closing_message": response.closing_message
        }
    except Exception as e:
        print(f"Error generating welcome/closing messages with LLM: {str(e)}")
        return generate_template_welcome_closing_messages(lead_data)

def generate_template_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]:
    """Generate template-based welcome and closing messages as fallback"""
    first_name = lead_data.get("first_name", "there")
    welcome_message = f"Hi {first_name}, I came across your profile and was impressed by your background."
    closing_message = f"Looking forward to connecting with you, {first_name}!"
    return {"welcome_message": welcome_message, "closing_message": closing_message}

# *** MODIFIED: generate_sequences_with_llm now creates a smart template ***
async def generate_sequences_with_llm(job_description: str, campaign_id: Optional[int] = None) -> List[CampaignSequence]:
    """Generate an email sequence template using LangChain and OpenAI, optionally informed by campaign lead data."""
    
    class EmailContent(BaseModel):
        subject: str = Field(description="Subject line for the email")
        body: str = Field(description="Body of the email, using placeholders")

    class SequenceStructure(BaseModel):
        introduction: EmailContent
        follow_up_1: EmailContent
        follow_up_2: EmailContent

    if not LANGCHAIN_AVAILABLE:
        return generate_template_sequences(job_description)
    
    try:
        openai_api_key = os.getenv("OPENAI_API_KEY")
        if not openai_api_key:
            print("Warning: OPENAI_API_KEY not set. Using template sequences.")
            return generate_template_sequences(job_description)
        
        # If campaign_id is provided, fetch lead data to inform the template
        lead_context = ""
        if campaign_id:
            try:
                leads_response = await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads", params={"limit": 10})
                campaign_leads = leads_response.get("leads", []) if isinstance(leads_response, dict) else leads_response
                
                if campaign_leads:
                    # Sample lead data to inform template generation
                    sample_leads = campaign_leads[:3]
                    lead_info = []
                    for lead in sample_leads:
                        custom_fields = lead.get("custom_fields", {})
                        lead_info.append({
                            "first_name": lead.get("first_name", ""),
                            "company": lead.get("company_name", ""),
                            "title": custom_fields.get("Title", ""),
                            "welcome_msg": custom_fields.get("Welcome_Message", ""),
                            "closing_msg": custom_fields.get("Closing_Message", "")
                        })
                    
                    lead_context = f"\n\nCampaign Lead Context (sample of {len(campaign_leads)} leads):\n{json.dumps(lead_info, indent=2)}"
                    
            except Exception as e:
                print(f"Could not fetch lead data for campaign {campaign_id}: {e}")
        
        llm = ChatOpenAI(model="gpt-4o", temperature=0.7, openai_api_key=openai_api_key)
        structured_llm = llm.with_structured_output(SequenceStructure)
        
        system_prompt = """You are an expert email sequence template generator for recruitment campaigns on behalf of 'Ali Taghikhani, CEO SRN'. Your task is to generate a 3-step email sequence template for a given job description. 

Email Sequence Structure:
1. INTRODUCTION (Day 1): Ask for consent and interest in the role, In the starting use the welcome message placeholder after the salutation, and in the end use closing message template along with the name and title of sender
2. OUTREACH (Day 3): Provide detailed job information
3. FOLLOW-UP (Day 5): Follow up on updates and next steps

Requirements:
- First sequence will only ask about the consent and interest in the role, mentioning the company name, role title, and location type (onsite/hybrid/remote)
- Second and third sequences are follow-ups (no subject line needed) providing more details about the role, company mission, and salary information (if available)
- All emails should be HTML formatted with proper <br> tags
- Professional but friendly tone
- Each email must include a clear call-to-action: "If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP."
- Focus on building consent and trust

**CRITICAL FORMATTING RULES:**
1. **PLACEHOLDER FORMAT IS ESSENTIAL:** You MUST use double curly braces for all placeholders.
   - **CORRECT:** `{{first_name}}`
   - **INCORRECT:** `{first_name}` or `[first_name]` or `<first_name>`

2. **REQUIRED PLACEHOLDERS:** You MUST include `{{Welcome_Message}}` and `{{Closing_Message}}` in the first email. You should also use `{{first_name}}` in follow-ups.

3. **FIRST EMAIL STRUCTURE:** The first email's body MUST begin with `{{Welcome_Message}}` and end with `{{Closing_Message}}`.

4. **SIGNATURE:** End EVERY email body with `<br><br>Best regards`.

5. **CONTENT REQUIREMENTS:** Each email must mention:
   - Company name
   - Role title
   - Work location (onsite/hybrid/remote)
   - Company mission (in emails 2 and 3)
   - Salary information (if available, in emails 2 and 3)

6. **CALL-TO-ACTION:** Each email must end with the CTA (before the signature): "If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP."

7. **EXAMPLE BODY:**
```html
{{Welcome_Message}}<br><br>I saw your profile and was impressed. We have an opening for a Senior Engineer at [Company Name] that seems like a great fit. This is a [remote/hybrid/onsite] position.<br><br>If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP.<br><br>{{Closing_Message}}<br><br>Best regards,<br>Ali Taghikhani<br>CEO, SRN
```

Always try to start the message with the salutation except for the first email. If lead context is provided, use it to make the templates more relevant. Respond with ONLY a valid JSON object matching the required structure.
        """
        
        prompt = ChatPromptTemplate.from_messages([
            ("system", system_prompt),
            ("human", "Generate the 3-step email sequence template for this job description: {job_description}{lead_context}")
        ])
        
        # By using .partial, we tell LangChain to treat the Smartlead placeholders as literals
        # and not expect them as input variables. This is the correct way to handle this.
        partial_prompt = prompt.partial(
            first_name="", 
            company_name="",
            **{"Welcome_Message": "", "Closing_Message": "", "Title": ""}
        )

        chain = partial_prompt | structured_llm
        response = await chain.ainvoke({"job_description": job_description, "lead_context": lead_context})
        
        # Post-process the AI's response to enforce double curly braces.
        # This is a robust way to fix the AI's tendency to use single braces.
        def fix_braces(text: str) -> str:
            if not text:
                return ""
            # This regex finds all occurrences of `{...}` that are not `{{...}}`
            # and replaces them with `{{...}}`.
            return re.sub(r'{([^{}\n]+)}', r'{{\1}}', text)

        sequences = [
            CampaignSequence(
                seq_number=1,
                seq_delay_details=SeqDelayDetails(delay_in_days=1),
                seq_variants=[SeqVariant(
                    subject=fix_braces(response.introduction.subject), 
                    email_body=fix_braces(response.introduction.body), 
                    variant_label="A"
                )]
            ),
            CampaignSequence(
                seq_number=2,
                seq_delay_details=SeqDelayDetails(delay_in_days=3),
                subject="", # Same thread
                email_body=fix_braces(response.follow_up_1.body)
            ),
            CampaignSequence(
                seq_number=3,
                seq_delay_details=SeqDelayDetails(delay_in_days=5),
                subject="", # Same thread
                email_body=fix_braces(response.follow_up_2.body)
            )
        ]
        return sequences
        
    except Exception as e:
        print(f"Error generating sequences with LLM: {str(e)}. Falling back to template.")
        return generate_template_sequences(job_description)

def generate_template_sequences(job_description: str) -> List[CampaignSequence]:
    """Generate template-based sequences as fallback, using correct placeholders."""
    
    # This is the corrected structure for the first email
    first_email_body = f"""<p>{{{{custom.Welcome_Message}}}}<br><br>
I'm reaching out because we have some exciting opportunities for a {job_description} that might be a great fit for your background. Are you currently open to exploring new roles?<br><br>
{{{{custom.Closing_Message}}}}<br><br>
Best regards,<br>Ali Taghikhani<br>CEO, SRN</p>"""

    follow_up_1_body = f"""<p>Hi {{{{first_name}}}},<br><br>
Just wanted to follow up on my previous email regarding the {job_description} role. I'd love to hear your thoughts when you have a moment.<br><br>
Best regards,<br>Ali Taghikhani<br>CEO, SRN</p>"""

    follow_up_2_body = f"""<p>Hi {{{{first_name}}}},<br><br>
Checking in one last time about the {job_description} opportunity. If the timing isn't right, no worries at all. Otherwise, I look forward to hearing from you.<br><br>
Best regards,<br>Ali Taghikhani<br>CEO, SRN</p>"""

    sequences = [
        CampaignSequence(
            seq_number=1,
            seq_delay_details=SeqDelayDetails(delay_in_days=1),
            seq_variants=[
                SeqVariant(
                    subject=f"Regarding a {job_description} opportunity",
                    email_body=first_email_body,
                    variant_label="A"
                )
            ]
        ),
        CampaignSequence(
            seq_number=2,
            seq_delay_details=SeqDelayDetails(delay_in_days=3),
            subject="",
            email_body=follow_up_1_body
        ),
        CampaignSequence(
            seq_number=3,
            seq_delay_details=SeqDelayDetails(delay_in_days=5),
            subject="",
            email_body=follow_up_2_body
        )
    ]
    return sequences

# ============================================================================
# RATE LIMITING MIDDLEWARE
# ============================================================================

class RateLimiter:
    def __init__(self, max_requests: int = 10, window_seconds: int = 2):
        self.max_requests = max_requests
        self.window_seconds = window_seconds
        self.requests = []
    
    def is_allowed(self) -> bool:
        now = time.time()
        self.requests = [req_time for req_time in self.requests if now - req_time < self.window_seconds]
        if len(self.requests) >= self.max_requests:
            return False
        self.requests.append(now)
        return True

rate_limiter = RateLimiter(max_requests=10, window_seconds=2)

@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
    """Rate limiting middleware to respect Smartlead's API limits"""
    if not rate_limiter.is_allowed():
        return JSONResponse(
            status_code=429,
            content={"error": "Rate limit exceeded"}
        )
    response = await call_next(request)
    return response

# ============================================================================
# ERROR HANDLING
# ============================================================================

@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    """Custom HTTP exception handler"""
    return JSONResponse(
        status_code=exc.status_code,
        content={"error": True, "message": exc.detail}
    )

@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
    """General exception handler"""
    return JSONResponse(
        status_code=500,
        content={
            "error": True, 
            "message": "Internal server error",
            "detail": str(exc) if os.getenv("DEBUG") else None
        }
    )

# ============================================================================
# CUSTOM OPENAPI SCHEMA
# ============================================================================

def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    
    openapi_schema = get_openapi(
        title="Smartlead API - Complete Integration",
        version="2.1.0",
        description="A comprehensive FastAPI wrapper for the Smartlead email automation platform.",
        routes=app.routes,
    )
    
    openapi_schema["tags"] = [
        {"name": "Campaigns", "description": "Campaign management operations"},
        {"name": "Leads", "description": "Lead management operations"},
        {"name": "Sequences", "description": "Email sequence management"},
        {"name": "Webhooks", "description": "Webhook management"},
        {"name": "Clients", "description": "Client account management"},
        {"name": "Messages", "description": "Message history and reply operations"},
        {"name": "Analytics", "description": "Campaign analytics and statistics"},
        {"name": "Email Accounts", "description": "Email account management"},
        {"name": "Utilities", "description": "Utility endpoints"}
    ]
    
    app.openapi_schema = openapi_schema
    return app.openapi_schema

app.openapi = custom_openapi

# ============================================================================
# MAIN APPLICATION ENTRY POINT
# ============================================================================

if __name__ == "__main__":
    import uvicorn
    
    print("Starting Smartlead API - Complete Integration")
    uvicorn.run(
        "__main__:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )