File size: 72,837 Bytes
ad13c80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# bot_concours_unified.py - Bot de concours unifié et optimisé
# Version complète avec toutes les optimisations + launcher intelligent intégré

import asyncio
import aiohttp
from playwright.async_api import async_playwright, Page
from bs4 import BeautifulSoup
import requests
import tweepy
import pandas as pd
import time
import re
from datetime import datetime, timedelta
from huggingface_hub import InferenceClient
import os
import sys
import random
# import google.generativeai as genai  # Supprimé - utilisation d'alternatives locales
import sqlite3
import logging
import imaplib
import email
import smtplib
from email.mime.text import MIMEText
import schedule
import hashlib
import json
import threading
import subprocess
from contextlib import contextmanager
from dataclasses import dataclass
from typing import List, Dict, Optional, Tuple
from urllib.parse import urljoin, urlparse

# =====================================================
# CONFIGURATION ET DATACLASSES
# =====================================================

@dataclass
class PersonalInfo:
    prenom: str = "Valentin"
    nom: str = "Cora"
    email: str = "valouassol@outlook.com"
    email_derivee: str = "valouassol+concours@outlook.com"
    telephone: str = "+41791234567"
    adresse: str = "Av Chantemerle 9"
    code_postal: str = "1009"
    ville: str = "Pully"
    pays: str = "Suisse"

@dataclass
class APIConfig:
    hf_token: Optional[str] = None
    x_token: Optional[str] = None
    google_api_key: Optional[str] = None
    google_cx: Optional[str] = None
    gemini_key: Optional[str] = None
    email_app_password: Optional[str] = None
    telegram_bot_token: Optional[str] = None
    telegram_chat_id: Optional[str] = None

    @classmethod
    def from_env(cls):
        return cls(
            hf_token=os.getenv("HF_TOKEN"),
            x_token=os.getenv("X_TOKEN"),
            google_api_key=os.getenv("GOOGLE_API_KEY"),
            google_cx=os.getenv("GOOGLE_CX"),
            gemini_key=os.getenv("GEMINI_KEY"),
            email_app_password=os.getenv("EMAIL_APP_PASSWORD"),
            telegram_bot_token=os.getenv("TELEGRAM_BOT_TOKEN"),
            telegram_chat_id=os.getenv("TELEGRAM_CHAT_ID")
        )

@dataclass
class Contest:
    title: str
    url: str
    description: str
    source: str
    deadline: Optional[str] = None
    prize: Optional[str] = None
    difficulty_score: int = 0

@dataclass
class FormField:
    selector: str
    field_type: str
    label: str
    required: bool
    current_value: str = ""
    ai_context: str = ""

@dataclass
class FormAnalysis:
    fields: List[FormField]
    complexity_score: int
    estimated_success_rate: float
    requires_captcha: bool
    requires_social_media: bool
    form_url: str

# =====================================================
# CONFIGURATION GLOBALE
# =====================================================

# Configuration
PERSONAL_INFO = PersonalInfo()
API_CONFIG = APIConfig.from_env()

# Sites suisses à scraper
SITES_CH = [
    'https://www.concours.ch/concours/tous',
    'https://www.jeu-concours.biz/concours-pays_suisse.html',
    'https://www.loisirs.ch/concours/',
    'https://www.radin.ch/',
    'https://win4win.ch/fr/',
    'https://www.concours-suisse.ch/',
    'https://corporate.migros.ch/fr/concours',
    'https://www.20min.ch/fr/concours-et-jeux',
    'https://dein-gewinnspiel.ch/en',
    'https://www.myswitzerland.com/fr/planification/vie-pratique/concours/'
]

# Proxies et User Agents
PROXY_LIST = ["http://20.206.106.192:80", "http://51.15.242.202:8888"]
USER_AGENTS = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
]

# Logging setup
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('concours_bot.log'),
        logging.StreamHandler()
    ]
)

# =====================================================
# SYSTÈME DE CONFIGURATION ET TEST DES APIs
# =====================================================

class APITester:
    def __init__(self, api_config: APIConfig):
        self.api_config = api_config
    
    def test_all_apis(self) -> Dict[str, bool]:
        """Teste toutes les APIs configurées"""
        results = {}
        
        print("🔍 Test de la configuration des APIs...")
        print("=" * 40)
        
        results['gemini'] = self._test_gemini()
        results['telegram'] = self._test_telegram()
        results['google_search'] = self._test_google_search()
        results['huggingface'] = self._test_huggingface()
        results['email'] = self._test_email_config()
        
        return results
    
    def _test_gemini(self) -> bool:
        """Test de l'API Gemini - DÉSACTIVÉ"""
        print("🤖 Test Gemini API... DÉSACTIVÉ (utilisation d'alternatives locales)")
        print("✅ Système de réponses locales activé")
        return True  # Toujours vrai car on utilise le système local
    
    def _test_telegram(self) -> bool:
        """Test du Bot Telegram"""
        print("\n📱 Test Telegram Bot...")
        
        if not self.api_config.telegram_bot_token:
            print("❌ TELEGRAM_BOT_TOKEN non configuré")
            return False
        
        if not self.api_config.telegram_chat_id:
            print("❌ TELEGRAM_CHAT_ID non configuré")
            return False
        
        try:
            url = f"https://api.telegram.org/bot{self.api_config.telegram_bot_token}/getMe"
            response = requests.get(url, timeout=10)
            
            if response.status_code == 200:
                bot_info = response.json()
                print(f"✅ Bot connecté: {bot_info['result']['first_name']}")
                
                # Test d'envoi de message
                message_url = f"https://api.telegram.org/bot{self.api_config.telegram_bot_token}/sendMessage"
                test_message = {
                    'chat_id': self.api_config.telegram_chat_id,
                    'text': '🎉 Test de configuration réussi ! Votre bot de concours est prêt.'
                }
                
                msg_response = requests.post(message_url, json=test_message, timeout=10)
                if msg_response.status_code == 200:
                    print("✅ Message de test envoyé avec succès")
                    return True
                else:
                    print(f"❌ Erreur envoi message: {msg_response.text}")
                    return False
            else:
                print(f"❌ Erreur connexion bot: {response.text}")
                return False
                
        except Exception as e:
            print(f"❌ Erreur Telegram: {e}")
            return False
    
    def _test_google_search(self) -> bool:
        """Test de l'API Google Search"""
        print("\n🔍 Test Google Search API...")
        
        if not self.api_config.google_api_key:
            print("❌ GOOGLE_API_KEY non configurée")
            return False
        
        if not self.api_config.google_cx:
            print("❌ GOOGLE_CX non configuré")
            return False
        
        try:
            url = "https://www.googleapis.com/customsearch/v1"
            params = {
                "key": self.api_config.google_api_key,
                "cx": self.api_config.google_cx,
                "q": "concours suisse test",
                "num": 1
            }
            
            response = requests.get(url, params=params, timeout=10)
            
            if response.status_code == 200:
                results = response.json()
                if 'items' in results:
                    print(f"✅ Google Search fonctionne: {len(results['items'])} résultat(s)")
                    return True
                else:
                    print("⚠️ Google Search configuré mais aucun résultat")
                    return True
            else:
                print(f"❌ Erreur Google Search: {response.text}")
                return False
                
        except Exception as e:
            print(f"❌ Erreur Google Search: {e}")
            return False
    
    def _test_huggingface(self) -> bool:
        """Test de l'API HuggingFace"""
        print("\n🤗 Test HuggingFace API...")
        
        if not self.api_config.hf_token:
            print("❌ HF_TOKEN non configuré")
            return False
        
        try:
            client = InferenceClient(token=self.api_config.hf_token)
            result = client.text_generation(
                "Bonjour, comment allez-vous?", 
                model="gpt2", 
                max_new_tokens=10
            )
            print("✅ HuggingFace fonctionne")
            return True
        except Exception as e:
            print(f"❌ Erreur HuggingFace: {e}")
            return False
    
    def _test_email_config(self) -> bool:
        """Test de la configuration Email"""
        print("\n📧 Test Configuration Email...")
        
        if not self.api_config.email_app_password:
            print("⚠️ EMAIL_APP_PASSWORD non configuré (optionnel)")
            return False
        
        if len(self.api_config.email_app_password) >= 10:
            print("✅ Mot de passe Email configuré")
            return True
        else:
            print("❌ Mot de passe Email semble invalide")
            return False

# =====================================================
# LAUNCHER INTELLIGENT INTÉGRÉ
# =====================================================

class SmartLauncher:
    def __init__(self):
        self.api_config = API_CONFIG
        self.api_tester = APITester(self.api_config)
        self.api_status = self._check_api_configuration()
        
    def _check_api_configuration(self) -> Dict[str, bool]:
        """Vérifie quelles APIs sont configurées"""
        return {
            'gemini': bool(self.api_config.gemini_key),
            'telegram': bool(self.api_config.telegram_bot_token and self.api_config.telegram_chat_id),
            'google_search': bool(self.api_config.google_api_key and self.api_config.google_cx),
            'huggingface': bool(self.api_config.hf_token),
            'email': bool(self.api_config.email_app_password)
        }
    
    def display_status(self):
        """Affiche le statut de configuration"""
        print("🔍 DÉTECTION DE LA CONFIGURATION")
        print("=" * 40)
        
        descriptions = {
            'gemini': "Gemini API (IA intelligente)",
            'telegram': "Telegram Bot (Alertes)",
            'google_search': "Google Search (Plus de concours)",
            'huggingface': "HuggingFace (IA backup)",
            'email': "Email Analysis (Détection gains)"
        }
        
        configured_count = 0
        for api, configured in self.api_status.items():
            icon = "✅" if configured else "❌"
            desc = descriptions[api]
            print(f"{icon} {desc}")
            if configured:
                configured_count += 1
        
        print(f"\n📊 APIs configurées: {configured_count}/5")
        return configured_count
    
    def calculate_effectiveness(self) -> int:
        """Calcule l'efficacité estimée du bot"""
        base_effectiveness = 40  # Sans APIs
        
        if self.api_status['gemini']:
            base_effectiveness += 30
        if self.api_status['telegram']:
            base_effectiveness += 10
        if self.api_status['google_search']:
            base_effectiveness += 15
        if self.api_status['huggingface']:
            base_effectiveness += 3
        if self.api_status['email']:
            base_effectiveness += 2
        
        return min(base_effectiveness, 100)
    
    def show_performance_impact(self):
        """Montre l'impact sur les performances"""
        print("\n🎯 IMPACT SUR LES PERFORMANCES:")
        print("-" * 40)
        
        if self.api_status['gemini']:
            print("✅ Réponses IA personnalisées et intelligentes")
        else:
            print("⚠️ Utilisation de réponses préprogrammées")
        
        if self.api_status['telegram']:
            print("✅ Alertes en temps réel des victoires")
        else:
            print("⚠️ Pas d'alertes automatiques")
        
        if self.api_status['google_search']:
            print("✅ Recherche étendue de concours")
        else:
            print("⚠️ Scraping limité aux sites directs")
        
        effectiveness = self.calculate_effectiveness()
        print(f"\n📈 Efficacité estimée: {effectiveness}%")
    
    def show_setup_guide(self):
        """Affiche le guide de configuration"""
        print("\n📖 GUIDE DE CONFIGURATION DES APIs GRATUITES")
        print("=" * 50)
        
        print("\n🤖 1. GEMINI API (Google) - PRIORITÉ HAUTE")
        print("   • Aller sur: https://aistudio.google.com/")
        print("   • Se connecter avec Google")
        print("   • Cliquer 'Get API Key'")
        print("   • Copier la clé (AIza...)")
        print("   • Variable: GEMINI_KEY")
        
        print("\n📱 2. TELEGRAM BOT - PRIORITÉ HAUTE")
        print("   • Chercher @BotFather sur Telegram")
        print("   • Envoyer: /newbot")
        print("   • Suivre les instructions")
        print("   • Chercher @userinfobot pour Chat ID")
        print("   • Variables: TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID")
        
        print("\n🔍 3. GOOGLE SEARCH API - PRIORITÉ MOYENNE")
        print("   • Aller sur: console.cloud.google.com")
        print("   • Activer Custom Search API")
        print("   • Créer une clé API")
        print("   • Créer un moteur de recherche sur: cse.google.com")
        print("   • Variables: GOOGLE_API_KEY, GOOGLE_CX")
        
        print("\n🤗 4. HUGGINGFACE TOKEN - PRIORITÉ BASSE")
        print("   • Créer compte sur: huggingface.co")
        print("   • Aller dans Settings > Tokens")
        print("   • Créer un token 'Read'")
        print("   • Variable: HF_TOKEN")
        
        print("\n📧 5. EMAIL OUTLOOK - OPTIONNEL")
        print("   • Aller sur: outlook.live.com")
        print("   • Paramètres > Sécurité")
        print("   • Créer mot de passe d'application")
        print("   • Variable: EMAIL_APP_PASSWORD")
    
    def launch_options(self):
        """Affiche les options de lancement"""
        print("\n🚀 OPTIONS DISPONIBLES:")
        print("-" * 30)
        print("1️⃣ Lancer le bot avec la configuration actuelle")
        print("2️⃣ Tester toutes les APIs configurées")
        print("3️⃣ Afficher le guide de configuration")
        print("4️⃣ Configuration rapide PowerShell")
        print("5️⃣ Quitter")
    
    def show_powershell_config(self):
        """Affiche la configuration PowerShell rapide"""
        print("\n⚡ CONFIGURATION RAPIDE POWERSHELL")
        print("=" * 40)
        print("Copiez-collez ces commandes dans PowerShell (remplacez par vos vraies clés):")
        print()
        print('$env:GEMINI_KEY="AIzaSyC-VOTRE-CLE-GEMINI"')
        print('$env:TELEGRAM_BOT_TOKEN="123456:ABC-VOTRE-TOKEN"')
        print('$env:TELEGRAM_CHAT_ID="VOTRE-CHAT-ID"')
        print('$env:GOOGLE_API_KEY="AIzaSyD-VOTRE-CLE-GOOGLE"')
        print('$env:GOOGLE_CX="VOTRE-SEARCH-ENGINE-ID"')
        print('$env:HF_TOKEN="hf_VOTRE-TOKEN"')
        print('$env:EMAIL_APP_PASSWORD="VOTRE-MOT-DE-PASSE"')
        print()
        print("⚠️ Redémarrez votre terminal après configuration")
    
    def main_menu(self):
        """Menu principal intelligent"""
        print("🎰 BOT DE CONCOURS SUISSE - LANCEUR INTELLIGENT")
        print("=" * 55)
        
        configured_count = self.display_status()
        self.show_performance_impact()
        
        if configured_count == 0:
            print("\n⚠️ AUCUNE API CONFIGURÉE")
            print("Le bot fonctionnera en mode basique uniquement.")
            print("Recommandation: Configurez au moins Gemini + Telegram")
            
        elif configured_count < 3:
            print("\n💡 CONFIGURATION PARTIELLE")
            print("Pour de meilleures performances, configurez plus d'APIs.")
            
        else:
            print("\n🎉 EXCELLENTE CONFIGURATION !")
            print("Votre bot est prêt pour des performances optimales.")
        
        self.launch_options()
        
        while True:
            choice = input("\n🎯 Votre choix (1-5): ").strip()
            
            if choice == "1":
                print("\n🎬 Lancement du bot...")
                return True  # Lancer le bot
                
            elif choice == "2":
                print("\n🧪 Test des APIs...")
                results = self.api_tester.test_all_apis()
                working_count = sum(results.values())
                print(f"\n📊 Résumé: {working_count}/5 APIs fonctionnelles")
                
                if working_count >= 2:
                    print("🚀 Prêt à lancer le bot !")
                else:
                    print("⚠️ Configurez au moins 2 APIs pour de meilleurs résultats")
                
            elif choice == "3":
                self.show_setup_guide()
                
            elif choice == "4":
                self.show_powershell_config()
                
            elif choice == "5":
                print("👋 À bientôt !")
                return False
                
            else:
                print("❌ Veuillez choisir 1, 2, 3, 4 ou 5")

# =====================================================
# GESTIONNAIRE DE BASE DE DONNÉES
# =====================================================

class DatabaseManager:
    def __init__(self, db_path: str = 'concours_optimized.sqlite'):
        self.db_path = db_path
        self.local = threading.local()
        self._init_db()
    
    def _get_connection(self):
        if not hasattr(self.local, 'conn'):
            self.local.conn = sqlite3.connect(self.db_path)
            self.local.conn.row_factory = sqlite3.Row
        return self.local.conn
    
    @contextmanager
    def transaction(self):
        conn = self._get_connection()
        try:
            yield conn
            conn.commit()
        except Exception:
            conn.rollback()
            raise
    
    def _init_db(self):
        with self.transaction() as conn:
            conn.execute('''

                CREATE TABLE IF NOT EXISTS participations (

                    url TEXT PRIMARY KEY,

                    title TEXT,

                    source TEXT,

                    status TEXT,

                    difficulty_score INTEGER,

                    success_rate REAL,

                    date TEXT,

                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

                )

            ''')
            conn.execute('''

                CREATE TABLE IF NOT EXISTS victories (

                    email_id TEXT PRIMARY KEY,

                    date TEXT,

                    lot TEXT,

                    source TEXT,

                    confirmed BOOLEAN DEFAULT FALSE,

                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP

                )

            ''')
            conn.execute('''

                CREATE TABLE IF NOT EXISTS contest_cache (

                    url TEXT PRIMARY KEY,

                    content TEXT,

                    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP

                )

            ''')
            conn.execute('CREATE INDEX IF NOT EXISTS idx_participations_date ON participations(date)')
            conn.execute('CREATE INDEX IF NOT EXISTS idx_cache_timestamp ON contest_cache(timestamp)')
    
    def add_participation(self, contest: Contest, status: str = 'pending', success_rate: float = 0.0):
        with self.transaction() as conn:
            conn.execute('''

                INSERT OR REPLACE INTO participations 

                (url, title, source, status, difficulty_score, success_rate, date) 

                VALUES (?, ?, ?, ?, ?, ?, date('now'))

            ''', (contest.url, contest.title, contest.source, status, contest.difficulty_score, success_rate))
    
    def participation_exists(self, url: str) -> bool:
        conn = self._get_connection()
        result = conn.execute("SELECT 1 FROM participations WHERE url = ?", (url,)).fetchone()
        return result is not None
    
    def add_victory(self, email_id: str, lot: str, source: str):
        with self.transaction() as conn:
            conn.execute('''

                INSERT OR IGNORE INTO victories (email_id, date, lot, source) 

                VALUES (?, date('now'), ?, ?)

            ''', (email_id, lot, source))
    
    def get_stats(self) -> Dict:
        conn = self._get_connection()
        stats = {}
        
        stats['total_participations'] = conn.execute("SELECT COUNT(*) FROM participations").fetchone()[0]
        stats['successful_participations'] = conn.execute("SELECT COUNT(*) FROM participations WHERE status='success'").fetchone()[0]
        stats['total_victories'] = conn.execute("SELECT COUNT(*) FROM victories").fetchone()[0]
        
        source_stats = conn.execute('''

            SELECT source, COUNT(*) as count 

            FROM participations 

            GROUP BY source 

            ORDER BY count DESC

        ''').fetchall()
        stats['by_source'] = {row[0]: row[1] for row in source_stats}
        
        return stats

# =====================================================
# MOTEUR IA AMÉLIORÉ
# =====================================================

class AIEngine:
    def __init__(self, api_config: APIConfig):
        self.api_config = api_config
        self.cache = {}
    
    def generate_response(self, question: str, context: str = "", response_type: str = "qa") -> str:
        """Génère une réponse IA avec fallback entre Gemini et HuggingFace"""
        cache_key = hashlib.md5(f"{question}{context}{response_type}".encode()).hexdigest()
        
        if cache_key in self.cache:
            return self.cache[cache_key]
        
        response = self._try_gemini(question, context, response_type)
        if not response:
            response = self._try_huggingface(question, context, response_type)
        
        if not response:
            response = self._generate_fallback_response(question, response_type)
        
        self.cache[cache_key] = response
        return response
    
    def _try_gemini(self, question: str, context: str, response_type: str) -> Optional[str]:
        # Gemini supprimé - utilisation d'alternatives locales uniquement
        return None
    
    def _try_huggingface(self, question: str, context: str, response_type: str) -> Optional[str]:
        if not self.api_config.hf_token:
            return None
        
        try:
            client = InferenceClient(token=self.api_config.hf_token)
            
            if response_type == "qa" and context:
                result = client.question_answering(
                    question=question, 
                    context=context, 
                    model="distilbert-base-cased-distilled-squad"
                )
                return result.answer
            else:
                prompt = f"Question: {question}\nContexte: {context}\nRéponse:"
                result = client.text_generation(
                    prompt, 
                    model="gpt2", 
                    max_new_tokens=100, 
                    temperature=0.7
                )
                return result[0]['generated_text'].split('Réponse:')[-1].strip()
                
        except Exception as e:
            logging.warning(f"HuggingFace API error: {e}")
            return None
    
    def _generate_fallback_response(self, question: str, response_type: str) -> str:
        """Système de réponses intelligentes sans API"""
        question_lower = question.lower()
        
        if response_type == "motivation":
            # Réponses de motivation personnalisées selon le contexte
            if any(word in question_lower for word in ["voyage", "vacances", "séjour"]):
                return random.choice([
                    "J'adore voyager et découvrir de nouveaux horizons. Ce prix serait une opportunité fantastique pour moi de vivre une expérience inoubliable.",
                    "Voyager est ma passion et ce concours représente le voyage de mes rêves. J'espère avoir la chance de le remporter.",
                    "En tant que passionné de voyages, ce prix m'offrirait l'occasion parfaite de découvrir de nouveaux paysages et cultures."
                ])
            elif any(word in question_lower for word in ["produit", "cosmétique", "beauté"]):
                return random.choice([
                    "Je suis toujours à la recherche de nouveaux produits de qualité et j'aimerais beaucoup tester cette gamme.",
                    "Ces produits m'intéressent énormément et je serais ravi de pouvoir les découvrir.",
                    "J'ai entendu beaucoup de bien de cette marque et j'aimerais avoir l'opportunité de l'essayer."
                ])
            elif any(word in question_lower for word in ["technologie", "smartphone", "ordinateur"]):
                return random.choice([
                    "En tant que passionné de technologie, ce prix m'intéresse beaucoup et m'aiderait dans mes projets.",
                    "J'ai besoin de ce type d'équipement pour mes études et ce serait formidable de le gagner.",
                    "La technologie fait partie de ma vie quotidienne et ce prix serait très utile."
                ])
            else:
                return random.choice([
                    "Je participe avec enthousiasme à ce concours car le prix m'intéresse vraiment et correspond à mes besoins.",
                    "Ce concours m'attire particulièrement et je serais très heureux de remporter ce magnifique prix.",
                    "J'espère avoir la chance de gagner car ce prix me ferait énormément plaisir.",
                    "Je suis motivé à participer car cette opportunité pourrait vraiment changer ma journée."
                ])
        
        elif response_type == "quiz":
            # Système de réponses intelligentes pour les quiz
            if "suisse" in question_lower:
                if "capitale" in question_lower:
                    return "Berne"
                elif "langue" in question_lower:
                    return "Français, Allemand, Italien, Romanche"
                elif "monnaie" in question_lower:
                    return "Franc suisse"
                elif "population" in question_lower:
                    return "8.7 millions"
            
            if "couleur" in question_lower:
                return random.choice(["Rouge", "Bleu", "Vert", "Jaune"])
            
            if any(word in question_lower for word in ["combien", "nombre", "quantité"]):
                return random.choice(["3", "5", "10", "12", "20"])
            
            if any(word in question_lower for word in ["année", "date", "quand"]):
                return random.choice(["2024", "2023", "2025"])
            
            # Réponses par défaut pour quiz
            return random.choice(["A", "B", "C", "Oui", "Non", "Vrai", "Faux"])
        
        # Autres types de réponses
        response_mapping = {
            "age": random.choice(["25", "28", "30", "32"]),
            "profession": random.choice(["Étudiant", "Employé", "Consultant"]),
            "ville": "Pully",
            "pays": "Suisse",
            "default": "Merci"
        }
        
        return response_mapping.get(response_type, response_mapping["default"])

# =====================================================
# SCRAPER INTELLIGENT
# =====================================================

class IntelligentScraper:
    def __init__(self, db_manager: DatabaseManager, cache_duration_hours: int = 6):
        self.db = db_manager
        self.cache_duration = timedelta(hours=cache_duration_hours)
        self.session = None
    
    async def __aenter__(self):
        connector = aiohttp.TCPConnector(limit=10, limit_per_host=3)
        timeout = aiohttp.ClientTimeout(total=30, connect=10)
        self.session = aiohttp.ClientSession(
            connector=connector,
            timeout=timeout,
            headers={'User-Agent': random.choice(USER_AGENTS)}
        )
        return self
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self.session:
            await self.session.close()
    
    async def scrape_all_sources(self) -> List[Contest]:
        """Scrape tous les sites et sources"""
        all_contests = []
        
        # Scraper les sites web
        web_contests = await self._scrape_websites()
        all_contests.extend(web_contests)
        
        # Scraper Google Search si configuré
        if API_CONFIG.google_api_key and API_CONFIG.google_cx:
            google_contests = await self._scrape_google_search()
            all_contests.extend(google_contests)
        
        # Scraper Twitter/X si configuré
        if API_CONFIG.x_token:
            twitter_contests = await self._scrape_twitter()
            all_contests.extend(twitter_contests)
        
        # Filtrer les doublons
        unique_contests = self._filter_unique_contests(all_contests)
        
        logging.info(f"Total contests found: {len(all_contests)}, unique new: {len(unique_contests)}")
        return unique_contests
    
    async def _scrape_websites(self) -> List[Contest]:
        """Scrape les sites web en parallèle"""
        batch_size = 3
        all_contests = []
        
        for i in range(0, len(SITES_CH), batch_size):
            batch = SITES_CH[i:i + batch_size]
            tasks = [self._scrape_single_site(site) for site in batch]
            
            batch_results = await asyncio.gather(*tasks, return_exceptions=True)
            
            for result in batch_results:
                if isinstance(result, list):
                    all_contests.extend(result)
                elif isinstance(result, Exception):
                    logging.error(f"Batch scraping error: {result}")
            
            await asyncio.sleep(2)
        
        return all_contests
    
    async def _scrape_single_site(self, url: str) -> List[Contest]:
        """Scrape un site web spécifique"""
        try:
            content = await self._fetch_with_retry(url)
            if not content:
                return []
            
            soup = BeautifulSoup(content, 'html.parser')
            contests = self._extract_contests_from_soup(soup, url)
            
            logging.info(f"Found {len(contests)} contests on {url}")
            return contests
            
        except Exception as e:
            logging.error(f"Error scraping {url}: {e}")
            return []
    
    async def _fetch_with_retry(self, url: str, max_retries: int = 3) -> Optional[str]:
        """Fetch avec retry et gestion d'erreurs"""
        for attempt in range(max_retries):
            try:
                proxy = random.choice(PROXY_LIST) if PROXY_LIST else None
                async with self.session.get(url, proxy=proxy) as response:
                    if response.status == 200:
                        return await response.text()
                    elif response.status == 429:
                        wait_time = 2 ** attempt * 5
                        logging.warning(f"Rate limited on {url}, waiting {wait_time}s")
                        await asyncio.sleep(wait_time)
                    else:
                        logging.warning(f"HTTP {response.status} for {url}")
                        
            except Exception as e:
                logging.error(f"Attempt {attempt+1} failed for {url}: {e}")
                if attempt < max_retries - 1:
                    await asyncio.sleep(2 ** attempt)
        
        return None
    
    def _extract_contests_from_soup(self, soup: BeautifulSoup, base_url: str) -> List[Contest]:
        """Extrait les concours d'une page HTML"""
        contests = []
        
        selectors = [
            '.contest', '.concours', '.jeu', '.competition', '.giveaway',
            '[data-contest]', '[data-concours]', '.prize', '.lot',
            'article[class*="concours"]', '.entry', '.participate'
        ]
        
        containers = []
        for selector in selectors:
            containers.extend(soup.select(selector))
        
        if not containers:
            containers = soup.find_all('a', href=re.compile(r'concours|jeu|contest|participate', re.I))
        
        for container in containers[:20]:
            try:
                contest = self._parse_contest_container(container, base_url)
                if contest and self._is_valid_contest(contest):
                    contests.append(contest)
            except Exception as e:
                logging.debug(f"Error parsing container: {e}")
        
        return contests
    
    def _parse_contest_container(self, container, base_url: str) -> Optional[Contest]:
        """Parse un conteneur de concours"""
        title_selectors = ['h1', 'h2', 'h3', '.title', '.titre', '.contest-title']
        title = ""
        for selector in title_selectors:
            title_elem = container.select_one(selector)
            if title_elem:
                title = title_elem.get_text(strip=True)
                break
        
        if not title:
            title = container.get_text(strip=True)[:100]
        
        url = ""
        link_elem = container if container.name == 'a' else container.find('a')
        if link_elem and link_elem.get('href'):
            url = urljoin(base_url, link_elem['href'])
        
        description = container.get_text(strip=True)[:500]
        deadline = self._extract_deadline(description)
        prize = self._extract_prize(description)
        
        if not title or not url:
            return None
        
        return Contest(
            title=title[:200],
            url=url,
            description=description,
            source=base_url,
            deadline=deadline,
            prize=prize,
            difficulty_score=self._estimate_difficulty(description)
        )
    
    def _extract_deadline(self, text: str) -> Optional[str]:
        """Extrait la date limite du texte"""
        patterns = [
            r"jusqu[\'']?au (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4})",
            r"avant le (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4})",
            r"fin le (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4})"
        ]
        
        for pattern in patterns:
            match = re.search(pattern, text, re.I)
            if match:
                return match.group(1)
        return None
    
    def _extract_prize(self, text: str) -> Optional[str]:
        """Extrait le prix du texte"""
        patterns = [
            r"gagne[rz]?\s+([^.!?]{1,50})",
            r"prix[:\s]+([^.!?]{1,50})",
            r"lot[:\s]+([^.!?]{1,50})",
            r"(\d+\s*CHF|\d+\s*euros?|\d+\s*francs?)"
        ]
        
        for pattern in patterns:
            match = re.search(pattern, text, re.I)
            if match:
                return match.group(1).strip()
        return None
    
    def _estimate_difficulty(self, description: str) -> int:
        """Estime la difficulté de participation (0-10)"""
        difficulty = 0
        
        if re.search(r'justifi|motivation|pourquoi|essay', description, re.I):
            difficulty += 3
        if re.search(r'photo|image|créat', description, re.I):
            difficulty += 2
        if re.search(r'quiz|question|répond', description, re.I):
            difficulty += 1
        if re.search(r'partag|social|facebook|twitter', description, re.I):
            difficulty += 1
        if re.search(r'inscription|compte|profil', description, re.I):
            difficulty += 1
        
        return min(difficulty, 10)
    
    def _is_valid_contest(self, contest: Contest) -> bool:
        """Valide qu'un concours est légitime"""
        swiss_indicators = [
            'suisse', 'switzerland', 'ch', 'romandie', 'genève', 'lausanne',
            'zurich', 'bern', 'ouvert en suisse', 'résidents suisses'
        ]
        
        full_text = (contest.title + " " + contest.description).lower()
        has_swiss_access = any(indicator in full_text for indicator in swiss_indicators)
        
        excluded_terms = [
            'payant', 'payment', 'carte bancaire', 'spam', 'phishing',
            'adult', 'casino', 'bitcoin', 'crypto', 'investment'
        ]
        
        has_excluded = any(term in full_text for term in excluded_terms)
        valid_url = contest.url.startswith(('http://', 'https://'))
        
        return has_swiss_access and not has_excluded and valid_url
    
    async def _scrape_google_search(self) -> List[Contest]:
        """Scrape via Google Custom Search API"""
        try:
            query = "concours gratuit Suisse 2025 site:.ch"
            response = requests.get(
                "https://www.googleapis.com/customsearch/v1",
                params={
                    "key": API_CONFIG.google_api_key,
                    "cx": API_CONFIG.google_cx,
                    "q": query,
                    "num": 10
                },
                timeout=10
            )
            
            results = response.json().get('items', [])
            contests = []
            
            for res in results:
                title = res.get('title', '')
                url = res.get('link', '')
                description = res.get('snippet', '')
                
                contest = Contest(
                    title=title,
                    url=url,
                    description=description,
                    source='Google Search',
                    difficulty_score=self._estimate_difficulty(description)
                )
                
                if self._is_valid_contest(contest):
                    contests.append(contest)
            
            logging.info(f"Found {len(contests)} contests via Google Search")
            return contests
            
        except Exception as e:
            logging.error(f"Google Search API error: {e}")
            return []
    
    async def _scrape_twitter(self) -> List[Contest]:
        """Scrape Twitter/X pour les concours"""
        try:
            client = tweepy.Client(bearer_token=API_CONFIG.x_token)
            tweets = client.search_recent_tweets(
                query="concours gratuit Suisse lang:fr", 
                max_results=10
            )
            
            contests = []
            if tweets.data:
                for tweet in tweets.data:
                    if self._is_swiss_accessible(tweet.text):
                        contest = Contest(
                            title=tweet.text[:50] + "...",
                            url=f"https://x.com/i/status/{tweet.id}",
                            description=tweet.text,
                            source='Twitter/X',
                            difficulty_score=5
                        )
                        contests.append(contest)
            
            logging.info(f"Found {len(contests)} contests on Twitter/X")
            return contests
            
        except Exception as e:
            logging.error(f"Twitter/X API error: {e}")
            return []
    
    def _is_swiss_accessible(self, text: str) -> bool:
        """Vérifie si accessible depuis la Suisse"""
        swiss_pattern = r"(suisse|ch|romandie|ouvert\s+a\s+la\s+suisse|geneve|lausanne)"
        return bool(re.search(swiss_pattern, text.lower(), re.IGNORECASE))
    
    def _filter_unique_contests(self, contests: List[Contest]) -> List[Contest]:
        """Filtre les concours uniques et non déjà traités"""
        unique_contests = []
        seen_urls = set()
        
        for contest in contests:
            if contest.url not in seen_urls and not self.db.participation_exists(contest.url):
                unique_contests.append(contest)
                seen_urls.add(contest.url)
        
        return unique_contests

# =====================================================
# SYSTÈME DE PARTICIPATION INTELLIGENT
# =====================================================

class SmartParticipator:
    def __init__(self, db_manager: DatabaseManager, ai_engine: AIEngine):
        self.db = db_manager
        self.ai = ai_engine
        self.personal_info = PERSONAL_INFO
        
        self.field_patterns = {
            'prenom': [r'prenom|prénom|first.*name'],
            'nom': [r'nom(?!.*prenom)|last.*name|family.*name'],
            'email': [r'email|e-mail|courriel'],
            'telephone': [r'tel|phone|telephone|téléphone'],
            'adresse': [r'adresse|address|rue|street'],
            'code_postal': [r'code.postal|zip|postal'],
            'ville': [r'ville|city|localité'],
            'pays': [r'pays|country|nation'],
            'motivation': [r'motivation|pourquoi|why|reason'],
            'quiz': [r'question|quiz|réponse|answer']
        }
    
    async def participate_in_contest(self, contest: Contest) -> bool:
        """Participe à un concours de manière intelligente"""
        async with async_playwright() as p:
            browser = await p.chromium.launch(
                headless=True,
                args=['--no-sandbox', '--disable-blink-features=AutomationControlled']
            )
            
            context = await browser.new_context(
                user_agent=random.choice(USER_AGENTS),
                viewport={'width': 1920, 'height': 1080}
            )
            
            page = await context.new_page()
            
            try:
                analysis = await self._analyze_form(page, contest.url)
                
                if analysis.estimated_success_rate < 0.3:
                    logging.warning(f"Low success rate for {contest.url}: {analysis.estimated_success_rate}")
                    self.db.add_participation(contest, 'skipped_low_success', analysis.estimated_success_rate)
                    return False
                
                if analysis.requires_captcha:
                    logging.warning(f"CAPTCHA detected for {contest.url}, skipping")
                    self.db.add_participation(contest, 'skipped_captcha', analysis.estimated_success_rate)
                    return False
                
                success = await self._fill_and_submit_form(page, analysis, contest)
                
                status = 'success' if success else 'failed'
                self.db.add_participation(contest, status, analysis.estimated_success_rate)
                
                logging.info(f"Participation {'successful' if success else 'failed'} for {contest.title}")
                return success
                
            except Exception as e:
                logging.error(f"Participation error for {contest.url}: {e}")
                self.db.add_participation(contest, 'error', 0.0)
                return False
                
            finally:
                await browser.close()
    
    async def _analyze_form(self, page: Page, url: str) -> FormAnalysis:
        """Analyse un formulaire de concours"""
        try:
            await page.goto(url, wait_until='networkidle', timeout=15000)
            
            fields = await self._detect_form_fields(page)
            complexity = sum(self._calculate_field_complexity(field) for field in fields)
            has_captcha = await self._detect_captcha(page)
            has_social_requirements = await self._detect_social_requirements(page)
            success_rate = self._estimate_success_rate(fields, complexity, has_captcha)
            
            return FormAnalysis(
                fields=fields,
                complexity_score=complexity,
                estimated_success_rate=success_rate,
                requires_captcha=has_captcha,
                requires_social_media=has_social_requirements,
                form_url=url
            )
            
        except Exception as e:
            logging.error(f"Form analysis error: {e}")
            return FormAnalysis([], 10, 0.0, True, True, url)
    
    async def _detect_form_fields(self, page: Page) -> List[FormField]:
        """Détecte tous les champs de formulaire"""
        fields = []
        
        selectors = [
            'input[type="text"]', 'input[type="email"]', 'input[type="tel"]',
            'input[type="number"]', 'input:not([type])', 'textarea', 'select'
        ]
        
        for selector in selectors:
            elements = await page.query_selector_all(selector)
            
            for element in elements:
                try:
                    field = await self._analyze_single_field(element, page)
                    if field:
                        fields.append(field)
                except Exception:
                    continue
        
        return fields
    
    async def _analyze_single_field(self, element, page: Page) -> Optional[FormField]:
        """Analyse un champ individuel"""
        try:
            field_type = await element.evaluate('el => el.type || el.tagName.toLowerCase()')
            name = await element.evaluate('el => el.name || el.id || ""')
            placeholder = await element.evaluate('el => el.placeholder || ""')
            required = await element.evaluate('el => el.required')
            
            label_text = await self._find_field_label(element, page)
            selector = await self._create_unique_selector(element)
            
            return FormField(
                selector=selector,
                field_type=field_type,
                label=label_text,
                required=required,
                ai_context=f"Name: {name}, Placeholder: {placeholder}, Label: {label_text}"
            )
            
        except Exception:
            return None
    
    async def _find_field_label(self, element, page: Page) -> str:
        """Trouve le label associé à un champ"""
        try:
            element_id = await element.evaluate('el => el.id')
            if element_id:
                label = await page.query_selector(f'label[for="{element_id}"]')
                if label:
                    return await label.inner_text()
            
            parent_label = await element.evaluate('''

                el => {

                    let parent = el.parentElement;

                    while (parent && parent.tagName !== 'BODY') {

                        if (parent.tagName === 'LABEL') {

                            return parent.innerText;

                        }

                        parent = parent.parentElement;

                    }

                    return '';

                }

            ''')
            
            if parent_label:
                return parent_label.strip()
            
            prev_text = await element.evaluate('''

                el => {

                    const prev = el.previousElementSibling;

                    return prev ? prev.innerText : '';

                }

            ''')
            
            return prev_text.strip()
            
        except Exception:
            return ""
    
    async def _create_unique_selector(self, element) -> str:
        """Crée un sélecteur CSS unique"""
        element_id = await element.evaluate('el => el.id')
        if element_id:
            return f'#{element_id}'
        
        name = await element.evaluate('el => el.name')
        if name:
            return f'[name="{name}"]'
        
        class_name = await element.evaluate('el => el.className')
        tag_name = await element.evaluate('el => el.tagName.toLowerCase()')
        
        if class_name:
            return f'{tag_name}.{class_name.split()[0]}'
        
        return f'{tag_name}:nth-of-type(1)'
    
    def _calculate_field_complexity(self, field: FormField) -> int:
        """Calcule la complexité d'un champ"""
        complexity = 1
        
        if field.field_type == 'textarea':
            complexity += 3
        elif field.field_type == 'select':
            complexity += 2
        elif field.required:
            complexity += 1
        
        if re.search(r'motivation|justifi|pourquoi', field.label, re.I):
            complexity += 3
        elif re.search(r'quiz|question', field.label, re.I):
            complexity += 2
        
        return complexity
    
    async def _detect_captcha(self, page: Page) -> bool:
        """Détecte la présence de CAPTCHA"""
        captcha_selectors = [
            '.g-recaptcha', '.h-captcha', '#captcha', '.captcha',
            'iframe[src*="recaptcha"]', 'iframe[src*="hcaptcha"]'
        ]
        
        for selector in captcha_selectors:
            element = await page.query_selector(selector)
            if element:
                return True
        
        return False
    
    async def _detect_social_requirements(self, page: Page) -> bool:
        """Détecte les exigences de réseaux sociaux"""
        content = await page.content()
        social_patterns = [
            r'follow.*us', r'partag.*facebook', r'retweet',
            r'like.*page', r'subscribe.*channel'
        ]
        
        for pattern in social_patterns:
            if re.search(pattern, content, re.I):
                return True
        
        return False
    
    def _estimate_success_rate(self, fields: List[FormField], complexity: int, has_captcha: bool) -> float:
        """Estime le taux de succès"""
        base_rate = 0.8
        
        if has_captcha:
            base_rate *= 0.1
        
        if complexity > 15:
            base_rate *= 0.4
        elif complexity > 10:
            base_rate *= 0.6
        elif complexity > 5:
            base_rate *= 0.8
        
        required_fields = [f for f in fields if f.required]
        if len(required_fields) <= 3:
            base_rate *= 1.1
        
        return min(base_rate, 1.0)
    
    async def _fill_and_submit_form(self, page: Page, analysis: FormAnalysis, contest: Contest) -> bool:
        """Remplit et soumet le formulaire"""
        try:
            filled_fields = 0
            
            for field in analysis.fields:
                try:
                    await page.wait_for_selector(field.selector, timeout=3000)
                    element = await page.query_selector(field.selector)
                    
                    if not element:
                        continue
                    
                    value = await self._generate_field_value(field, contest, page)
                    if not value:
                        continue
                    
                    if field.field_type == 'select':
                        await self._fill_select_field(element, value, page)
                    else:
                        await element.fill(value)
                    
                    filled_fields += 1
                    await asyncio.sleep(random.uniform(0.3, 0.8))
                    
                except Exception as e:
                    logging.debug(f"Error filling field {field.selector}: {e}")
                    continue
            
            submit_success = await self._submit_form(page)
            
            logging.info(f"Filled {filled_fields}/{len(analysis.fields)} fields, submitted: {submit_success}")
            return filled_fields > 0 and submit_success
            
        except Exception as e:
            logging.error(f"Form filling error: {e}")
            return False
    
    async def _generate_field_value(self, field: FormField, contest: Contest, page: Page) -> Optional[str]:
        """Génère une valeur pour un champ"""
        field_type = self._identify_field_type(field)
        
        personal_mapping = {
            'prenom': self.personal_info.prenom,
            'nom': self.personal_info.nom,
            'email': self.personal_info.email_derivee,
            'telephone': self.personal_info.telephone,
            'adresse': self.personal_info.adresse,
            'code_postal': self.personal_info.code_postal,
            'ville': self.personal_info.ville,
            'pays': self.personal_info.pays
        }
        
        if field_type in personal_mapping:
            return personal_mapping[field_type]
        
        if field_type == 'motivation':
            return self.ai.generate_response(
                field.label, 
                contest.description, 
                "motivation"
            )
        elif field_type == 'quiz':
            return self.ai.generate_response(
                field.label, 
                contest.description, 
                "quiz"
            )
        
        default_values = {
            'age': '25',
            'genre': 'Monsieur',
            'profession': 'Étudiant'
        }
        
        for key, value in default_values.items():
            if key in field.label.lower():
                return value
        
        return None
    
    def _identify_field_type(self, field: FormField) -> str:
        """Identifie le type de champ"""
        combined_text = f"{field.ai_context} {field.label}".lower()
        
        for field_type, patterns in self.field_patterns.items():
            for pattern in patterns:
                if re.search(pattern, combined_text, re.I):
                    return field_type
        
        return 'unknown'
    
    async def _fill_select_field(self, element, value: str, page: Page):
        """Remplit un champ select"""
        try:
            options = await element.query_selector_all('option')
            
            for option in options:
                option_text = await option.inner_text()
                option_value = await option.get_attribute('value')
                
                if (value.lower() in option_text.lower() or 
                    value.lower() in (option_value or "").lower()):
                    await element.select_option(value=option_value)
                    return
            
            if options and len(options) > 1:
                first_option = await options[1].get_attribute('value')
                await element.select_option(value=first_option)
                
        except Exception as e:
            logging.debug(f"Select field error: {e}")
    
    async def _submit_form(self, page: Page) -> bool:
        """Soumet le formulaire"""
        submit_selectors = [
            'input[type="submit"]',
            'button[type="submit"]',
            'button:has-text("Participer")',
            'button:has-text("Envoyer")',
            'button:has-text("Valider")',
            '.submit-btn',
            '.participate-btn'
        ]
        
        for selector in submit_selectors:
            try:
                element = await page.query_selector(selector)
                if element:
                    is_visible = await element.is_visible()
                    is_enabled = await element.is_enabled()
                    
                    if is_visible and is_enabled:
                        await element.click()
                        await page.wait_for_timeout(3000)
                        return True
                        
            except Exception:
                continue
        
        return False

# =====================================================
# GESTIONNAIRE D'EMAILS ET ALERTES
# =====================================================

class EmailManager:
    def __init__(self, api_config: APIConfig, ai_engine: AIEngine, db_manager: DatabaseManager):
        self.api_config = api_config
        self.ai = ai_engine
        self.db = db_manager
        self.personal_info = PERSONAL_INFO
    
    def check_and_analyze_emails(self):
        """Vérifie et analyse les emails pour détecter les victoires"""
        if not self.api_config.email_app_password:
            logging.warning("Email app password not configured")
            return
        
        try:
            mail = imaplib.IMAP4_SSL('outlook.office365.com')
            mail.login(self.personal_info.email, self.api_config.email_app_password)
            mail.select('inbox')
            
            since_date = (datetime.now() - timedelta(days=7)).strftime('%d-%b-%Y')
            status, messages = mail.search(None, f'(SINCE "{since_date}")')
            
            victories = []
            email_count = 0
            
            for num in messages[0].split()[-20:]:
                try:
                    email_count += 1
                    _, msg = mail.fetch(num, '(RFC822)')
                    email_msg = email.message_from_bytes(msg[0][1])
                    
                    subject = email_msg['Subject'] or ""
                    from_addr = email_msg['From'] or ""
                    
                    body = self._extract_email_body(email_msg)
                    
                    analysis = self.ai.generate_response(
                        f"Cet email indique-t-il une victoire dans un concours ? Sujet: {subject}",
                        body[:1000],
                        "quiz"
                    )
                    
                    if 'oui' in analysis.lower() or 'gagne' in analysis.lower():
                        prize = self._extract_prize_from_email(body, subject)
                        victories.append({
                            'email_id': num.decode(),
                            'date': datetime.now().strftime('%Y-%m-%d'),
                            'prize': prize,
                            'source': from_addr,
                            'subject': subject
                        })
                        
                        self.db.add_victory(num.decode(), prize, from_addr)
                        self._send_victory_alert(prize, from_addr, subject)
                        
                        logging.info(f"Victory detected: {prize} from {from_addr}")
                
                except Exception as e:
                    logging.error(f"Error processing email {num}: {e}")
                    continue
            
            mail.logout()
            logging.info(f"Processed {email_count} emails, found {len(victories)} victories")
            
        except Exception as e:
            logging.error(f"Email checking error: {e}")
    
    def _extract_email_body(self, email_msg) -> str:
        """Extrait le corps de l'email"""
        body = ""
        
        if email_msg.is_multipart():
            for part in email_msg.walk():
                if part.get_content_type() == 'text/plain':
                    try:
                        body = part.get_payload(decode=True).decode('utf-8', errors='ignore')
                        break
                    except:
                        continue
        else:
            try:
                body = email_msg.get_payload(decode=True).decode('utf-8', errors='ignore')
            except:
                body = str(email_msg.get_payload())
        
        return body
    
    def _extract_prize_from_email(self, body: str, subject: str) -> str:
        """Extrait le prix gagné de l'email"""
        text = f"{subject} {body}"
        
        prize_patterns = [
            r"vous avez gagné\s+([^.!?\n]{1,100})",
            r"prix[:\s]+([^.!?\n]{1,100})",
            r"lot[:\s]+([^.!?\n]{1,100})",
            r"remporté\s+([^.!?\n]{1,100})",
            r"(\d+\s*CHF|\d+\s*euros?|\d+\s*francs?)"
        ]
        
        for pattern in prize_patterns:
            match = re.search(pattern, text, re.I)
            if match:
                return match.group(1).strip()
        
        return "Prix non spécifié"
    
    def _send_victory_alert(self, prize: str, source: str, subject: str):
        """Envoie une alerte de victoire"""
        if not self.api_config.telegram_bot_token or not self.api_config.telegram_chat_id:
            return
        
        message = f"""

🎉 **VICTOIRE DÉTECTÉE !**



🏆 **Prix**: {prize}

📧 **Source**: {source}

📋 **Sujet**: {subject}

📅 **Date**: {datetime.now().strftime('%d/%m/%Y %H:%M')}



Félicitations ! 🎊

        """
        
        try:
            url = f"https://api.telegram.org/bot{self.api_config.telegram_bot_token}/sendMessage"
            payload = {
                'chat_id': self.api_config.telegram_chat_id,
                'text': message,
                'parse_mode': 'Markdown'
            }
            
            response = requests.post(url, json=payload, timeout=10)
            if response.status_code == 200:
                logging.info("Victory alert sent successfully")
            else:
                logging.error(f"Telegram alert failed: {response.text}")
                
        except Exception as e:
            logging.error(f"Telegram alert error: {e}")

# =====================================================
# SYSTÈME DE MONITORING ET STATISTIQUES
# =====================================================

class MonitoringSystem:
    def __init__(self, db_manager: DatabaseManager):
        self.db = db_manager
    
    def generate_daily_report(self) -> str:
        """Génère un rapport quotidien"""
        stats = self.db.get_stats()
        
        today = datetime.now().strftime('%Y-%m-%d')
        conn = self.db._get_connection()
        
        today_participations = conn.execute(
            "SELECT COUNT(*) FROM participations WHERE date = ?", (today,)
        ).fetchone()[0]
        
        today_successes = conn.execute(
            "SELECT COUNT(*) FROM participations WHERE date = ? AND status = 'success'", (today,)
        ).fetchone()[0]
        
        success_rate = (today_successes / max(today_participations, 1)) * 100
        
        report = f"""

📊 **RAPPORT QUOTIDIEN - {today}**



🎯 **Aujourd'hui**:

   • Participations: {today_participations}

   • Succès: {today_successes}

   • Taux de succès: {success_rate:.1f}%



📈 **Total**:

   • Participations totales: {stats['total_participations']}

   • Participations réussies: {stats['successful_participations']}

   • Victoires détectées: {stats['total_victories']}



🌐 **Par source**:

"""
        
        for source, count in stats['by_source'].items():
            report += f"   • {source}: {count}\n"
        
        return report
    
    def send_daily_report(self):
        """Envoie le rapport quotidien"""
        if not API_CONFIG.telegram_bot_token or not API_CONFIG.telegram_chat_id:
            return
        
        report = self.generate_daily_report()
        
        try:
            url = f"https://api.telegram.org/bot{API_CONFIG.telegram_bot_token}/sendMessage"
            payload = {
                'chat_id': API_CONFIG.telegram_chat_id,
                'text': report,
                'parse_mode': 'Markdown'
            }
            
            response = requests.post(url, json=payload, timeout=10)
            if response.status_code == 200:
                logging.info("Daily report sent successfully")
            else:
                logging.error(f"Daily report failed: {response.text}")
                
        except Exception as e:
            logging.error(f"Daily report error: {e}")

# =====================================================
# ORCHESTRATEUR PRINCIPAL
# =====================================================

class ContestBotOrchestrator:
    def __init__(self):
        self.db = DatabaseManager()
        self.ai = AIEngine(API_CONFIG)
        self.scraper = None
        self.participator = SmartParticipator(self.db, self.ai)
        self.email_manager = EmailManager(API_CONFIG, self.ai, self.db)
        self.monitor = MonitoringSystem(self.db)
    
    async def run_full_cycle(self):
        """Execute un cycle complet de scraping et participation"""
        logging.info("Starting full contest bot cycle")
        
        try:
            # 1. Scraping des concours
            async with IntelligentScraper(self.db) as scraper:
                self.scraper = scraper
                contests = await scraper.scrape_all_sources()
            
            if not contests:
                logging.info("No new contests found")
                return
            
            # 2. Trier par score de difficulté (plus faciles en premier)
            contests.sort(key=lambda x: x.difficulty_score)
            
            # 3. Participer aux concours (limiter à 20 par jour)
            participation_count = 0
            max_daily_participations = 20
            
            for contest in contests[:max_daily_participations]:
                try:
                    # Pause entre participations pour éviter la détection
                    if participation_count > 0:
                        wait_time = random.uniform(30, 120)  # 30s à 2min
                        logging.info(f"Waiting {wait_time:.0f}s before next participation")
                        await asyncio.sleep(wait_time)
                    
                    success = await self.participator.participate_in_contest(contest)
                    participation_count += 1
                    
                    if success:
                        logging.info(f"✅ Successfully participated in: {contest.title}")
                    else:
                        logging.warning(f"❌ Failed to participate in: {contest.title}")
                    
                    # Pause plus longue après succès
                    if success:
                        await asyncio.sleep(random.uniform(60, 180))
                    
                except Exception as e:
                    logging.error(f"Error participating in {contest.title}: {e}")
                    continue
            
            logging.info(f"Participation cycle completed: {participation_count} attempts")
            
        except Exception as e:
            logging.error(f"Full cycle error: {e}")
    
    def run_email_check(self):
        """Vérifie les emails pour les victoires"""
        try:
            logging.info("Checking emails for victories")
            self.email_manager.check_and_analyze_emails()
        except Exception as e:
            logging.error(f"Email check error: {e}")
    
    def run_daily_report(self):
        """Génère et envoie le rapport quotidien"""
        try:
            logging.info("Generating daily report")
            self.monitor.send_daily_report()
        except Exception as e:
            logging.error(f"Daily report error: {e}")

# =====================================================
# SCHEDULER ET POINT D'ENTRÉE
# =====================================================

def run_bot_cycle():
    """Point d'entrée pour le scheduler"""
    bot = ContestBotOrchestrator()
    
    # Cycle principal
    asyncio.run(bot.run_full_cycle())
    
    # Vérification des emails
    bot.run_email_check()

def run_daily_report():
    """Point d'entrée pour le rapport quotidien"""
    bot = ContestBotOrchestrator()
    bot.run_daily_report()

def main():
    """Fonction principale avec launcher intelligent intégré"""
    
    # Vérifier les arguments de ligne de commande
    if len(sys.argv) > 1:
        if sys.argv[1] == "--run-now":
            logging.info("Running immediate cycle")
            run_bot_cycle()
            return
        elif sys.argv[1] == "--test-apis":
            # Mode test des APIs
            tester = APITester(API_CONFIG)
            results = tester.test_all_apis()
            working_count = sum(results.values())
            print(f"\n📊 Résumé: {working_count}/5 APIs fonctionnelles")
            return
        elif sys.argv[1] == "--scheduler":
            # Mode scheduler direct (sans menu)
            logging.info("Starting Contest Bot with scheduler")
            
            schedule.every().day.at("08:00").do(run_bot_cycle)
            schedule.every().day.at("14:00").do(run_bot_cycle)
            schedule.every().day.at("20:00").do(run_daily_report)
            
            logging.info("Scheduler started. Waiting for scheduled tasks...")
            
            while True:
                try:
                    schedule.run_pending()
                    time.sleep(60)
                except KeyboardInterrupt:
                    logging.info("Bot stopped by user")
                    break
                except Exception as e:
                    logging.error(f"Scheduler error: {e}")
                    time.sleep(300)
            return
    
    # Mode launcher intelligent (par défaut)
    try:
        launcher = SmartLauncher()
        should_launch = launcher.main_menu()
        
        if should_launch:
            # L'utilisateur veut lancer le bot
            print("\n🤔 Comment voulez-vous lancer le bot ?")
            print("1️⃣ Test immédiat (--run-now)")
            print("2️⃣ Mode scheduler automatique")
            
            mode_choice = input("\nVotre choix (1/2): ").strip()
            
            if mode_choice == "1":
                print("\n🎬 Lancement immédiat...")
                run_bot_cycle()
            elif mode_choice == "2":
                print("\n⏰ Démarrage du scheduler...")
                print("Programmation: 08:00, 14:00 (concours) et 20:00 (rapport)")
                
                schedule.every().day.at("08:00").do(run_bot_cycle)
                schedule.every().day.at("14:00").do(run_bot_cycle)
                schedule.every().day.at("20:00").do(run_daily_report)
                
                print("Scheduler démarré. Utilisez Ctrl+C pour arrêter.")
                
                while True:
                    try:
                        schedule.run_pending()
                        time.sleep(60)
                    except KeyboardInterrupt:
                        print("\n👋 Bot arrêté par l'utilisateur")
                        break
                    except Exception as e:
                        logging.error(f"Scheduler error: {e}")
                        time.sleep(300)
            else:
                print("❌ Choix invalide")
        else:
            print("👋 À bientôt !")
            
    except KeyboardInterrupt:
        print("\n\n👋 Arrêté par l'utilisateur")
    except Exception as e:
        print(f"\n❌ Erreur: {e}")

if __name__ == "__main__":
    main()