File size: 393,331 Bytes
9e497a6
18f750b
5063ea7
18f750b
9e497a6
 
9ec255b
 
4d5557c
e0a42b5
 
5063ea7
9e497a6
b50b6c9
b4fbd6e
5e005fe
e0a42b5
 
1267ddc
d4ac480
 
e0a42b5
 
 
017347c
01b1e15
 
8163b32
84839a3
e2e9e62
 
84839a3
 
2ff713b
01b1e15
 
 
 
 
2ff713b
 
 
 
 
 
e0a42b5
 
 
 
049cd25
41a083c
 
403ad0a
d4ac480
 
e0a42b5
c8f5f98
5063ea7
 
9e497a6
60dfd28
 
 
 
 
 
 
 
d26ae7a
60dfd28
 
 
 
 
d26ae7a
 
60dfd28
5e005fe
 
 
 
 
 
 
 
 
1fa1225
017347c
1fa1225
 
14fbecd
7b38420
 
 
fd477ea
0aefca0
1fa1225
 
 
 
 
 
 
 
 
 
 
 
14fbecd
1fa1225
 
 
 
 
 
 
 
 
 
 
017347c
 
 
0aefca0
 
 
a9acb8f
0aefca0
 
 
 
 
 
 
 
 
14fbecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aefca0
 
8163b32
 
 
 
 
2ff713b
8163b32
 
a5e9e6a
 
f54c9c8
 
 
 
 
 
 
 
8163b32
f54c9c8
 
 
 
 
 
 
 
 
8163b32
 
 
 
 
d26ae7a
 
 
 
 
 
3f64f7a
 
 
 
8163b32
0979f89
 
4df05ee
ccc7032
0979f89
4df05ee
ccc7032
4df05ee
0979f89
 
 
4df05ee
0979f89
 
ccc7032
0979f89
4df05ee
 
 
 
 
 
 
 
0979f89
4df05ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
932439c
 
4df05ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7032
4df05ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144d95d
4df05ee
 
 
 
 
 
 
 
144d95d
 
 
 
4df05ee
 
ccc7032
144d95d
 
 
 
 
 
 
 
 
 
ccc7032
 
0ac11fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f211d9b
8163b32
9e497a6
 
 
 
 
 
 
 
049cd25
ccc7032
 
 
049cd25
5bef62b
4d5557c
ccc7032
049cd25
5bef62b
4d5557c
ccc7032
 
 
049cd25
5bef62b
4d5557c
ccc7032
 
d4ac480
049cd25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144d95d
 
 
 
 
 
 
049cd25
144d95d
 
 
 
049cd25
 
144d95d
 
 
 
 
 
 
 
 
 
049cd25
 
144d95d
 
 
 
 
 
 
 
 
 
 
 
049cd25
 
 
 
c46905a
144d95d
 
049cd25
 
 
 
 
144d95d
 
 
 
 
 
049cd25
 
 
 
 
c46905a
049cd25
 
 
 
 
 
 
 
 
 
144d95d
049cd25
c46905a
 
049cd25
 
144d95d
 
 
049cd25
 
 
 
 
144d95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
049cd25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144d95d
 
 
 
 
049cd25
 
 
 
144d95d
 
 
 
 
 
 
 
 
049cd25
 
 
 
 
 
 
8a49d3e
 
 
 
 
d4ac480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7032
d4ac480
ccc7032
d4ac480
ccc7032
 
 
 
 
 
 
 
 
d4ac480
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
 
d26ae7a
0ac11fa
 
 
 
 
 
 
9e497a6
e0a42b5
 
9e497a6
 
 
41a083c
20be0a1
41a083c
1830912
20be0a1
1830912
 
20be0a1
1830912
20be0a1
1830912
 
41a083c
1830912
 
41a083c
1830912
 
 
 
 
 
 
41a083c
1830912
 
 
 
 
 
41a083c
20be0a1
1830912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20be0a1
1830912
d26ae7a
1830912
 
d26ae7a
1830912
20be0a1
 
1830912
 
09e1fa5
1830912
09e1fa5
1830912
 
 
 
 
 
 
 
 
09e1fa5
 
20be0a1
1830912
20be0a1
41a083c
 
80078d8
 
41a083c
 
 
 
 
 
 
403ad0a
321f116
 
41a083c
80078d8
 
41a083c
80078d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41a083c
5063ea7
 
9e497a6
 
 
 
 
 
d20d896
 
 
 
 
 
 
9e497a6
 
d20d896
 
9e497a6
 
 
 
b4fbd6e
 
8163b32
 
b4fbd6e
 
 
8163b32
9e497a6
 
 
e0a42b5
41a083c
9e497a6
3f64f7a
 
 
ac02b4d
 
 
3f64f7a
 
ac02b4d
 
 
ed2fb7c
 
6c21225
ed2fb7c
 
 
 
 
 
 
 
 
 
6c21225
b3d2c70
6c21225
 
 
1947db2
6c21225
1947db2
6c21225
 
 
3f64f7a
ac02b4d
 
3f64f7a
ac02b4d
 
 
 
 
 
 
 
 
3f64f7a
ac02b4d
1830912
ac02b4d
 
 
1830912
 
 
 
 
ac02b4d
 
 
1830912
 
3f64f7a
 
1830912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f64f7a
 
bbc4c5a
 
 
 
 
 
 
 
 
 
 
3f64f7a
1830912
ac02b4d
1830912
 
ac02b4d
 
 
1830912
 
 
 
 
 
 
3f64f7a
ac02b4d
3f64f7a
 
 
bf64432
3f64f7a
 
 
 
 
 
 
 
 
 
 
 
33d9a63
 
 
 
 
 
 
d4ac480
33d9a63
d4ac480
 
 
 
 
 
33d9a63
 
 
d4ac480
33d9a63
 
 
d4ac480
ccc7032
d4ac480
 
 
 
33d9a63
 
 
d4ac480
 
 
 
 
 
33d9a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
bee7763
6b2f36c
b31cb3a
 
 
 
 
 
9e497a6
 
 
 
 
 
1fa7188
 
9e497a6
 
 
 
 
 
 
5ad28da
9e497a6
 
 
5ad28da
 
9e497a6
1fa1225
9ec255b
e767948
9e497a6
 
 
 
ed191e7
5ad28da
 
9ec255b
ed191e7
 
 
 
9e497a6
 
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
 
 
 
 
 
 
 
 
6b2f36c
 
b31cb3a
9e497a6
 
b31cb3a
 
 
 
 
 
 
 
 
9e497a6
 
 
6b2f36c
b31cb3a
e81b8a2
b31cb3a
 
e81b8a2
b31cb3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fb76ee
9e497a6
 
9fb76ee
6b2f36c
 
b31cb3a
 
 
9e497a6
 
 
b31cb3a
 
 
 
 
 
9e497a6
 
 
 
 
6b2f36c
 
 
 
 
 
b31cb3a
 
 
 
 
 
 
6b2f36c
9e497a6
 
 
028f096
 
95243f9
028f096
 
 
1fa1225
017347c
1fa1225
 
 
028f096
 
1fa1225
95243f9
028f096
1fa7188
c8f5f98
 
1fa7188
 
0eb786d
 
 
1fa7188
9e497a6
 
1fa7188
9e497a6
 
 
 
 
 
 
 
d26ae7a
1fa7188
9e497a6
 
 
 
 
 
e0a42b5
9e497a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4ac480
 
 
 
9e497a6
 
 
50257ac
d4ac480
 
 
 
 
 
ccc7032
d4ac480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
50257ac
9e497a6
 
 
e0a42b5
9e497a6
 
 
 
 
 
 
e02e015
 
 
 
 
 
 
 
 
 
 
ccc7032
e02e015
 
 
 
 
 
 
 
 
 
 
 
5063ea7
 
 
9e497a6
 
 
 
 
 
 
 
 
 
 
a4ac7b0
9e497a6
243e9bd
 
 
 
 
 
 
3f64f7a
1947db2
e08355e
 
d26ae7a
3b8f5eb
e169f89
 
 
 
 
 
 
d3be83c
e169f89
0cea1f8
09b6531
ccc7032
3b8f5eb
0cea1f8
ccc7032
d3be83c
 
 
 
4df05ee
ccc7032
d3be83c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7032
d3be83c
4df05ee
d3be83c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4df05ee
 
 
e169f89
 
 
 
 
0cea1f8
 
e169f89
ccc7032
 
e169f89
 
3b8f5eb
e169f89
a4ac7b0
 
 
1947db2
a4ac7b0
 
 
 
 
 
 
1947db2
9e497a6
243e9bd
 
 
9e497a6
243e9bd
6c03b31
243e9bd
1947db2
9e497a6
0cea1f8
 
 
 
 
 
b4f6b0d
 
 
0cea1f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2084ad0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b38420
 
 
 
 
0cea1f8
 
 
 
 
 
 
 
d20d896
 
 
 
 
 
 
 
 
 
 
 
 
0cea1f8
 
 
 
 
 
 
 
 
 
 
 
09b6531
5bef62b
0cea1f8
 
 
09b6531
5bef62b
0cea1f8
 
5bef62b
 
 
 
 
 
 
 
 
0cea1f8
 
 
 
 
 
 
 
5bef62b
 
0cea1f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4f6b0d
 
 
 
 
 
 
 
 
 
0cea1f8
b4f6b0d
 
0cea1f8
b4f6b0d
0cea1f8
 
 
b4f6b0d
 
 
 
 
 
 
7d230dc
 
 
 
 
 
 
 
 
 
b4f6b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d230dc
 
b4f6b0d
 
 
 
7d230dc
b4f6b0d
 
 
 
7d230dc
 
b4f6b0d
 
 
 
7d230dc
b4f6b0d
 
 
 
 
 
 
 
 
 
 
7d230dc
b4f6b0d
7d230dc
 
 
9ed52dd
7d230dc
9ed52dd
 
7d230dc
b4f6b0d
 
 
 
 
9ed52dd
7d230dc
b4f6b0d
 
7d230dc
 
b4f6b0d
 
 
 
 
 
7d230dc
b4f6b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cea1f8
 
41a083c
 
0eb786d
c8f5f98
 
0eb786d
 
 
 
 
 
 
 
 
 
 
 
33d9a63
 
0eb786d
 
33d9a63
 
 
0eb786d
 
 
41a083c
 
 
 
 
 
 
33d9a63
41a083c
33d9a63
 
 
 
 
 
 
 
41a083c
33d9a63
 
 
 
 
 
 
 
41a083c
9e497a6
 
 
ed2fb7c
0abe660
ed2fb7c
 
 
 
 
 
 
 
 
 
 
0abe660
ed2fb7c
 
 
 
 
 
0abe660
 
 
 
ed2fb7c
 
 
 
 
0abe660
9e497a6
 
 
 
 
 
637b36c
 
d4ac480
 
 
 
 
 
e7566f8
ac02e1d
ed2fb7c
 
9e497a6
 
ac02e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18f750b
 
 
e7566f8
 
 
 
18f750b
 
 
 
 
 
 
 
3f64f7a
18f750b
 
 
3f64f7a
18f750b
 
 
 
e7566f8
 
 
 
18f750b
 
 
 
 
 
9e497a6
2ff713b
017347c
2ff713b
017347c
2ff713b
 
 
 
 
 
 
 
0bb57c4
 
 
 
 
 
 
 
 
 
 
06167f4
0bb57c4
 
06167f4
 
0bb57c4
 
06167f4
0bb57c4
 
 
 
 
 
 
2ff713b
 
 
 
 
 
 
 
0bb57c4
2ff713b
f211d9b
2ff713b
 
 
 
 
6c03b31
 
2ff713b
 
41a083c
 
6c03b31
 
2ff713b
 
 
 
6c03b31
2ff713b
 
 
 
 
6c03b31
 
2ff713b
 
6c03b31
 
 
 
 
2ff713b
2b4315e
0979f89
 
 
 
 
 
 
 
 
 
06167f4
2b4315e
2ff713b
 
 
 
06167f4
2ff713b
 
 
 
06167f4
 
2b4315e
 
ed191e7
 
2b4315e
f211d9b
2b4315e
2ff713b
 
 
 
 
 
 
 
 
 
 
 
 
 
9b28960
69197c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2fb7c
 
39e1e78
ccc7032
 
 
 
 
 
 
 
 
 
 
 
 
39e1e78
ed2fb7c
 
 
 
 
9b3b998
ed2fb7c
 
9b3b998
ed2fb7c
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
ed2fb7c
 
 
69197c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2fb7c
 
 
 
 
 
 
2ff713b
39e1e78
3f64f7a
39e1e78
3f64f7a
2ff713b
 
e02925c
bf64432
 
2ff713b
 
 
 
41a083c
 
 
 
2ff713b
41a083c
2ff713b
41a083c
2ff713b
 
 
69197c3
 
 
 
 
 
 
05415a5
 
 
 
2ff713b
 
 
 
 
 
06167f4
 
 
 
 
2b4315e
05415a5
2ff713b
 
 
 
 
 
 
 
41a083c
06167f4
 
2ff713b
2b4315e
bf64432
2b4315e
ccc7032
41a083c
6c03b31
 
 
41a083c
d26ae7a
41a083c
6c03b31
41a083c
 
 
d26ae7a
 
 
 
 
6c03b31
41a083c
6c03b31
 
 
d0e4595
6c03b31
 
 
41a083c
6c03b31
 
 
 
d0e4595
 
41a083c
e64eb90
6c03b31
41a083c
e02925c
41a083c
69197c3
 
 
 
 
 
 
 
 
6c03b31
e64eb90
06167f4
 
 
 
 
 
e64eb90
6c03b31
69197c3
e64eb90
09b6531
06167f4
e64eb90
 
 
 
 
06167f4
 
e64eb90
 
ccc7032
 
41a083c
 
 
6c03b31
2ff713b
e02925c
 
2001775
 
 
 
 
 
 
 
 
 
 
 
69197c3
e02925c
 
 
2ff713b
 
bf64432
2ff713b
 
 
 
bf64432
9b28960
69197c3
9b3b998
 
 
 
 
69197c3
9b3b998
 
 
 
 
 
69197c3
 
 
9b3b998
69197c3
9b3b998
 
 
 
2ff713b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50257ac
2ff713b
 
 
 
39e1e78
2ff713b
 
39e1e78
2ff713b
 
 
 
 
 
 
 
9b3b998
2ff713b
 
9b3b998
 
 
06167f4
 
 
 
 
 
2b4315e
2ff713b
9b3b998
2ff713b
 
06167f4
2ff713b
 
 
 
06167f4
 
2b4315e
 
0bb57c4
2b4315e
1267ddc
2ff713b
 
 
 
 
 
 
 
 
 
 
 
 
0979f89
 
 
 
9b3b998
 
 
 
0979f89
06167f4
2b4315e
2ff713b
9b3b998
2ff713b
0979f89
06167f4
2ff713b
 
 
 
06167f4
 
2b4315e
 
 
1267ddc
2ff713b
39e1e78
2ff713b
9b3b998
2ff713b
39e1e78
2ff713b
 
 
39e1e78
2ff713b
 
 
 
39e1e78
2ff713b
 
 
9b3b998
 
 
 
 
79e85da
9b3b998
 
 
 
 
 
79e85da
9b3b998
 
 
 
79e85da
9b3b998
 
 
 
 
 
79e85da
9b3b998
 
 
 
 
 
 
79e85da
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
 
 
 
9b3b998
 
 
 
4eb7bcd
 
 
 
 
 
 
 
 
 
 
 
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
4eb7bcd
 
 
 
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06167f4
9b3b998
 
 
 
 
06167f4
9b3b998
 
 
 
06167f4
 
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b4315e
 
 
 
 
 
ed2fb7c
9b3b998
 
 
2b4315e
 
 
028f096
0aefca0
028f096
 
 
 
0aefca0
 
 
 
 
 
 
 
 
 
 
017347c
0aefca0
 
 
 
 
 
fd477ea
 
 
1fa1225
0aefca0
fd477ea
0aefca0
218c279
0aefca0
e5ee4a5
fd477ea
95243f9
e5ee4a5
 
 
 
5f0a440
 
 
 
 
 
e5ee4a5
 
 
 
 
 
 
 
 
028f096
5f0a440
 
 
 
 
 
 
 
 
 
 
69197c3
 
9dc18a1
69197c3
028f096
ac02b4d
 
b3d2c70
ac02b4d
e02925c
ac02b4d
2b4315e
 
e02925c
 
2b4315e
9b3b998
 
2b4315e
 
 
 
 
 
 
 
 
ed2fb7c
2b4315e
9b3b998
 
 
 
 
 
 
 
79e85da
9b3b998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bb57c4
2b4315e
 
 
 
 
 
 
 
9b3b998
 
2b4315e
9b3b998
2b4315e
 
 
 
9b3b998
2b4315e
 
 
 
 
a9acb8f
2b4315e
 
1267ddc
9dc18a1
a9acb8f
9dc18a1
2b4315e
9dc18a1
a9acb8f
9dc18a1
 
 
 
2b4315e
 
 
 
 
 
 
 
 
 
 
 
2ff713b
 
 
 
 
 
 
 
ac02b4d
017347c
1fa1225
 
 
 
ac02b4d
b3d2c70
ac02b4d
b3d2c70
2ff713b
1fa1225
 
 
 
 
 
 
017347c
1fa1225
fd477ea
 
1fa1225
fd477ea
1fa1225
fd477ea
1fa1225
 
 
5b1fd67
fd477ea
1fa1225
5b1fd67
 
 
 
 
 
 
1fa1225
 
 
 
 
 
 
2ff713b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017347c
2ff713b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e85c99e
1fa1225
 
 
e85c99e
3f64f7a
 
 
 
e85c99e
 
2ff713b
e85c99e
 
3f64f7a
e85c99e
 
2ff713b
017347c
2ff713b
 
 
 
 
9e497a6
50257ac
 
 
 
 
 
18f750b
50257ac
 
18f750b
50257ac
 
 
 
 
 
e0a42b5
50257ac
18f750b
50257ac
 
238a2ce
d26ae7a
e85c99e
d26ae7a
e0a42b5
 
 
18f750b
e0a42b5
50257ac
 
 
18f750b
 
 
 
 
 
 
 
017347c
18f750b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b38420
 
 
 
 
 
 
 
 
 
 
18f750b
 
 
 
 
 
 
5b2e478
 
b3d2c70
5b2e478
 
 
 
 
 
 
18f750b
 
 
 
 
 
 
 
 
 
 
 
3f64f7a
 
 
 
 
 
18f750b
 
 
 
20be0a1
18f750b
80078d8
 
 
 
 
ac02e1d
 
 
80078d8
ac02e1d
 
 
41a083c
 
3f64f7a
d26ae7a
41a083c
3f64f7a
18f750b
 
ac02e1d
3f64f7a
80078d8
 
 
017347c
ac02e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
80078d8
3f64f7a
 
18f750b
 
3f64f7a
 
18f750b
 
 
 
 
 
 
238a2ce
 
 
 
3f64f7a
ccc7032
 
 
 
3f64f7a
ccc7032
 
 
3f64f7a
ccc7032
 
 
 
 
 
 
 
 
3f64f7a
 
ccc7032
 
 
 
 
 
3f64f7a
ccc7032
3f64f7a
ccc7032
 
 
 
 
3f64f7a
 
 
ccc7032
 
 
3f64f7a
 
ccc7032
 
 
 
 
3f64f7a
 
 
ccc7032
 
 
 
 
 
 
 
 
 
 
 
 
3f64f7a
 
 
 
 
ccc7032
 
 
 
3f64f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238a2ce
3f64f7a
 
 
 
 
0aefca0
 
017347c
0aefca0
 
028f096
 
 
3f64f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028f096
 
504c45c
 
 
 
 
 
 
 
 
 
3a8fd83
4d78862
3a8fd83
 
 
 
4d78862
3a8fd83
 
5af9972
3a8fd83
 
5af9972
3a8fd83
 
5af9972
3a8fd83
 
e2e9e62
3a8fd83
 
a6b2606
3a8fd83
 
a6b2606
e81b8a2
 
3a8fd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81b8a2
 
3a8fd83
 
e81b8a2
3a8fd83
 
6b2f36c
3a8fd83
 
6b2f36c
3a8fd83
 
6b2f36c
3a8fd83
 
6b2f36c
3a8fd83
 
6b2f36c
e81b8a2
3a8fd83
 
 
e81b8a2
 
3a8fd83
e2e9e62
 
4d78862
a6b2606
 
3a8fd83
6b2f36c
e81b8a2
 
6b2f36c
e81b8a2
 
 
3a8fd83
 
 
e81b8a2
 
 
 
3a8fd83
e81b8a2
 
 
3a8fd83
6b2f36c
028f096
 
0aefca0
 
 
 
028f096
 
 
 
 
017347c
e5ee4a5
 
 
 
 
 
 
 
 
 
028f096
e5ee4a5
0aefca0
 
 
6b2f36c
 
0aefca0
017347c
e5ee4a5
 
 
 
 
0aefca0
 
 
 
14fbecd
 
e5ee4a5
0aefca0
028f096
 
 
 
4ede7da
 
 
2a848f1
4ede7da
 
 
7b38420
 
 
fd477ea
4ede7da
 
 
 
 
 
 
 
fd477ea
 
 
 
 
 
 
 
 
 
 
 
4ede7da
 
 
 
 
 
6b2f36c
4ede7da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2f36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81b8a2
6b2f36c
 
4ede7da
 
 
 
 
 
 
 
 
 
 
 
 
 
017347c
4ede7da
 
 
 
 
 
 
028f096
 
 
 
 
 
52fa484
 
 
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028f096
 
 
aa58c7d
 
 
 
 
8fded15
 
 
028f096
 
 
 
 
aa58c7d
 
8fded15
aa58c7d
 
 
8fded15
028f096
 
 
 
 
 
 
1d80adf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028f096
 
 
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028f096
 
 
 
 
9fb76ee
017347c
 
9fb76ee
42afeac
9fb76ee
aa58c7d
 
 
9fb76ee
 
 
 
 
 
42afeac
 
bee7763
6b2f36c
9fb76ee
aa58c7d
 
 
 
 
 
9fb76ee
 
028f096
42afeac
 
 
 
 
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42afeac
 
 
f8ec92e
42afeac
fd477ea
2084ad0
fd477ea
aa58c7d
 
 
 
 
 
 
fd477ea
aa58c7d
 
fd477ea
 
 
 
 
 
 
 
 
 
 
 
 
 
cf944ae
028f096
42afeac
aa58c7d
fd477ea
aa58c7d
 
 
f8ec92e
bee7763
6b2f36c
aa58c7d
 
 
0aefca0
aa58c7d
0aefca0
aa58c7d
cf944ae
028f096
cf944ae
aa58c7d
 
fd477ea
 
aa58c7d
fd477ea
028f096
 
 
 
95243f9
028f096
 
95243f9
028f096
95243f9
028f096
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
028f096
aa58c7d
028f096
0aefca0
aa58c7d
 
 
 
 
 
 
 
 
 
0aefca0
aa58c7d
028f096
aa58c7d
 
 
028f096
aa58c7d
 
 
 
 
 
 
 
 
028f096
95243f9
 
 
aa58c7d
028f096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b2f36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b38420
 
 
fd477ea
6b2f36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e81b8a2
6b2f36c
 
 
e81b8a2
6b2f36c
 
 
 
 
 
 
 
 
 
 
 
6c2d0ad
 
6b2f36c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58440a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017347c
58440a3
14fbecd
 
 
 
 
58440a3
 
 
 
 
1fa1225
14fbecd
 
58440a3
14fbecd
58440a3
 
 
 
 
 
b31cb3a
e81b8a2
b31cb3a
 
 
e81b8a2
58440a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b31cb3a
58440a3
 
14fbecd
 
 
 
58440a3
14fbecd
 
58440a3
14fbecd
58440a3
 
b31cb3a
e81b8a2
b31cb3a
e81b8a2
58440a3
 
 
 
 
3f64f7a
 
 
 
 
 
 
238a2ce
 
 
 
 
 
 
 
 
 
 
 
06167f4
238a2ce
 
 
 
 
06167f4
238a2ce
 
06167f4
 
 
238a2ce
 
 
 
 
 
 
 
028f096
0aefca0
 
017347c
0aefca0
 
028f096
 
 
238a2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06167f4
238a2ce
 
 
 
 
80078d8
 
 
 
 
 
 
238a2ce
 
 
 
 
 
 
 
2b4315e
 
 
 
 
 
 
 
 
 
 
 
 
238a2ce
 
 
 
 
0979f89
238a2ce
1fa1225
0cfba53
9ec255b
e767948
238a2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cfba53
04042c5
 
238a2ce
 
 
1fa1225
 
 
0cfba53
 
 
0979f89
 
 
9ec255b
 
 
e767948
 
 
238a2ce
 
0cfba53
 
 
 
 
238a2ce
 
 
 
 
 
 
 
2b4315e
ccc7032
50257ac
028f096
0bb57c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
ed191e7
 
 
 
5ad28da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed191e7
 
 
 
14fbecd
 
aa58c7d
14fbecd
017347c
aa58c7d
14fbecd
 
5ad28da
aa58c7d
14fbecd
9ec255b
 
5ad28da
9ec255b
 
 
 
 
 
 
 
14fbecd
 
 
ed191e7
 
14fbecd
 
 
 
 
 
 
 
 
 
 
 
 
ed191e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e767948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
0979f89
 
bfaa533
 
0979f89
bfaa533
 
 
 
 
0979f89
 
 
 
 
 
 
 
 
bfaa533
0979f89
bfaa533
 
 
0979f89
9ec255b
 
 
5bef62b
9ec255b
 
5bef62b
9ec255b
 
5bef62b
 
 
 
 
 
 
 
 
 
9ec255b
 
9e497a6
 
 
5ad28da
 
e767948
5ad28da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e767948
 
 
 
 
 
 
5ad28da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e767948
 
 
 
5ad28da
 
 
 
 
 
 
9e497a6
 
 
 
0aefca0
 
 
 
 
 
 
 
 
 
 
017347c
0aefca0
1fa1225
 
0aefca0
bfaa533
0aefca0
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aefca0
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfaa533
9e497a6
2ff713b
5ad28da
 
 
aa58c7d
 
 
80078d8
 
 
 
aa58c7d
80078d8
9e497a6
e767948
 
9e497a6
 
aa58c7d
9e497a6
5ad28da
9e497a6
 
 
 
 
e0a42b5
9e497a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d26ae7a
9e497a6
 
 
 
 
 
 
 
 
 
 
 
d26ae7a
9e497a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1fd67
 
 
 
 
 
 
9e497a6
 
5b1fd67
 
 
 
 
 
 
 
3122b46
 
 
 
 
 
 
 
50257ac
de60c2e
50257ac
 
b3f93f6
50257ac
b3f93f6
 
 
 
50257ac
b3f93f6
 
50257ac
 
de60c2e
50257ac
 
 
 
 
 
e0a42b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2fb7c
 
 
 
 
 
 
 
 
 
 
e0a42b5
 
 
 
 
 
 
ed2fb7c
 
 
 
 
 
 
e0a42b5
ed2fb7c
 
 
 
e0a42b5
 
 
 
9e497a6
 
 
ed2fb7c
d20d896
ed2fb7c
 
d20d896
ed2fb7c
9e497a6
ed2fb7c
 
 
 
932439c
79e85da
b6d629c
 
 
 
 
 
 
 
 
a91ee9c
b6d629c
 
932439c
a91ee9c
 
 
b6d629c
 
 
 
 
a91ee9c
b6d629c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b50b6c9
a91ee9c
b50b6c9
 
 
b6d629c
 
 
 
b50b6c9
b6d629c
a91ee9c
 
 
 
 
 
 
 
 
 
 
b6d629c
 
 
 
 
 
b50b6c9
b6d629c
a91ee9c
b6d629c
a91ee9c
b6d629c
 
b50b6c9
b6d629c
 
 
a91ee9c
b6d629c
a91ee9c
b6d629c
 
 
 
b50b6c9
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d629c
a91ee9c
b50b6c9
79e85da
b50b6c9
 
79e85da
 
 
 
 
 
b50b6c9
 
 
 
 
 
 
 
79e85da
 
 
 
 
 
 
b50b6c9
 
79e85da
 
 
b50b6c9
 
79e85da
 
 
 
 
2084ad0
79e85da
 
 
 
2084ad0
 
 
 
 
 
 
79e85da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2084ad0
 
 
 
 
 
 
 
 
 
79e85da
 
 
ed2fb7c
79e85da
ed2fb7c
 
 
 
 
79e85da
ed2fb7c
 
79e85da
ed2fb7c
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2fb7c
 
 
 
 
79e85da
ed2fb7c
 
79e85da
ed2fb7c
 
 
 
 
 
 
79e85da
ed2fb7c
7b38420
 
fd477ea
7b38420
fd477ea
7b38420
fd477ea
 
 
 
7b38420
 
 
 
 
 
 
 
fd477ea
 
 
 
 
 
 
 
b50b6c9
ed2fb7c
9b3b998
 
 
79e85da
9b3b998
ed2fb7c
 
 
 
b50b6c9
9b3b998
b6d629c
 
 
 
 
 
 
 
9b3b998
 
 
b50b6c9
 
 
 
 
b6d629c
 
b50b6c9
 
 
 
 
79e85da
 
 
 
 
 
 
 
b50b6c9
79e85da
 
 
b50b6c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb7bcd
b50b6c9
 
 
79e85da
b50b6c9
 
 
 
 
2084ad0
 
 
 
 
9b3b998
 
 
79e85da
ed2fb7c
9b3b998
 
b6d629c
 
 
 
79e85da
b6d629c
9b3b998
7b38420
 
a91ee9c
 
 
 
 
 
 
 
 
7b38420
 
a91ee9c
 
 
 
ed2fb7c
fd477ea
 
 
 
 
7b38420
ed2fb7c
 
 
 
fd477ea
7b38420
fd477ea
79e85da
2084ad0
 
84839a3
ed2fb7c
9e497a6
b6d629c
 
 
 
 
932439c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d629c
 
932439c
aa58c7d
 
 
 
 
 
 
 
 
 
b6d629c
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7032
 
 
 
 
 
 
 
 
 
 
 
 
932439c
 
 
 
 
 
 
 
 
ccc7032
b6d629c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
932439c
 
b6d629c
 
 
 
 
 
2084ad0
 
 
 
 
 
b6d629c
 
 
 
 
 
 
 
a91ee9c
b6d629c
 
 
 
ccc7032
b6d629c
ebd6217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc25e4
ebd6217
ecc25e4
 
 
 
 
 
ebd6217
 
 
 
 
 
 
 
 
ecc25e4
 
 
ebd6217
ecc25e4
ebd6217
 
ecc25e4
 
 
ebd6217
 
ecc25e4
 
 
 
ebd6217
ecc25e4
 
 
 
 
 
ebd6217
ecc25e4
 
 
 
 
 
 
 
 
 
 
ebd6217
ecc25e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d629c
 
ccc7032
b6d629c
 
 
 
 
 
 
 
a91ee9c
ccc7032
b6d629c
 
 
 
 
 
 
ccc7032
ebd6217
 
ccc7032
 
ebd6217
 
ccc7032
 
ebd6217
 
ccc7032
 
b6d629c
ccc7032
 
b6d629c
 
a91ee9c
 
 
ecc25e4
 
 
 
 
 
 
 
 
 
a91ee9c
 
b6d629c
ebd6217
 
ecc25e4
a91ee9c
ecc25e4
ebd6217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecc25e4
ebd6217
b6d629c
 
ecc25e4
b6d629c
 
 
 
ebd6217
 
 
 
 
b6d629c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
630c629
 
 
 
 
 
 
b6d629c
630c629
 
 
 
 
b6d629c
630c629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d629c
 
ecc25e4
 
b6d629c
ecc25e4
2084ad0
 
 
 
 
 
b6d629c
ecc25e4
 
b6d629c
 
 
 
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
932439c
a91ee9c
932439c
 
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2084ad0
 
 
 
 
 
 
a91ee9c
 
 
60de0ac
 
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
4df05ee
a91ee9c
 
 
4df05ee
a91ee9c
 
 
60de0ac
a91ee9c
 
 
 
 
 
4df05ee
 
2084ad0
 
 
 
 
 
a91ee9c
 
4df05ee
 
932439c
4df05ee
 
932439c
4df05ee
 
 
 
 
 
 
 
 
 
 
 
 
 
932439c
 
 
a91ee9c
932439c
4df05ee
 
 
 
 
 
 
 
 
 
a91ee9c
 
4df05ee
932439c
4d5557c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a91ee9c
 
4d5557c
a91ee9c
4d5557c
 
 
 
 
 
a91ee9c
 
 
 
4df05ee
a91ee9c
4df05ee
a91ee9c
 
 
 
4df05ee
a91ee9c
 
ccc7032
4d5557c
 
 
 
 
e8ea947
 
4d5557c
 
 
 
 
 
e8ea947
 
 
 
4d5557c
e8ea947
4d5557c
e8ea947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d5557c
e8ea947
 
 
 
 
 
 
 
 
4d5557c
 
 
e8ea947
 
 
 
 
 
 
 
4d5557c
84839a3
 
 
4d5557c
 
84839a3
4d5557c
 
e8ea947
4d5557c
84839a3
 
4d5557c
 
 
84839a3
4d5557c
 
 
 
 
 
 
ccc7032
 
4df05ee
ccc7032
 
 
4df05ee
ccc7032
 
 
 
2084ad0
 
 
 
 
 
ccc7032
 
4df05ee
 
 
 
932439c
4df05ee
 
 
 
 
 
 
 
 
 
 
 
 
932439c
 
 
ccc7032
932439c
ccc7032
 
 
932439c
 
ccc7032
932439c
ccc7032
 
 
2084ad0
 
ccc7032
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ea947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a91ee9c
 
e9969e0
a91ee9c
 
 
 
e8ea947
a91ee9c
 
e9969e0
a91ee9c
 
e9969e0
a91ee9c
 
 
 
 
e9969e0
 
 
 
 
 
 
 
 
e8ea947
 
 
 
 
 
 
 
 
 
 
e9969e0
 
 
 
 
 
 
 
 
 
 
 
 
a91ee9c
e9969e0
 
ccc7032
e9969e0
 
 
 
 
 
 
 
 
 
 
 
 
ff1aa72
a91ee9c
ff1aa72
a91ee9c
ff1aa72
 
 
a91ee9c
 
 
 
e8ea947
a91ee9c
 
 
 
e8ea947
a91ee9c
 
 
ff1aa72
e8ea947
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
e9969e0
 
 
 
 
 
 
 
 
2084ad0
 
e8ea947
 
 
 
 
 
 
2084ad0
e8ea947
 
 
 
 
 
 
 
e9969e0
 
 
 
 
 
 
 
 
ff1aa72
 
 
 
 
 
 
 
e9969e0
ff1aa72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9969e0
e8ea947
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a91ee9c
345e0f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eb7bcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e005fe
 
 
 
01b1e15
 
 
 
9879972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01b1e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9879972
01b1e15
 
 
 
 
 
 
 
 
9879972
 
 
 
 
 
 
 
 
 
 
 
 
01b1e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9879972
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01b1e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b38420
 
01b1e15
aa58c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
514a6de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01b1e15
 
900d043
514a6de
 
 
 
 
9e497a6
3f64f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
 
0abe660
 
 
 
 
 
 
fc44113
0abe660
 
 
 
 
 
 
 
 
fc44113
0abe660
fc44113
0abe660
 
 
 
b3f93f6
fc44113
 
 
b3f93f6
0abe660
fc44113
0abe660
 
 
 
 
 
 
 
 
 
fc44113
0abe660
fc44113
0abe660
504c45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bb57c4
2b4315e
d4ac480
 
 
2b4315e
0bb57c4
9e497a6
 
0bb57c4
9e497a6
 
0bb57c4
9e497a6
50257ac
 
 
 
 
 
0bb57c4
2b4315e
d4ac480
504c45c
 
9e497a6
50257ac
 
2b4315e
 
 
 
 
 
9e497a6
 
 
 
 
 
 
 
 
 
 
1fa1225
 
 
 
 
 
 
 
a4921c5
1fa1225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4921c5
 
 
1fa1225
 
 
 
 
017347c
1fa1225
 
7b1f500
 
 
9e497a6
 
2ff713b
 
e767948
2ff713b
e767948
 
 
 
 
 
 
 
 
2ff713b
 
0979f89
2ff713b
 
3f64f7a
 
 
 
900d043
 
 
 
2ff713b
 
e9d6d18
 
1ecd488
 
 
 
 
 
e9d6d18
2ff713b
 
 
 
 
0979f89
 
 
 
 
ed191e7
 
 
 
 
 
 
 
 
 
 
 
 
 
0979f89
ed191e7
0979f89
18f750b
d26ae7a
18f750b
e9d6d18
18f750b
d26ae7a
e9d6d18
0979f89
 
 
 
 
 
 
 
 
 
 
 
 
e9d6d18
18f750b
bfaa533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
017347c
bfaa533
 
 
 
 
 
 
 
 
 
2ff713b
18f750b
 
0979f89
 
 
 
 
 
 
a91ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0979f89
 
 
18f750b
 
 
 
 
e9d6d18
20be0a1
e9d6d18
20be0a1
 
 
 
 
 
 
d26ae7a
18f750b
e9d6d18
18f750b
a5e9e6a
e9d6d18
 
18f750b
e9d6d18
18f750b
1ecd488
 
 
 
 
 
 
e9d6d18
e02e015
 
80078d8
 
 
 
 
 
 
 
 
 
 
18f750b
 
e02e015
18f750b
e767948
0979f89
 
d26ae7a
 
e767948
 
0979f89
 
 
 
e767948
 
 
 
 
 
 
 
18f750b
 
 
e767948
9ec255b
ccc7032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bef62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccc7032
 
 
5bef62b
 
e767948
5bef62b
 
e767948
5bef62b
 
e767948
5bef62b
 
 
 
 
 
 
 
e767948
ccc7032
e767948
9ec255b
e767948
 
 
 
 
18f750b
e02e015
64e57f1
80078d8
 
 
bfaa533
 
a91ee9c
 
 
 
 
 
 
 
80078d8
 
 
e02e015
0979f89
e02e015
80078d8
 
e02e015
18f750b
a5e9e6a
 
 
 
 
 
e02e015
a5e9e6a
e767948
 
 
 
 
 
 
 
 
 
0979f89
e02e015
e767948
 
2ff713b
18f750b
e9d6d18
18f750b
e9d6d18
2ff713b
e02e015
2ff713b
bfaa533
 
 
2ff713b
bfaa533
1fa1225
 
 
 
 
 
 
 
bfaa533
 
0979f89
 
bfaa533
 
 
cf944ae
0979f89
cf944ae
0979f89
e767948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf944ae
e767948
 
cf944ae
e767948
bfaa533
 
 
0979f89
bfaa533
2ff713b
 
 
 
 
 
 
 
 
 
 
 
9e497a6
 
 
b4fbd6e
8163b32
 
e02e015
9e497a6
e767948
9e497a6
 
900d043
e02e015
 
 
 
 
 
 
 
9e497a6
2ff713b
e02e015
 
 
 
 
2ff713b
 
 
 
 
900d043
e02e015
 
9e497a6
 
 
2ff713b
9e497a6
50257ac
9e497a6
 
5063ea7
18f750b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
import os
import sys
from dotenv import load_dotenv  # type: ignore
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
load_dotenv()  # Load .env file before anything else

import shutil
from fastapi import FastAPI, HTTPException, Depends, status, Request, UploadFile, File  # type: ignore
from fastapi.responses import JSONResponse, StreamingResponse, FileResponse  # type: ignore
from pydantic import BaseModel  # type: ignore
from fastapi.middleware.cors import CORSMiddleware  # type: ignore
from database import get_db_connection  # type: ignore
import uuid
import time
from datetime import datetime, timedelta, timezone
import collections
import asyncio
import random
import json
import base64
from typing import Optional, List, Dict, Any, Union, cast
import re
import ast
import traceback
import functools
import platform
import threading
from contextlib import asynccontextmanager
import smtplib
import socket
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import httpx  # pyre-ignore[21]
try:
    import psutil  # type: ignore
    PSUTIL_AVAILABLE = True
except ImportError:
    PSUTIL_AVAILABLE = False
try:
    from bs4 import BeautifulSoup  # type: ignore
    BS4_AVAILABLE = True
except ImportError:
    BS4_AVAILABLE = False
    print("WARNING: beautifulsoup4 not installed. Directory scraping disabled. Run: pip install beautifulsoup4 lxml")

# Telethon Imports
from telethon.sync import TelegramClient  # type: ignore
from telethon.sessions import StringSession  # type: ignore
from telethon.tl.functions.channels import JoinChannelRequest, GetParticipantsRequest, GetFullChannelRequest  # type: ignore
from telethon.tl.functions.messages import SearchGlobalRequest, CheckChatInviteRequest, ImportChatInviteRequest  # type: ignore
from telethon.tl.functions.contacts import SearchRequest # type: ignore
from telethon.tl.types import ChannelParticipantsSearch, InputMessagesFilterEmpty, InputPeerEmpty, InputPeerChannel, InputPeerChat, User # type: ignore
from telethon.tl.functions.help import GetConfigRequest # type: ignore
from fastapi.staticfiles import StaticFiles # type: ignore

from config import CORE_PROVIDER_ID, CORE_PROVIDER_KEY  # type: ignore
from auth import get_password_hash, verify_password, create_access_token, get_current_user_id  # type: ignore
import uvicorn  # type: ignore

# Backward-compatible async timeout helper (Python 3.11+ has asyncio.timeout natively)
if hasattr(asyncio, "timeout"):
    async_timeout = asyncio.timeout
else:
    @asynccontextmanager
    async def async_timeout(delay: float):
        loop = asyncio.get_running_loop()
        task = asyncio.current_task()
        handle = loop.call_later(delay, task.cancel) if task else None
        try:
            yield
        except asyncio.CancelledError:
            raise asyncio.TimeoutError()
        finally:
            if handle:
                handle.cancel()

# Simple in-memory log buffer
LOG_BUFFER = collections.deque(maxlen=100)

def log_debug(msg: str):
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    formatted = f"[{timestamp}] {msg}"
    print(formatted)
    LOG_BUFFER.append(formatted)

# --- Tiered Plan Configurations (Dynamic from DB) ---
@functools.lru_cache(maxsize=1)
def get_plan_configs() -> Dict[str, Dict[str, Any]]:
    configs: Dict[str, Dict[str, Any]] = {
        "free": {"max_daily_campaigns": 20, "max_accounts": 1, "max_daily_keywords": 5, "scrape_limit": 50, "max_templates": 1, "has_premium_access": False},
        "basic": {"max_daily_campaigns": 50, "max_accounts": 2, "max_daily_keywords": 15, "scrape_limit": 100, "max_templates": 2, "has_premium_access": True},
        "standard": {"max_daily_campaigns": 150, "max_accounts": 5, "max_daily_keywords": 30, "scrape_limit": 250, "max_templates": 5, "has_premium_access": True},
        "premium": {"max_daily_campaigns": 300, "max_accounts": 10, "max_daily_keywords": 50, "scrape_limit": 500, "max_templates": 10, "has_premium_access": True},
        "unlimited": {"max_daily_campaigns": 999999, "max_accounts": 999999, "max_daily_keywords": 999999, "scrape_limit": 999999, "max_templates": 999999, "has_premium_access": True}
    }
    conn = None
    try:
        conn = get_db_connection()
        rows = conn.execute("SELECT * FROM plans").fetchall()
        for r in rows:
            configs[r["key"]] = {
                "name": r["name"],
                "price": r["price"],
                "max_daily_campaigns": r["max_daily_campaigns"],
                "max_accounts": r["max_accounts"],
                "max_daily_keywords": r["max_daily_keywords"],
                "scrape_limit": r["scrape_limit"],
                "max_templates": r["max_templates"],
                "has_premium_access": bool(r["has_premium_access"]),
                "perks": json.loads(r["perks"]) if r["perks"] else []
            }
    except Exception as e:
        print(f"Error loading plan configs: {e}")
    finally:
        if conn and hasattr(conn, "close"): 
            conn.close()
    
    return configs

def get_current_plan_configs() -> Dict[str, Dict[str, Any]]:
    # Bridge for cached lookup
    return get_plan_configs()

def check_and_reset_daily_limits(conn, user_id, user_row):
    """Resets daily counts if the day has changed."""
    last_reset = user_row["last_reset_date"]
    now_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
    
    if last_reset != now_date:
        conn.execute(
            "UPDATE users SET daily_campaign_count = 0, daily_keyword_count = 0, last_reset_date = ? WHERE id = ?",
            (now_date, user_id)
        )
        if hasattr(conn, "commit"): conn.commit()
        return True
    
    # Subscription Expiry Check
    expiry_date_str = user_row["plan_expires_at"]
    if expiry_date_str and user_row["plan"] != "free":
        try:
            # Format: 2024-03-24T12:00
            expiry_dt = datetime.fromisoformat(expiry_date_str.replace('Z', ''))
            if datetime.now() > expiry_dt:
                log_debug(f"SUBSCRIPTION: Plan expired for {user_id}. Resetting to Free.")
                conn.execute(
                    "UPDATE users SET plan = 'free', plan_expires_at = NULL, is_approved = 0 WHERE id = ?",
                    (user_id,)
                )
                if hasattr(conn, "commit"): conn.commit()
                return True
        except:
            pass

    return False

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup logic
    print("CORE: Initializing Database Schema...")
    try:
        from database import init_db  # type: ignore
        init_db()
        print("CORE: Database Schema Initialized.")
        
        # Cleanup stuck tasks
        try:
            conn = get_db_connection()
            conn.execute("UPDATE tasks SET status = 'pending' WHERE status = 'processing'")
            if hasattr(conn, "commit"): conn.commit()
            if hasattr(conn, "close"): conn.close()
            print("CORE: Stuck tasks reset to pending.")
        except Exception as db_e:
            log_debug(f"CORE_DB_CLEANUP_ERROR: {db_e}")

        print("CORE: Starting Task Automation Poller...")
        poller_task = asyncio.create_task(task_poller())
        print("CORE: System Ready and Poller Started.")
    except Exception as e:
        log_debug(f"CRITICAL_STARTUP_ERROR: {e}")
        print(f"CRITICAL_STARTUP_ERROR: {e}")
        # We DO NOT re-raise, so the FastAPI server can still start on port 7860
        # and we can debug it via the UI if needed.
        poller_task = None # type: ignore
    
    yield
    
    # Shutdown logic
    print("CORE: Shutting down...")
    if poller_task is not None:
        poller_task.cancel()
        try:
            await poller_task
        except asyncio.CancelledError:
            print("CORE: Poller task cancelled.")
    
    # Close pool
    await POOL.disconnect_all()
    print("CORE: All clients disconnected. Shutdown complete.")

async def verify_telegram_info(username: str) -> Dict[str, Any]:
    """
    Checks t.me/s/username and t.me/username anonymously to get title, member count, photo, bio, and live channel feed posts.
    Supports K/M abbreviations, spaces, and commas.
    """
    if not username: return {"exists": False, "participants_count": 0, "posts": []}
    clean_user = username.replace('@', '').replace('https://t.me/', '').replace('t.me/', '').strip().split('/')[0].split('?')[0]
    if not clean_user: return {"exists": False, "participants_count": 0, "posts": []}

    # Safety check for BeautifulSoup availability
    if not BS4_AVAILABLE:
        return {"exists": True, "participants_count": 0, "posts": []}

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
    }

    title = ""
    description = ""
    photo_url = None
    members = 0
    chat_type = "group"
    posts = []

    try:
        async with httpx.AsyncClient(follow_redirects=True, timeout=10) as client:
            # 1. First fetch t.me/s/ for live posts & channel metadata
            stream_url = f"https://t.me/s/{clean_user}"
            try:
                resp_s = await client.get(stream_url, headers=headers)
                if resp_s.status_code == 200:
                    soup_s = BeautifulSoup(resp_s.text, 'html.parser')
                    
                    # Title
                    t_elem = soup_s.find("div", class_="tgme_channel_info_header_title") or soup_s.find("div", class_="tgme_page_title")
                    if t_elem: title = t_elem.get_text(strip=True)

                    # Description
                    d_elem = soup_s.find("div", class_="tgme_channel_info_description") or soup_s.find("div", class_="tgme_page_description")
                    if d_elem: description = d_elem.get_text(strip=True)

                    # Photo
                    p_elem = soup_s.find("img", class_="tgme_page_photo_image")
                    if p_elem: photo_url = p_elem.get("src")

                    # Counters
                    counters = soup_s.find("div", class_="tgme_channel_info_counters") or soup_s.find("div", class_="tgme_page_extra")
                    if counters:
                        text_cnt = counters.get_text()
                        if "subscriber" in text_cnt.lower(): chat_type = "channel"
                        m = re.search(r'([\d\s,.]+)\s*([KkMm]?)\s*(subscribers|members|sub|subs|online)', text_cnt, re.IGNORECASE)
                        if m:
                            num_str = m.group(1).replace(' ', '').replace(',', '').strip()
                            unit = (m.group(2) or '').upper()
                            try:
                                val = float(num_str)
                                if unit == 'K': members = int(val * 1000)
                                elif unit == 'M': members = int(val * 1000000)
                                else: members = int(val)
                            except Exception:
                                digits = re.sub(r'\D', '', num_str)
                                members = int(digits) if digits else 0

                    # Live Posts
                    for m_elem in soup_s.find_all("div", class_="tgme_widget_message")[-15:]:
                        text_box = m_elem.find("div", class_="tgme_widget_message_text")
                        time_box = m_elem.find("time")
                        views_box = m_elem.find("span", class_="tgme_widget_message_views")
                        photo_box = m_elem.find("a", class_="tgme_widget_message_photo_wrap")
                        post_photo = None
                        if photo_box and photo_box.get("style"):
                            style_val = str(photo_box.get("style") or "")
                            pmatch = re.search(r'url\(\s*[\'"]?(.*?)[\'"]?\s*\)', style_val)
                            if pmatch: post_photo = pmatch.group(1)

                        post_txt = text_box.get_text(separator="\n", strip=True) if text_box else ""
                        post_date = time_box.get_text(strip=True) if time_box else (time_box.get("datetime") if time_box else "")
                        post_views = views_box.get_text(strip=True) if views_box else ""
                        p_id = m_elem.get("data-post", "")

                        if post_txt or post_photo:
                            posts.append({
                                "id": p_id,
                                "text": post_txt,
                                "date": post_date,
                                "views": post_views,
                                "photo_url": post_photo
                            })
            except Exception:
                pass

            # 2. Fallback to t.me/clean_user if title or members wasn't found
            if not title or members == 0:
                resp = await client.get(f"https://t.me/{clean_user}", headers=headers)
                if resp.status_code == 200:
                    soup = BeautifulSoup(resp.text, 'html.parser')
                    title_elem = soup.find("div", class_="tgme_page_title")
                    if title_elem and not title:
                        title = title_elem.get_text(strip=True)
                    
                    if not description:
                        desc_elem = soup.find("div", class_="tgme_page_description")
                        if desc_elem: description = desc_elem.get_text(strip=True)
                    
                    if not photo_url:
                        photo_elem = soup.find("img", class_="tgme_page_photo_image")
                        if photo_elem: photo_url = photo_elem.get("src")
                    
                    extra_elem = soup.find("div", class_="tgme_page_extra")
                    if extra_elem and members == 0:
                        text = extra_elem.get_text()
                        if "subscriber" in text.lower(): chat_type = "channel"
                        m = re.search(r'([\d\s,.]+)\s*([KkMm]?)\s*(subscribers|members|sub|subs|online)', text, re.IGNORECASE)
                        if m:
                            num_str = m.group(1).replace(' ', '').replace(',', '').strip()
                            unit = (m.group(2) or '').upper()
                            try:
                                val = float(num_str)
                                if unit == 'K': members = int(val * 1000)
                                elif unit == 'M': members = int(val * 1000000)
                                else: members = int(val)
                            except Exception:
                                digits = re.sub(r'\D', '', num_str)
                                members = int(digits) if digits else 0

        can_send = chat_type != "channel"
        return {
            "exists": bool(title or members > 0 or posts),
            "participants_count": members,
            "title": title or clean_user,
            "description": description,
            "photo_url": photo_url,
            "type": chat_type,
            "username": clean_user,
            "can_send_messages": can_send,
            "posting_permission": "Open (Members Can Post)" if can_send else "Broadcast Channel (Admins Only)",
            "posting_status": "allowed" if can_send else "restricted",
            "posting_reason": "Public Telegram group - members can post." if can_send else "Broadcast channel: only admins can post messages.",
            "posts": posts
        }
    except Exception as e:
        return {
            "exists": True, 
            "participants_count": 0, 
            "username": clean_user, 
            "can_send_messages": True,
            "posting_permission": "Public Group",
            "posting_status": "allowed",
            "posting_reason": "Public Telegram community.",
            "posts": []
        }


async def get_current_admin(user_id: str = Depends(get_current_user_id)):
    print(f"AUTH_DEBUG: Checking admin rights for {user_id}")
    if user_id == "admin_virtual_id":
        return user_id
    conn = get_db_connection()
    try:
        row = conn.execute("SELECT role FROM users WHERE id = ?", (user_id,)).fetchone()
        if not row:
            print(f"AUTH_DEBUG: User {user_id} not found in DB")
            raise HTTPException(status_code=403, detail="Admin access required: User not found")
        if row[0] != 'admin':
            print(f"AUTH_DEBUG: User {user_id} has role {row[0]}, expected admin")
            raise HTTPException(status_code=403, detail=f"Admin access required: role is {row[0]}")
        return user_id
    finally:
        if hasattr(conn, "close"): conn.close()

app = FastAPI(title="ArkiTel Automation API", lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # In production, set this to process.env.FRONTEND_URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Static files for profile photos and preview post media - resolve both local and root static paths safely
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
_STATIC_DIR = os.path.join(_BASE_DIR, "static")
_AVATARS_DIR = os.path.join(_STATIC_DIR, "avatars")
_PREVIEWS_DIR = os.path.join(_STATIC_DIR, "previews")
_UPLOADS_DIR = os.path.join(_STATIC_DIR, "uploads")
_DM_MEDIA_DIR = os.path.join(_STATIC_DIR, "dm_media")
os.makedirs(_AVATARS_DIR, exist_ok=True)
os.makedirs(_PREVIEWS_DIR, exist_ok=True)
os.makedirs(_UPLOADS_DIR, exist_ok=True)
os.makedirs(_DM_MEDIA_DIR, exist_ok=True)
_ROOT_STATIC_DIR = os.path.abspath("static")
if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
    os.makedirs(os.path.join(_ROOT_STATIC_DIR, "avatars"), exist_ok=True)
    os.makedirs(os.path.join(_ROOT_STATIC_DIR, "previews"), exist_ok=True)
    os.makedirs(os.path.join(_ROOT_STATIC_DIR, "uploads"), exist_ok=True)
    os.makedirs(os.path.join(_ROOT_STATIC_DIR, "dm_media"), exist_ok=True)

app.mount("/static", StaticFiles(directory=_STATIC_DIR), name="static")

@app.get("/api/telegram/preview")
async def preview_telegram_channel(target: str, user_id: str = Depends(get_current_user_id)):
    """Fetches in-app preview info (avatar, title, members, description, live feed posts) for any Telegram channel/group."""
    raw_target = (target or '').strip()
    if not raw_target:
        raise HTTPException(status_code=400, detail="Target username or link required")

    clean_target = raw_target
    for prefix in ['https://t.me/s/', 'http://t.me/s/', 'https://t.me/', 'http://t.me/', 't.me/s/', 't.me/', '@']:
        if clean_target.lower().startswith(prefix.lower()):
            clean_target = clean_target[len(prefix):]
    clean_target = clean_target.split('?')[0].strip()
    if '/' in clean_target and not ('joinchat' in raw_target or '+' in raw_target):
        clean_target = clean_target.split('/')[0].strip()

    if not clean_target:
        raise HTTPException(status_code=400, detail="Target username or link required")

    # 1. First attempt anonymous public web scraping
    info = await verify_telegram_info(clean_target)

    # 2. If web posts are empty (e.g. groups or channels where t.me/s/ redirects or is disabled), fallback to connected MTProto client
    if not info.get("posts") or len(info.get("posts", [])) == 0:
        try:
            conn = get_db_connection()
            acc_row = None
            try:
                acc_row = conn.execute(
                    "SELECT phone_number, user_id FROM accounts WHERE (user_id = ? OR user_id = 'admin_virtual_id') AND status = 'active' LIMIT 1",
                    (user_id,)
                ).fetchone()
                if not acc_row:
                    acc_row = conn.execute(
                        "SELECT phone_number, user_id FROM accounts WHERE status = 'active' LIMIT 1"
                    ).fetchone()
            finally:
                if hasattr(conn, "close"): conn.close()

            if acc_row:
                phone_num = acc_row[0]
                owner_uid = acc_row[1]
                client = await POOL.get_client(owner_uid, phone_num)
                if client and client.is_connected():
                    # Handle invite link hashes or standard usernames
                    is_invite = '+' in raw_target or 'joinchat' in raw_target
                    if is_invite:
                        inv_hash = ""
                        if '+' in raw_target:
                            inv_hash = raw_target.split('+')[-1].split('/')[0].split('?')[0].strip()
                        elif 'joinchat/' in raw_target:
                            inv_hash = raw_target.split('joinchat/')[-1].split('/')[0].split('?')[0].strip()
                        if inv_hash:
                            try:
                                check_res = await client(CheckChatInviteRequest(inv_hash))
                                chat_obj = getattr(check_res, 'chat', None)
                                if chat_obj:
                                    info["title"] = getattr(chat_obj, 'title', info.get("title"))
                                    info["participants_count"] = getattr(chat_obj, 'participants_count', info.get("participants_count"))
                                    info["type"] = "channel" if getattr(chat_obj, 'broadcast', False) else "group"
                                    info["exists"] = True
                            except Exception as inv_e:
                                log_debug(f"PREVIEW: Invite link check error: {inv_e}")
                    else:
                        entity = None
                        try:
                            target_arg = int(clean_target) if clean_target.lstrip('-').isdigit() else clean_target
                            entity = await client.get_entity(target_arg)
                        except Exception as ent_err:
                            log_debug(f"PREVIEW: get_entity failed for {clean_target}: {ent_err}")

                        if entity:
                            info["exists"] = True
                            if not info.get("title") or info.get("title") == clean_target:
                                info["title"] = getattr(entity, 'title', getattr(entity, 'first_name', clean_target))
                            
                            is_broadcast = bool(getattr(entity, 'broadcast', False))
                            default_banned_rights = getattr(entity, 'default_banned_rights', None)
                            send_messages_banned = False
                            if default_banned_rights:
                                send_messages_banned = bool(getattr(default_banned_rights, 'send_messages', False))

                            if is_broadcast:
                                info["type"] = "channel"
                                info["can_send_messages"] = False
                                info["posting_permission"] = "Broadcast Channel (Admins Only)"
                                info["posting_status"] = "restricted"
                                info["posting_reason"] = "Broadcast channel: only channel admins can post messages. Regular members cannot send messages or run campaigns here."
                            elif getattr(entity, 'megagroup', False) or getattr(entity, 'gigagroup', False):
                                info["type"] = "supergroup"
                                if send_messages_banned:
                                    info["can_send_messages"] = False
                                    info["posting_permission"] = "Chat Restricted (Admins Only)"
                                    info["posting_status"] = "restricted"
                                    info["posting_reason"] = "Group admins have disabled message sending for regular members. Outreach campaigns will fail."
                                else:
                                    info["can_send_messages"] = True
                                    info["posting_permission"] = "Open (Members Can Post)"
                                    info["posting_status"] = "allowed"
                                    info["posting_reason"] = "Members are allowed to send messages in this group. Safe for campaign outreach."
                            else:
                                info["type"] = "group"
                                if send_messages_banned:
                                    info["can_send_messages"] = False
                                    info["posting_permission"] = "Chat Restricted (Admins Only)"
                                    info["posting_status"] = "restricted"
                                    info["posting_reason"] = "Group admins have disabled message sending for regular members. Outreach campaigns will fail."
                                else:
                                    info["can_send_messages"] = True
                                    info["posting_permission"] = "Open (Members Can Post)"
                                    info["posting_status"] = "allowed"
                                    info["posting_reason"] = "Members are allowed to send messages in this group. Safe for campaign outreach."

                            full_chat_obj = None
                            if not info.get("participants_count") or info.get("participants_count") == 0:
                                p_cnt = getattr(entity, 'participants_count', 0)
                                if not p_cnt:
                                    try:
                                        full = await client(GetFullChannelRequest(cast(Any, entity)))  # type: ignore
                                        full_chat_obj = getattr(full, 'full_chat', None)
                                        p_cnt = getattr(full_chat_obj, 'participants_count', 0)
                                    except Exception:
                                        pass
                                if p_cnt:
                                    info["participants_count"] = p_cnt

                            if full_chat_obj:
                                slowmode = getattr(full_chat_obj, 'slowmode_seconds', None)
                                if slowmode and slowmode > 0:
                                    info["slowmode_seconds"] = slowmode
                                    info["posting_reason"] = f"{info.get('posting_reason', '')} (Slowmode: {slowmode}s delay)"

                            if not info.get("photo_url"):
                                try:
                                    avatar_filename = f"avatar_{clean_target}.jpg"
                                    avatar_path = os.path.join(_AVATARS_DIR, avatar_filename)
                                    if not os.path.exists(avatar_path):
                                        await client.download_profile_photo(cast(Any, entity), file=avatar_path)  # type: ignore
                                    if os.path.exists(avatar_path):
                                        info["photo_url"] = f"/static/avatars/{avatar_filename}"
                                        if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
                                            try:
                                                shutil.copy2(avatar_path, os.path.join(_ROOT_STATIC_DIR, "avatars", avatar_filename))
                                            except Exception:
                                                pass
                                except Exception:
                                    pass

                            # Fetch recent messages directly via MTProto with sender profiling
                            try:
                                raw_msgs: Any = await client.get_messages(cast(Any, entity), limit=20)  # type: ignore
                                msgs: List[Any] = list(raw_msgs) if raw_msgs and hasattr(raw_msgs, '__iter__') else []
                                telethon_posts = []
                                media_download_count = 0
                                sender_avatar_download_count = 0
                                sender_cache: Dict[int, Dict[str, Any]] = {}

                                for m in msgs:
                                    text = (m.message or '').strip()
                                    date_str = m.date.strftime("%b %d, %Y, %H:%M") if getattr(m, 'date', None) else "Recent"
                                    views_str = str(m.views) if getattr(m, 'views', None) is not None else ""
                                    
                                    # Resolve sender information
                                    sender_name = "Unknown Member"
                                    sender_username = None
                                    sender_avatar = None
                                    sender_is_channel = False
                                    
                                    s_id = getattr(m, 'sender_id', None)
                                    if s_id and s_id in sender_cache:
                                        c_info = sender_cache[s_id]
                                        sender_name = c_info["name"]
                                        sender_username = c_info["username"]
                                        sender_avatar = c_info["avatar"]
                                        sender_is_channel = c_info["is_channel"]
                                    else:
                                        try:
                                            s_obj = await m.get_sender()
                                            if s_obj:
                                                if getattr(s_obj, 'broadcast', False) or hasattr(s_obj, 'title'):
                                                    sender_name = getattr(s_obj, 'title', clean_target)
                                                    sender_username = getattr(s_obj, 'username', None)
                                                    sender_is_channel = True
                                                else:
                                                    fn = (getattr(s_obj, 'first_name', '') or '').strip()
                                                    ln = (getattr(s_obj, 'last_name', '') or '').strip()
                                                    sender_name = f"{fn} {ln}".strip() or getattr(s_obj, 'username', 'Member')
                                                    sender_username = getattr(s_obj, 'username', None)
                                                    sender_is_channel = False

                                                real_id = getattr(s_obj, 'id', s_id)
                                                if real_id and sender_avatar_download_count < 10:
                                                    s_filename = f"avatar_sender_{real_id}.jpg"
                                                    s_path = os.path.join(_AVATARS_DIR, s_filename)
                                                    try:
                                                        if not os.path.exists(s_path):
                                                            await client.download_profile_photo(cast(Any, s_obj), file=s_path)  # type: ignore
                                                        if os.path.exists(s_path):
                                                            sender_avatar = f"/static/avatars/{s_filename}"
                                                            sender_avatar_download_count += 1
                                                            if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
                                                                try:
                                                                    shutil.copy2(s_path, os.path.join(_ROOT_STATIC_DIR, "avatars", s_filename))
                                                                except Exception:
                                                                    pass
                                                    except Exception:
                                                        pass

                                                if s_id:
                                                    sender_cache[s_id] = {
                                                        "name": sender_name,
                                                        "username": sender_username,
                                                        "avatar": sender_avatar,
                                                        "is_channel": sender_is_channel
                                                    }
                                        except Exception as s_err:
                                            log_debug(f"PREVIEW: sender resolution error: {s_err}")

                                    photo_url = None
                                    has_media = bool(getattr(m, 'photo', None) or getattr(m, 'video', None) or getattr(m, 'document', None))
                                    if has_media and media_download_count < 8:
                                        p_filename = f"post_{clean_target}_{m.id}.jpg"
                                        p_path = os.path.join(_PREVIEWS_DIR, p_filename)
                                        try:
                                            if not os.path.exists(p_path):
                                                await client.download_media(m, file=p_path, thumb=-1)
                                            if os.path.exists(p_path):
                                                photo_url = f"/static/previews/{p_filename}"
                                                media_download_count += 1
                                                if os.path.abspath(_STATIC_DIR) != _ROOT_STATIC_DIR:
                                                    try:
                                                        shutil.copy2(p_path, os.path.join(_ROOT_STATIC_DIR, "previews", p_filename))
                                                    except Exception:
                                                        pass
                                        except Exception as dl_e:
                                            log_debug(f"PREVIEW: Media download error for {m.id}: {dl_e}")

                                    # Enrich text if blank
                                    if not text:
                                        if getattr(m, 'file', None) and getattr(m.file, 'name', None):
                                            text = f"📎 {m.file.name}"
                                        elif getattr(m, 'poll', None):
                                            try:
                                                text = f"📊 Poll: {m.poll.poll.question.text}"
                                            except Exception:
                                                text = "📊 Poll"
                                        elif getattr(m, 'voice', None):
                                            text = "🎤 Voice message"
                                        elif getattr(m, 'video_note', None):
                                            text = "📹 Video note"
                                        elif getattr(m, 'sticker', None):
                                            text = "✨ Telegram Sticker"

                                    if text or photo_url:
                                        telethon_posts.append({
                                            "id": str(m.id),
                                            "text": text,
                                            "date": date_str,
                                            "views": views_str,
                                            "photo_url": photo_url,
                                            "sender_name": sender_name,
                                            "sender_username": sender_username,
                                            "sender_avatar": sender_avatar,
                                            "sender_is_channel": sender_is_channel
                                        })

                                if telethon_posts:
                                    info["posts"] = telethon_posts
                                    distinct_senders = {p["sender_name"] for p in telethon_posts if p.get("sender_name")}
                                    info["unique_senders_count"] = len(distinct_senders)
                                    if len(distinct_senders) <= 1 and len(telethon_posts) >= 2:
                                        info["single_sender_warning"] = True
                                        info["sender_insight"] = "Single sender detected in recent messages (likely admin broadcast only)."
                                    else:
                                        info["single_sender_warning"] = False
                                        info["sender_insight"] = f"{len(distinct_senders)} distinct members chatting in recent messages."
                                    log_debug(f"PREVIEW: Telethon loaded {len(telethon_posts)} live messages for {clean_target} ({len(distinct_senders)} unique senders)")
                            except Exception as msgs_e:
                                log_debug(f"PREVIEW: Telethon get_messages error for {clean_target}: {msgs_e}")
        except Exception as e:
            log_debug(f"PREVIEW: Telethon fallback error for {clean_target}: {e}")

    return {"status": "success", "data": info}

# Moved here so 'app' is defined before this route is registered (was crashing import).
@app.get("/api/debug/logs")
async def get_logs(user_id: str = Depends(get_current_admin)):
    return {"logs": list(LOG_BUFFER)}

async def fetch_tg_profile_data(client: TelegramClient, phone_number: str):
    """Fetches profile details and downloads photo for an authenticated client."""
    me = await client.get_me()
    first_name = getattr(me, 'first_name', '')
    last_name = getattr(me, 'last_name', '')
    username = getattr(me, 'username', '')
    
    # Try to get country from phone number prefix
    country = "Unknown"
    if phone_number.startswith('+'):
        prefixes = {"+1": "USA", "+44": "UK", "+234": "Nigeria", "+91": "India", "+62": "Indonesia"}
        for p, c in prefixes.items():
            if phone_number.startswith(p):
                country = c
                break

    # Download profile photo
    photo_path = None
    try:
        user_id = getattr(me, 'id', 0)
        user_photo = getattr(me, 'photo', None)
        if user_photo and user_id:
            filename = f"avatar_{user_id}.jpg"
            save_path = os.path.join(_AVATARS_DIR, filename)
            await client.download_profile_photo(me, file=save_path)
            
            # Keep root static copy synced in case server launched from workspace root
            root_save_path = os.path.join(_ROOT_STATIC_DIR, "avatars", filename)
            if os.path.abspath(save_path) != os.path.abspath(root_save_path):
                try:
                    import shutil
                    shutil.copy2(save_path, root_save_path)
                except Exception:
                    pass
            photo_path = f"/static/avatars/{filename}"
    except Exception as e:
        print(f"BKG: Photo download error: {e}")
        
    return {
        "first_name": first_name,
        "last_name": last_name,
        "username": username,
        "profile_photo": photo_path,
        "country": country
    }


@app.get("/api/debug/db")
async def debug_db():
    url = os.getenv("TURSO_DATABASE_URL", "NOT_SET")
    token = os.getenv("TURSO_AUTH_TOKEN", "NOT_SET")
    u_str = url if url else "NOT_SET"
    display_url = "NOT_SET"
    if u_str != "NOT_SET":
        d_url = ""
        for i, char in enumerate(u_str):
            if i >= 20: break
            d_url += char
        display_url = d_url + "..."
    return {
        "url": display_url,
        "token_set": bool(token and token != "NOT_SET"),
        "env_file_exists": os.path.exists(".env")
    }

async def resolve_tg_entity(client, target):
    """Smarter handle resolver for Telegram joining & Campaigns."""
    try:
        # 1. Normalize target
        target_str = str(target).strip()
        if not target_str:
            return None
            
        log_debug(f"RESOLVE: Attempting to resolve '{target_str}'...")

        # 2. Try numeric ID directly
        if target_str.lstrip('-').isdigit():
            try:
                # Try as-is
                entity = await client.get_entity(int(target_str))
                return entity
            except Exception:
                # If it's a positive number, try adding -100 prefix (common for Channels/Supergroups)
                if not target_str.startswith('-'):
                    try:
                        entity = await client.get_entity(int(f"-100{target_str}"))
                        return entity
                    except: pass
        
        # 3. Try resolving as invite link / URL
        if "t.me/" in target_str or "telegram.me/" in target_str:
            clean_url = target_str.replace("https://", "").replace("http://", "")
            # Handle joinchat links (private)
            if "joinchat/" in clean_url or "/+" in clean_url:
                hash_val = clean_url.split('/')[-1].replace('+', '')
                return ("invite", hash_val)
            else:
                # Extract username from public link
                username = clean_url.split('/')[-1]
                target_str = username # Continue with username logic

        # 4. Handle usernames (@name or name)
        username = target_str.lstrip('@')
        if not username.replace('_', '').isalnum():
            # If it's not a valid username format, maybe it's still a numeric ID we missed?
            pass
        else:
            try:
                # Try getting by username
                entity = await client.get_entity(username)
                return entity
            except Exception as e:
                log_debug(f"RESOLVE: Username '{username}' direct resolve failed: {e}")
                # Fallback: Search globally to "prime" the session
                try:
                    from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
                    from telethon.tl.types import InputMessagesFilterEmpty, InputPeerEmpty # type: ignore
                    
                    # Search globally for this username
                    await client(SearchGlobalRequest(q=username, filter=InputMessagesFilterEmpty(), min_date=None, max_date=None, offset_rate=0, offset_peer=InputPeerEmpty(), offset_id=0, limit=1))
                    # Try again after search
                    entity = await client.get_entity(username)
                    return entity
                except Exception as e2:
                    log_debug(f"RESOLVE: Global search fallback for '{username}' failed: {e2}")

        # 5. DB lookup as last resort (if we have a mapping stored)
        try:
            conn = get_db_connection()
            # Try finding a username for this ID if we have it
            row = conn.execute("SELECT username FROM scraped_groups WHERE id = ?", (target_str,)).fetchone()
            if row and row[0]:
                try:
                    entity = await client.get_entity(row[0])
                    return entity
                except: pass
            if hasattr(conn, "close"): conn.close()
        except: pass

    except Exception as gl_e:
        log_debug(f"RESOLVE_FATAL_ERR for {target}: {str(gl_e)}")

    return None

async def smart_join(client, target, user_id: str, phone_number: str):
    """Joins a group/channel and records it in the persistence layer."""
    from telethon.tl.functions.channels import JoinChannelRequest # type: ignore
    from telethon.tl.functions.messages import ImportChatInviteRequest # type: ignore
    
    res = await resolve_tg_entity(client, target)
    if not res:
        raise Exception(f"Could not resolve entity for {target}")
        
    if isinstance(res, User):
        raise Exception(f"Target '{target}' resolved to a User entity. You cannot join a User.")
        
    if isinstance(res, tuple) and res[0] == "invite":
        result = await client(ImportChatInviteRequest(res[1]))
        target_id = str(target) # For invite links, we use the link as ID or the hash
    else:
        result = await client(JoinChannelRequest(res))
        target_id = str(getattr(res, 'id', target))

    # Record Persistence (Multi-mapping for robust UI matching)
    try:
        conn = get_db_connection()
        # 1. Record the exact identifier used for the join request (e.g. @username or numeric ID)
        conn.execute(
            "INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
            (user_id, phone_number, str(target))
        )
        if not isinstance(res, tuple):
            # 2. Record the resolved numeric ID (The most stable reference)
            res_id = str(getattr(res, 'id', ''))
            if res_id:
                conn.execute(
                    "INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
                    (user_id, phone_number, res_id)
                )
            # 3. Record the username if it exists
            username = getattr(res, 'username', None)
            if username:
                conn.execute(
                    "INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
                    (user_id, phone_number, str(username))
                )
        if hasattr(conn, "commit"): conn.commit()
        if hasattr(conn, "close"): conn.close()
    except Exception as db_e:
        log_debug(f"DB_JOIN_REC_ERR: {str(db_e)}")

    return result

from fastapi import Request  # type: ignore
from fastapi.responses import JSONResponse  # type: ignore
import traceback

@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    print(f"GLOBAL_ERROR: {str(exc)}")
    traceback.print_exc()
    origin = request.headers.get("origin")
    headers = {}
    if origin:
        headers["Access-Control-Allow-Origin"] = origin
        headers["Access-Control-Allow-Credentials"] = "true"
        headers["Access-Control-Allow-Headers"] = "*"
        headers["Access-Control-Allow-Methods"] = "*"
    return JSONResponse(
        status_code=500,
        content={"detail": f"Internal Server Error: {str(exc)}"},
        headers=headers
    )

@app.get("/health")
async def health():
    # Use UTC for absolute consistency across platforms
    now_utc = datetime.now(timezone.utc)
    return {
        "status": "healthy", 
        "server_time_utc": now_utc.isoformat(),
        "poller_format_utc": now_utc.strftime('%Y-%m-%dT%H:%M'),
        "note": "All campaigns are processed in UTC time to match browser standards."
    }

# Temporary in-memory store for login sessions
# phone -> { "client": TelegramClient, "phone_code_hash": str, "api_id": str, "api_hash": str }
auth_sessions: Dict[str, Dict[str, Any]] = {}
qr_sessions: Dict[str, Dict[str, Any]] = {}

class TelegramPool:
    """Manages a pool of persistent Telegram clients for low-latency operations."""
    def __init__(self):
        # Key: phone_number (Each account has exactly one session regardless of who uses it)
        self._clients: Dict[str, TelegramClient] = {}
        self._locks: Dict[str, asyncio.Lock] = {}

    async def get_client(self, user_id: str, phone_number: str) -> TelegramClient:
        # 1. Resolve ownership first to prevent duplicate sessions
        conn = get_db_connection()
        try:
            # Check if user is admin (virtual admin or user with admin role)
            is_admin = False
            if user_id == "admin_virtual_id":
                is_admin = True
            else:
                try:
                    u_row = conn.execute("SELECT role FROM users WHERE id = ?", (user_id,)).fetchone()
                    if u_row and u_row[0] == "admin":
                        is_admin = True
                except Exception:
                    pass

            if is_admin:
                row = conn.execute(
                    "SELECT session_string, api_id, api_hash, user_id FROM accounts WHERE phone_number = ?", 
                    (phone_number,)
                ).fetchone()
            else:
                # If a regular user tries to grab an account, check if it's theirs OR if it belongs to admin globally
                row = conn.execute(
                    "SELECT session_string, api_id, api_hash, user_id FROM accounts WHERE phone_number = ? AND (user_id = ? OR user_id = 'admin_virtual_id')", 
                    (phone_number, user_id)
                ).fetchone()
            
            if not row:
                print(f"POOL_ERROR: Account {phone_number} not found for user {user_id}")
                raise HTTPException(status_code=404, detail="Account not found.")
                
            session_str, api_id, api_hash, owner_id = row[0], int(row[1]), row[2], row[3]
        finally:
            if hasattr(conn, "close"): conn.close()

        # Session-unique key (Phone number is the unique axis)
        session_key = phone_number
        
        if session_key not in self._locks:
            self._locks[session_key] = asyncio.Lock()
            
        async with self._locks[session_key]:
            # Trust existing connected clients first to avoid over-pinging get_me()
            if session_key in self._clients:
                client = self._clients[session_key]
                if client.is_connected():
                    # Optional: only check authenticity if it's been idle for a long time
                    # For now, trust the connected socket to stay snappy and avoid FloodWait on pings
                    return client
                else: 
                    log_debug(f"POOL: Node {phone_number} socket closed. Reconnecting...")
                
            log_debug(f"POOL: Deploying persistent kernel for {phone_number} (Owner: {owner_id})")
            new_client = TelegramClient(StringSession(session_str), api_id, api_hash)
            
            # Use a slightly longer timeout for connection to prevent flakes on slow proxies/VPS
            await new_client.connect()
            
            # Multi-check for authorization (Don't disconnect instantly on flakes)
            auth_retries = 2
            authorized = False
            while auth_retries > 0:
                try:
                    authorized = await new_client.is_user_authorized()
                    break
                except Exception as auth_flake:
                    auth_retries -= 1
                    if auth_retries > 0:
                        await asyncio.sleep(1)
                    else:
                        log_debug(f"POOL: Auth check fatal for {phone_number}: {auth_flake}")

            if not authorized:
                await new_client.disconnect()
                conn = get_db_connection()
                try:
                    conn.execute("UPDATE accounts SET status = 'expired', status_detail = 'Session expired/invalid' WHERE phone_number = ?", (phone_number,))
                    if hasattr(conn, "commit"): conn.commit()
                except Exception:
                    try:
                        conn.execute("UPDATE accounts SET status = 'expired' WHERE phone_number = ?", (phone_number,))
                        if hasattr(conn, "commit"): conn.commit()
                    except: pass
                finally:
                    if hasattr(conn, "close"): conn.close()
                raise HTTPException(status_code=400, detail=f"Telegram session for {phone_number} has expired. Please re-authenticate the account in Accounts.")
                
            # Node Success: Heartbeat for username sync
            try:
                me = await new_client.get_me() if not hasattr(new_client, 'me_cache') else getattr(new_client, 'me_cache')
                new_client.me_cache = me # type: ignore # Simple custom cache
                log_debug(f"POOL: Node {phone_number} ( @{getattr(me,'username','?')} ) online.")
            except: pass
            
            # Persist status
            try:
                conn = get_db_connection()
                conn.execute("UPDATE accounts SET status = 'active' WHERE phone_number = ?", (phone_number,))
                if hasattr(conn, "commit"): conn.commit()
                if hasattr(conn, "close"): conn.close()
            except: pass
            
            self._clients[session_key] = new_client
            return new_client

    async def disconnect_all(self):

        """Cleans up all clients on server shutdown."""
        log_debug(f"POOL: Terminating {len(self._clients)} active node sessions...")
        for client in self._clients.values():
            try:
                await client.disconnect()
            except:
                pass
        self._clients.clear()

# Initialize Global Instance
POOL = TelegramPool()

async def qr_login_worker(client: TelegramClient, qr, user_id, api_id, api_hash, token):
    """Background task to wait for QR scan and save the session."""
    try:
        print(f"QR_WORKER[{token}]: Waiting for user scan...")
        user = await qr.wait()
        print(f"QR_WORKER[{token}]: Scan SUCCESS. Logged in as ID {user.id}")
        
        # Login success! Get full profile
        me = await client.get_me()
        user_id_val = getattr(me, 'id', 0)
        phone = getattr(me, 'phone', f"ID_{user_id_val}")
        # Fetch profile data and ensure it's fully resolved
        profile_data = await fetch_tg_profile_data(client, phone)
        profile: Dict[str, Any] = cast(Dict[str, Any], profile_data)
        session_string = str(client.session.save()) # type: ignore
        
        # Save to DB
        conn = get_db_connection()
        # Check if already exists
        existing = conn.execute("SELECT 1 FROM accounts WHERE phone_number = ?", (phone,)).fetchone()
        if existing:
             conn.execute(
                """UPDATE accounts SET user_id = ?, session_string = ?, api_id = ?, api_hash = ?, 
                   first_name = ?, last_name = ?, username = ?, profile_photo = COALESCE(?, profile_photo), country = ?, status = 'active' 
                   WHERE phone_number = ?""",
                (user_id, session_string, str(api_id), api_hash, 
                 profile['first_name'], profile['last_name'], profile['username'], 
                 profile['profile_photo'], profile['country'], phone)
            )
        else:
            conn.execute(
                """INSERT INTO accounts (user_id, phone_number, session_string, api_id, api_hash, 
                   first_name, last_name, username, profile_photo, country, status) 
                   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
                (user_id, phone, session_string, str(api_id), api_hash,
                 profile['first_name'], profile['last_name'], profile['username'], 
                 profile['profile_photo'], profile['country'], "active")
            )
        if hasattr(conn, "commit"): conn.commit()
        if hasattr(conn, "close"): conn.close()
        
        # Update session state
        if token in qr_sessions:
            qr_sessions[token]["status"] = "success"
            qr_sessions[token]["phone"] = phone
            print(f"QR_WORKER[{token}]: Account {phone} saved to database.")
            
    except Exception as e:
        print(f"QR_WORKER[{token}]: ERROR: {str(e)}")
        if token in qr_sessions:
            qr_sessions[token]["status"] = "error"
            qr_sessions[token]["error"] = str(e)
    finally:
        # We don't disconnect immediately because we might need the client for another second
        # but the status poll will pop it from the dict anyway.
        pass


class UserRegister(BaseModel):
    username: str
    password: str
    email: Optional[str] = None
    country: Optional[str] = "Nigeria"
    full_name: Optional[str] = None
    profile_picture: Optional[str] = None
    business_type: Optional[str] = None
    company_name: Optional[str] = None
    platform_goal: Optional[str] = None
    heard_about: Optional[str] = None

class PasskeyVerify(BaseModel):
    passkey: str

class PhoneLoginRequest(BaseModel):
    phone_number: str
    api_id: Optional[str] = None
    api_hash: Optional[str] = None

class VerifyCodeRequest(BaseModel):
    phone_number: str
    phone_code_hash: str
    code: str

class CampaignRequest(BaseModel):
    name: Optional[str] = "Broadcast Campaign"
    phone_number: str
    schedule_time: str
    message: str
    groups: List[str] = []
    exclude_groups: List[str] = []
    interval_hours: int = 0
    interval_minutes: int = 0
    media_url: Optional[str] = None
    speed_mode: Optional[str] = "balanced"

class AdminLoginRequest(BaseModel):
    password: str

class TemplateRequest(BaseModel):
    name: str = "Untitled Template"
    content: str = ""
    media_url: Optional[str] = None

class PermanentExcludeRequest(BaseModel):
    ids: list[str]

# --- Authentication Endpoints ---

@app.post("/api/auth/upload-avatar")
async def upload_avatar_public(file: UploadFile = File(...)):
    """Allows uploading avatar profile photos during onboarding."""
    os.makedirs(_UPLOADS_DIR, exist_ok=True)
    ext = os.path.splitext(file.filename or "")[1].lower()
    if not ext or ext not in [".jpg", ".jpeg", ".png", ".webp", ".gif"]:
        ext = ".jpg"
    safe_name = f"avatar_{uuid.uuid4().hex}{ext}"
    dest_path = os.path.join(_UPLOADS_DIR, safe_name)
    with open(dest_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    try:
        root_uploads = os.path.join(_ROOT_STATIC_DIR, "uploads")
        if os.path.abspath(_UPLOADS_DIR) != os.path.abspath(root_uploads):
            os.makedirs(root_uploads, exist_ok=True)
            shutil.copy2(dest_path, os.path.join(root_uploads, safe_name))
    except Exception as e:
        log_debug(f"AVATAR UPLOAD: Root mirror failed: {e}")

    return {"status": "success", "url": f"/static/uploads/{safe_name}"}

@app.post("/api/auth/register")
async def register(user: UserRegister):
    conn = get_db_connection()
    # Check if user exists
    user_check = conn.execute("SELECT id FROM users WHERE username = ?", (user.username,)).fetchone()
    if user_check:
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=400, detail="Username already taken")
    
    user_id = str(uuid.uuid4())
    pw_hash = get_password_hash(user.password)
    email_val = user.email or user.username
    country_val = user.country or "Nigeria"
    display_name = user.full_name or user.username
    
    conn.execute(
        """INSERT INTO users (
            id, username, password_hash, role, email, country,
            full_name, profile_picture, business_type, company_name, platform_goal, heard_about, plan
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
        (
            user_id, user.username, pw_hash, "user", email_val, country_val,
            display_name, user.profile_picture, user.business_type,
            user.company_name, user.platform_goal, user.heard_about, "free"
        )
    )
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    print(f"AUTH: Registered new user {user.username} with ID {user_id} (Country: {country_val})")
    
    # Dispatch Welcome Email with Welcome Coupon (non-blocking)
    welcome_coupon = "WELCOME20"
    try:
        send_welcome_email_async(
            to_email=email_val,
            username=user.username,
            full_name=display_name,
            coupon_code=welcome_coupon
        )
    except Exception as e:
        log_debug(f"Welcome email dispatch failed: {e}")

    # Generate immediate access token so the user is instantly logged in
    access_token = create_access_token(data={"sub": user_id, "role": "user"})

    return {
        "status": "success", 
        "message": "Registered successfully",
        "access_token": access_token,
        "token_type": "bearer",
        "user": {
            "id": user_id,
            "username": user.username,
            "email": email_val,
            "full_name": display_name,
            "profile_picture": user.profile_picture,
            "plan": "free"
        }
    }

@app.post("/api/auth/login")
async def login(user: UserRegister):
    conn = get_db_connection()
    db_user = conn.execute("SELECT id, password_hash, role FROM users WHERE username = ?", (user.username,)).fetchone()
    if hasattr(conn, "close"): conn.close()
    
    if not db_user or not verify_password(user.password, db_user[1]):
        raise HTTPException(status_code=401, detail="Invalid credentials")
    
    access_token = create_access_token(data={"sub": db_user[0], "role": db_user[2]})
    return {"access_token": access_token, "token_type": "bearer"}

@app.post("/api/auth/admin-login")
async def admin_login(req: AdminLoginRequest):
    conn = get_db_connection()
    # Check if a passkey exists in settings. Use 'admin_password' or fallback to 'global_passkey'
    row = conn.execute("SELECT value FROM system_settings WHERE key = 'admin_password'").fetchone()
    if not row:
        row = conn.execute("SELECT value FROM system_settings WHERE key = 'global_passkey'").fetchone()
    
    if hasattr(conn, "close"): conn.close()
    
    # Default admin password if none ever set
    admin_pw = row[0] if row else "admin123" 
    
    if req.password != admin_pw:
        raise HTTPException(status_code=401, detail="Invalid administrative password")
    
    access_token = create_access_token(data={"sub": "admin_virtual_id", "role": "admin"})
    return {"access_token": access_token, "token_type": "bearer"}

@app.post("/api/auth/verify-passkey")
async def verify_passkey(req: PasskeyVerify, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    # Fetch global passkey
    stored_passkey = conn.execute("SELECT value FROM system_settings WHERE key = 'global_passkey'").fetchone()
    if not stored_passkey:
        # Default passkey if none set (e.g., initial setup)
        # You should probably set this during init_db or admin setup
        stored_passkey = ["123456"] 
    
    if req.passkey != stored_passkey[0]:
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=401, detail="Invalid passkey")
    
    conn.execute("UPDATE users SET passkey_verified = 1 WHERE id = ?", (user_id,))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

@app.get("/api/auth/me")
async def get_me(user_id: str = Depends(get_current_user_id)):
    if user_id == "admin_virtual_id":
        return {
            "id": "admin_virtual_id",
            "username": "System Administrator",
            "role": "admin",
            "plan": "unlimited",
            "email": "admin@arkitel.app",
            "country": "Nigeria",
            "passkey_verified": True,
            "full_name": "System Administrator",
            "profile_picture": None
        }
    
    conn = get_db_connection()
    user = conn.execute(
        """SELECT username, role, passkey_verified, email, country, plan,
                  full_name, profile_picture, business_type, company_name, platform_goal, heard_about
           FROM users WHERE id = ?""", 
        (user_id,)
    ).fetchone()
    if hasattr(conn, "close"): conn.close()
    
    if not user:
        raise HTTPException(status_code=401, detail="User not found")
        
    return {
        "username": user["username"], 
        "role": user["role"], 
        "passkey_verified": bool(user["passkey_verified"]),
        "email": user["email"] or user["username"],
        "country": user["country"] or "Nigeria",
        "plan": user["plan"] or "free",
        "full_name": user["full_name"] or user["username"],
        "profile_picture": user["profile_picture"],
        "business_type": user["business_type"],
        "company_name": user["company_name"],
        "platform_goal": user["platform_goal"],
        "heard_about": user["heard_about"]
    }

@app.post("/api/telegram/send-code")
async def send_code(req: PhoneLoginRequest, user_id: str = Depends(get_current_user_id)):
    # Limit check
    conn = get_db_connection()
    user = conn.execute("SELECT max_accounts, plan FROM users WHERE id = ?", (user_id,)).fetchone()
    count_row = conn.execute("SELECT COUNT(*) FROM accounts WHERE user_id = ?", (user_id,)).fetchone()
    if hasattr(conn, "close"): conn.close()
    
    plan = (user["plan"] if user else "free") or "free"
    plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
    
    # Use user-specific override or fallback to plan default
    max_acc = user["max_accounts"] if user and user["max_accounts"] is not None else plan_cfg["max_accounts"]
    current_count = count_row[0] if count_row else 0
    
    if user_id != "admin_virtual_id" and plan != "unlimited" and current_count >= max_acc:
        raise HTTPException(status_code=403, detail=f"Account limit reached ({max_acc}). Upgrade your plan to link more accounts.")

    # Fallback to defaults from environment/config
    api_id = req.api_id or CORE_PROVIDER_ID
    api_hash = req.api_hash or CORE_PROVIDER_KEY

    if not api_id or not api_hash:
        # Telegram iOS credentials - more stable than Android ones
        api_id = "2040"
        api_hash = "b18441a1ff607e10a989891a5462e627"
        
    try:
        # Each login can use its own API ID and Hash
        client = TelegramClient(StringSession(), int(api_id), api_hash)
        await client.connect()
        send_code_res = await client.send_code_request(req.phone_number)
        
        # Store metadata for verification step
        auth_key = f"{user_id}:{req.phone_number}"
        auth_sessions[auth_key] = {
            "client": client,
            "phone_code_hash": send_code_res.phone_code_hash,
            "api_id": api_id,
            "api_hash": api_hash
        }
        return {"phone_code_hash": send_code_res.phone_code_hash}
    except Exception as e:
        auth_key = f"{user_id}:{req.phone_number}"
        if auth_key in auth_sessions:
            await auth_sessions[auth_key]["client"].disconnect()
            auth_sessions.pop(auth_key, None)
        raise HTTPException(status_code=400, detail=str(e))

@app.post("/api/telegram/verify-code")
async def verify_code(req: VerifyCodeRequest, user_id: str = Depends(get_current_user_id)):
    auth_key = f"{user_id}:{req.phone_number}"
    session_data = auth_sessions.get(auth_key)
    if not session_data:
        raise HTTPException(status_code=400, detail="Login session expired or phone number mismatch. Please request a new code.")
    
    client = session_data["client"]
    api_id = int(session_data["api_id"])
    api_hash = session_data["api_hash"]
    
    try:
        # Use the ALREADY CONNECTED client
        await client.sign_in(
            phone=req.phone_number,
            code=req.code,
            phone_code_hash=req.phone_code_hash
        )
        # Success! Fetch profile
        profile_data = await fetch_tg_profile_data(client, req.phone_number)
        profile: Dict[str, Any] = cast(Dict[str, Any], profile_data)
        session_string = str(client.session.save()) # type: ignore
        
        # Save to DB with API credentials
        conn = get_db_connection()
        print(f"DB: Saving account {req.phone_number} for user {user_id}")
        
        # Check if already exists
        existing = conn.execute("SELECT 1 FROM accounts WHERE phone_number = ?", (req.phone_number,)).fetchone()
        if existing:
            conn.execute(
                """UPDATE accounts SET user_id = ?, session_string = ?, api_id = ?, api_hash = ?, 
                   first_name = ?, last_name = ?, username = ?, profile_photo = COALESCE(?, profile_photo), country = ?, status = 'active' 
                   WHERE phone_number = ?""",
                (user_id, session_string, str(api_id), api_hash, 
                 profile['first_name'], profile['last_name'], profile['username'], 
                 profile['profile_photo'], profile['country'], req.phone_number)
            )
        else:
            conn.execute(
                """INSERT INTO accounts (user_id, phone_number, session_string, api_id, api_hash, 
                   first_name, last_name, username, profile_photo, country, status) 
                   VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
                (user_id, req.phone_number, session_string, str(api_id), api_hash,
                 profile['first_name'], profile['last_name'], profile['username'], 
                 profile['profile_photo'], profile['country'], "active")
            )
            
        if hasattr(conn, "commit"): conn.commit()
        if hasattr(conn, "close"): conn.close()
        print(f"DB: Account {req.phone_number} saved successfully.")
        
        # Success cleanup
        await client.disconnect()
        auth_sessions.pop(auth_key, None)
        
        return {"status": "success", "message": "Account connected successfully."}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        pass

@app.post("/api/telegram/accounts/{phone_number}/sync")
async def sync_account_profile(phone_number: str, user_id: str = Depends(get_current_user_id)):
    """Manually refresh profile photo, name, and username from Telegram."""
    try:
        client = await POOL.get_client(user_id, phone_number)
        profile_data = await fetch_tg_profile_data(client, phone_number)
        profile: Dict[str, Any] = cast(Dict[str, Any], profile_data)
        
        conn = get_db_connection()
        conn.execute(
            """UPDATE accounts SET 
               first_name = ?, last_name = ?, username = ?, profile_photo = COALESCE(?, profile_photo), country = ?, status = 'active' 
               WHERE phone_number = ? AND user_id = ?""",
            (profile['first_name'], profile['last_name'], profile['username'], 
             profile['profile_photo'], profile['country'], phone_number, user_id)
        )
        if hasattr(conn, "commit"): conn.commit()
        if hasattr(conn, "close"): conn.close()
        
        return {"status": "success", "profile": profile}
    except Exception as e:
        log_debug(f"SYNC_ERROR for {phone_number}: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Sync failed: {str(e)}")

from telethon.tl.functions.contacts import SearchRequest  # type: ignore
from telethon.tl.functions.channels import GetParticipantsRequest  # type: ignore
from telethon.tl.types import ChannelParticipantsSearch  # type: ignore

class FetchDialogsRequest(BaseModel):
    phone_number: str

class ScrapeKeywordRequest(BaseModel):
    phone_number: str
    keyword: str
    limit: int = 10

@app.post("/api/telegram/dialogs")
async def get_dialogs(req: FetchDialogsRequest, user_id: str = Depends(get_current_user_id)):
    """Fetch all groups and channels for a given phone number, with robust error reporting."""
    try:
        phone = req.phone_number.strip()
        if not phone:
            return {"dialogs": [], "error": "No phone number provided"}

        log_debug(f"DIALOGS: Fetching kernel for {phone} (User: {user_id})...")
        client = await POOL.get_client(user_id, phone)
        
        dialogs_list = []
        try:
            # Fetch all dialogs across main list and all folders (archived=True was restricting to folder 1 only)
            async with async_timeout(120):
                async for dialog in client.iter_dialogs(ignore_migrated=False):
                    # Robust check for any container that isn't a single user
                    if dialog.is_group or dialog.is_channel:
                        did = str(dialog.id)
                        title = getattr(dialog, 'name', '') or getattr(dialog, 'title', '') or did
                        
                        is_group = bool(dialog.is_group)
                        is_channel = bool(dialog.is_channel)
                        
                        # Fix for Telethon Megagroups (Supergroups) vs Broadcast Channels vs Basic Groups
                        is_supergroup = False
                        username = None
                        participants_count = 0
                        can_send = True
                        try:
                            if hasattr(dialog, 'entity') and dialog.entity:
                                ent = dialog.entity
                                ent_class = ent.__class__.__name__

                                # If entity is forbidden or empty, user has no access / banned
                                if 'Forbidden' in ent_class or 'Empty' in ent_class:
                                    can_send = False
                                else:
                                    username = getattr(ent, 'username', None)
                                    participants_count = getattr(ent, 'participants_count', 0) or 0
                                    
                                    # In Telegram TL, supergroups have megagroup=True
                                    if is_channel and hasattr(ent, 'megagroup'):
                                        is_supergroup = bool(ent.megagroup)  # type: ignore

                                    # Any channel that is not a supergroup IS a broadcast channel
                                    is_broadcast = bool(getattr(ent, 'broadcast', False)) or (is_channel and not is_supergroup)
                                    is_creator = bool(getattr(ent, 'creator', False))
                                    admin_rights = getattr(ent, 'admin_rights', None)
                                    default_banned = getattr(ent, 'default_banned_rights', None)
                                    banned = getattr(ent, 'banned_rights', None)
                                    left = bool(getattr(ent, 'left', False))
                                    kicked = bool(getattr(ent, 'kicked', False))
                                    deactivated = bool(getattr(ent, 'deactivated', False))
                                    restricted = bool(getattr(ent, 'restricted', False))
                                    migrated_to = getattr(ent, 'migrated_to', None)

                                    if left or kicked or deactivated or restricted or (migrated_to is not None):
                                        can_send = False
                                    elif is_creator:
                                        can_send = True
                                    elif is_broadcast:
                                        # Telegram broadcast channel: ONLY admins with post_messages can broadcast
                                        can_send = bool(admin_rights and getattr(admin_rights, 'post_messages', False))
                                    else:
                                        # Group / Supergroup
                                        if admin_rights:
                                            can_send = True
                                        else:
                                            # Check if group has banned regular members from sending messages
                                            banned_by_default = False
                                            if default_banned:
                                                if (getattr(default_banned, 'send_messages', False) or 
                                                    getattr(default_banned, 'send_plain', False)):
                                                    banned_by_default = True

                                            # Check if this user specifically has banned rights
                                            user_banned = False
                                            if banned:
                                                if (getattr(banned, 'send_messages', False) or 
                                                    getattr(banned, 'send_plain', False) or 
                                                    getattr(banned, 'view_messages', False)):
                                                    user_banned = True

                                            if banned_by_default or user_banned:
                                                can_send = False
                                            else:
                                                can_send = True
                        except Exception as perm_err:
                            log_debug(f"DIALOGS_PERM_CHECK_ERR: {str(perm_err)}")
                            can_send = False
                        
                        dialogs_list.append({
                            "id": did,
                            "name": title,
                            "title": title,
                            "username": username,
                            "participants_count": participants_count,
                            "is_group": is_group or is_supergroup,
                            "is_channel": is_channel and not is_supergroup,
                            "can_send": can_send
                        })
                
                log_debug(f"DIALOGS: Scanned {len(dialogs_list)} entities for {phone}. Success.")
        except asyncio.TimeoutError:
            log_debug(f"DIALOGS_TIMEOUT: {phone}. Returning {len(dialogs_list)} partial.")
            if not dialogs_list:
                return {"dialogs": [], "error": "Request timed out after 60s. This account might have too many groups or a slow connection. Please try again."}
        except Exception as dialog_err:
            err_str = str(dialog_err)
            log_debug(f"DIALOGS_ITER_ERROR for {phone}: {err_str}")
            if not dialogs_list:
                # If it's a FloodWait, the user should see it!
                if "flood" in err_str.lower():
                    return {"dialogs": [], "error": f"Telegram is rate-limiting this request (FloodWait). Please wait a few minutes before trying to browse groups for this account."}
                return {"dialogs": [], "error": f"Internal Error: {err_str}"}
            
        return {"dialogs": dialogs_list}
    except HTTPException as he:
        log_debug(f"DIALOGS_AUTH_ERROR for {req.phone_number}: {he.detail}")
        return {"dialogs": [], "error": he.detail}
    except Exception as e:
        log_debug(f"DIALOGS_FATAL_ERROR for {req.phone_number}: {str(e)}")
        POOL._clients.pop(req.phone_number, None)
        return {"dialogs": [], "error": str(e)}


class CompareAccountsRequest(BaseModel):
    phone_a: str
    phone_b: str

class SingleJoinRequest(BaseModel):
    phone_number: str
    target: Optional[str] = None
    group_id: Optional[str] = None
    source_phone: Optional[str] = None

def _normalize_tg_id(raw_id: Any) -> str:
    s = str(raw_id).strip()
    if s.startswith("-100"):
        return s[4:]
    if s.startswith("-"):
        return s[1:]
    return s

def _dialogs_match(a: Dict[str, Any], b: Dict[str, Any]) -> bool:
    id_a = _normalize_tg_id(a.get("id", ""))
    id_b = _normalize_tg_id(b.get("id", ""))
    if id_a and id_b and id_a == id_b:
        return True
    u_a = (a.get("username") or "").lower().strip().lstrip("@")
    u_b = (b.get("username") or "").lower().strip().lstrip("@")
    if u_a and u_b and u_a == u_b:
        return True
    return False

@app.post("/api/telegram/compare")
async def compare_accounts(req: CompareAccountsRequest, user_id: str = Depends(get_current_user_id)):
    """Compare groups and channels between two logged-in accounts."""
    # Verify user plan - Fleet Compare requires an upgraded plan (Admins have unlimited access)
    is_admin = (user_id == "admin_virtual_id")
    u_plan = "unlimited" if is_admin else "free"
    if not is_admin:
        conn_chk = get_db_connection()
        try:
            u_chk = conn_chk.execute("SELECT plan, role FROM users WHERE id = ?", (user_id,)).fetchone()
            if u_chk:
                u_plan = u_chk[0] or "free"
                u_role = u_chk[1] if len(u_chk) > 1 else "user"
                if u_role == "admin" or u_plan == "unlimited":
                    is_admin = True
            else:
                u_plan = "free"
        finally:
            if hasattr(conn_chk, "close"): conn_chk.close()

    if not is_admin and u_plan == "free":
        raise HTTPException(
            status_code=403, 
            detail="Fleet Compare is only available on upgraded plans (Growth Basic, Pro Standard, or Enterprise Premium). Please upgrade to unlock."
        )

    phone_a = req.phone_a.strip()
    phone_b = req.phone_b.strip()
    if not phone_a or not phone_b:
        raise HTTPException(status_code=400, detail="Please select both Account A and Account B to compare.")

    if phone_a == phone_b:
        raise HTTPException(status_code=400, detail="Please select two different accounts to compare.")

    try:
        client_a = await POOL.get_client(user_id, phone_a)
    except HTTPException as e:
        raise HTTPException(status_code=400, detail=f"Account A ({phone_a}): {e.detail}")
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Account A ({phone_a}) connection failed: {str(e)}")

    try:
        client_b = await POOL.get_client(user_id, phone_b)
    except HTTPException as e:
        raise HTTPException(status_code=400, detail=f"Account B ({phone_b}): {e.detail}")
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Account B ({phone_b}) connection failed: {str(e)}")

    async def _fetch_clean_dialogs(client, phone):
        items = []
        async with async_timeout(120):
            async for dialog in client.iter_dialogs(ignore_migrated=False):
                if dialog.is_group or dialog.is_channel:
                    did = str(dialog.id)
                    title = getattr(dialog, 'name', '') or getattr(dialog, 'title', '') or did
                    is_group = bool(dialog.is_group)
                    is_channel = bool(dialog.is_channel)
                    is_supergroup = False
                    username = None
                    participants_count = 0
                    can_send = True
                    try:
                        if hasattr(dialog, 'entity') and dialog.entity:
                            username = getattr(dialog.entity, 'username', None)
                            participants_count = getattr(dialog.entity, 'participants_count', 0) or 0
                            is_broadcast = bool(getattr(dialog.entity, 'broadcast', False))
                            if is_channel and hasattr(dialog.entity, 'megagroup'):
                                is_supergroup = bool(dialog.entity.megagroup)

                            default_banned = getattr(dialog.entity, 'default_banned_rights', None)
                            banned_rights = getattr(dialog.entity, 'banned_rights', None)
                            if is_broadcast and not is_supergroup:
                                can_send = False
                            elif default_banned and getattr(default_banned, 'send_messages', False):
                                can_send = False
                            elif banned_rights and getattr(banned_rights, 'send_messages', False):
                                can_send = False
                    except: pass
                    items.append({
                        "id": did,
                        "title": title,
                        "name": title,
                        "username": username,
                        "participants_count": participants_count,
                        "is_group": is_group or is_supergroup,
                        "is_channel": is_channel and not is_supergroup,
                        "can_send_messages": can_send
                    })
        return items

    try:
        dialogs_a, dialogs_b = await asyncio.gather(
            _fetch_clean_dialogs(client_a, phone_a),
            _fetch_clean_dialogs(client_b, phone_b)
        )
    except Exception as e:
        log_debug(f"COMPARE_FETCH_ERR: {e}")
        raise HTTPException(status_code=500, detail=f"Failed to fetch account dialogs: {str(e)}")

    only_in_a = []
    in_both = []
    for item in dialogs_a:
        match_in_b = next((b for b in dialogs_b if _dialogs_match(item, b)), None)
        if match_in_b:
            in_both.append({
                **item,
                "present_in": [phone_a, phone_b],
                "missing_in": [],
                "can_join_with": None,
                "join_target": f"@{item['username']}" if item.get("username") else item["id"]
            })
        else:
            only_in_a.append({
                **item,
                "present_in": [phone_a],
                "missing_in": [phone_b],
                "can_join_with": phone_b,
                "join_target": f"@{item['username']}" if item.get("username") else item["id"]
            })

    only_in_b = []
    for item in dialogs_b:
        match_in_a = next((a for a in dialogs_a if _dialogs_match(item, a)), None)
        if not match_in_a:
            only_in_b.append({
                **item,
                "present_in": [phone_b],
                "missing_in": [phone_a],
                "can_join_with": phone_a,
                "join_target": f"@{item['username']}" if item.get("username") else item["id"]
            })

    return {
        "status": "success",
        "account_a": {
            "phone_number": phone_a,
            "count": len(dialogs_a)
        },
        "account_b": {
            "phone_number": phone_b,
            "count": len(dialogs_b)
        },
        "stats": {
            "total_a": len(dialogs_a),
            "total_b": len(dialogs_b),
            "only_a_count": len(only_in_a),
            "only_b_count": len(only_in_b),
            "both_count": len(in_both)
        },
        "only_in_a": only_in_a,
        "only_in_b": only_in_b,
        "in_both": in_both
    }

@app.post("/api/telegram/join")
async def join_single_target(req: SingleJoinRequest, user_id: str = Depends(get_current_user_id)):
    """Join a single group or channel with a specific account, with assistance for private groups."""
    target_str = (req.target or req.group_id or "").strip()
    if not target_str:
        raise HTTPException(status_code=400, detail="Missing target or group_id to join.")

    phone_number = req.phone_number.strip()
    client = await POOL.get_client(user_id, phone_number)

    # 1. Try standard direct join
    direct_err = None
    try:
        await smart_join(client, target_str, user_id, phone_number)
        return {"status": "success", "message": f"Successfully joined {target_str}"}
    except Exception as e:
        direct_err = e
        err_msg = str(e)
        if "flood" in err_msg.lower():
            raise HTTPException(status_code=429, detail="Telegram rate limit (FloodWait). Please wait a few moments before joining again.")
        log_debug(f"DIRECT_JOIN_FAILED for {target_str}: {err_msg}")

    # 2. If direct join failed (e.g. Could not resolve entity for -100...) and source_phone is available:
    if req.source_phone and req.source_phone.strip() and req.source_phone.strip() != phone_number:
        source_phone = req.source_phone.strip()
        try:
            client_source = await POOL.get_client(user_id, source_phone)
            res_obj = await resolve_tg_entity(client_source, target_str)
            if isinstance(res_obj, tuple) and res_obj[0] == "invite":
                try:
                    await smart_join(client, target_str, user_id, phone_number)
                    return {"status": "success", "message": "Successfully joined via invite"}
                except Exception as inv_join_err:
                    log_debug(f"INVITE_JOIN_ERR: {inv_join_err}")

            source_entity: Any = res_obj
            if source_entity and not isinstance(source_entity, tuple):
                # If the entity actually has a public username, join by username
                uname = getattr(source_entity, 'username', None)
                if uname:
                    try:
                        await smart_join(client, f"@{uname}", user_id, phone_number)
                        return {"status": "success", "message": f"Successfully joined @{uname}"}
                    except Exception as u_err:
                        log_debug(f"USERNAME_JOIN_FALLBACK_ERR: {u_err}")

                # Try to extract or export invite link through the source account
                invite_link = None
                try:
                    is_chan = getattr(source_entity, 'broadcast', False) or getattr(source_entity, 'megagroup', False)
                    if is_chan:
                        from telethon.tl.functions.channels import GetFullChannelRequest
                        from telethon.tl.functions.messages import ExportChatInviteRequest
                        source_input = await client_source.get_input_entity(source_entity)
                        full = await client_source(GetFullChannelRequest(source_input))  # type: ignore
                        if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
                            invite_link = getattr(full.full_chat.exported_invite, 'link', None)
                        if not invite_link:
                            try:
                                exp = await client_source(ExportChatInviteRequest(source_input))  # type: ignore
                                invite_link = getattr(exp, 'link', None)
                            except Exception: pass
                    else:
                        from telethon.tl.functions.messages import GetFullChatRequest, ExportChatInviteRequest
                        source_id = int(getattr(source_entity, 'id', 0))
                        full = await client_source(GetFullChatRequest(source_id))  # type: ignore
                        if hasattr(full.full_chat, 'exported_invite') and full.full_chat.exported_invite:
                            invite_link = getattr(full.full_chat.exported_invite, 'link', None)
                        if not invite_link:
                            try:
                                exp = await client_source(ExportChatInviteRequest(source_id))  # type: ignore
                                invite_link = getattr(exp, 'link', None)
                            except Exception: pass
                except Exception as exp_err:
                    log_debug(f"SOURCE_LINK_EXTRACTION_ERR: {exp_err}")

                if invite_link:
                    await smart_join(client, invite_link, user_id, phone_number)
                    return {"status": "success", "message": "Successfully joined via chat invite link"}

                # Try direct invitation via source account
                try:
                    user_b: Any = None
                    try:
                        user_b = await client_source.get_input_entity(phone_number)
                    except Exception:
                        try:
                            ub_ent: Any = await client_source.get_entity(phone_number)
                            if ub_ent:
                                target_peer = ub_ent[0] if isinstance(ub_ent, list) else ub_ent
                                user_b = await client_source.get_input_entity(target_peer)  # type: ignore
                        except Exception: pass

                    if user_b:
                        is_chan = getattr(source_entity, 'broadcast', False) or getattr(source_entity, 'megagroup', False)
                        if is_chan:
                            from telethon.tl.functions.channels import InviteToChannelRequest
                            source_input = await client_source.get_input_entity(source_entity)  # type: ignore
                            await client_source(InviteToChannelRequest(channel=source_input, users=[user_b]))  # type: ignore
                        else:
                            from telethon.tl.functions.messages import AddChatUserRequest
                            source_id = int(getattr(source_entity, 'id', 0))
                            await client_source(AddChatUserRequest(chat_id=source_id, user_id=user_b, fwd_limit=100))  # type: ignore

                        # Record persistence in database
                        try:
                            conn = get_db_connection()
                            conn.execute(
                                "INSERT OR IGNORE INTO account_joins (user_id, phone_number, group_id) VALUES (?, ?, ?)",
                                (user_id, phone_number, target_str)
                            )
                            if hasattr(conn, "commit"): conn.commit()
                            if hasattr(conn, "close"): conn.close()
                        except: pass
                        return {"status": "success", "message": f"Account {source_phone} added {phone_number} to {getattr(source_entity, 'title', target_str)}"}
                except Exception as inv_err:
                    log_debug(f"SOURCE_INVITATION_ERR: {inv_err}")
        except Exception as src_err:
            log_debug(f"SOURCE_ASSISTANCE_FAILED: {src_err}")

    # Fallback error messaging
    err_str = str(direct_err)
    if "Could not resolve entity" in err_str:
        raise HTTPException(
            status_code=400,
            detail="This group/channel is private without a public link. Account has no admin permissions to export an invite link or add members."
        )
    raise HTTPException(status_code=400, detail=f"Join failed: {err_str}")



@app.post("/api/telegram/qr/init")
async def qr_init(user_id: str = Depends(get_current_user_id)):
    try:
        api_id = int(os.getenv("CORE_PROVIDER_ID", "2040"))
        api_hash = os.getenv("CORE_PROVIDER_KEY", "b18441a1ff607e10a989891a5462e627")
        
        client = TelegramClient(StringSession(''), api_id, api_hash)
        await client.connect()
        
        qr = await client.qr_login()
        token = str(uuid.uuid4())
        qr_sessions[token] = {
            "client": client,
            "qr": qr,
            "user_id": user_id,
            "api_id": api_id,
            "api_hash": api_hash,
            "created_at": datetime.now(),
            "status": "pending"
        }
        
        # Trigger background waiter
        asyncio.create_task(qr_login_worker(client, qr, user_id, api_id, api_hash, token))
        
        return {"token": token, "url": qr.url}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"QR init failed: {str(e)}")

@app.get("/api/telegram/qr/status/{token}")
async def qr_status(token: str):
    sess = qr_sessions.get(token)
    if not sess:
        return {"status": "expired"}
        
    status = sess.get("status", "pending")
    
    if status == "success":
        phone = sess.get("phone")
        # Pop only after success is consumed
        qr_sessions.pop(token, None)
        return {"status": "success", "phone": phone}
    
    if status == "error":
        err = sess.get("error", "Unknown error")
        qr_sessions.pop(token, None)
        return {"status": "error", "detail": err}
        
    # Check for timeout (5 minutes)
    if datetime.now() - sess["created_at"] > timedelta(minutes=5):
        qr_sessions.pop(token, None)
        return {"status": "expired"}
        
    return {"status": "pending"}

@app.get("/api/telegram/accounts")
async def get_accounts(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    is_admin = False
    if user_id == "admin_virtual_id":
        is_admin = True
    else:
        try:
            u_row = conn.execute("SELECT role FROM users WHERE id = ?", (user_id,)).fetchone()
            if u_row and u_row[0] == "admin":
                is_admin = True
        except Exception:
            pass

    if is_admin:
        # Admin sees all accounts across the entire fleet
        rows = conn.execute("""
            SELECT a.phone_number, a.status, a.api_id, a.api_hash, 
                   a.first_name, a.last_name, a.username, a.profile_photo, a.country, a.created_at, a.user_id, a.is_active,
                   COALESCE(u.username, 'System') as owner_name
            FROM accounts a
            LEFT JOIN users u ON a.user_id = u.id
            ORDER BY a.created_at DESC
        """).fetchall()
    else:
        # Regular user only sees their own accounts
        rows = conn.execute("""
            SELECT a.phone_number, a.status, a.api_id, a.api_hash, 
                   a.first_name, a.last_name, a.username, a.profile_photo, a.country, a.created_at, a.user_id, a.is_active,
                   'Self' as owner_name
            FROM accounts a WHERE a.user_id = ?
            ORDER BY a.is_active DESC, a.created_at DESC
        """, (user_id,)).fetchall()
    if hasattr(conn, "close"): conn.close()
    
    accounts = []
    for row in rows:
        accounts.append({
            "phone_number": row[0],
            "status": row[1],
            "api_id": row[2],
            "api_hash": row[3],
            "first_name": row[4],
            "last_name": row[5],
            "username": row[6],
            "profile_photo": row[7],
            "country": row[8],
            "created_at": row[9],
            "user_id": row[10],
            "is_active": row[11],
            "owner_name": row[12] if len(row) > 12 else "Self"
        })
    return {"accounts": accounts}

@app.post("/api/telegram/accounts/{phone_number}/active")
async def set_active_account(phone_number: str, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        # 1. Deactivate all other accounts for this user
        conn.execute("UPDATE accounts SET is_active = 0 WHERE user_id = ?", (user_id,))
        # 2. Activate the selected account
        conn.execute("UPDATE accounts SET is_active = 1 WHERE phone_number = ? AND user_id = ?", (phone_number, user_id))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "msg": f"Account {phone_number} is now active."}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()
@app.delete("/api/telegram/accounts/{phone_number}")
async def delete_account(phone_number: str, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    if user_id == "admin_virtual_id":
        conn.execute("DELETE FROM accounts WHERE phone_number = ?", (phone_number,))
    else:
        conn.execute("DELETE FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    print(f"AUTH: Account {phone_number} deleted by user {user_id}")
    return {"status": "success"}

@app.post("/api/telegram/accounts/{phone_number}/validate")
async def validate_account_session(phone_number: str, user_id: str = Depends(get_current_user_id)):
    try:
        client = await POOL.get_client(user_id, phone_number)
        is_auth = await client.is_user_authorized()
        return {"status": "success", "authorized": is_auth}
    except Exception as e:
        return {"status": "error", "message": f"Session verification failed: {str(e)}"}

@app.get("/api/telegram/accounts/{phone_number}/session")
async def dump_session_string(phone_number: str, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    if user_id == "admin_virtual_id":
        row = conn.execute("SELECT session_string FROM accounts WHERE phone_number = ?", (phone_number,)).fetchone()
    else:
        row = conn.execute("SELECT session_string FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id)).fetchone()
    if hasattr(conn, "close"): conn.close()
    
    if not row:
        raise HTTPException(status_code=404, detail="Account not found")
    
    return {"phone_number": phone_number, "session_string": row[0]}


# ─────────────────────────────────────────────────────────────────────────────
# HYBRID SCRAPER HELPERS  (Layer 2 & 3: web directories)
# ─────────────────────────────────────────────────────────────────────────────

SCRAPER_HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                  "(KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
    "Accept-Language": "en-US,en;q=0.9",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
}

def save_scraped_group(user_id: str, item: Dict[str, Any]):
    """Helper to save a detected group/channel to the database immediately."""
    try:
        conn = get_db_connection()
        gid = item.get('id')
        if not gid: return
        
        # Save to history
        conn.execute("INSERT OR IGNORE INTO scrape_history (group_id, user_id) VALUES (?, ?)", (str(gid), user_id))
        
        # Save to persistent leads table
        can_send_val = 1 if (item.get('can_send_messages') is not False and item.get('type') != 'channel') else 0
        conn.execute(
            """INSERT OR REPLACE INTO scraped_groups 
               (id, user_id, title, username, participants_count, type, is_private, country, user_shows, global_shows, source, can_send_messages) 
               VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
            (str(gid), user_id, item.get('title', 'Unknown'), item.get('username'), item.get('participants_count', 0), 
             item.get('type', 'group'), 1 if item.get('is_private') else 0, item.get('country', 'Global'), 
             item.get('user_shows', 1), item.get('global_shows', 1), item.get('source', 'scraper'), can_send_val)
        )
        if hasattr(conn, "commit"): conn.commit()
        if hasattr(conn, "close"): conn.close()
    except Exception as e:
        print(f"Error auto-saving lead: {e}")

async def _scrape_lyzem(base_query: str, max_pages: int = 10, queue: Optional[asyncio.Queue] = None, user_id: str = "") -> List[Dict[str, Any]]:
    """Scrape lyzem.com custom search engine for Telegram. Verified working."""
    if not BS4_AVAILABLE:
        return []
    results: List[Dict[str, Any]] = []
    seen: set = set()
    try:
        async with httpx.AsyncClient(headers=SCRAPER_HEADERS, timeout=15, follow_redirects=True) as client:
            for page in range(1, max_pages + 1):
                url = f"https://lyzem.com/search?q={base_query}&p={page}"
                try:
                    if queue: await queue.put({"type": "progress", "msg": f"Lyzem: Scanning Page {page}/{max_pages}..."}) # type: ignore
                    resp = await client.get(url)
                    if resp.status_code != 200:
                        break
                    
                    soup = BeautifulSoup(resp.text, "lxml")
                    # Lyzem modernized selectors: find t.me links in results
                    links = soup.select("a[href*='t.me/'], div.result a, .search-result a")
                    
                    found_on_page = 0
                    for link in links: # type: ignore
                        href = link.get("href", "") # type: ignore
                        # Handle both direct t.me and redirect urls
                        m = re.search(r'(?:t\.me|telegram\.me)/([^/?#\s]+)', str(href))
                        if not m:
                            continue
                        
                        username = m.group(1).lower()
                        if username in seen or username.lower() in ('joinchat', 'share', 'addstickers', 'iv', 's'):
                            continue
                            
                        seen.add(username)
                        title = link.get_text(strip=True) or username
                        
                        # Find parent to extract member count hint
                        parent = link.find_parent(["div", "section"], class_=re.compile(r'result|card|item|content'))
                        members = 0
                        if parent:
                            txt = parent.get_text()
                            num_match = re.search(r'([\d,]+)\s*(?:subscribers|members|sub)', txt, re.IGNORECASE)
                            if num_match:
                                try: members = int(re.sub(r'\D', '', num_match.group(1)))
                                except: pass
                                
                        
                        # Check if it actually exists if we found 0 members or if it's from Lyzem (highly prone to dead links)
                        v_info = await verify_telegram_info(username)
                        if not v_info["exists"]:
                            continue
                        
                        # Use verified count if available
                        if v_info["participants_count"] > 0:
                            members = v_info["participants_count"]
                            title = v_info["title"] or title

                        l_type = v_info.get("type", "channel")
                        item = {
                            "id": f"@{username}",
                            "title": title,
                            "username": username,
                            "participants_count": members,
                            "type": l_type,
                            "is_private": False,
                            "country": "Global",
                            "global_shows": 1,
                            "user_shows": 1,
                            "source": "lyzem",
                            "can_send_messages": bool(v_info.get("can_send_messages", l_type != "channel"))
                        }
                        results.append(item)
                        # Auto-saving disabled - user must manually select and save to leads
                        # if user_id: asyncio.create_task(asyncio.to_thread(lambda: save_scraped_group(user_id, item))) # type: ignore
                        if queue:
                            await queue.put({"type": "result", "layer": 2, "data": item})  # type: ignore
                            
                        found_on_page += 1
                        
                    print(f"SCRAPER[lyzem] page {page}: +{found_on_page} results")
                    if found_on_page == 0:
                        break
                    await asyncio.sleep(1.0) # Polite delay
                except Exception as page_err:
                    print(f"SCRAPER[lyzem] page {page} error: {page_err}")
                    break
    except Exception as e:
        print(f"SCRAPER[lyzem] fatal error: {e}")
    return results


def is_chat_matching_keyword(title: Optional[str], username: Optional[str], query: str) -> bool:
    """Ensures a chat returned from Telegram contacts/messages actually relates to the keyword query."""
    if not query:
        return True
    t = (title or "").lower()
    u = (username or "").lower()
    q = query.lower().strip()
    
    # Direct substring match
    if q in t or q in u:
        return True
        
    tokens = [w for w in re.findall(r'[a-zA-Z0-9]+', q) if len(w) >= 2]
    if not tokens:
        return True
        
    if len(tokens) == 1:
        tok = tokens[0]
        # Match token or stem
        stem = tok[:4] if len(tok) >= 5 else tok
        return stem in t or stem in u
        
    # Multi-token (e.g. "website development"): at least one token (>= 3 chars) or stem must match
    for tok in tokens:
        if len(tok) >= 3:
            stem = tok[:4] if len(tok) >= 5 else tok
            if stem in t or stem in u:
                return True
                
    return False


async def _scrape_telegram_search(base_query: str, rows: list, queue: Optional[asyncio.Queue] = None, user_id: str = "", mode: str = "fixed") -> Dict[str, Dict[str, Any]]:
    """Layer 1: Use Telegram API contacts.search with keyword variations (Fixed Mode) or pure natural search (Free Mode)."""
    unique_groups: Dict[str, Dict[str, Any]] = {}

    async def _enrich_and_send(it: dict, q: Optional[asyncio.Queue]):
        if it.get("username") and it.get("participants_count", 0) == 0:
            try:
                v = await verify_telegram_info(it["username"])
                if v and v.get("participants_count"):
                    it["participants_count"] = v["participants_count"]
                    if v.get("title") and it.get("title") in ("Unknown", "", None):
                        it["title"] = v["title"]
            except Exception:
                pass
        if q:
            await q.put({"type": "result", "layer": 1, "data": it})
    
    # In Free Mode: scrape naturally with the pure base_query (no IPTV/preset variations)
    if mode == "free":
        search_queries = [base_query]
        print(f"SCRAPER[telegram] Free Mode active: Pure natural search with '{base_query}' across {len(rows)} accounts")
    else:
        # Fixed Mode: Build 60+ universal generic and community variations (category-neutral)
        search_queries = [base_query]
        suffixes = [
            # Community & Networking (14)
            "official", "group", "channel", "community", "chat", "hub",
            "network", "team", "zone", "world", "global", "club", "lounge", "forum",
            # Authority & Quality (12)
            "main", "real", "original", "vip", "pro", "hq", "direct", "portal",
            "top", "best", "new", "prime",
            # Information & Updates (12)
            "updates", "news", "info", "alerts", "broadcast", "notices", "daily",
            "feed", "insights", "trends", "guide", "academy",
            # Business & Market (12)
            "market", "store", "shop", "deals", "leads", "clients", "services",
            "support", "help", "prices", "exchange", "b2b",
            # Scope & Connectivity (12)
            "online", "free", "live", "premium", "international", "access",
            "connect", "links", "directory", "showcase", "solutions", "discussion"
        ]
        for suffix in suffixes:
            search_queries.append(f"{base_query} {suffix}")

        # Domain-Adaptive Variations: Restores 800-900+ high yields for specific niches without cross-contamination
        q_lower = base_query.lower()
        domain_suffixes = []
        if any(w in q_lower for w in ["iptv", "stream", "tv", "movie", "cinema", "media", "video", "broadcast", "sport"]):
            domain_suffixes = [
                "streams", "vvod", "players", "subscriptions", "servers", 
                "resellers", "panels", "smart tv", "firestick", "movies", 
                "series", "sports", "m3u", "4k", "vod", "channels", "live tv"
            ]
        elif any(w in q_lower for w in ["web", "site", "dev", "code", "software", "tech", "bot", "api", "app", "script"]):
            domain_suffixes = [
                "software", "development", "coders", "bots", "api", 
                "tech", "designs", "freelancers", "gigs", "jobs", 
                "tools", "scripts", "source", "github", "templates"
            ]
        elif any(w in q_lower for w in ["crypto", "bitcoin", "trading", "forex", "money", "invest", "coin", "token"]):
            domain_suffixes = [
                "signals", "trading", "crypto", "bitcoin", "forex", 
                "investments", "airdrops", "nft", "web3", "calls", "pumps"
            ]
        elif any(w in q_lower for w in ["market", "leads", "business", "sales", "traffic", "seo", "ad", "shop"]):
            domain_suffixes = [
                "marketing", "seo", "b2b", "leads gen", "traffic", 
                "affiliate", "ecommerce", "dropshipping", "sales", "ads", "promotions"
            ]
        else:
            domain_suffixes = ["hub chat", "direct", "access", "portal hub", "official group", "community chat", "leads group"]

        for ds in domain_suffixes:
            cand = f"{base_query} {ds}"
            if cand not in search_queries:
                search_queries.append(cand)
        
        # Numeric variants for short single-word keywords
        if " " not in base_query and len(base_query) < 15:
            for n in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]:
                search_queries.append(f"{base_query}{n}")

        print(f"SCRAPER[telegram] Fixed Mode active: {len(search_queries)} variations x {len(rows)} accounts")

    for row in rows:
        acc_phone = row[3]
        try:
            client = await POOL.get_client(user_id, acc_phone)
            for q in search_queries:
                try:
                    if queue: await queue.put({"type": "progress", "msg": f"TG Search ({acc_phone[-4:]}): Testing '{q}'..."}) # type: ignore
                    search_result = await client(SearchRequest(q=q, limit=500))

                    for chat in search_result.chats:
                        if not chat:
                            continue
                        chat_id_str = str(getattr(chat, 'id', '0'))
                        
                        # Membership check
                        is_member = not getattr(chat, 'left', True)
                        
                        if chat_id_str in unique_groups:
                            if is_member: unique_groups[chat_id_str]["is_member"] = True  # type: ignore
                            continue
                            
                        is_private = getattr(chat, 'username', None) is None
                        title = getattr(chat, 'title', 'Unknown')
                        username = getattr(chat, 'username', None)

                        # Filter: Never leak unrelated personal/account chats that happen to be in the user's account
                        if is_member and not is_chat_matching_keyword(title, username, base_query):
                            continue
                        # In Free Mode: strictly ensure relevance to user query
                        if mode == "free" and not is_chat_matching_keyword(title, username, base_query):
                            continue
                        
                        # PREFER username as the ID if it exists - globally resolvable!
                        final_id = f"@{username}" if username else chat_id_str
                        
                        participants_count = (
                            getattr(chat, 'participants_count', 0)
                            or getattr(chat, 'megagroup_participants', 0)
                            or 0
                        )
                        chat_type = 'channel' if getattr(chat, 'broadcast', False) else 'group'
                        can_send = False if chat_type == 'channel' else True
                        if chat_type == 'group':
                            b_rights = getattr(chat, 'default_banned_rights', None)
                            if b_rights and getattr(b_rights, 'send_messages', False):
                                can_send = False
                        item = {
                            "id": final_id,
                            "title": title,
                            "username": username,
                            "participants_count": participants_count,
                            "type": chat_type,
                            "is_private": is_private,
                            "country": "Global",
                            "global_shows": 1,
                            "user_shows": 1,
                            "source": "telegram",
                            "is_member": is_member,
                            "can_send_messages": can_send
                        }
                        unique_groups[chat_id_str] = item

                        if queue:
                            asyncio.create_task(_enrich_and_send(item, queue))
                    
                    # Phrase-based Global Message Search (Discovery Hack) - THE SMOKING GUN
                    # RUN ON ALL VARIATIONS: restores 800+ volume
                    if True: 
                        from telethon.tl.functions.messages import SearchGlobalRequest # type: ignore
                        from telethon.tl.types import InputMessagesFilterEmpty, InputPeerEmpty # type: ignore
                        try:
                            # Bump limit to 100 for maximum discovery volume
                            msg_results = await client(SearchGlobalRequest(
                                q=q,
                                filter=InputMessagesFilterEmpty(),
                                min_date=None,
                                max_date=None,
                                offset_rate=0,
                                offset_peer=InputPeerEmpty(),
                                offset_id=0,
                                limit=100 
                            ))
                            # ZERO-COST REFINEMENT: Cross-reference chats ALREADY in the response.
                            # No extra API calls = 50x speed and no Bans.
                            res_chats = getattr(msg_results, 'chats', [])
                            if not res_chats: continue
                            
                            res_messages = getattr(msg_results, 'messages', [])
                            for msg in res_messages:
                                try:
                                    p_id = getattr(msg, 'peer_id', None)
                                    peer_id = getattr(p_id, 'channel_id', None) or getattr(p_id, 'chat_id', None)
                                    if not peer_id: continue
                                    
                                    # Safe loop-based lookup avoids complex dict inference lints
                                    chat = next((c for c in res_chats if getattr(c, 'id', None) == peer_id), None)
                                    if not chat: continue
                                    
                                    cid = str(chat.id)
                                    if cid not in unique_groups:
                                        msg_username = getattr(chat, 'username', None)
                                        is_member = not getattr(chat, 'left', True)
                                        c_title = getattr(chat, 'title', 'Unknown')
                                        
                                        # Filter: Never leak unrelated personal/account chats
                                        if is_member and not is_chat_matching_keyword(c_title, msg_username, base_query):
                                            continue
                                        # In Free Mode: strictly ensure relevance to user query
                                        if mode == "free" and not is_chat_matching_keyword(c_title, msg_username, base_query):
                                            continue

                                        # Strict Filter: Only public groups/channels found globally
                                        if getattr(chat, 'broadcast', False) or getattr(chat, 'megagroup', False) or msg_username:
                                            m_type = 'channel' if getattr(chat, 'broadcast', False) else 'group'
                                            m_can_send = False if m_type == 'channel' else True
                                            if m_type == 'group':
                                                b_rights = getattr(chat, 'default_banned_rights', None)
                                                if b_rights and getattr(b_rights, 'send_messages', False):
                                                    m_can_send = False
                                            item = {
                                                "id": f"@{msg_username}" if msg_username else cid,
                                                "title": c_title,
                                                "username": msg_username,
                                                "participants_count": getattr(chat, 'participants_count', 0) or getattr(chat, 'megagroup_participants', 0) or 0,
                                                "type": m_type,
                                                "is_private": msg_username is None,
                                                "country": "Global",
                                                "global_shows": 1,
                                                "user_shows": 1,
                                                "source": "telegram_msg",
                                                "is_member": is_member,
                                                "can_send_messages": m_can_send
                                            }
                                            unique_groups[cid] = item
                                            if queue:
                                                asyncio.create_task(_enrich_and_send(item, queue))
                                except: continue
                        except: pass

                    await asyncio.sleep(0.2) # Reduced delay for high-performance feel
                except Exception as q_err:
                    err_str = str(q_err).lower()
                    if "flood" in err_str or "wait" in err_str:
                        # Extract the required wait seconds from Telethon error (e.g. "A wait of 34 seconds is required")
                        import re
                        match = re.search(r'wait of (\d+)', err_str)
                        wait_sec = int(match.group(1)) if match else 5
                        
                        # Cap extreme waits so stream doesn't infinitely hang, but wait out the standard 5-60s bans
                        if wait_sec > 60:
                            print(f"SCRAPER[telegram] acc={acc_phone} hit HUGE {wait_sec}s FloodWait. Skipping to next account.")
                            break
                        
                        print(f"SCRAPER[telegram] acc={acc_phone} hit {wait_sec}s FloodWait on '{q}', pausing then continuing...")
                        await asyncio.sleep(wait_sec + 1)
                        continue # DO NOT BREAK! Continue the variations to hit max yield!
                    else:
                        print(f"SCRAPER[telegram] query='{q}' acc={acc_phone} err: {q_err}")
                        continue
        except Exception as e:
            print(f"SCRAPER[telegram] account {acc_phone} connection error: {e}")
        # REMOVED disconnect() to keep POOL connections alive for campaigns
    
    return unique_groups



def is_relevant_channel(title: Optional[str], username: Optional[str], query: str) -> bool:
    """Checks whether a discovered channel/group matches the query context and rejects spam/unrelated niches (e.g. IPTV ads in non-IPTV queries)."""
    if not query:
        return True
    
    t_lower = (title or "").lower()
    u_lower = (username or "").lower()
    q_lower = query.lower().strip()
    
    # 1. Unrelated niche / spam blacklist: reject unless explicitly in the user's query
    unrelated_spam = [
        "iptv", "firestick", "m3u", "vvod", "reseller panel", "porn", "xxx", "casino", "gambling", "adult 18+", "betting"
    ]
    for term in unrelated_spam:
        # If term appears in candidate, but is NOT part of user's search query, reject it!
        if (term in t_lower or term in u_lower) and term not in q_lower:
            return False
            
    return True


async def _scrape_spider(seed_groups: List[Dict[str, Any]], rows: list, max_seeds: int = 15, queue: Optional[asyncio.Queue] = None, user_id: str = "", base_query: str = "") -> List[Dict[str, Any]]:
    """Layer 3: Spider Crawler. Reads recent messages of deep channels to find forwarded links and mentions with relevance verification."""
    results = []
    seen = set()
    
    # Extract public channels from the seed groups
    public_seeds = [g for g in seed_groups if g.get('username') and not g.get('is_private')]
    
    # Sort by participants count to spider the largest groups (more likely to have ads/forwards)
    public_seeds.sort(key=lambda x: int(x.get('participants_count', 0)), reverse=True)
    targets = list(public_seeds)[:max_seeds]  # type: ignore
    
    if not targets or not rows:
        return []
        
    print(f"SCRAPER[spider] Starting deep crawl on {len(targets)} prominent channels...")
    
    # Connect with the first available active account for spidering
    session_str, api_id, api_hash, acc_phone = rows[0][0], int(rows[0][1]), rows[0][2], rows[0][3]
    client = TelegramClient(StringSession(session_str), api_id, api_hash)
    
    try:
        await client.connect()
        if not await client.is_user_authorized():
            return []
            
        for seed in targets:
            try:
                username = seed['username']
                entity = await client.get_entity(username)
                
                # Get last 50 messages
                messages = await client.get_messages(entity, limit=50)  # type: ignore
                
                found_in_seed: int = 0
                for msg in messages:  # type: ignore
                    # Look for forwards
                    if hasattr(msg, 'forward') and msg.forward and getattr(msg.forward, 'chat', None):  # type: ignore
                        f_chat = msg.forward.chat  # type: ignore
                        f_username = getattr(f_chat, 'username', None)
                        if f_username:
                            l_lower = f_username.lower()
                            cand_title = getattr(f_chat, 'title', f_username)
                            if l_lower not in seen:
                                seen.add(l_lower)
                                # Strict relevance filter against forwarded spam/unrelated niches
                                if base_query and not is_relevant_channel(cand_title, f_username, base_query):
                                    continue
                                s_type = 'channel' if getattr(f_chat, 'broadcast', False) else 'group'
                                s_can_send = False if s_type == 'channel' else True
                                if s_type == 'group':
                                    b_rights = getattr(f_chat, 'default_banned_rights', None)
                                    if b_rights and getattr(b_rights, 'send_messages', False):
                                        s_can_send = False
                                item = {
                                    "id": f"@{f_username}",
                                    "title": cand_title,
                                    "username": f_username,
                                    "participants_count": getattr(f_chat, 'participants_count', 0) or getattr(f_chat, 'megagroup_participants', 0) or 0,
                                    "type": s_type,
                                    "is_private": False,
                                    "country": "Global",
                                    "global_shows": 1,
                                    "user_shows": 1,
                                    "source": "spider",
                                    "can_send_messages": s_can_send
                                }
                                results.append(item)
                                if user_id: save_scraped_group(user_id, item)
                                if queue:
                                    await queue.put({"type": "result", "layer": 3, "data": item})  # pyre-ignore
                                found_in_seed = int(found_in_seed) + 1  # type: ignore
                    
                    # Look for t.me links or @mentions in text
                    if msg.text:
                        # Find t.me/xxxxx
                        links = re.findall(r'(?:t\.me|telegram\.me)/([^/?#\s\'\"]+)', msg.text)
                        # Find @xxxxx
                        mentions = re.findall(r'@([a-zA-Z0-9_]{5,32})', msg.text)
                        
                        for l in links + mentions:
                            l_lower = l.lower()
                            if l_lower not in seen and l_lower not in ('joinchat', 'share', 'addstickers', 'iv'):
                                seen.add(l_lower)
                                # Verification boost for Spider results (expiring links)
                                v_info = await verify_telegram_info(l_lower)
                                if not v_info["exists"]:
                                    continue
                                cand_title = v_info["title"] or l
                                # Strict relevance check
                                if base_query and not is_relevant_channel(cand_title, l_lower, base_query):
                                    continue

                                s2_type = v_info.get("type", "channel")
                                item2 = {
                                    "id": f"@{l_lower}",
                                    "title": cand_title,
                                    "username": l_lower,
                                    "participants_count": v_info["participants_count"],
                                    "type": s2_type,
                                    "is_private": False,
                                    "country": "Global",
                                    "global_shows": 1,
                                    "user_shows": 1,
                                    "source": "spider",
                                    "can_send_messages": bool(v_info.get("can_send_messages", s2_type != "channel"))
                                }
                                results.append(item2)
                                if queue:
                                    await queue.put({"type": "result", "layer": 3, "data": item2})  # pyre-ignore
                                found_in_seed = int(found_in_seed) + 1  # type: ignore
                
                if int(found_in_seed) > 0:  # type: ignore
                    print(f"SCRAPER[spider] Crawled @{username} -> found {found_in_seed} new verified links")
                await asyncio.sleep(0.5)
                
            except Exception as e:
                # Expected if channel is private or deleted
                continue
                
    except Exception as e:
        print(f"SCRAPER[spider] Fatal error: {e}")
    finally:
        await client.disconnect()

    return results


async def _scrape_ai_web_discovery(
    base_query: str,
    country: Optional[str] = None,
    queue: Optional[asyncio.Queue] = None,
    user_id: str = "",
    provider: str = "openrouter",
    api_key: str = "",
    model: Optional[str] = None,
    rows: list = []
) -> List[Dict[str, Any]]:
    """
    AI Scraper: Scours the web and Telegram directories for active, public Telegram groups and channels
    matching base_query and country using OpenRouter with the user's selected model.
    Discovered channels are verified in real-time via Telegram t.me metadata and streamed.
    """
    results = []
    seen = set()
    prov = (provider or "openrouter").lower().strip()
    key = (api_key or "").strip()

    if not key:
        if queue:
            await queue.put({
                "type": "error",
                "msg": "AI Scraper requires an API key. Please configure your OpenRouter API key in settings."
            })
        return []

    c_info = f" specifically focused on {country.strip()}" if country and country.strip() else ""
    if queue:
        await queue.put({
            "type": "progress",
            "msg": f"AI Web Discovery: Asking OpenRouter ({model or 'default model'}) to uncover active Telegram communities for '{base_query}'{c_info}..."
        })

    prompt = (
        f"You are an advanced web intelligence and OSINT Telegram discovery engine. "
        f"Identify and list 40 to 60 real, active, public Telegram channels, groups, and discussion communities "
        f"across the web and Telegram directories for the niche/topic: '{base_query}'{c_info}. "
        f"Return ONLY a valid JSON object in this format:\n"
        f"{{\n"
        f'  "channels": [\n'
        f'    {{"username": "telegram_handle_without_at", "title": "Channel/Group Name", "type": "channel"}}\n'
        f"  ]\n"
        f"}}\n"
        f"Do NOT include markdown explanation, preamble, or code fences. Only valid raw JSON."
    )

    discovered_candidates: List[Dict[str, str]] = []

    async with httpx.AsyncClient(timeout=35.0) as client:
        try:
            if prov == "openai":
                res = await client.post(
                    "https://api.openai.com/v1/chat/completions",
                    headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
                    json={
                        "model": model or "gpt-4o-mini",
                        "messages": [
                            {"role": "system", "content": "You are a Telegram directory and web intelligence engine. Output valid JSON only."},
                            {"role": "user", "content": prompt}
                        ],
                        "temperature": 0.5
                    }
                )
                if res.status_code == 200:
                    choices = res.json().get("choices", [])
                    raw = (choices[0].get("message", {}).get("content") or choices[0].get("text") or "") if choices else ""
                    if raw and isinstance(raw, str):
                        match = re.search(r'\{.*\}', raw, re.DOTALL)
                        if match:
                            try:
                                parsed = json.loads(match.group(0))
                                discovered_candidates = parsed.get("channels", [])
                            except Exception:
                                pass
                else:
                    if queue:
                        await queue.put({"type": "error", "msg": f"OpenAI error ({res.status_code}): {res.text[:120]}"})
                    return []

            elif prov in ("anthropic", "claude"):
                res = await client.post(
                    "https://api.anthropic.com/v1/messages",
                    headers={
                        "x-api-key": key,
                        "anthropic-version": "2023-06-01",
                        "content-type": "application/json"
                    },
                    json={
                        "model": model or "claude-3-5-haiku-20241022",
                        "max_tokens": 2048,
                        "messages": [{"role": "user", "content": prompt}]
                    }
                )
                if res.status_code == 200:
                    content_blocks = res.json().get("content", [])
                    raw = content_blocks[0].get("text", "") if content_blocks else ""
                    if raw and isinstance(raw, str):
                        match = re.search(r'\{.*\}', raw, re.DOTALL)
                        if match:
                            try:
                                parsed = json.loads(match.group(0))
                                discovered_candidates = parsed.get("channels", [])
                            except Exception:
                                pass
                else:
                    if queue:
                        await queue.put({"type": "error", "msg": f"Claude error ({res.status_code}): {res.text[:120]}"})
                    return []

            elif prov == "openrouter":
                res = await client.post(
                    "https://openrouter.ai/api/v1/chat/completions",
                    headers={
                        "Authorization": f"Bearer {key}",
                        "Content-Type": "application/json",
                        "HTTP-Referer": "https://arkitel.com",
                        "X-Title": "ArkiTel"
                    },
                    json={
                        "model": model or "openai/gpt-4o-mini",
                        "messages": [
                            {"role": "user", "content": prompt}
                        ],
                        "temperature": 0.5
                    }
                )
                if res.status_code == 200:
                    choices = res.json().get("choices", [])
                    raw = ""
                    if choices:
                        msg = choices[0].get("message", {})
                        raw = msg.get("content") or msg.get("reasoning") or msg.get("reasoning_content") or choices[0].get("text") or ""
                    if raw and isinstance(raw, str):
                        match = re.search(r'\{.*\}', raw, re.DOTALL)
                        if match:
                            try:
                                parsed = json.loads(match.group(0))
                                discovered_candidates = parsed.get("channels", [])
                            except Exception:
                                pass
                else:
                    if queue:
                        await queue.put({"type": "error", "msg": f"OpenRouter error ({res.status_code}): {res.text[:120]}"})
                    return []

            elif prov == "groq":
                res = await client.post(
                    "https://api.groq.com/openai/v1/chat/completions",
                    headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
                    json={
                        "model": model or "llama-3.3-70b-versatile",
                        "messages": [
                            {"role": "user", "content": prompt}
                        ],
                        "temperature": 0.5
                    }
                )
                if res.status_code == 200:
                    choices = res.json().get("choices", [])
                    raw = (choices[0].get("message", {}).get("content") or choices[0].get("text") or "") if choices else ""
                    if raw and isinstance(raw, str):
                        match = re.search(r'\{.*\}', raw, re.DOTALL)
                        if match:
                            try:
                                parsed = json.loads(match.group(0))
                                discovered_candidates = parsed.get("channels", [])
                            except Exception:
                                pass
                else:
                    if queue:
                        await queue.put({"type": "error", "msg": f"Groq error ({res.status_code}): {res.text[:120]}"})
                    return []
        except Exception as e:
            if queue:
                await queue.put({"type": "error", "msg": f"AI Web Discovery exception: {str(e)}"})
            return []

    # Also extract any handles matching regex from raw response if JSON extraction was sparse
    handles_to_verify: List[Dict[str, str]] = []
    for cand in discovered_candidates:
        u = cand.get("username", "").replace("@", "").strip()
        if u and len(u) >= 4 and u.lower() not in seen:
            seen.add(u.lower())
            handles_to_verify.append({
                "username": u,
                "title": cand.get("title") or u,
                "type": cand.get("type") or "channel"
            })

    if queue:
        await queue.put({
            "type": "progress",
            "msg": f"AI Web Discovery: {len(handles_to_verify)} public handles discovered. Verifying live subscriber data..."
        })

    # Concurrently verify live status and subscriber counts using verify_telegram_info
    semaphore = asyncio.Semaphore(6)
    async def verify_and_stream(cand):
        u = cand["username"]
        async with semaphore:
            try:
                v_info = await verify_telegram_info(u)
                if not v_info["exists"]:
                    return
                
                title = v_info.get("title") or cand["title"]
                # Apply anti-spam relevance check
                if not is_relevant_channel(title, u, base_query):
                    return

                cand_type = v_info.get("type", cand.get("type") or "channel")
                item = {
                    "id": f"@{u}",
                    "title": title,
                    "username": u,
                    "participants_count": v_info.get("participants_count", 0),
                    "type": cand_type,
                    "is_private": False,
                    "country": country or "Global",
                    "global_shows": 1,
                    "user_shows": 1,
                    "source": f"ai_{prov}",
                    "can_send_messages": bool(v_info.get("can_send_messages", cand_type != "channel"))
                }
                results.append(item)
                if user_id:
                    save_scraped_group(user_id, item)
                if queue:
                    await queue.put({"type": "result", "layer": "AI Intelligence", "data": item})
            except Exception as e:
                pass

    await asyncio.gather(*[verify_and_stream(c) for c in handles_to_verify], return_exceptions=True)

    if queue:
        await queue.put({
            "type": "progress",
            "msg": f"AI Web Discovery complete: Verified {len(results)} active communities."
        })

    return results


@app.get("/api/telegram/scrape_stream")
async def scrape_keyword_stream(
    query: str,
    country: Optional[str] = None,
    limit: int = 100,
    phone_number: Optional[str] = None,
    mode: Optional[str] = "fixed",
    ai_provider: Optional[str] = None,
    ai_key: Optional[str] = None,
    ai_model: Optional[str] = None,
    user_id: str = Depends(get_current_user_id)
):
    conn = get_db_connection()
    # Premium check
    user_row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
    if not user_row and user_id != "admin_virtual_id":
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=404, detail="User profile not found.")
    
    # Handle Virtual Admin
    if user_id == "admin_virtual_id":
        plan = "premium"
        allowed_limit = 1000
        max_keywords = 9999
        current_keywords = 0
    else:
        # Reset daily counts if needed
        check_and_reset_daily_limits(conn, user_id, user_row)
        
        plan = user_row["plan"] or "free"
        plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
        
        # Check Premium Access
        if not plan_cfg["has_premium_access"]:
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(status_code=403, detail=f"Access Denied: The {plan.capitalize()} plan does not include Scraper access. Upgrade to UNLOCK.")
        
        is_unlimited_plan = plan.lower() in ("unlimited", "admin", "lifetime", "enterprise") or user_id == "admin_virtual_id"
        allowed_limit = 999999 if is_unlimited_plan else (user_row["scrape_limit"] or plan_cfg["scrape_limit"])
        max_keywords = 999999 if is_unlimited_plan else int(user_row["max_daily_keywords"] or plan_cfg["max_daily_keywords"])
        current_keywords = int(user_row["daily_keyword_count"] or 0)
        
        if not is_unlimited_plan and current_keywords >= max_keywords:
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(status_code=403, detail="Daily usage limit reached for scraping. Reset at Midnight UTC.")

        # Increment keyword use and Log Search Intelligence
        if not is_unlimited_plan:
            conn.execute("UPDATE users SET daily_keyword_count = daily_keyword_count + 1 WHERE id = ?", (user_id,))
            
        # Unified global logging for Admin
        try:
            uname = "Admin"
            if user_id != "admin_virtual_id" and user_row:
                if hasattr(user_row, "get"):
                    uname = user_row.get("username", "User")
                else:
                    try: uname = user_row["username"]
                    except Exception: uname = "User"
            
            conn.execute(
                "INSERT INTO keyword_logs (user_id, username, keyword) VALUES (?, ?, ?)",
                (user_id, uname, query.strip())
            )
        except Exception as e:
            print(f"FAILED TO LOG KEYWORD: {e}")

        if hasattr(conn, "commit"): conn.commit()

    user_role = "user"
    if user_id == "admin_virtual_id":
        user_role = "admin"
    elif user_row:
        if hasattr(user_row, "get"):
            user_role = user_row.get("role", "user")
        else:
            try: user_role = user_row["role"]
            except Exception: user_role = "user"

    is_admin = (user_id == "admin_virtual_id") or (user_role == "admin")
    if plan in ("unlimited", "premium") or is_admin:
        final_limit: int = max(1500, allowed_limit)
    else:
        final_limit: int = max(limit, allowed_limit)

    if user_id == "admin_virtual_id":
        if phone_number:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE phone_number = ?", (phone_number,)).fetchall()
        else:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE status = 'active' ORDER BY is_active DESC").fetchall()
    elif phone_number:
        rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id)).fetchall()
    else:
        # Use ALL active accounts to maximize yield for the sweep
        rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE user_id = ? AND status = 'active' ORDER BY is_active DESC", (user_id,)).fetchall()
    if hasattr(conn, "close"): conn.close()
    if not rows and mode != "ai":
        raise HTTPException(status_code=404, detail="No active accounts found.")

    base_query = query.strip()
    if country:
        base_query = f"{base_query}{country}"

    queue = asyncio.Queue()

    async def scraper_task():
        try:
            search_mode = (mode or "fixed").lower().strip()
            
            if search_mode == "ai":
                # AI Scraper: Uses configured AI model for web & directory discovery
                await queue.put({"type": "progress", "msg": f"Initializing AI Web Discovery Engine for '{base_query}'..."})
                await _scrape_ai_web_discovery(
                    base_query=base_query,
                    country=country,
                    queue=queue,
                    user_id=user_id,
                    provider=ai_provider or "openrouter",
                    api_key=ai_key or "",
                    model=ai_model,
                    rows=rows
                )
                await queue.put({"type": "done", "msg": "AI Scrape completed"})
                return

            if search_mode == "free":
                # Free Mode: Pure natural keyword search (No spidering, no cross-contamination)
                await queue.put({"type": "progress", "msg": f"Layer 1 & 2: Natural Telegram Search & Lyzem Engine for '{base_query}'..."})
                t_task = asyncio.create_task(_scrape_telegram_search(base_query, rows, queue, user_id, mode="free"))
                l_task = asyncio.create_task(_scrape_lyzem(base_query, 10, queue, user_id))
                await asyncio.gather(t_task, l_task, return_exceptions=True)
                await queue.put({"type": "done", "msg": "Natural search completed"})
                return

            # Fixed Mode: 60+ Universal variations + Lyzem + Spider (with relevance verification)
            await queue.put({"type": "progress", "msg": f"Layer 1 & 2: 60+ Suffix Sweep & Lyzem Engine for '{base_query}'..."})
            t_task = asyncio.create_task(_scrape_telegram_search(base_query, rows, queue, user_id, mode="fixed"))
            l_task = asyncio.create_task(_scrape_lyzem(base_query, 10, queue, user_id))
            t_res, l_res = await asyncio.gather(t_task, l_task, return_exceptions=True)
            
            ug = {}
            if isinstance(t_res, dict): ug.update(t_res)
            for item in (l_res if isinstance(l_res, list) else []):
                k = f"@{item.get('username', '')}"
                if k not in ug and item.get('username'): ug[k] = item
            
            await queue.put({"type": "progress", "msg": f"Layer 3: Spidering {len(ug)} prominent groups (Relevance verified)..."})
            await _scrape_spider(list(ug.values()), rows, 15, queue, user_id, base_query=base_query)

            await queue.put({"type": "done", "msg": "Fixed mode sweep completed"})
        except asyncio.CancelledError:
            print("SCRAPER_STREAM canceled by client disconnect")
            raise
        except Exception as e:
            await queue.put({"type": "error", "msg": str(e)})

    # Start explicitly outside event_generator
    bg_task = asyncio.create_task(scraper_task())

    async def event_generator():
        state = {"count": 0}
        try:
            while True:
                msg = await queue.get()  # pyre-ignore
                if msg["type"] == "result":
                    state["count"] += 1
                
                yield f"data: {json.dumps(msg)}\n\n"
                
                if msg["type"] == "result" and state["count"] >= final_limit:
                    yield f"data: {json.dumps({'type': 'done', 'msg': 'Scrape completed (limit reached)'})}\n\n"
                    bg_task.cancel()
                    break
                    
                if msg["type"] in ["done", "error"]:
                    break
        except asyncio.CancelledError:
            bg_task.cancel()
            print("Client disconnected, stopping scraper stream task.")
            raise

    response = StreamingResponse(event_generator(), media_type="text/event-stream")
    response.headers["Cache-Control"] = "no-cache"
    response.headers["Connection"] = "keep-alive"
    return response

@app.get("/api/telegram/scrape")
async def scrape_keyword(
    query: str,
    limit: int = 100,
    phone_number: Optional[str] = None,
    user_id: str = Depends(get_current_user_id)
):
    conn = get_db_connection()
    if user_id == "admin_virtual_id":
        plan_cfg = get_plan_configs()["unlimited"]
        max_keywords = 9999
        current_keywords = 0
        scrape_limit = 9999
        
        if phone_number:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE phone_number = ?", (phone_number,)).fetchall()
        else:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE status = 'active'").fetchall()
    else:
        user_row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        if not user_row:
             if hasattr(conn, "close"): conn.close()
             raise HTTPException(status_code=404, detail="User not found")
        
        check_and_reset_daily_limits(conn, user_id, user_row)
        plan = (user_row["plan"] or "free")
        plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
        
        is_unlimited_plan = plan.lower() in ("unlimited", "admin", "lifetime", "enterprise") or user_id == "admin_virtual_id"
        max_keywords = 999999 if is_unlimited_plan else int(user_row["max_daily_keywords"] or plan_cfg["max_daily_keywords"])
        current_keywords = int(user_row["daily_keyword_count"] or 0)
        scrape_limit = 999999 if is_unlimited_plan else int(user_row["scrape_limit"] or plan_cfg["scrape_limit"])

        if not is_unlimited_plan and current_keywords >= max_keywords:
             if hasattr(conn, "close"): conn.close()
             raise HTTPException(status_code=403, detail="Daily search limit reached.")
        
        # Increment search count and Log Keyword
        if not is_unlimited_plan:
            conn.execute("UPDATE users SET daily_keyword_count = daily_keyword_count + 1 WHERE id = ?", (user_id,))
            
        # Global log for admin
        conn.execute(
            "INSERT INTO keyword_logs (user_id, username, keyword) VALUES (?, ?, ?)",
            (user_id, user_row["username"], query.strip())
        )
        if hasattr(conn, "commit"): conn.commit()

        if phone_number:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE phone_number = ? AND user_id = ?", (phone_number, user_id)).fetchall()
        else:
            rows = conn.execute("SELECT session_string, api_id, api_hash, phone_number FROM accounts WHERE user_id = ? AND status = 'active'", (user_id,)).fetchall()

    if hasattr(conn, "close"): conn.close()

    if not rows:
        raise HTTPException(status_code=404, detail="No active accounts found.")

    base_query = query.strip()

    # Run layers concurrently
    print(f"SCRAPER: Starting hybrid search for '{base_query}' (Telegram API + Lyzem Crawler)")
    telegram_task = asyncio.create_task(_scrape_telegram_search(base_query, rows))
    lyzem_task    = asyncio.create_task(_scrape_lyzem(base_query, max_pages=10))

    telegram_results, lyzem_results = await asyncio.gather(
        telegram_task, lyzem_task, return_exceptions=True
    )

    # Merge — Telegram results keyed by numeric ID, directory results keyed by @username
    unique_groups: Dict[str, Dict[str, Any]] = {}

    if isinstance(telegram_results, dict):
        unique_groups.update(telegram_results)

    for item in (lyzem_results if isinstance(lyzem_results, list) else []):
        key = f"@{item.get('username', '')}"
        if key not in unique_groups and item.get('username'):
            unique_groups[key] = item

    # LAYER 3: Spider Crawler
    print(f"SCRAPER: Starting Layer 3 (Spider Crawler) based on top results...")
    spider_results = await _scrape_spider(list(unique_groups.values()), rows)
    for item in spider_results:
        key = f"@{item.get('username', '')}"
        if key not in unique_groups and item.get('username'):
            unique_groups[key] = item
            
    final_list = list(unique_groups.values())
    
    # Enforce scrape_limit from plan - cast to Any to satisfy Pyre's slicing check
    final_list = cast(Any, final_list)[:scrape_limit]

    # Auto-saving to scraped_groups disabled as per user request
    # Manual save will be triggered via /api/telegram/leads/groups/bulk-save
    
    # Record only to history (discovery log)
    conn2 = get_db_connection()
    for item in final_list:
        gid = item.get('id')
        if not gid: continue
        conn2.execute("INSERT OR IGNORE INTO scrape_history (group_id, user_id) VALUES (?, ?)", (gid, user_id))
    
    if hasattr(conn2, "commit"): conn2.commit()
    if hasattr(conn2, "close"): conn2.close()

    print(f"SCRAPER: Done — {len(final_list)} unique results for '{base_query}' "
          f"(TG:{len(telegram_results) if isinstance(telegram_results, dict) else 0} "
          f"Lyzem:{len(lyzem_results) if isinstance(lyzem_results, list) else 0} "
          f"Spider:{len(spider_results)})")

    return {"groups": final_list}

class BulkJoinRequest(BaseModel):
    group_ids: list[str]
    phone_number: str

@app.post("/api/telegram/bulk-join")
async def bulk_join(req: BulkJoinRequest, user_id: str = Depends(get_current_user_id)):
    """Legacy non-streaming bulk join (kept for compatibility). Use /stream for real-time tracking."""
    conn = get_db_connection()
    row = conn.execute("SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?", (req.phone_number, user_id)).fetchone()
    if hasattr(conn, "close"): conn.close()
    if not row: raise HTTPException(status_code=404, detail="Account not found")
    
    session_str, api_id, api_hash = row[0], int(row[1]), row[2]
    client = TelegramClient(StringSession(session_str), api_id, api_hash)
    await client.connect()
    
    results = {'joined': 0, 'failed': 0}
    try:
        from telethon.tl.functions.channels import JoinChannelRequest  # pyre-ignore[21]
        for g_id in req.group_ids:
            try:
                await asyncio.sleep(random.randint(10, 15))
                resolved_id = int(g_id) if g_id.lstrip('-').isdigit() else g_id
                entity = await client.get_entity(resolved_id)
                await client(JoinChannelRequest(cast(Any, entity)))
                results['joined'] += 1
            except Exception as e:
                results['failed'] += 1
                if "FloodWaitError" in str(e): break
        return {"status": "success", **results}
    finally:
        await client.disconnect()


@app.get("/api/telegram/bulk-join/stream")
async def bulk_join_stream(
    group_ids: str,
    phone_number: str,
    token: str = ""
):
    """
    SSE Streaming Bulk Join – emits real-time progress events.
    Uses token query param instead of Authorization header (EventSource limitation).
    group_ids: JSON-encoded list of group IDs.
    """
    import json as _json
    from jose import JWTError, jwt as _jwt  # type: ignore
    SECRET_KEY_LOCAL = os.getenv("JWT_SECRET", "super-secret-key-change-this-in-production")
    
    # Manually validate the token (can't use Depends for SSE endpoints)
    user_id = None
    try:
        payload = _jwt.decode(token, SECRET_KEY_LOCAL, algorithms=["HS256"])
        user_id = payload.get("sub")
    except Exception:
        pass
    
    if not user_id:
        async def unauth_stream():
            yield f"data: {_json.dumps({'type': 'error', 'msg': 'Unauthorized'})}\n\n"
        return StreamingResponse(unauth_stream(), media_type="text/event-stream")

    # Fleet Bulk Join requires an upgraded plan
    if user_id != "admin_virtual_id":
        conn_u = get_db_connection()
        u_row = conn_u.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
        if hasattr(conn_u, "close"): conn_u.close()
        u_plan = u_row[0] if u_row and u_row[0] else "free"
        if u_plan == "free":
            async def unauth_plan_stream():
                yield f"data: {_json.dumps({'type': 'error', 'msg': 'Bulk Join is an upgraded feature. Please upgrade your plan to unlock.'})}\n\n"
            return StreamingResponse(unauth_plan_stream(), media_type="text/event-stream")
    
    try:
        ids = _json.loads(group_ids)
    except Exception:
        ids = [group_ids]

    conn = get_db_connection()
    if user_id == "admin_virtual_id":
        row = conn.execute(
            "SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ?",
            (phone_number,)
        ).fetchone()
    else:
        row = conn.execute(
            "SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?",
            (phone_number, user_id)
        ).fetchone()
    if hasattr(conn, "close"): conn.close()
    
    if not row:
        async def error_stream():
            yield f"data: {_json.dumps({'type': 'error', 'msg': 'Account not found'})}\n\n"
        return StreamingResponse(error_stream(), media_type="text/event-stream")

    session_str, api_id_val, api_hash_val = row[0], int(row[1]), row[2]
    total = len(ids)

    async def event_generator():
        try:
            client = await POOL.get_client(user_id, phone_number) # type: ignore
            
            joined: int = 0
            failed: int = 0
            failed_groups = []
            
            yield f"data: {_json.dumps({'type': 'start', 'total': total})}\n\n"
            
            for idx, g_id in enumerate(ids, start=1):
                group_label = str(g_id)

                try:
                    # Human Rhythms: Longer randomized intervals (45-120s)
                    wait_time = random.randint(45, 120)
                    
                    # Micro-Rest: Pause for 2 mins every 5 joins to avoid account flagging
                    if idx > 1 and (idx - 1) % 5 == 0:
                        for remaining in range(120, 0, -1):
                            yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'Micro-Rest: Resuming in {remaining}s...', 'remaining': remaining})}\n\n"
                            await asyncio.sleep(1)

                    for remaining in range(wait_time, 0, -1):
                        yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'Safety delay: {remaining}s...', 'remaining': remaining})}\n\n"
                        await asyncio.sleep(1)
                    
                    yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'Joining {group_label}...'})}\n\n"
                    # Use pool-based client
                    await smart_join(client, g_id, user_id, phone_number)
                    
                    joined = joined + 1
                    yield f"data: {_json.dumps({'type': 'joined', 'name': group_label, 'idx': idx, 'total': total, 'joined': joined, 'failed': failed})}\n\n"
                except Exception as e:
                    from telethon.errors import FloodWaitError, ChannelsTooMuchError, UserDeactivatedError # type: ignore
                    err_r = str(e)
                    
                    if isinstance(e, FloodWaitError):
                        wait_seconds: int = cast(int, getattr(e, 'seconds', 0))
                        yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'🚨 RATE LIMIT: Telegram says wait {wait_seconds}s. Use the manual Copy button if you are in a hurry.'})}\n\n"
                        # We stop the automated process for FloodWait to protect the account
                        break
                    
                    if "too many channels" in err_r.lower() or isinstance(e, ChannelsTooMuchError):
                        err_s = "Account Limit: You are in 500 groups already. Leave some groups first."
                    elif "privacy" in err_r.lower():
                        err_s = "Privacy: Group doesn't allow invites or joining."
                    elif "deactivated" in err_r.lower() or isinstance(e, UserDeactivatedError):
                        err_s = "Account Alert: This account seems banned/deactivated by Telegram."
                    elif "chat_admin_required" in err_r.lower():
                         err_s = "Restricted: Admin required to join."
                    else:
                        err_s = "Join Ignored: Telegram restricted this action. Use the 'Copy' button and join manually."
                    
                    failed = failed + 1
                    failed_groups.append({'name': group_label, 'reason': err_s})
                    yield f"data: {_json.dumps({'type': 'failed', 'name': group_label, 'reason': err_s, 'idx': idx, 'total': total, 'joined': joined, 'failed': failed})}\n\n"
            
            yield f"data: {_json.dumps({'type': 'done', 'joined': joined, 'failed': failed, 'failed_groups': failed_groups})}\n\n"
        except Exception as global_e:
            yield f"data: {_json.dumps({'type': 'error', 'msg': f'Stream error: {str(global_e)}'})}\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}
    )

class JoinRequest(BaseModel):
    group_id: str
    phone_number: str


class ExtractRequest(BaseModel):
    group_id: str
    phone_number: str

@app.post("/api/telegram/extract")
async def extract_group_members(req: ExtractRequest, user_id: str = Depends(get_current_user_id)):
    """Fetches real group participants up to 1000, saves to leads, and returns full member list to UI."""
    conn = get_db_connection()
    try:
        if user_id != "admin_virtual_id":
            user_row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
            if not user_row:
                raise HTTPException(status_code=404, detail="User not found")
            plan = user_row["plan"] or "free"
            plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
            if not plan_cfg.get("has_premium_access"):
                raise HTTPException(status_code=403, detail="Member Extraction is a Premium feature. Upgrade to Standard or Premium to unlock.")

        client = await POOL.get_client(user_id, req.phone_number)
        
        target = req.group_id.strip()
        if target.lstrip('-').isdigit():
            target_entity = int(target)
        else:
            target_entity = target.lstrip('@')

        try:
            entity = await client.get_entity(target_entity)
        except Exception as e:
            try:
                # Secondary fallback resolution with @ if not present
                entity = await client.get_entity(f"@{target_entity}")
            except Exception:
                raise HTTPException(status_code=400, detail=f"Could not resolve group entity '{req.group_id}': {str(e)}")

        members = []
        try:
            async for user in client.iter_participants(cast(Any, entity), limit=1000):
                if getattr(user, 'bot', False):
                    continue
                members.append({
                    "id": str(user.id),
                    "username": user.username or "",
                    "first_name": user.first_name or "",
                    "last_name": user.last_name or "",
                    "phone": getattr(user, 'phone', None) or "",
                    "is_bot": bool(getattr(user, 'bot', False))
                })
        except Exception as e:
            log_debug(f"Extraction restricted or failed for {req.group_id}: {str(e)}")
            raise HTTPException(status_code=403, detail="Member list restricted by Telegram group admins or privacy settings.")

        # Save to Leads table for persistence
        entity_id = getattr(entity, 'id', target)
        for m in members:
            try:
                conn.execute(
                    "INSERT OR IGNORE INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
                    (user_id, str(entity_id), m['username'], m['first_name'], m['last_name'])
                )
            except Exception:
                pass
        if hasattr(conn, "commit"): conn.commit()

        return {"status": "success", "count": len(members), "members": members}
    except HTTPException:
        raise
    except Exception as e:
        log_debug(f"EXTRACT_ERROR for {req.phone_number}: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")
    finally:
        if hasattr(conn, "close"): conn.close()

class MemberSaveRequest(BaseModel):
    id: str
    username: Optional[str] = None
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    phone: Optional[str] = None
    group_id: Optional[str] = None

@app.post("/api/telegram/leads/members/bulk-save")
async def bulk_save_members(req_list: List[MemberSaveRequest], user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        for m in req_list:
            conn.execute(
                """INSERT INTO lead_members (user_id, group_id, tg_id, username, first_name, last_name, phone)
                   VALUES (?, ?, ?, ?, ?, ?, ?)""",
                (user_id, m.group_id, m.id, m.username, m.first_name, m.last_name, m.phone)
            )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "count": len(req_list)}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/telegram/leads/members")
async def get_saved_members(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
    plan = user_row["plan"] if user_row else "free"
    plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
    
    if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
        if hasattr(conn, "close"): conn.close()
        return {"members": [], "locked": True}

    rows = conn.execute("SELECT * FROM lead_members WHERE user_id = ? ORDER BY scraped_at DESC", (user_id,)).fetchall()
    if hasattr(conn, "close"): conn.close()
    return {
        "members": [
            {
                "id": r["id"],
                "group_id": r["group_id"],
                "username": r["username"],
                "first_name": r["first_name"],
                "last_name": r["last_name"],
                "phone": r["phone"],
                "scraped_at": r["scraped_at"]
            } for r in rows
        ]
    }

@app.delete("/api/telegram/leads/members/{member_id}")
async def delete_saved_member(member_id: int, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    conn.execute("DELETE FROM lead_members WHERE id = ? AND user_id = ?", (member_id, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

@app.get("/api/broadcasts")
async def get_active_broadcasts():
    conn = get_db_connection()
    rows = conn.execute("SELECT * FROM broadcasts WHERE is_active = 1 ORDER BY created_at DESC").fetchall()
    if hasattr(conn, "close"): conn.close()
    return {"broadcasts": [dict(id=r["id"], message=r["message"], type=r["type"], created_at=r["created_at"]) for r in rows]}

@app.post("/api/admin/broadcast")
async def create_broadcast(message: str, type: str = 'info', admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute("INSERT INTO broadcasts (message, type) VALUES (?, ?)", (message, type))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

# --- Monetization & Plans ---

def extract_pure_email(address_str: str) -> str:
    """Extracts pure username@domain.com from 'Display Name <username@domain.com>' or raw string."""
    if not address_str:
        return ""
    import re
    match = re.search(r'<([^>]+)>', address_str)
    if match:
        return match.group(1).strip()
    return address_str.strip()

# --- Email Feature Disabled (Safe No-Ops) ---

def extract_pure_email(address_str: str) -> str:
    """Extracts pure email string or fallback."""
    if not address_str:
        return ""
    import re
    match = re.search(r'<([^>]+)>', address_str)
    return match.group(1).strip() if match else address_str.strip()

def send_resend_email(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def verify_resend_connection(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def send_gmail_relay_email(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def verify_gmail_relay_connection(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def verify_smtp_connection(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def get_email_credentials() -> Dict[str, Any]:
    return {
        "host": "localhost",
        "port": 587,
        "user": "",
        "pure_user": "",
        "pass": "",
        "from": "",
        "envelope_from": "",
        "email_provider": "disabled",
        "resend_api_key": "",
        "resend_from": "",
        "resend_reply_to": "",
        "gmail_relay_url": "",
        "gmail_relay_secret": "",
        "smtp_configured": False,
        "resend_configured": False,
        "gmail_relay_configured": False,
        "is_configured": False,
        "disabled": True
    }

def send_smtp_email_detailed(*args, **kwargs) -> tuple[bool, str]:
    return True, "Email notifications are disabled"

def dispatch_smtp_email(*args, **kwargs) -> bool:
    return True

def send_welcome_email(*args, **kwargs):
    return True

def send_welcome_email_async(*args, **kwargs):
    pass

def send_plan_activation_email(*args, **kwargs):
    return True

def send_plan_activation_email_async(*args, **kwargs):
    pass

class SmtpSettingsUpdateRequest(BaseModel):
    smtp_host: Optional[str] = None
    smtp_port: Optional[int] = None
    smtp_user: Optional[str] = None
    smtp_pass: Optional[str] = None
    smtp_from: Optional[str] = None
    email_provider: Optional[str] = None
    resend_api_key: Optional[str] = None
    resend_from: Optional[str] = None
    resend_reply_to: Optional[str] = None
    gmail_relay_url: Optional[str] = None
    gmail_relay_secret: Optional[str] = None
    skip_verify: Optional[bool] = True

class TestEmailRequest(BaseModel):
    to_email: str

@app.get("/api/admin/system/smtp-settings")
async def get_admin_smtp_settings(admin_id: str = Depends(get_current_admin)):
    return {
        "is_configured": False,
        "email_disabled": True,
        "message": "Email notifications are disabled."
    }

@app.post("/api/admin/system/smtp-settings")
async def update_admin_smtp_settings(req: SmtpSettingsUpdateRequest, admin_id: str = Depends(get_current_admin)):
    return {"status": "disabled", "message": "Email notifications are disabled."}

@app.post("/api/admin/system/test-email")
async def send_admin_test_email(req: TestEmailRequest, admin_id: str = Depends(get_current_admin)):
    return {"status": "disabled", "message": "Email notifications are disabled."}

class UserVitalsUpdateRequest(BaseModel):
    plan: str
    scrape_limit: Optional[int] = None
    max_accounts: Optional[int] = None
    max_daily_campaigns: Optional[int] = None
    max_daily_keywords: Optional[int] = None
    is_approved: int

@app.get("/api/admin/monetization/users")
async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    # Join with plans to get default limits dynamically
    query = """
        SELECT u.*, p.max_templates as plan_max_templates,
               p.max_daily_campaigns as plan_max_campaigns,
               p.max_daily_keywords as plan_max_keywords,
               p.scrape_limit as plan_scrape_limit,
               p.max_accounts as plan_max_accounts
        FROM users u
        LEFT JOIN plans p ON u.plan = p.key
    """
    rows = conn.execute(query).fetchall()
    if hasattr(conn, "close"): conn.close()
    
    return {"users": [dict(
        id=r["id"], 
        username=r["username"], 
        email=r["email"] if ("email" in r.keys() and r["email"]) else r["username"],
        country=r["country"] if ("country" in r.keys() and r["country"]) else "Nigeria",
        plan=r["plan"], 
        # Dynamically pulling from plan if user fields are the system default (1 or NULL)
        max_templates=r["plan_max_templates"] if r["max_templates"] == 1 or r["max_templates"] is None else r["max_templates"],
        max_daily_campaigns=r["plan_max_campaigns"] if r["max_daily_campaigns"] is None else r["max_daily_campaigns"],
        max_daily_keywords=r["plan_max_keywords"] if r["max_daily_keywords"] is None else r["max_daily_keywords"],
        scrape_limit=r["plan_scrape_limit"] if r["scrape_limit"] is None else r["scrape_limit"],
        max_accounts=r["plan_max_accounts"] if r["max_accounts"] is None else r["max_accounts"],
        daily_campaign_count=r["daily_campaign_count"],
        daily_keyword_count=r["daily_keyword_count"],
        is_approved=r["is_approved"], 
        total_scraped=r["total_scraped"], 
        payment_proof=r["payment_proof"],
        plan_activated_at=r["plan_activated_at"],
        plan_expires_at=r["plan_expires_at"]
    ) for r in rows]}

@app.post("/api/admin/monetization/users/{uid}/vitals")
async def update_user_vitals(uid: str, req: UserVitalsUpdateRequest, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        # Always look up the plan config live from DB to avoid stale cache issues
        plan_row = conn.execute("SELECT * FROM plans WHERE key = ?", (req.plan,)).fetchone()
        
        # Safe defaults in case plan doesn't exist in DB
        DEFAULTS = {
            "free":      {"scrape_limit": 50,   "max_accounts": 1,  "max_daily_campaigns": 20,  "max_daily_keywords": 5,   "max_templates": 1,   "price": 0},
            "basic":     {"scrape_limit": 100,  "max_accounts": 2,  "max_daily_campaigns": 50,  "max_daily_keywords": 15,  "max_templates": 2,   "price": 2000},
            "standard":  {"scrape_limit": 250,  "max_accounts": 5,  "max_daily_campaigns": 150, "max_daily_keywords": 30,  "max_templates": 5,   "price": 3500},
            "premium":   {"scrape_limit": 500,  "max_accounts": 10, "max_daily_campaigns": 300, "max_daily_keywords": 50,  "max_templates": 10,  "price": 5000},
            "unlimited": {"scrape_limit": 999999, "max_accounts": 999999, "max_daily_campaigns": 999999, "max_daily_keywords": 999999, "max_templates": 999999, "price": 0},
        }
        
        if plan_row:
            plan_cfg = dict(plan_row)
        else:
            plan_cfg = DEFAULTS.get(req.plan, DEFAULTS["free"])
        
        # Use provided override values or fall back to plan defaults
        if req.plan and req.plan.lower() in ("unlimited", "admin", "lifetime", "enterprise"):
            s_limit = 999999
            m_acc   = 999999
            m_camp  = 999999
            m_key   = 999999
            m_tmpl  = 999999
        else:
            s_limit = req.scrape_limit if req.scrape_limit is not None else plan_cfg["scrape_limit"]
            m_acc   = req.max_accounts if req.max_accounts is not None else plan_cfg["max_accounts"]
            m_camp  = req.max_daily_campaigns if req.max_daily_campaigns is not None else plan_cfg["max_daily_campaigns"]
            m_key   = req.max_daily_keywords if req.max_daily_keywords is not None else plan_cfg["max_daily_keywords"]
            m_tmpl  = plan_cfg.get("max_templates", 1)
        
        # Handle Subscription Dates
        activated_at = None
        expires_at   = None
        
        # Check current user status for payment logging
        curr_user = conn.execute("SELECT plan, is_approved, username, email FROM users WHERE id = ?", (uid,)).fetchone()
        already_paid = (curr_user and curr_user["is_approved"] == 1 and curr_user["plan"] != 'free')
        
        if req.plan != 'free' and req.is_approved:
            now = datetime.now()
            activated_at = now.strftime('%Y-%m-%dT%H:%M')
            expires_at   = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')
            
            # Log payment only on first-time upgrade
            if not already_paid:
                try:
                    price = plan_cfg.get("price", 0)
                    uname = curr_user["username"] if curr_user else "Unknown"
                    conn.execute(
                        "INSERT INTO payments (user_id, username, plan, amount) VALUES (?, ?, ?, ?)",
                        (uid, uname, req.plan, price)
                    )
                    log_debug(f"MONEY: Logged payment of #{price} for {uname} (Plan: {req.plan})")
                except Exception as pay_err:
                    log_debug(f"MONEY: Payment log skipped (table may not exist): {pay_err}")

            # Also mark any pending payment_requests for this user as approved
            try:
                conn.execute(
                    "UPDATE payment_requests SET status = 'approved', verified_at = ? WHERE user_id = ? AND status = 'pending'",
                    (activated_at, uid)
                )
            except Exception as pr_err:
                log_debug(f"Payment requests update: {pr_err}")

            # Send Email Notification
            if not already_paid:
                try:
                    to_email = (curr_user["email"] if curr_user and curr_user["email"] else curr_user["username"]) if curr_user else None
                    if to_email:
                        send_plan_activation_email_async(to_email, curr_user["username"], req.plan, expires_at)
                except Exception as mail_err:
                    log_debug(f"Email dispatch error: {mail_err}")
        
        # Perform the actual user update
        conn.execute(
            """UPDATE users SET 
               plan = ?, scrape_limit = ?, max_accounts = ?, 
               max_daily_campaigns = ?, max_daily_keywords = ?, 
               is_approved = ?, plan_activated_at = ?, plan_expires_at = ?,
               max_templates = ?
               WHERE id = ?""",
            (req.plan, s_limit, m_acc, m_camp, m_key, req.is_approved, activated_at, expires_at, m_tmpl, uid)
        )
        if hasattr(conn, "commit"): conn.commit()
        log_debug(f"ADMIN: Updated user {uid} to plan '{req.plan}', approved={req.is_approved}")
        return {"status": "success", "activated_at": activated_at, "expires_at": expires_at}
    
    except Exception as e:
        log_debug(f"ERROR in update_user_vitals: {e}")
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"Failed to update user: {str(e)}")
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/admin/monetization/coupons")
async def get_coupons_admin(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    rows = conn.execute("SELECT * FROM coupons ORDER BY created_at DESC").fetchall()
    if hasattr(conn, "close"): conn.close()
    def _safe(r, k):
        try: return r[k]
        except: return None
    return {"coupons": [
        dict(
            id=r["id"],
            code=r["code"],
            price=r["price"],
            is_active=r["is_active"],
            created_at=r["created_at"],
            target_plan=_safe(r, "target_plan") or "premium",
            duration_hours=_safe(r, "duration_hours") or 0,
            max_accounts=_safe(r, "max_accounts"),
            max_templates=_safe(r, "max_templates"),
            max_daily_campaigns=_safe(r, "max_daily_campaigns"),
            max_daily_keywords=_safe(r, "max_daily_keywords"),
            scrape_limit=_safe(r, "scrape_limit")
        ) for r in rows
    ]}

class CouponCreateRequest(BaseModel):
    code: str
    price: int = 0
    target_plan: Optional[str] = "premium"
    duration_hours: Optional[int] = 0
    max_accounts: Optional[int] = None
    max_templates: Optional[int] = None
    max_daily_campaigns: Optional[int] = None
    max_daily_keywords: Optional[int] = None
    scrape_limit: Optional[int] = None

@app.post("/api/admin/monetization/coupons")
async def create_coupon_admin(req: CouponCreateRequest, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        clean_code = req.code.strip().upper()
        target_plan = (req.target_plan or "premium").strip().lower()
        conn.execute(
            """INSERT INTO coupons (code, price, target_plan, duration_hours, max_accounts, max_templates, max_daily_campaigns, max_daily_keywords, scrape_limit) 
               VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", 
            (clean_code, req.price, target_plan, req.duration_hours or 0, req.max_accounts, req.max_templates, req.max_daily_campaigns, req.max_daily_keywords, req.scrape_limit)
        )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success"}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.put("/api/admin/monetization/coupons/{code}")
async def update_coupon_admin(code: str, req: CouponCreateRequest, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        old_code = code.strip().upper()
        new_code = req.code.strip().upper() if req.code else old_code
        target_plan = (req.target_plan or "premium").strip().lower()
        conn.execute(
            """UPDATE coupons SET 
               code = ?, price = ?, target_plan = ?, duration_hours = ?, 
               max_accounts = ?, max_templates = ?, max_daily_campaigns = ?, 
               max_daily_keywords = ?, scrape_limit = ?
               WHERE UPPER(code) = ?""",
            (new_code, req.price, target_plan, req.duration_hours or 0, req.max_accounts, req.max_templates, req.max_daily_campaigns, req.max_daily_keywords, req.scrape_limit, old_code)
        )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "code": new_code}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/monetization/coupons/{code}/toggle")
async def toggle_coupon_admin(code: str, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        code_clean = code.strip().upper()
        c = conn.execute("SELECT is_active FROM coupons WHERE UPPER(code) = ?", (code_clean,)).fetchone()
        if not c:
            raise HTTPException(status_code=404, detail="Coupon not found")
        new_active = 0 if c["is_active"] else 1
        conn.execute("UPDATE coupons SET is_active = ? WHERE UPPER(code) = ?", (new_active, code_clean))
        
        # If pausing (deactivating), revert active users back to their original plan immediately!
        if new_active == 0:
            conn.execute(
                """UPDATE users SET 
                   plan = COALESCE(original_plan, 'free'),
                   active_coupon_code = NULL,
                   coupon_expires_at = NULL,
                   max_daily_campaigns = NULL,
                   max_daily_keywords = NULL,
                   scrape_limit = NULL,
                   max_accounts = NULL,
                   max_templates = NULL
                   WHERE UPPER(active_coupon_code) = ?""",
                (code_clean,)
            )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "is_active": new_active}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.delete("/api/admin/monetization/coupons/{code}")
async def delete_coupon_admin(code: str, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        code_clean = code.strip().upper()
        # Automatically revert all users with this coupon active back to their original baseline plan!
        conn.execute(
            """UPDATE users SET 
               plan = COALESCE(original_plan, 'free'),
               active_coupon_code = NULL,
               coupon_expires_at = NULL,
               max_daily_campaigns = NULL,
               max_daily_keywords = NULL,
               scrape_limit = NULL,
               max_accounts = NULL,
               max_templates = NULL
               WHERE UPPER(active_coupon_code) = ?""",
            (code_clean,)
        )
        conn.execute("DELETE FROM coupons WHERE UPPER(code) = ?", (code_clean,))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success"}
    finally:
        if hasattr(conn, "close"): conn.close()

# --- User Side Monetization ---

@app.get("/api/monetization/status")
async def get_user_status(user_id: str = Depends(get_current_user_id)):
    if user_id == "admin_virtual_id":
        cfg = get_plan_configs()["unlimited"]
        cfg = get_plan_configs().get("unlimited", get_plan_configs()["free"])
        return {
            "status": "success",
            "plan": "unlimited",
            "original_plan": "unlimited",
            "active_coupon_code": None,
            "coupon_expires_at": None,
            "scrape_limit": cfg["scrape_limit"],
            "max_accounts": cfg["max_accounts"],
            "max_daily_campaigns": cfg["max_daily_campaigns"],
            "daily_campaign_count": 0,
            "max_daily_keywords": cfg["max_daily_keywords"],
            "daily_keyword_count": 0,
            "max_templates": cfg["max_templates"],
            "template_count": 0,
            "email": "admin@arkitel.app",
            "country": "Nigeria",
            "is_approved": 1,
            "has_proof": True,
            "allowed_speeds": ["balanced", "turbo", "stealth", "ultra"],
            "has_spintax": True,
            "has_fleet_compare": True,
            "can_export_csv": True,
            "ai_keyword_cap": 100
        }

    conn = get_db_connection()
    try:
        row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        if not row:
            raise HTTPException(status_code=404, detail="User not found")
            
        # Reset daily counts if needed
        check_and_reset_daily_limits(conn, user_id, row)

        # Check if active coupon has expired
        try:
            exp_str = row["coupon_expires_at"]
            if exp_str:
                exp_dt = datetime.fromisoformat(exp_str)
                if datetime.now(timezone.utc) > exp_dt:
                    revert_p = row["original_plan"] or "free"
                    conn.execute(
                        """UPDATE users SET 
                           plan = ?,
                           active_coupon_code = NULL,
                           coupon_expires_at = NULL,
                           max_daily_campaigns = NULL,
                           max_daily_keywords = NULL,
                           scrape_limit = NULL,
                           max_accounts = NULL,
                           max_templates = NULL
                           WHERE id = ?""",
                        (revert_p, user_id)
                    )
                    if hasattr(conn, "commit"): conn.commit()
        except Exception as e:
            pass

        # Fetch latest row after possible reset or coupon revert
        row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        t_count = conn.execute("SELECT COUNT(*) FROM templates WHERE user_id = ?", (user_id,)).fetchone()[0]
    finally:
        if hasattr(conn, "close"): conn.close()
    
    # Tier mapping
    user_role = str(row["role"] if ("role" in row.keys() and row["role"]) else "user").lower().strip()
    active_plan = "unlimited" if user_role == "admin" else (row["plan"] or "free")
    is_unlimited = active_plan.lower() in ["unlimited", "admin", "lifetime", "enterprise"] or user_role == "admin"
    p_cfg = get_plan_configs().get(active_plan, get_plan_configs()["free"])

    allowed_speeds = ["balanced"]
    if active_plan == "basic":
        allowed_speeds = ["balanced", "turbo"]
    elif active_plan == "standard":
        allowed_speeds = ["balanced", "turbo", "stealth"]
    elif is_unlimited or active_plan == "premium":
        allowed_speeds = ["balanced", "turbo", "stealth", "ultra"]

    if is_unlimited:
        max_camps = 999999
        max_kws = 999999
        scrape_lim = 999999
        max_accs = 999999
        max_tpls = 999999
        ai_kw_cap = 100
    else:
        max_camps = row["max_daily_campaigns"] or p_cfg["max_daily_campaigns"]
        max_kws = row["max_daily_keywords"] or p_cfg["max_daily_keywords"]
        scrape_lim = row["scrape_limit"] or p_cfg["scrape_limit"]
        max_accs = row["max_accounts"] or p_cfg["max_accounts"]
        max_tpls = row["max_templates"] or p_cfg.get("max_templates", 1)
        ai_kw_cap = min(100, max_kws)
    
    return {
        "status": "success",
        "plan": active_plan,
        "is_unlimited": is_unlimited,
        "original_plan": row["original_plan"] or "free",
        "active_coupon_code": row["active_coupon_code"],
        "coupon_expires_at": row["coupon_expires_at"],
        "username": row["username"], 
        "email": row["email"] or f"{row['username']}@arkitel.app",
        "country": row["country"] if ("country" in row.keys() and row["country"]) else "Nigeria",
        "scrape_limit": scrape_lim,
        "max_accounts": max_accs,
        "max_daily_campaigns": max_camps,
        "daily_campaign_count": row["daily_campaign_count"] or 0,
        "max_daily_keywords": max_kws,
        "daily_keyword_count": row["daily_keyword_count"] or 0,
        "max_templates": max_tpls,
        "template_count": t_count,
        "is_approved": row["is_approved"],
        "has_proof": bool(row["payment_proof"]),
        "plan_expires_at": row["plan_expires_at"],
        "allowed_speeds": allowed_speeds,
        "has_spintax": is_unlimited or active_plan != "free",
        "has_fleet_compare": is_unlimited or active_plan != "free",
        "can_export_csv": True,
        "ai_keyword_cap": ai_kw_cap
    }

@app.post("/api/monetization/apply-coupon")
async def apply_coupon(code: str, user_id: str = Depends(get_current_user_id)):
    code = code.strip().upper()
    conn = get_db_connection()
    try:
        coupon = conn.execute("SELECT * FROM coupons WHERE UPPER(code) = ? AND is_active = 1", (code,)).fetchone()
        if not coupon:
            raise HTTPException(status_code=400, detail="Invalid or expired coupon code. Please check and try again.")
        
        user_row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        if not user_row:
            raise HTTPException(status_code=404, detail="User not found")
        
        orig_plan = "free"
        try:
            if user_row["original_plan"]:
                orig_plan = user_row["original_plan"]
            elif user_row["plan"] and user_row["plan"] != "premium":
                orig_plan = user_row["plan"]
        except Exception:
            orig_plan = user_row["plan"] or "free"

        price = int(coupon["price"] or 0)
        target_plan = "premium"
        try:
            if coupon["target_plan"]:
                target_plan = coupon["target_plan"].lower()
        except Exception:
            pass

        duration_hours = 0
        try:
            if coupon["duration_hours"]:
                duration_hours = int(coupon["duration_hours"])
        except Exception:
            pass

        expires_at_str = None
        if duration_hours > 0:
            expires_at = datetime.now(timezone.utc) + timedelta(hours=duration_hours)
            expires_at_str = expires_at.isoformat()

        def _safe_val(c_row, key):
            try:
                v = c_row[key]
                return int(v) if v is not None and v != "" else None
            except Exception:
                return None

        c_camps = _safe_val(coupon, "max_daily_campaigns")
        c_kws = _safe_val(coupon, "max_daily_keywords")
        c_scrape = _safe_val(coupon, "scrape_limit")
        c_accs = _safe_val(coupon, "max_accounts")
        c_tpls = _safe_val(coupon, "max_templates")

        if price == 0:
            # FREE access code - unlock plan immediately with coupon's custom allowances
            conn.execute(
                """UPDATE users SET 
                   original_plan = COALESCE(original_plan, ?),
                   plan = ?,
                   active_coupon_code = ?,
                   coupon_expires_at = ?,
                   max_daily_campaigns = ?,
                   max_daily_keywords = ?,
                   scrape_limit = ?,
                   max_accounts = ?,
                   max_templates = ?,
                   is_approved = 1
                   WHERE id = ?""",
                (orig_plan, target_plan, code, expires_at_str, c_camps, c_kws, c_scrape, c_accs, c_tpls, user_id)
            )
            # Deactivate single-use coupon if duration is 0
            if duration_hours == 0:
                conn.execute("UPDATE coupons SET is_active = 0 WHERE UPPER(code) = ?", (code,))
            if hasattr(conn, "commit"): conn.commit()
            
            dur_msg = f" (Access active for {duration_hours} hours)" if duration_hours > 0 else ""
            return {
                "status": "success", 
                "message": f"{target_plan.capitalize()} plan privileges activated successfully!{dur_msg}", 
                "discount": "free",
                "plan": target_plan,
                "coupon_expires_at": expires_at_str
            }
        else:
            # Discount code - deactivate so it can't be reused, user still needs to pay 'price'
            conn.execute("UPDATE coupons SET is_active = 0 WHERE UPPER(code) = ?", (code,))
            if hasattr(conn, "commit"): conn.commit()
            return {"status": "success", "message": f"Coupon applied! Pay only ₦{price:,} instead.", "discount": "partial", "new_price": price}
    finally:
        if hasattr(conn, "close"): conn.close()

class PaymentProofRequest(BaseModel):
    proof_details: str
    amount: int

@app.post("/api/monetization/submit-proof")
async def submit_proof(req: PaymentProofRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        # Update user record with proof string
        conn.execute("UPDATE users SET payment_proof = ? WHERE id = ?", (req.proof_details, user_id))
        # Record in payments table
        conn.execute(
            "INSERT INTO payments (user_id, amount, proof_details, status) VALUES (?, ?, ?, ?)",
            (user_id, req.amount, req.proof_details, 'pending')
        )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "message": "Proof submitted. Admin will verify shortly."}
    finally:
        if hasattr(conn, "close"): conn.close()

class PaymentSubmissionRequest(BaseModel):
    plan_key: str
    method: str  # 'bank' or 'crypto'
    currency: str  # 'NGN', 'USD', 'GBP', 'EUR', 'USDT', 'BTC', 'SOL'
    amount: float
    proof_details: Optional[str] = ""

@app.post("/api/monetization/submit-payment")
async def submit_payment(req: PaymentSubmissionRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        user_row = conn.execute("SELECT username, email, country FROM users WHERE id = ?", (user_id,)).fetchone()
        uname = user_row["username"] if user_row else "Unknown"
        email_addr = (user_row["email"] if user_row and user_row["email"] else uname) or f"{uname}@arkitel.app"

        proof_summary = f"[{req.method.upper()}-{req.currency}] {req.amount} | {req.proof_details or 'WhatsApp DM Verification'}"
        
        # Save to users table so UI status has_proof becomes true
        conn.execute("UPDATE users SET payment_proof = ? WHERE id = ?", (proof_summary, user_id))
        
        # Save to payment_requests table
        conn.execute(
            """INSERT INTO payment_requests 
               (user_id, username, email, plan_key, method, currency, amount, proof_details, status) 
               VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'pending')""",
            (user_id, uname, email_addr, req.plan_key, req.method, req.currency, req.amount, req.proof_details or "WhatsApp DM Proof")
        )
        if hasattr(conn, "commit"): conn.commit()
        return {
            "status": "success", 
            "message": "Payment recorded. Please proceed to WhatsApp DM to send your screenshot."
        }
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/admin/monetization/payment-requests")
async def get_payment_requests(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        rows = conn.execute("SELECT * FROM payment_requests ORDER BY id DESC LIMIT 100").fetchall()
        return {"requests": [dict(r) for r in rows]}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/monetization/payment-requests/{req_id}/approve")
async def approve_payment_request(req_id: int, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        pay_req = conn.execute("SELECT * FROM payment_requests WHERE id = ?", (req_id,)).fetchone()
        if not pay_req:
            raise HTTPException(status_code=404, detail="Payment request not found")
        
        uid = pay_req["user_id"]
        plan_key = pay_req["plan_key"]
        
        # Plan config lookup
        plan_row = conn.execute("SELECT * FROM plans WHERE key = ?", (plan_key,)).fetchone()
        DEFAULTS = {
            "free":      {"scrape_limit": 50,   "max_accounts": 1,  "max_daily_campaigns": 20,  "max_daily_keywords": 5,   "max_templates": 1,   "price": 0},
            "basic":     {"scrape_limit": 100,  "max_accounts": 2,  "max_daily_campaigns": 50,  "max_daily_keywords": 15,  "max_templates": 2,   "price": 2000},
            "standard":  {"scrape_limit": 250,  "max_accounts": 5,  "max_daily_campaigns": 150, "max_daily_keywords": 30,  "max_templates": 5,   "price": 3500},
            "premium":   {"scrape_limit": 500,  "max_accounts": 10, "max_daily_campaigns": 300, "max_daily_keywords": 50,  "max_templates": 10,  "price": 5000},
            "unlimited": {"scrape_limit": 999999, "max_accounts": 999999, "max_daily_campaigns": 999999, "max_daily_keywords": 999999, "max_templates": 999999, "price": 0},
        }
        plan_cfg = dict(plan_row) if plan_row else DEFAULTS.get(plan_key, DEFAULTS["basic"])

        now = datetime.now()
        act_date = now.strftime('%Y-%m-%dT%H:%M')
        exp_date = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')

        # Update user record
        conn.execute(
            """UPDATE users SET 
               plan = ?, is_approved = 1,
               scrape_limit = ?, max_accounts = ?, 
               max_daily_campaigns = ?, max_daily_keywords = ?, 
               max_templates = ?,
               plan_activated_at = ?, plan_expires_at = ?
               WHERE id = ?""",
            (plan_key, plan_cfg["scrape_limit"], plan_cfg["max_accounts"],
             plan_cfg["max_daily_campaigns"], plan_cfg["max_daily_keywords"],
             plan_cfg.get("max_templates", 1), act_date, exp_date, uid)
        )

        # Update payment_request status
        conn.execute(
            "UPDATE payment_requests SET status = 'approved', verified_at = ? WHERE id = ?",
            (act_date, req_id)
        )
        if hasattr(conn, "commit"): conn.commit()

        # Send Email Notification (non-blocking)
        user_row = conn.execute("SELECT username, email FROM users WHERE id = ?", (uid,)).fetchone()
        if user_row:
            to_email = user_row["email"] or user_row["username"]
            send_plan_activation_email_async(to_email, user_row["username"], plan_key, exp_date)

        return {"status": "success", "message": f"Payment #{req_id} approved and user upgraded to {plan_key}!"}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/system/settings")
async def get_system_settings():
    conn = get_db_connection()
    try:
        rows = conn.execute("SELECT key, value FROM system_settings").fetchall()
        settings = {r["key"]: r["value"] for r in rows}
        return {
            "whatsapp_number": settings.get("whatsapp_number", "https://wa.link/p0uv2l"),
            "support_email": settings.get("support_email", "arkilostudios@gmail.com"),
            "telegram_support": settings.get("telegram_support", "arkitel_support")
        }
    finally:
        if hasattr(conn, "close"): conn.close()

class UpdateSystemSettingsRequest(BaseModel):
    whatsapp_number: Optional[str] = None
    support_email: Optional[str] = None
    telegram_support: Optional[str] = None

@app.post("/api/admin/system/settings")
async def update_system_settings(req: UpdateSystemSettingsRequest, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        if req.whatsapp_number:
            conn.execute("INSERT OR REPLACE INTO system_settings (key, value) VALUES ('whatsapp_number', ?)", (req.whatsapp_number.strip(),))
        if req.support_email:
            conn.execute("INSERT OR REPLACE INTO system_settings (key, value) VALUES ('support_email', ?)", (req.support_email.strip(),))
        if req.telegram_support:
            conn.execute("INSERT OR REPLACE INTO system_settings (key, value) VALUES ('telegram_support', ?)", (req.telegram_support.strip(),))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "message": "Support settings updated successfully"}
    finally:
        if hasattr(conn, "close"): conn.close()

class PaystackVerifyRequest(BaseModel):
    reference: str
    plan_key: str

@app.post("/api/monetization/verify-paystack")
async def verify_paystack(req: PaystackVerifyRequest, user_id: str = Depends(get_current_user_id)):
    """Verifies a Paystack transaction and upgrades the user automatically."""
    secret_key = os.getenv("PAYSTACK_SECRET_KEY")
    if not secret_key:
        raise HTTPException(status_code=500, detail="Payment gateway not configured.")

    async with httpx.AsyncClient() as client:
        try:
            headers = {"Authorization": f"Bearer {secret_key}"}
            response = await client.get(f"https://api.paystack.co/transaction/verify/{req.reference}", headers=headers)
            res_data = response.json()

            if response.status_code == 200 and res_data.get("status") and res_data["data"]["status"] == "success":
                # Payment confirmed! Upgrade user.
                plan_key = req.plan_key
                plan_cfg = get_plan_configs().get(plan_key, get_plan_configs()["basic"])
                
                # Subscription timing
                now = datetime.now()
                act_date = now.strftime('%Y-%m-%dT%H:%M')
                exp_date = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')

                conn = get_db_connection()
                try:
                    conn.execute(
                        """UPDATE users SET 
                           plan = ?, is_approved = 1, payment_proof = ?, 
                           scrape_limit = NULL, max_accounts = NULL, 
                           max_daily_campaigns = NULL, max_daily_keywords = NULL,
                           plan_activated_at = ?, plan_expires_at = ?
                           WHERE id = ?""",
                        (plan_key, f"Paystack:{req.reference}", act_date, exp_date, user_id)
                    )
                    conn.execute(
                        "INSERT INTO payments (user_id, amount, proof_details, status) VALUES (?, ?, ?, ?)",
                        (user_id, res_data["data"]["amount"] / 100, f"Paystack:{req.reference}", 'completed')
                    )
                    if hasattr(conn, "commit"): conn.commit()

                    # Send Plan Activation Email (non-blocking)
                    u_row = conn.execute("SELECT username, email FROM users WHERE id = ?", (user_id,)).fetchone()
                    if u_row:
                        target_mail = u_row["email"] or u_row["username"]
                        send_plan_activation_email_async(target_mail, u_row["username"], plan_key, exp_date)
                finally:
                    if hasattr(conn, "close"): conn.close()
                
                return {"status": "success", "message": f"Upgrade to {plan_key} successful!"}
            else:
                raise HTTPException(status_code=400, detail="Payment verification failed.")
        except Exception as e:
            log_debug(f"Paystack Verify Error: {str(e)}")
            raise HTTPException(status_code=500, detail="Verification failed.")

@app.post("/api/payments/webhook")
async def paystack_webhook(request: Request):
    """Reliability backup for Paystack payments."""
    # Note: In production you should verify the x-paystack-signature header
    payload = await request.json()
    if payload.get("event") == "charge.success":
        data = payload["data"]
        ref = data["reference"]
        cust_email = data["customer"]["email"]
        amount = data["amount"] / 100
        
        # Determine plan from amount or metadata
        plan_key = "basic"
        if amount >= 5000: plan_key = "premium"
        elif amount >= 3500: plan_key = "standard"
        
        # Find user by email (mapping email to username if needed)
        conn = get_db_connection()
        try:
            # We assume username or a stored email matches
            user = conn.execute("SELECT id, username, email FROM users WHERE username = ? OR id = ? OR email = ?", (cust_email, data.get("metadata", {}).get("user_id"), cust_email)).fetchone()
            if user:
                u_id = user["id"]
                now = datetime.now()
                act_date = now.strftime('%Y-%m-%dT%H:%M')
                exp_date = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')
                
                conn.execute(
                    """UPDATE users SET plan = ?, is_approved = 1, payment_proof = ?,
                       plan_activated_at = ?, plan_expires_at = ?
                       WHERE id = ? AND (plan = 'free' OR plan IS NULL)""",
                    (plan_key, f"Webhook:{ref}", act_date, exp_date, u_id)
                )
                if hasattr(conn, "commit"): conn.commit()

                # Dispatch Plan Activation Email (non-blocking)
                target_email = user["email"] or cust_email
                send_plan_activation_email_async(target_email, user["username"], plan_key, exp_date)
        finally:
            if hasattr(conn, "close"): conn.close()
            
    return {"status": "success"}

@app.delete("/api/admin/broadcast/{broadcast_id}")
async def delete_broadcast(broadcast_id: int, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute("DELETE FROM broadcasts WHERE id = ?", (broadcast_id,))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

class SaveGroupRequest(BaseModel):
    id: str
    title: str
    username: Optional[str] = None
    participants_count: int = 0
    type: str
    is_private: bool = False
    country: str = "Global"
    user_shows: int = 1
    global_shows: int = 1
    source: str = "manual"
    can_send_messages: Optional[bool] = None

@app.post("/api/telegram/leads/groups/bulk-save")
async def bulk_save_groups(groups: List[SaveGroupRequest], user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    for g in groups:
        can_send_val = 1 if (getattr(g, 'can_send_messages', None) is not False and g.type != 'channel') else 0
        conn.execute(
            """INSERT OR REPLACE INTO scraped_groups 
               (id, user_id, title, username, participants_count, type, is_private, country, user_shows, global_shows, source, can_send_messages) 
               VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
            (g.id, user_id, g.title, g.username, g.participants_count, g.type, 1 if g.is_private else 0, g.country, g.user_shows, g.global_shows, g.source, can_send_val)
        )
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "count": len(groups)}

@app.get("/api/telegram/leads/groups")
async def get_saved_groups(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    # Premium check
    user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
    plan = user_row["plan"] if user_row else "free"
    plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
    
    if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
        if hasattr(conn, "close"): conn.close()
        return {"groups": [], "locked": True}

    rows = conn.execute("SELECT * FROM scraped_groups WHERE user_id = ? ORDER BY created_at DESC", (user_id,)).fetchall()
    if hasattr(conn, "close"): conn.close()
    
    return {
        "groups": [
            {
                "id": r["id"],
                "title": r["title"],
                "username": r["username"],
                "participants_count": r["participants_count"],
                "type": r["type"],
                "is_private": bool(r["is_private"]),
                "country": r["country"],
                "user_shows": r["user_shows"],
                "global_shows": r["global_shows"],
                "source": r["source"],
                "can_send_messages": bool(r["can_send_messages"]) if (hasattr(r, "keys") and "can_send_messages" in r.keys()) else (r["type"] != 'channel'),
                "created_at": r["created_at"]
            } for r in rows
        ]
    }

@app.get("/api/telegram/leads/joined-status")
async def get_joined_status(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    rows = conn.execute("SELECT group_id FROM account_joins WHERE user_id = ?", (user_id,)).fetchall()
    if hasattr(conn, "close"): conn.close()
    return {"joined_ids": [r[0] for r in rows]}

@app.delete("/api/telegram/leads/groups/{group_id}")
async def delete_saved_group(group_id: str, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    conn.execute("DELETE FROM scraped_groups WHERE id = ? AND user_id = ?", (group_id, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

@app.delete("/api/telegram/campaigns/{task_id}")
async def delete_campaign(task_id: int, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    row = conn.execute("SELECT id FROM tasks WHERE id = ? AND user_id = ?", (task_id, user_id)).fetchone()
    if not row:
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=404, detail="Campaign not found")
        
    conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "message": "Campaign deleted successfully"}

class CampaignEditRequest(BaseModel):
    name: Optional[str] = None
    message_text: Optional[str] = None
    scheduled_time: Optional[str] = None
    target_groups: Optional[List[str]] = None
    exclude_groups: Optional[List[str]] = None
    interval_hours: Optional[int] = None
    interval_minutes: Optional[int] = None
    phone_number: Optional[str] = None
    media_url: Optional[str] = None
    speed_mode: Optional[str] = None

@app.put("/api/telegram/campaigns/{task_id}")
async def edit_campaign(task_id: int, req: CampaignEditRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    task = conn.execute("SELECT id FROM tasks WHERE id = ? AND user_id = ?", (task_id, user_id)).fetchone()
    if not task:
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=404, detail="Campaign not found")
    
    updates = []
    params: List[Any] = []
    if req.name is not None:
        updates.append("name = ?")
        params.append(req.name)
    if req.message_text is not None:
        updates.append("message_text = ?")
        params.append(req.message_text)
    if req.scheduled_time is not None:
        updates.append("scheduled_time = ?")
        params.append(req.scheduled_time)
    if req.target_groups is not None:
        updates.append("target_groups = ?")
        updates.append("total_targets = ?")
        params.append(json.dumps(req.target_groups))
        params.append(len(cast(list, req.target_groups)))
    if req.interval_hours is not None:
        updates.append("interval_hours = ?")
        params.append(req.interval_hours)
    if req.interval_minutes is not None:
        updates.append("interval_minutes = ?")
        params.append(req.interval_minutes)
    if req.phone_number is not None:
        updates.append("phone_number = ?")
        params.append(req.phone_number)
    if req.exclude_groups is not None:
        updates.append("exclude_groups = ?")
        params.append(json.dumps(req.exclude_groups))
    if req.media_url is not None:
        updates.append("media_url = ?")
        params.append(req.media_url)
    if req.speed_mode is not None:
        updates.append("speed_mode = ?")
        params.append(req.speed_mode)
    
    if updates:
        # Reset to pending on edit so it re-runs
        updates.append("status = 'pending'")
        updates.append("failed_groups = '[]'")
        updates.append("sent_count = 0")
        
        params.append(task_id)
        params.append(user_id)
        conn.execute(f"UPDATE tasks SET {', '.join(updates)} WHERE id = ? AND user_id = ?", tuple(params))
        if hasattr(conn, "commit"): conn.commit()
    
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}


# Redundant /api/telegram/extract removed (single canonical handler at line 2722)

# Redundant endpoint removed (get_member_leads was a duplicate of get_saved_members)

@app.delete("/api/telegram/leads/members/{member_id}")
async def delete_member_lead(member_id: int, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    conn.execute("DELETE FROM leads WHERE id = ? AND user_id = ?", (member_id, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

class BulkDeleteRequest(BaseModel):
    ids: List[Union[str, int]]

@app.post("/api/telegram/leads/groups/bulk-delete")
async def bulk_delete_groups(req: BulkDeleteRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    placeholders = ', '.join(['?'] * len(req.ids))
    conn.execute(f"DELETE FROM scraped_groups WHERE user_id = ? AND id IN ({placeholders})", (user_id, *req.ids))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

@app.post("/api/telegram/leads/members/bulk-delete")
async def bulk_delete_members(req: BulkDeleteRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    placeholders = ', '.join(['?'] * len(req.ids))
    conn.execute(f"DELETE FROM leads WHERE user_id = ? AND id IN ({placeholders})", (user_id, *req.ids))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}



# --- TEMPLATES ENDPOINTS ---
@app.get("/api/telegram/templates")
async def get_templates(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        rows = conn.execute("SELECT id, name, content, media_url FROM templates WHERE user_id = ? ORDER BY created_at DESC", (user_id,)).fetchall()
        templates_list = []
        for r in rows:
            def _val(col_name, idx, default=None):
                if hasattr(r, "get"):
                    v = r.get(col_name)
                    if v is not None:
                        return v
                try:
                    if len(r) > idx and r[idx] is not None:
                        return r[idx]
                except:
                    pass
                return default

            templates_list.append(dict(
                id=_val("id", 0), 
                name=_val("name", 1, "Untitled Template"), 
                content=_val("content", 2, ""), 
                media_url=_val("media_url", 3, None)
            ))
        return {"templates": templates_list}
    except Exception as e:
        print(f"TEMPLATES: Error in get_templates: {e}")
        return {"templates": []}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/telegram/templates")
async def create_template(req: TemplateRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        # Check limits
        u_row = conn.execute("SELECT plan, max_templates FROM users WHERE id = ?", (user_id,)).fetchone()
        plan = u_row["plan"] if u_row else "free"
        plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
        max_templates = (u_row["max_templates"] if u_row and u_row["max_templates"] is not None else None) or plan_cfg.get("max_templates", 1)
        
        current_count = conn.execute("SELECT COUNT(*) FROM templates WHERE user_id = ?", (user_id,)).fetchone()[0]
        if plan != "unlimited" and current_count >= max_templates:
            raise HTTPException(status_code=403, detail=f"Your plan allows a maximum of {max_templates} template(s). Upgrade to save more templates.")

        conn.execute("INSERT INTO templates (user_id, name, content, media_url) VALUES (?, ?, ?, ?)", (user_id, req.name, req.content, req.media_url))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success"}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.put("/api/telegram/templates/{template_id}")
async def update_template(template_id: int, req: TemplateRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        conn.execute("UPDATE templates SET name = ?, content = ?, media_url = ? WHERE id = ? AND user_id = ?", (req.name, req.content, req.media_url, template_id, user_id))
        if hasattr(conn, "commit"): conn.commit()
    finally:
        if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

@app.get("/api/admin/templates")
async def admin_get_all_templates(admin_id: str = Depends(get_current_admin)):
    """Allows admin to see all templates from all users."""
    conn = get_db_connection()
    rows = conn.execute("""
        SELECT t.*, u.username as creator 
        FROM templates t 
        JOIN users u ON t.user_id = u.id 
        ORDER BY t.created_at DESC
    """).fetchall()
    if hasattr(conn, "close"): conn.close()
    return {"templates": [dict(r) for r in rows]}

@app.delete("/api/telegram/templates/{template_id}")
async def delete_template(template_id: int, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    conn.execute("DELETE FROM templates WHERE id = ? AND user_id = ?", (template_id, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

# --- PERMANENT EXCLUDES ---
@app.get("/api/users/permanent-excludes")
async def get_permanent_excludes(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    row = conn.execute("SELECT permanent_excludes FROM users WHERE id = ?", (user_id,)).fetchone()
    if hasattr(conn, "close"): conn.close()
    if row:
        return {"ids": json.loads(row[0] or "[]")}
    return {"ids": []}

@app.post("/api/users/permanent-excludes")
async def update_permanent_excludes(req: PermanentExcludeRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    conn.execute("UPDATE users SET permanent_excludes = ? WHERE id = ?", (json.dumps(req.ids), user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

class AddExcludeRequest(BaseModel):
    ids: List[str]

class RemoveExcludeRequest(BaseModel):
    id: str

@app.post("/api/users/permanent-excludes/add")
async def add_permanent_exclude(req: AddExcludeRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        row = conn.execute("SELECT permanent_excludes FROM users WHERE id = ?", (user_id,)).fetchone()
        current_ids = json.loads(row[0] or "[]") if row and row[0] else []
        for new_id in req.ids:
            clean = new_id.strip()
            if clean and clean not in current_ids:
                current_ids.append(clean)
        conn.execute("UPDATE users SET permanent_excludes = ? WHERE id = ?", (json.dumps(current_ids), user_id))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "ids": current_ids}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/users/permanent-excludes/remove")
async def remove_permanent_exclude(req: RemoveExcludeRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        row = conn.execute("SELECT permanent_excludes FROM users WHERE id = ?", (user_id,)).fetchone()
        current_ids = json.loads(row[0] or "[]") if row and row[0] else []
        new_ids = [i for i in current_ids if str(i).strip() != req.id.strip()]
        conn.execute("UPDATE users SET permanent_excludes = ? WHERE id = ?", (json.dumps(new_ids), user_id))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "ids": new_ids}
    finally:
        if hasattr(conn, "close"): conn.close()

# Removed redundant get_accounts (already refactored earlier)

@app.post("/api/telegram/campaigns/{task_id}/toggle-pause")
async def toggle_campaign_pause(task_id: int, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        row = conn.execute("SELECT user_id, status FROM tasks WHERE id = ?", (task_id,)).fetchone()
        if not row:
            raise HTTPException(status_code=404, detail="Task not found")
        if row["user_id"] != user_id and user_id != "admin_virtual_id":
             raise HTTPException(status_code=403, detail="Unauthorized")
        
        current_status = row["status"]
        if current_status == "processing":
            new_status = 'paused'
        elif current_status == "paused":
            new_status = 'pending'
        else:
            raise HTTPException(status_code=400, detail="Campaign can only be paused while processing, or resumed while paused.")
            
        conn.execute("UPDATE tasks SET status = ? WHERE id = ?", (new_status, task_id))
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "new_status": new_status}
    finally:
        if hasattr(conn, "close"): conn.close()


# --- MEDIA UPLOAD ENDPOINT ---
@app.post("/api/upload")
async def upload_media_file(file: UploadFile = File(...), user_id: str = Depends(get_current_user_id)):
    os.makedirs(_UPLOADS_DIR, exist_ok=True)
    ext = os.path.splitext(file.filename or "")[1].lower()
    safe_name = f"{uuid.uuid4().hex}{ext}"
    dest_path = os.path.join(_UPLOADS_DIR, safe_name)
    with open(dest_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    # Also mirror to root static dir if distinct
    try:
        root_uploads = os.path.join(_ROOT_STATIC_DIR, "uploads")
        if os.path.abspath(_UPLOADS_DIR) != os.path.abspath(root_uploads):
            os.makedirs(root_uploads, exist_ok=True)
            shutil.copy2(dest_path, os.path.join(root_uploads, safe_name))
    except Exception as e:
        log_debug(f"UPLOAD: Root mirror failed: {e}")

    return {"status": "success", "url": f"/static/uploads/{safe_name}", "filename": file.filename}

@app.get("/api/telegram/campaigns")
async def get_campaigns(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        tasks = conn.execute(
            "SELECT id, phone_number, scheduled_time, message_text, target_groups, status, interval_hours, interval_minutes, total_targets, sent_count, batch_number, failed_groups, exclude_groups, media_url, name, speed_mode, sweep_status, deleted_groups FROM tasks WHERE user_id = ? ORDER BY scheduled_time DESC", 
            (user_id,)
        ).fetchall()
        
        campaigns_list = []
        for t in tasks:
            def _val(col_name, idx, default=None):
                if hasattr(t, "get"):
                    v = t.get(col_name)
                    if v is not None:
                        return v
                try:
                    if len(t) > idx and t[idx] is not None:
                        return t[idx]
                except:
                    pass
                return default

            del_groups_raw = _val("deleted_groups", 17, "[]")
            del_groups = []
            try:
                del_groups = json.loads(del_groups_raw) if isinstance(del_groups_raw, str) else (del_groups_raw or [])
            except Exception:
                del_groups = []

            campaigns_list.append(dict(
                id=_val("id", 0), 
                phone_number=_val("phone_number", 1, ""), 
                scheduled_time=_val("scheduled_time", 2, ""), 
                message=_val("message_text", 3, ""), 
                message_text=_val("message_text", 3, ""),
                groups=_val("target_groups", 4, "[]"), 
                status=_val("status", 5, "pending"), 
                interval_hours=_val("interval_hours", 6, 0), 
                interval_minutes=_val("interval_minutes", 7, 0), 
                total_targets=_val("total_targets", 8, 0), 
                sent_count=_val("sent_count", 9, 0), 
                batch_number=_val("batch_number", 10, 1), 
                failed_groups=_val("failed_groups", 11, "[]"), 
                exclude_groups=_val("exclude_groups", 12, "[]"),
                media_url=_val("media_url", 13, None),
                name=_val("name", 14) or "Broadcast Campaign",
                speed_mode=_val("speed_mode", 15, "balanced") or "balanced",
                sweep_status=_val("sweep_status", 16, "idle") or "idle",
                deleted_groups=del_groups
            ))
        return {"campaigns": campaigns_list}
    except Exception as e:
        print(f"CAMPAIGNS: Error in get_campaigns: {e}")
        return {"campaigns": []}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/telegram/campaigns")
async def create_campaign(req: CampaignRequest, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    # Plan Limit Check
    if user_id != "admin_virtual_id":
        user_row = conn.execute("SELECT * FROM users WHERE id = ?", (user_id,)).fetchone()
        if not user_row:
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(status_code=404, detail="User not found")
        
        # Reset daily counts if needed
        check_and_reset_daily_limits(conn, user_id, user_row)
        
        plan = user_row["plan"] or "free"
        plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
        
        max_daily = int(user_row["max_daily_campaigns"] or plan_cfg["max_daily_campaigns"])
        current_daily = int(user_row["daily_campaign_count"] or 0)
        
        if current_daily >= max_daily and plan != "unlimited":
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(status_code=403, detail=f"Daily broadcast limit reached ({current_daily}/{max_daily}). Upgrade your plan to send more campaigns today.")

        # Sender Phone validation
        if not req.phone_number:
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(status_code=400, detail="Please select a sender phone account before deploying.")

        # Speed Mode Plan Gating
        camp_speed = (req.speed_mode or "balanced").lower()
        allowed_speeds = ["balanced"]
        if plan == "basic":
            allowed_speeds = ["balanced", "turbo"]
        elif plan == "standard":
            allowed_speeds = ["balanced", "turbo", "stealth"]
        elif plan in ["premium", "unlimited"]:
            allowed_speeds = ["balanced", "turbo", "stealth", "ultra"]
        
        if camp_speed not in allowed_speeds:
            req_plan = "Growth Basic" if camp_speed == "turbo" else ("Pro Standard" if camp_speed == "stealth" else "Enterprise Premium")
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(
                status_code=403, 
                detail=f"The '{camp_speed.capitalize()}' delivery speed is locked on your current plan. Upgrade to {req_plan} or higher to use this speed."
            )

        # Spintax Gating for Free Tier
        if plan == "free" and req.message and ("{" in req.message and "|" in req.message and "}" in req.message):
            if hasattr(conn, "close"): conn.close()
            raise HTTPException(
                status_code=403,
                detail="Dynamic Spintax rotation is a premium feature. Upgrade to Growth Basic or higher to deploy campaigns with Spintax syntax."
            )

    try:
        now_utc = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M')
        camp_name = req.name or "Broadcast Campaign"
        camp_groups = req.groups or []
        camp_exclude = req.exclude_groups or []
        camp_speed = (req.speed_mode or "balanced").lower()
        
        clean_time = req.schedule_time or now_utc
        if 'T' in clean_time:
            parts = clean_time.split(':')
            if len(parts) > 2:
                clean_time = f"{parts[0]}:{parts[1]}"
        print(f"CAMPAIGN: New campaign '{camp_name}' | Speed: {camp_speed} | Scheduled(UTC): {clean_time} | Server UTC now: {now_utc}")

        conn.execute(
            "INSERT INTO tasks (user_id, name, phone_number, scheduled_time, message_text, target_groups, status, interval_hours, interval_minutes, total_targets, sent_count, batch_number, failed_groups, exclude_groups, media_url, speed_mode, sweep_status, deleted_groups) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            (user_id, camp_name, req.phone_number, clean_time, req.message, str(camp_groups), "pending", req.interval_hours or 0, req.interval_minutes or 0, len(camp_groups), 0, 1, "[]", str(camp_exclude), req.media_url, camp_speed, "idle", "[]")
        )
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "message": f"Campaign '{camp_name}' scheduled for {clean_time} UTC with {camp_speed} delivery speed."}
    except Exception as e:
        print(f"CAMPAIGNS: Error in create_campaign: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/telegram/members")
async def get_members(group_id: str, phone_number: Optional[str] = None, user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    if phone_number:
        row = conn.execute(
            "SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?", 
            (phone_number, user_id)
        ).fetchone()
    else:
        row = conn.execute(
            "SELECT session_string, api_id, api_hash FROM accounts WHERE user_id = ? LIMIT 1", 
            (user_id,)
        ).fetchone()
    
    if hasattr(conn, "close"): conn.close()
    
    if not row:
        raise HTTPException(status_code=404, detail="No accounts found")
        
    session_str, api_id, api_hash = row[0], int(row[1]), row[2]
    client = TelegramClient(StringSession(session_str), api_id, api_hash)
    await client.connect()
    
    members = []
    try:
        entity = await client.get_entity(group_id)
        async for user in client.iter_participants(cast(Any, entity), limit=100):
            members.append({
                "id": user.id,
                "first_name": user.first_name,
                "last_name": user.last_name,
                "username": user.username,
                "is_bot": user.bot
            })
            
            # Save leads to DB for admin overview
            conn = get_db_connection()
            conn.execute(
                "INSERT INTO leads (user_id, group_id, username, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
                (user_id, group_id, user.username, user.first_name, user.last_name)
            )
            if hasattr(conn, "commit"): conn.commit()
            if hasattr(conn, "close"): conn.close()

        return {"members": members}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))
    finally:
        await client.disconnect()

@app.get("/api/telegram/leads")
async def get_leads(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    leads = conn.execute(
        "SELECT id, group_id, username, first_name, last_name, created_at FROM leads WHERE user_id = ? ORDER BY created_at DESC", 
        (user_id,)
    ).fetchall()
    if hasattr(conn, "close"): conn.close()
    
    return {
        "leads": [
            dict(id=l[0], group_id=l[1], username=l[2], first_name=l[3], last_name=l[4], created_at=l[5]) 
            for l in leads
        ]
    }

@app.get("/api/admin/keyword-logs")
async def get_keyword_logs(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    logs = conn.execute("SELECT * FROM keyword_logs ORDER BY created_at DESC LIMIT 500").fetchall()
    if hasattr(conn, "close"): conn.close()
    return {"logs": [dict(l) for l in logs]}

# --- Admin Endpoints ---

@app.post("/api/admin/maintenance/clear-keyword-logs")
async def clear_keyword_logs(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute("DELETE FROM keyword_logs")
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "message": "All keyword search history has been purged."}

@app.post("/api/admin/maintenance/clear-templates")
async def clear_templates(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute("DELETE FROM templates")
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "message": "All user templates have been purged."}

@app.post("/api/admin/maintenance/clear-leads")
async def clear_leads(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    
    # Thoroughly wipe all lead-related data
    conn.execute("DELETE FROM leads")
    conn.execute("DELETE FROM scraped_groups")
    conn.execute("DELETE FROM lead_members")
    conn.execute("DELETE FROM scrape_history")
    
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "message": "All database lead intelligence has been purged."}

@app.post("/api/admin/maintenance/clear-tasks")
async def clear_tasks(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    
    conn.execute("DELETE FROM tasks WHERE status IN ('completed', 'failed')")
    if hasattr(conn, "commit"): conn.commit()
    return {"status": "success", "message": "Task history purged"}

@app.post("/api/admin/users/{user_id}/toggle-status")
async def admin_toggle_user_status(user_id: str, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    user = conn.execute("SELECT is_active FROM users WHERE id = ?", (user_id,)).fetchone()
    if not user:
        if hasattr(conn, "close"): conn.close()
        raise HTTPException(status_code=404, detail="User not found")
    
    new_status = 0 if user[0] == 1 else 1
    conn.execute("UPDATE users SET is_active = ? WHERE id = ?", (new_status, user_id))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success", "is_active": bool(new_status)}

@app.get("/api/admin/users/{user_id}/details")
async def admin_get_user_details(user_id: str, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    user_row = conn.execute(
        "SELECT id, username, email, role, is_active, plan, created_at, plan_activated_at, plan_expires_at, scrape_limit, max_accounts FROM users WHERE id = ?", 
        (user_id,)
    ).fetchone()
    
    # Accounts with full profile details
    accounts = conn.execute(
        "SELECT id, phone_number, first_name, last_name, username, status, country, is_active, created_at FROM accounts WHERE user_id = ? ORDER BY is_active DESC, created_at DESC", 
        (user_id,)
    ).fetchall()
    
    # Scrape history (counts only)
    scrape_count = conn.execute("SELECT COUNT(*) FROM scrape_history WHERE user_id = ?", (user_id,)).fetchone()[0]
    # Campaign stats
    tasks = conn.execute("SELECT status, COUNT(*) FROM tasks WHERE user_id = ? GROUP BY status", (user_id,)).fetchall()
    
    if hasattr(conn, "close"): conn.close()
    
    user_dict = dict(user_row) if user_row else {}
    acc_list = [dict(a) for a in accounts]
    acc_count = len(acc_list)
    username_display = user_dict.get("username") or "This user"
    acc_plural = "account" if acc_count == 1 else "accounts"
    summary_sentence = f"{username_display} has signed up and has linked {acc_count} Telegram {acc_plural}."
    
    return {
        "user": user_dict,
        "accounts": acc_list,
        "total_accounts": acc_count,
        "summary_sentence": summary_sentence,
        "total_scrapes": scrape_count,
        "campaign_summary": dict(tasks)
    }

@app.get("/api/admin/users")
async def admin_get_users(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    users = conn.execute("""
        SELECT u.id, u.username, u.email, u.role, u.is_active, u.plan, COALESCE(u.created_at, '') as created_at,
               (SELECT COUNT(*) FROM accounts WHERE user_id = u.id) as accounts_count
        FROM users u
        ORDER BY COALESCE(u.created_at, '') DESC
    """).fetchall()
    if hasattr(conn, "close"): conn.close()
    return {"users": [dict(u) for u in users]}

# --- AI Suggested Keywords & Model Connect Endpoints ---

_openrouter_models_cache: Dict[str, Any] = {"models": [], "timestamp": 0.0}

META_FILTER_PHRASES = (
    "the user", "rule", "markdown", "preamble", "json array", "prompt", "telegram search",
    "duplication", "repetition", "i should", "way more than", "count them all", "constraint",
    "near-duplicate", "sub-niche", "discussion query", "here are", "example:", "buyer/discussion",
    "realistic term", "community title", "string or bytes", "output only", "unique and distinct",
    "absolute zero", "varies significantly", "structure significantly", "if i count"
)

def _clean_keyword_item(text: str) -> str:
    """Cleans a single keyword string of quotes, numbering, bullets, commas, brackets, and -unique tags."""
    if not text:
        return ""
    t = text.strip()
    # Remove surrounding quotes, brackets, braces, commas, semicolons
    t = t.strip('"\'`[]{},;:')
    # Remove leading numbering or bullets e.g. "1. ", "1) ", "- ", "* "
    t = re.sub(r'^(?:\d+[\.\)]|\-|\*|\•)\s*', '', t).strip()
    # Remove trailing/leading '-unique', '- unique', 'unique:', '(unique)'
    t = re.sub(r'(?i)\s*[-_—]\s*unique\b', '', t)
    t = re.sub(r'(?i)\bunique\s*[-_—:]\s*', '', t)
    t = re.sub(r'(?i)\s*\(\s*unique\s*\)', '', t)
    return t.strip().strip('"\'`[]{},;:')

def _is_valid_keyword(text: str) -> bool:
    """Verifies that the string is a genuine, high-quality search keyword and not thought process/rules."""
    if not text or not isinstance(text, str):
        return False
    t = text.strip()
    if len(t) < 2 or len(t) > 65:
        return False
    if "\n" in t or "\r" in t:
        return False
    t_lower = t.lower()
    for phrase in META_FILTER_PHRASES:
        if phrase in t_lower:
            return False
    if t.startswith(("{", "}", "[", "]", "```", "#", "http://", "https://")):
        return False
    return True

def extract_keywords_from_ai_text(text: Optional[str]) -> List[str]:
    """Robust parser that extracts clean, usable Telegram keywords from any model format without thinking monologues."""
    if not text or not isinstance(text, str):
        return []
    
    # 1. Strip <think>...</think> and <thought>...</thought> tags completely
    cleaned = re.sub(r'<think>[\s\S]*?</think>', '', text, flags=re.IGNORECASE)
    cleaned = re.sub(r'<thought>[\s\S]*?</thought>', '', cleaned, flags=re.IGNORECASE)
    cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
    cleaned = cleaned.rstrip('`').strip()

    results: List[str] = []
    seen = set()

    def add_item(val: Any):
        c = _clean_keyword_item(str(val))
        if _is_valid_keyword(c):
            norm = c.lower()
            if norm not in seen:
                seen.add(norm)
                results.append(c)

    # 2. Extract JSON arrays. Search candidate [...] blocks starting from the last one (final answer)
    start_indices = [i for i, ch in enumerate(cleaned) if ch == '[']
    for start in reversed(start_indices):
        end = cleaned.rfind(']', start)
        if end != -1:
            slice_str = cleaned[start:end + 1].strip()
            try:
                parsed = json.loads(slice_str)
                if isinstance(parsed, list):
                    for item in parsed:
                        add_item(item)
                    if len(results) >= 5:
                        return results
            except Exception:
                try:
                    fixed = re.sub(r',\s*\]', ']', slice_str)
                    parsed = json.loads(fixed)
                    if isinstance(parsed, list):
                        for item in parsed:
                            add_item(item)
                        if len(results) >= 5:
                            return results
                except Exception:
                    pass

    # 3. Extract all quoted strings from anywhere in the response
    quoted = re.findall(r'"([^"\\]*(?:\\.[^"\\]*)*)"', cleaned)
    for q in quoted:
        add_item(q.replace(r'\"', '"'))
    if len(results) >= 5:
        return results

    # 4. Fallback: Parse line-by-line or comma-by-comma
    for line in cleaned.splitlines():
        line_str = line.strip()
        if not line_str:
            continue
        # If line contains comma-separated terms and is not an unparsed JSON object
        if ',' in line_str and not line_str.startswith('{') and not line_str.startswith('['):
            for part in line_str.split(','):
                add_item(part)
        else:
            add_item(line_str)

    return results

@app.get("/api/ai/openrouter-models")
async def get_openrouter_models():
    """Fetches and caches the available OpenRouter models without requiring authentication."""
    now = time.time()
    if _openrouter_models_cache["models"] and (now - _openrouter_models_cache["timestamp"] < 3600):
        return {"models": _openrouter_models_cache["models"]}
    
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            res = await client.get(
                "https://openrouter.ai/api/v1/models",
                headers={
                    "User-Agent": "ArkiTel/1.0",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                }
            )
            if res.status_code == 200:
                raw_models = res.json().get("data", [])
                models = []
                priority_prefixes = [
                    "meta-llama/llama-3.3-70b-instruct",
                    "anthropic/claude-3.5-sonnet",
                    "deepseek/deepseek-chat",
                    "deepseek/deepseek-r1",
                    "openai/gpt-4o-mini",
                    "openai/gpt-4o",
                    "google/gemini-2.0-flash-exp:free",
                    "google/gemini-flash-1.5",
                    "mistralai/mistral-nemo",
                    "qwen/qwen-2.5-72b-instruct"
                ]
                
                for m in raw_models:
                    m_id = m.get("id")
                    if m_id:
                        p_raw = m.get("pricing") or {}
                        models.append({
                            "id": m_id,
                            "name": m.get("name") or m_id,
                            "context_length": m.get("context_length", 0),
                            "description": (m.get("description") or "")[:140],
                            "pricing": {
                                "prompt": str(p_raw.get("prompt", "0")),
                                "completion": str(p_raw.get("completion", "0")),
                                "request": str(p_raw.get("request", "0")),
                                "image": str(p_raw.get("image", "0"))
                            }
                        })
                
                def sort_key(item):
                    item_id = item["id"]
                    for idx, prefix in enumerate(priority_prefixes):
                        if item_id == prefix or item_id.startswith(prefix):
                            return (0, idx)
                    return (1, item["name"].lower())
                    
                models.sort(key=sort_key)
                _openrouter_models_cache["models"] = models
                _openrouter_models_cache["timestamp"] = now
                return {"models": models}
    except Exception as e:
        log_debug(f"Failed to fetch OpenRouter models: {e}")
    
    # Fallback default models if network error
    fallback = [
        {"id": "meta-llama/llama-3.3-70b-instruct", "name": "Meta: Llama 3.3 70B Instruct", "context_length": 131072, "pricing": {"prompt": "0.00000012", "completion": "0.0000003"}},
        {"id": "anthropic/claude-3.5-sonnet", "name": "Anthropic: Claude 3.5 Sonnet", "context_length": 200000, "pricing": {"prompt": "0.000003", "completion": "0.000015"}},
        {"id": "deepseek/deepseek-chat", "name": "DeepSeek: DeepSeek V3", "context_length": 64000, "pricing": {"prompt": "0.00000014", "completion": "0.00000028"}},
        {"id": "deepseek/deepseek-r1", "name": "DeepSeek: DeepSeek R1", "context_length": 64000, "pricing": {"prompt": "0.00000055", "completion": "0.00000219"}},
        {"id": "openai/gpt-4o-mini", "name": "OpenAI: GPT-4o Mini", "context_length": 128000, "pricing": {"prompt": "0.00000015", "completion": "0.0000006"}},
        {"id": "openai/gpt-4o", "name": "OpenAI: GPT-4o", "context_length": 128000, "pricing": {"prompt": "0.0000025", "completion": "0.00001"}},
        {"id": "google/gemini-2.0-flash-exp:free", "name": "Google: Gemini 2.0 Flash Exp (free)", "context_length": 1048576, "pricing": {"prompt": "0", "completion": "0"}},
        {"id": "google/gemini-flash-1.5", "name": "Google: Gemini Flash 1.5", "context_length": 1000000, "pricing": {"prompt": "0.000000075", "completion": "0.0000003"}},
        {"id": "mistralai/mistral-nemo", "name": "Mistral: Mistral Nemo", "context_length": 128000, "pricing": {"prompt": "0.00000013", "completion": "0.00000013"}},
        {"id": "qwen/qwen-2.5-72b-instruct", "name": "Qwen: Qwen 2.5 72B Instruct", "context_length": 131072, "pricing": {"prompt": "0.00000035", "completion": "0.0000004"}}
    ]
    return {"models": _openrouter_models_cache["models"] or fallback}

class AIValidateRequest(BaseModel):
    provider: Optional[str] = "openrouter"
    api_key: str
    model: Optional[str] = None

@app.post("/api/ai/validate")
async def validate_ai_credentials(req: AIValidateRequest, user_id: str = Depends(get_current_user_id)):
    provider = (req.provider or "openrouter").lower().strip()
    key = req.api_key.strip()
    if not key:
        raise HTTPException(status_code=400, detail="OpenRouter API key is required.")
        
    async with httpx.AsyncClient(timeout=15.0) as client:
        try:
            res = await client.get(
                "https://openrouter.ai/api/v1/auth/key",
                headers={"Authorization": f"Bearer {key}"}
            )
            if res.status_code == 200:
                data = res.json().get("data", {})
                label = data.get("label") or "OpenRouter Key"
                usage = data.get("usage", 0)
                extra = f" (Usage: ${usage:.2f})" if usage else ""
                chosen_model = (req.model or "").strip()
                model_note = f" • Model: {chosen_model}" if chosen_model else ""
                return {
                    "valid": True,
                    "provider": "openrouter",
                    "message": f"OpenRouter connected & verified! {label}{extra}{model_note}",
                    "key_info": data,
                    "model": chosen_model or None
                }
            else:
                return {"valid": False, "provider": "openrouter", "message": f"OpenRouter auth error ({res.status_code}): {res.text[:120]}"}
        except Exception as e:
            return {"valid": False, "provider": provider, "message": f"Connection error: {str(e)}"}

class AISuggestRequest(BaseModel):
    keyword: str
    provider: Optional[str] = "openrouter"
    api_key: Optional[str] = None
    model: Optional[str] = None
    count: Optional[int] = 100

@app.post("/api/ai/suggest-keywords")
async def suggest_ai_keywords(req: AISuggestRequest, user_id: str = Depends(get_current_user_id)):
    seed = (req.keyword or "").strip()
    if not seed:
        raise HTTPException(status_code=400, detail="Search keyword or niche is required.")
        
    provider = (req.provider or "openrouter").lower().strip()
    key = (req.api_key or "").strip()

    # Enforce tiered keyword limits:
    # Free: 5, Basic: 15, Standard: 30, Premium: 50, Unlimited: 100 (Full natural batch)
    conn_u = get_db_connection()
    u_row = conn_u.execute("SELECT plan, max_daily_keywords, role FROM users WHERE id = ?", (user_id,)).fetchone()
    if hasattr(conn_u, "close"): conn_u.close()
    
    user_plan = (u_row["plan"] if u_row and "plan" in u_row.keys() and u_row["plan"] else (u_row[0] if u_row and u_row[0] else "free")).lower().strip()
    user_role = (u_row["role"] if u_row and "role" in u_row.keys() and u_row["role"] else (u_row[2] if u_row and len(u_row) > 2 and u_row[2] else "user")).lower().strip()
    is_unlimited = user_plan in ("unlimited", "admin", "lifetime", "enterprise") or user_role == "admin"

    KEYWORD_LIMITS = {
        "free": 5,
        "basic": 15,
        "standard": 30,
        "premium": 50,
        "unlimited": 100
    }
    
    if is_unlimited:
        max_allowed = 100
        target_count = 100
    else:
        custom_keywords_cap = u_row["max_daily_keywords"] if u_row and "max_daily_keywords" in u_row.keys() else (u_row[1] if u_row and len(u_row) > 1 else None)
        max_allowed = int(custom_keywords_cap) if (custom_keywords_cap and int(custom_keywords_cap) > 0) else KEYWORD_LIMITS.get(user_plan, 5)
        target_count = min(max_allowed, max(5, req.count or max_allowed))
    selected_model = (req.model or "").strip() or "meta-llama/llama-3.3-70b-instruct"
    
    if not key:
        raise HTTPException(
            status_code=400,
            detail="No OpenRouter API key configured. Please click 'AI Provider' to connect your OpenRouter key first."
        )

    ai_keywords: List[str] = []
    error_note = None

    async with httpx.AsyncClient(timeout=60.0) as client:
        prompt = (
            f"You are a Telegram search & community intelligence specialist.\n"
            f"Generate exactly {target_count} unique, high-traffic, natural Telegram search keywords and channel queries for the niche: '{seed}'.\n\n"
            f"STRICT FORMAT RULES:\n"
            f"1. Return ONLY a single raw JSON array of clean keyword strings: [\"keyword 1\", \"keyword 2\", ...].\n"
            f"2. Every item must be 2 to 45 characters long, representing realistic search queries active users type on Telegram.\n"
            f"3. Do NOT include ANY thought process, reasoning, explanations, numbering, rules, or text outside the JSON array.\n"
            f"4. Do NOT append '-unique' or any metadata tags to the keywords.\n"
            f"5. Start your output directly with '[' and end with ']'."
        )
        
        try:
            payload: Dict[str, Any] = {
                "model": selected_model,
                "messages": [
                    {"role": "user", "content": prompt}
                ],
                "max_tokens": 4000,
                "include_reasoning": False
            }
            # Only pass temperature for non-reasoning models
            if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
                payload["temperature"] = 0.7

            res = await client.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                },
                json=payload
            )
            if res.status_code == 200:
                data = res.json()
                choices = data.get("choices") or []
                if choices:
                    c = choices[0]
                    msg = c.get("message") or {}
                    raw_content = msg.get("content")
                    if not raw_content:
                        raw_content = msg.get("reasoning") or msg.get("reasoning_content") or c.get("text")
                    
                    if not raw_content:
                        if msg.get("refusal"):
                            error_note = f"Model refusal: {msg.get('refusal')}"
                        elif c.get("finish_reason") == "length":
                            error_note = "Model reached token limit during generation. Please try another model or request fewer keywords."
                        else:
                            error_note = f"Model returned empty content (finish_reason: {c.get('finish_reason', 'unknown')}). Please choose another model."
                    else:
                        ai_keywords = extract_keywords_from_ai_text(str(raw_content))
                        if not ai_keywords:
                            error_note = "Could not parse keyword array from model response. Please try another model."
                else:
                    err_info = data.get("error") or {}
                    err_msg = err_info.get("message") if isinstance(err_info, dict) else str(err_info)
                    error_note = f"OpenRouter returned empty choices: {err_msg or 'Unknown error'}"
            else:
                try:
                    err_json = res.json()
                    err_msg = err_json.get("error", {}).get("message") or res.text[:120]
                except Exception:
                    err_msg = res.text[:120]
                is_rate_limit = (res.status_code in (429, 402)) or any(w in str(err_msg).lower() for w in ("rate limit", "quota", "credit", "free tier", "limit exceeded"))
                if is_rate_limit:
                    error_note = f"FREE_MODEL_LIMIT: The model '{selected_model}' has reached OpenRouter's free tier rate limit or daily quota. Please select another free model or add credits."
                else:
                    error_note = f"OpenRouter error ({res.status_code}): {err_msg}"
        except Exception as e:
            error_note = f"AI request exception: {str(e)}"

    # Clean and strictly deduplicate AI returned strings
    clean_keywords: List[str] = []
    seen = set()
    for k in (ai_keywords or []):
        cleaned_k = _clean_keyword_item(k)
        if _is_valid_keyword(cleaned_k):
            norm = cleaned_k.lower()
            if norm not in seen:
                seen.add(norm)
                clean_keywords.append(cleaned_k)

    # If the model produced fewer results, supplement with high-traffic Telegram search patterns up to target_count
    if len(clean_keywords) < target_count:
        base_expansions = [
            f"{seed} official", f"{seed} group", f"{seed} channel", f"{seed} community",
            f"best {seed}", f"{seed} global", f"{seed} hub", f"{seed} chat",
            f"{seed} vip", f"{seed} updates", f"{seed} discussion", f"{seed} network",
            f"{seed} portal", f"{seed} live", f"{seed} pro", f"{seed} club",
            f"{seed} deals", f"{seed} market", f"{seed} world", f"{seed} direct",
            f"{seed} premium", f"{seed} link", f"{seed} alerts", f"{seed} zone"
        ]
        for be in base_expansions:
            if len(clean_keywords) >= target_count:
                break
            norm = be.lower()
            if norm not in seen:
                seen.add(norm)
                clean_keywords.append(be)

    # Strictly limit to plan allowance if not unlimited
    if not is_unlimited:
        clean_keywords = clean_keywords[:max_allowed]
    else:
        clean_keywords = clean_keywords[:100]

    return {
        "seed": seed,
        "keywords": clean_keywords,
        "total": len(clean_keywords),
        "max_allowed": 100 if is_unlimited else max_allowed,
        "plan": user_plan,
        "is_unlimited": is_unlimited,
        "source": "openrouter",
        "model": selected_model,
        "error_note": error_note,
        "is_free_limit": bool(error_note and "FREE_MODEL_LIMIT" in error_note)
    }

class AISpintaxRequest(BaseModel):
    text: str
    api_key: Optional[str] = None
    model: Optional[str] = None

def format_spintax_layout(text: str) -> str:
    if not text:
        return ""
    text = re.sub(r'^```(?:spintax|text)?\s*', '', text, flags=re.IGNORECASE)
    text = re.sub(r'\s*```$', '', text)
    text = text.strip()
    
    # Fix stuck spintax braces: }{ -> } { (except {{var}} and }})
    text = re.sub(r'(?<!\})\}(?![\s.,!?;:)}\]\n\r])(?!\})', '} ', text)
    text = re.sub(r'(?<![\s\n\r{(])(?<!\{)\{(?!\{)', ' {', text)
    text = re.sub(r'\}\}([a-zA-Z0-9])', r'}} \1', text)
    text = re.sub(r'([a-zA-Z0-9])\{\{', r'\1 {{', text)
    
    # Fix punctuation followed immediately by capital letter or { without space
    text = re.sub(r'([.!?])([A-Z{])', r'\1 \2', text)
    text = re.sub(r'([,:])([a-zA-Z{])', r'\1 \2', text)
    
    # Normalize carriage returns
    text = text.replace('\r\n', '\n').replace('\r', '\n')
    
    # If the text has no double newlines (\n\n) at all, let's intelligently add paragraphs
    if '\n\n' not in text:
        # 1. Separate greetings/openers
        text = re.sub(
            r'^(\s*(?:\b(?:Hello|Hi|Hey|Greetings|Welcome|Hey there|Good day|Good morning)\b|\{[^{}]+\}|\{\{[^}]+\}\})[^\n.!?:]*?[.,!?:])(\s+)',
            r'\1\n\n',
            text,
            flags=re.IGNORECASE
        )
        
        # 2. Break after questions (natural hook transition)
        text = re.sub(r'(\?)\s+([A-Z{])', r'\1\n\n\2', text)
        
        # 3. If colon is followed by bullet/emoji, break onto newline
        text = re.sub(r':\s*([✅🔥📺👉💎✔️⚡🚀🎬⚽📱•\-])', r':\n\1', text)
        
        # 4. Put emoji bullets on newlines
        text = re.sub(r'([.!?])\s+([✅🔥📺👉💎✔️⚡🚀🎬⚽📱•\-])', r'\1\n\n\2', text)
        text = re.sub(r'([^\n])\s+([✅🔥📺👉💎✔️⚡🚀🎬⚽📱•\-])\s+', r'\1\n\2 ', text)
        
        # 5. Separate CTAs / Closers before the end:
        cta_pattern = r'(\s+)(?=(\{[^{}]*?(?:reply|send|type|message|dm|contact|click|visit|join|grab)[^{}]*?\}|\b(?:reply|send|type|message|drop a dm|contact us|reach out|click below|click here|dm me|dm us|message us|join now|visit link)\b))'
        matches = list(re.finditer(cta_pattern, text, flags=re.IGNORECASE))
        if matches:
            last_match = matches[-1]
            if last_match.start() > len(text) * 0.3:
                text = text[:last_match.start()] + '\n\n' + text[last_match.end():]
    else:
        text = re.sub(r':\s*([✅🔥📺👉💎✔️⚡🚀🎬⚽📱•\-])', r':\n\1', text)
        text = re.sub(r'([.!?])\s+([✅🔥📺👉💎✔️⚡🚀🎬⚽📱•\-])', r'\1\n\2', text)
        
    # Normalize multiple newlines to maximum 2 (\n\n)
    cleaned_lines = [line.strip() for line in text.split('\n')]
    text = '\n'.join(cleaned_lines)
    text = re.sub(r'\n{3,}', '\n\n', text).strip()
    return text

@app.post("/api/ai/spintax")
async def generate_ai_spintax(req: AISpintaxRequest, user_id: str = Depends(get_current_user_id)):
    """Transforms standard text into natural, anti-ban Spintax using the configured AI model with clean paragraph structure."""
    conn_u = get_db_connection()
    u_row = conn_u.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
    if hasattr(conn_u, "close"): conn_u.close()
    user_plan = u_row[0] if u_row and u_row[0] else "free"
    if user_plan == 'free':
        raise HTTPException(
            status_code=403,
            detail="Dynamic AI Spintax generation is locked on the Free Starter plan. Please upgrade to Growth Basic or higher to use Spintax."
        )

    input_text = (req.text or "").strip()
    if not input_text:
        raise HTTPException(status_code=400, detail="Text to spintax is required.")
        
    key = (req.api_key or "").strip()
    selected_model = (req.model or "").strip() or "meta-llama/llama-3.3-70b-instruct"
    
    if not key:
        raise HTTPException(
            status_code=400,
            detail="No OpenRouter API key provided. Please connect your AI Key in Scraper > AI Provider."
        )

    prompt = (
        f"You are the world's foremost Telegram anti-ban Spintax engineer and copywriting specialist.\n"
        f"Your mission is to perform a DEEP HIGH-DENSITY SPINTAX transformation on the provided message, spintaxing 80% to 90% of all words so that every resolved variation achieves 90%+ uniqueness to completely bypass Telegram spam filters.\n\n"
        f"MANDATORY SPINTAX RULES:\n"
        f"1. DEEP COVERAGE (80%-90% Density): Do NOT lightly spin! Spin almost every verb, noun phrase, adjective, opener, closer, and sentence transition. Every sentence must have multiple spinning blocks.\n"
        f"2. ROTATING OPTIONS (3 to 5 Variations Each): For every spun element, provide 3 to 5 realistic, high-converting synonyms or interchangeable phrases: {{option 1|option 2|option 3|option 4}}.\n"
        f"   - Openers: {{Hey there|Hello everyone|Greetings|Hi folks|Quick shoutout}}\n"
        f"   - Verbs: {{boost|supercharge|elevate|maximize|scale up}}\n"
        f"   - Adjectives: {{top-tier|exceptional|proven|premier|game-changing}}\n"
        f"   - Transitions: {{Furthermore|On top of that|What's more|In addition|Besides}}\n"
        f"   - Calls to Action: {{drop me a DM|reach out right now|connect with us below|send a direct message|get in touch today}}\n"
        f"3. STRICT PRESERVATION: Do NOT modify or spin inside variables like {{{{First Name}}}}, {{{{Group Name}}}}, {{{{Username}}}}, {{{{first_name}}}}, {{{{group_name}}}}. Leave URLs (https://...), @usernames, and phone numbers untouched.\n"
        f"4. NATURAL FLOW: Every permutation must read 100% naturally, smoothly, and persuasively in English, without awkward phrasing or grammatical errors.\n"
        f"5. SYNTACTIC PERFECTION: Ensure 100% balanced curly braces {{ }} and pipes |. No unclosed braces or empty options.\n"
        f"6. ZERO MARKDOWN WRAPPERS: Return ONLY the raw spintaxed message. Do not include ```spintax``` code blocks, preamble, notes, or explanations.\n"
        f"7. STRUCTURE, PARAGRAPHS & SPACING (CRITICAL - DO NOT SQUEEZE TOGETHER):\n"
        f"   - You MUST format the output with clean, human-readable paragraphs, line breaks, and proper spacing.\n"
        f"   - Separate Greeting/Opener, Value Proposition / Core Offer, Feature Bullets, and Call-to-Action into distinct paragraphs using double line breaks (\\n\\n).\n"
        f"   - NEVER output all text squeezed together on a single line or as one giant continuous wall of text.\n"
        f"   - If the user's input text lacks paragraphs or was pasted as a single squeezed line, INTELLIGENTLY ADD paragraphs, blank lines, and spaces where appropriate so each section is spacious and readable.\n"
        f"   - If emoji bullet points are used, place each bullet on its own new line.\n"
        f"   - Place the final Call-to-Action (e.g. Reply SUBSCRIBE, DM us, Channel link) on its own separate paragraph with an empty line above it.\n"
        f"   - Maintain clean single spacing between words and spintax braces: e.g. '{{Hello|Hi}} {{friend|folks}}', NEVER squashed like '{{Hello|Hi}}{{friend|folks}}'.\n\n"
        f"Original Message to Deep-Spintax:\n{input_text}"
    )

    async with httpx.AsyncClient(timeout=45.0) as client:
        try:
            payload: Dict[str, Any] = {
                "model": selected_model,
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": 2500,
                "include_reasoning": False
            }
            if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
                payload["temperature"] = 0.7

            res = await client.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                },
                json=payload
            )
            if res.status_code == 200:
                data = res.json()
                choices = data.get("choices") or []
                if choices:
                    msg = choices[0].get("message") or {}
                    raw_spun = msg.get("content") or ""
                    # Strip any think tags or code blocks
                    raw_spun = re.sub(r'<think>[\s\S]*?</think>', '', raw_spun, flags=re.IGNORECASE)
                    raw_spun = re.sub(r'```(?:spintax|text)?', '', raw_spun).strip('`').strip()
                    if raw_spun:
                        formatted_spun = format_spintax_layout(raw_spun)
                        return {"spintax": formatted_spun, "model": selected_model}
                raise HTTPException(status_code=400, detail="AI returned empty spintax text.")
            else:
                try:
                    err_msg = res.json().get("error", {}).get("message") or res.text[:120]
                except Exception:
                    err_msg = res.text[:120]
                is_rate_limit = (res.status_code in (429, 402)) or any(w in str(err_msg).lower() for w in ("rate limit", "quota", "credit", "free tier", "limit exceeded"))
                if is_rate_limit:
                    raise HTTPException(
                        status_code=429,
                        detail=f"FREE_MODEL_LIMIT: The model '{selected_model}' has reached OpenRouter's free tier rate limit or daily quota. Please select another free model or add credits."
                    )
                raise HTTPException(status_code=res.status_code, detail=f"OpenRouter error: {err_msg}")
        except HTTPException:
            raise
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"AI Spintax failed: {str(e)}")

class AITemplateVariationsRequest(BaseModel):
    prompt: str
    sample_text: Optional[str] = None
    api_key: Optional[str] = None
    model: Optional[str] = None
    include_spintax: Optional[bool] = True
    count: Optional[int] = 5
    length: Optional[str] = "moderate"  # 'short' | 'moderate' | 'lengthy'

def clean_variation_line(line: str) -> str:
    """Strips meta and structural labels like Hook:, Paragraph:, Bullets:, CTA: from lines."""
    line = re.sub(r'(?i)^(?:Hook|Headline|Opener)\s*:\s*', '', line)
    line = re.sub(r'(?i)^(?:Paragraph|Body|Description)\s*:\s*', '', line)
    line = re.sub(r'(?i)^(?:CTA|Call to Action)\s*:\s*', '', line)
    line = re.sub(r'(?i)^(?:Spintax|Spintax version)\s*:\s*', '', line)
    line = re.sub(r'(?i)^Bullets\s*:\s*', '', line)
    line = re.sub(r'(?i)^\*\*(?:Hook|Headline|Paragraph|Body|Bullets|CTA|Call to Action)\s*:\*\*\s*', '', line)
    return line.strip()

def is_meta_or_thinking_line(line: str) -> bool:
    """Detects and filters out AI chain-of-thought drafting notes, word counts, and revision markers."""
    l = line.strip()
    if not l:
        return False
    # Meta / thinking / word count lines
    if re.search(r'(?i)^(?:let\'s|word count|total\s*~|headline:|para:|bullets:|add to paragraph)', l):
        return True
    if re.search(r'\b\w+\(\d+\)\s+\w+\(\d+\)', l):  # e.g. "Stop(1) overpaying(2)"
        return True
    if re.search(r'^~?\d+\s*(?:words?)?\.?$', l):  # e.g. "~15 words", "~10"
        return True
    if re.search(r'(?i)^(?:too short|need approx|revised|still a bit short|should hit\s*~?\d+)', l) and not any(e in l for e in ('🔥', '⚡', '✅', '💎', '👉', '{')):
        return True
    if re.search(r'(?i)^and expand cta:?\s*', l):
        return True
    if l in ('•', '-', '*'):  # Lone empty bullet artifact
        return True
    return False

def clean_and_format_telegram_copy(text: str) -> str:
    """Cleans and guarantees human-readable paragraph formatting, bullet alignment, and removes any AI meta-thinking."""
    if not text:
        return ""
    
    # 1. Normalize line endings and unescape literal \n
    text = text.replace('\\r\\n', '\n').replace('\\n', '\n').replace('\r\n', '\n').replace('\r', '\n')
    
    # 2. If the text has "Revised V\d+:" or "Let's write V\d+:", take content after the latest marker
    revised_splits = re.split(r'(?i)(?:Revised\s*V\d+:|Let\'s\s*write\s*V\d+:)', text)
    if len(revised_splits) > 1:
        for candidate in reversed(revised_splits):
            if len(candidate.strip()) > 35:
                text = candidate
                break

    # 3. Strip outer markdown code fences if present
    text = text.strip()
    if text.startswith('```') and text.endswith('```'):
        text = re.sub(r'^```[a-zA-Z]*\n', '', text)
        text = re.sub(r'\n```$', '', text).strip()
    
    # 4. Strip heading markers e.g. "Variation 1: Direct Value..."
    text = re.sub(r'(?i)^(?:#{1,4}\s*|(?:\*{1,2})?)(?:Variation|Option|Angle)\s*\d+[\:\.\-\)]?[^\n]*', '', text).strip()
    if (text.startswith('"') and text.endswith('"')) or (text.startswith("'") and text.endswith("'")):
        text = text[1:-1].strip()
        
    lines = [clean_variation_line(l) for l in text.split('\n')]
    filtered_lines = [l for l in lines if not is_meta_or_thinking_line(l)]
    bullet_symbols = r'[•\-\*▪️▫️🔹🔸▶️✅✔️⚡🔥💎📡🚀👉📺📱⚽🎬🌍🔧🤝🎉💥💰]'
    
    # Separate multiple bullets squashed on a single line
    expanded_lines = []
    for line in filtered_lines:
        if not line:
            expanded_lines.append("")
            continue
        split_items = re.split(rf'(?<=[^\s])\s+(?={bullet_symbols}\s+)', line)
        for item in split_items:
            sub = item.strip()
            if sub and sub not in ('•', '-', '*'):
                expanded_lines.append(sub)

    # Reassemble with smart paragraph spacing
    structured = []
    for line in expanded_lines:
        if not line:
            if structured and structured[-1] != "":
                structured.append("")
            continue

        is_bullet = bool(re.match(rf'^{bullet_symbols}', line))
        is_header = bool(re.match(r'^(?:[A-Z0-9\s\&\-\/]{4,}:|🔥|⚡|💎|📺|🚀|✨|🌟|🎯|📢|👇|💡|🏆|🔐|💬)', line) and len(line) < 65 and not line.endswith('.'))
        is_cta = bool(re.search(r'(?:t\.me\/|https?:\/\/|reply|dm\s+us|contact|get\s+started|claim\s+now|order\s+now|message\s+us)', line, re.IGNORECASE) and len(line) < 120)

        prev_line = structured[-1] if structured else None
        prev_is_bullet = bool(prev_line and re.match(rf'^{bullet_symbols}', prev_line))
        prev_is_blank = prev_line == "" or prev_line is None

        # Add blank line before section headers or standalone CTAs if not already separated
        if (is_header or is_cta) and not prev_is_blank and not prev_is_bullet:
            structured.append("")
        elif is_header and prev_is_bullet:
            structured.append("")
        elif not is_bullet and prev_is_bullet:
            # Bullet list just ended; separate following paragraph with a blank line
            structured.append("")

        structured.append(line)

    result_text = "\n".join(structured)
    # Ensure no more than 2 consecutive newlines
    result_text = re.sub(r'\n{3,}', '\n\n', result_text).strip()
    return result_text

@app.post("/api/ai/generate-template-variations")
async def generate_template_variations(req: AITemplateVariationsRequest, user_id: str = Depends(get_current_user_id)):
    """Generates 5 distinct high-converting Telegram template variations with custom length and structure."""
    user_prompt = (req.prompt or "").strip()
    if not user_prompt:
        raise HTTPException(status_code=400, detail="Prompt/description is required.")

    key = (req.api_key or "").strip()
    selected_model = (req.model or "").strip() or "meta-llama/llama-3.3-70b-instruct"
    target_count = max(3, min(10, req.count or 5))
    use_spintax = req.include_spintax if req.include_spintax is not None else True
    sample_copy = (req.sample_text or "").strip()
    volume = (req.length or "moderate").lower().strip()

    if not key:
        raise HTTPException(
            status_code=400,
            detail="No OpenRouter API key provided. Please connect your AI Key in Scraper > AI Provider."
        )

    if volume == "short":
        volume_instructions = (
            "LENGTH: Short & punchy broadcast message. Fast hook, 2-3 bullet highlights, quick CTA."
        )
    elif volume == "lengthy":
        volume_instructions = (
            "LENGTH: Comprehensive broadcast message. Eye-catching hook, opening problem-solution paragraph, 5-7 feature/benefit bullets, and clear CTA."
        )
    else:  # moderate
        volume_instructions = (
            "LENGTH: Balanced broadcast message. Engaging hook, 1 concise paragraph, 3-4 feature bullets, and clear CTA."
        )

    spintax_rule = (
        "Incorporate natural Spintax variations {Option 1|Option 2|Option 3} in greetings, CTAs, adjectives, and key phrases so each message rotates dynamically."
        if use_spintax else "Write clean, direct copy without spintax braces."
    )

    sample_section = ""
    if sample_copy:
        sample_section = (
            f"\n\n=================================================================\n"
            f"REFERENCE SAMPLE COPY (BENCHMARK FOR STYLE, STRUCTURE & LENGTH ONLY):\n"
            f"\"\"\"\n{sample_copy}\n\"\"\"\n\n"
            f"CRITICAL DIRECTIVE ON USING THIS SAMPLE:\n"
            f"- DO NOT COPY, REPHRASE, OR REUSE THE SAMPLE'S WORDS, PRODUCT NAMES, OR SPECIFIC OFFER DETAILS!\n"
            f"- The sample is provided SOLELY to show the desired layout style, tone of voice, pacing, and length.\n"
            f"- You must write 100% BRAND NEW, ORIGINAL copy dedicated EXCLUSIVELY to the user's specific offer/prompt:\n"
            f"  Offer/Topic: \"{user_prompt}\"\n"
            f"- Under no circumstances should you echo the sample text back.\n"
            f"=================================================================\n"
        )

    sys_prompt = (
        f"You are a top conversion copywriter writing high-converting Telegram group/channel promotional broadcast messages.\n\n"
        f"OFFER / TOPIC:\n"
        f"\"\"\"\n{user_prompt}\n\"\"\""
        f"{sample_section}\n\n"
        f"YOUR TASK:\n"
        f"Generate {target_count} distinct broadcast variations for Telegram:\n"
        f"- Variation 1: Direct Value & Starter / Free Trial angle\n"
        f"- Variation 2: Curiosity & VIP Access angle\n"
        f"- Variation 3: Social Proof & Problem-Solver angle\n"
        f"- Variation 4: Conversational Question / Dialogue angle\n"
        f"- Variation 5: High Urgency & Limited-Time Perk angle\n\n"
        f"STRICT FORMAT & CONTENT RULES (CRITICAL):\n"
        f"1. OUTPUT DIRECT COPY ONLY: Output ONLY the final broadcast message text ready to send. DO NOT think out loud. DO NOT draft or revise. DO NOT count words.\n"
        f"2. ZERO STRUCTURAL LABELS: DO NOT write labels like 'Hook:', 'Headline:', 'Paragraph:', 'Bullets:', 'CTA:', 'Spintax:', or 'Word count:'. Write the actual promotional copy directly.\n"
        f"3. TELEGRAM FORMAT: Never start with email letters like 'Hello {{First Name}}'. Hook the reader immediately with an announcement, question, or bold value.\n"
        f"4. PARAGRAPHS & BULLETS: Separate sections with blank lines (\\n\\n). Put each bullet item (•, ✅, ⚡, 🔥, 💎) on its own separate line.\n"
        f"5. {volume_instructions}\n"
        f"6. {spintax_rule}\n\n"
        f"DELIMITER INSTRUCTION:\n"
        f"Separate each variation ONLY with the line:\n"
        f"===VARIATION===\n\n"
        f"Do NOT return JSON. Do NOT wrap in markdown code blocks. Output plain variations separated by ===VARIATION===."
    )

    async with httpx.AsyncClient(timeout=85.0) as client:
        try:
            payload: Dict[str, Any] = {
                "model": selected_model,
                "messages": [{"role": "user", "content": sys_prompt}],
                "max_tokens": 2800,
                "reasoning": {
                    "effort": "none",
                    "exclude": True
                }
            }
            if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
                payload["temperature"] = 0.75

            res = await client.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                },
                json=payload
            )
            if res.status_code == 200:
                data = res.json()
                
                # 1. Check for OpenRouter error payload returned inside 200 OK
                if "error" in data:
                    err_obj = data["error"]
                    err_msg = err_obj.get("message") if isinstance(err_obj, dict) else str(err_obj)
                    raise HTTPException(status_code=400, detail=f"OpenRouter API error: {err_msg}")

                choices = data.get("choices") or []
                if not choices:
                    raise HTTPException(
                        status_code=400, 
                        detail="OpenRouter returned an empty choices list. Please verify your API key credits or switch AI model in Scraper > AI Provider."
                    )

                msg = choices[0].get("message") or {}
                # Extract content from all possible fields (standard content, reasoning, or legacy text)
                raw_text = (
                    msg.get("content") 
                    or choices[0].get("text") 
                    or msg.get("reasoning") 
                    or msg.get("reasoning_content") 
                    or ""
                ).strip()

                if not raw_text:
                    finish_reason = choices[0].get("finish_reason") or "unknown"
                    raise HTTPException(
                        status_code=400, 
                        detail=f"AI model produced empty response (finish_reason: {finish_reason}). Please try again with another model."
                    )

                # Safely handle <think> reasoning blocks without erasing the full text
                if "</think>" in raw_text:
                    after_think = raw_text.split("</think>", 1)[1].strip()
                    if len(after_think) > 20:
                        cleaned = after_think
                    else:
                        cleaned = re.sub(r'<\/?think>', '', raw_text).strip()
                else:
                    cleaned = re.sub(r'<\/?think>', '', raw_text).strip()

                raw_variations = []

                # Strategy 1: JSON array (if model output JSON)
                start_b = cleaned.find('[')
                end_b = cleaned.rfind(']')
                if start_b != -1 and end_b > start_b:
                    json_slice = cleaned[start_b:end_b+1]
                    for attempt in [json_slice, re.sub(r',\s*\]', ']', json_slice)]:
                        try:
                            parsed = json.loads(attempt)
                            if isinstance(parsed, list):
                                for item in parsed:
                                    s = str(item).strip()
                                    if len(s) > 20:
                                        raw_variations.append(s)
                                if len(raw_variations) >= 2:
                                    break
                        except Exception:
                            pass

                # Strategy 2: Delimiters like ===VARIATION===, === Variation 1 ===, === Variation 1: Title ===
                if len(raw_variations) < 2 and re.search(r'={3,}[^\n]*', cleaned):
                    parts = re.split(r'={3,}[^\n]*', cleaned)
                    cands = [p.strip() for p in parts if len(p.strip()) > 20]
                    if len(cands) >= 2:
                        raw_variations = cands

                # Strategy 3: Headers like **Variation 1**, ### Variation 1, Variation 1:, Option 1:
                if len(raw_variations) < 2 and re.search(r'(?i)(?:^|\n)\s*(?:#{1,4}\s*|(?:\*{1,2})?)(?:Variation|Option|Angle)\s*\d+', cleaned):
                    parts = re.split(r'(?i)(?:^|\n)\s*(?:#{1,4}\s*|(?:\*{1,2})?)(?:Variation|Option|Angle)\s*\d+[^\n]*', cleaned)
                    cands = [p.strip() for p in parts if len(p.strip()) > 20]
                    if len(cands) >= 2:
                        raw_variations = cands

                # Strategy 4: Numbered list e.g. 1. 🔥 Hook, 2) ⚡ Hook
                if len(raw_variations) < 2 and re.search(r'(?m)^\s*\d+[\.\)]\s+', cleaned):
                    parts = re.split(r'(?m)^\s*\d+[\.\)]\s+', cleaned)
                    cands = [p.strip() for p in parts if len(p.strip()) > 20]
                    if len(cands) >= 2:
                        raw_variations = cands

                # Strategy 5: Horizontal rules (---)
                if len(raw_variations) < 2 and re.search(r'(?m)^\s*(?:---|___|\*\*\*)\s*$', cleaned):
                    parts = re.split(r'(?m)^\s*(?:---|___|\*\*\*)\s*$', cleaned)
                    cands = [p.strip() for p in parts if len(p.strip()) > 20]
                    if len(cands) >= 2:
                        raw_variations = cands

                # Strategy 6: Separate by clusters of blank lines if multiple sections exist
                if len(raw_variations) < 2:
                    blocks = re.split(r'\n{3,}', cleaned)
                    cands = [b.strip() for b in blocks if len(b.strip()) > 35]
                    if len(cands) >= 2:
                        raw_variations = cands

                # Strategy 7: Fallback to entire text if longer than 20 chars
                if not raw_variations and len(cleaned) > 20:
                    raw_variations = [cleaned]

                # Filter out accidental conversational preamble
                def is_conversational_preamble(t: str) -> bool:
                    st = t.strip()
                    if len(st) < 90 and not any(sym in st for sym in ('•', '✅', '✔️', '⚡', '🔥', '💎', '{', '|')):
                        if re.search(r'^(?:Here are|Below are|Sure|Certainly|These are|Generated|Here is|Enjoy)\b', st, re.IGNORECASE):
                            return True
                    return False

                if len(raw_variations) > 1 and is_conversational_preamble(raw_variations[0]):
                    raw_variations = raw_variations[1:]

                # Clean, format paragraphs & filter variations
                variations = []
                sample_norm = sample_copy.strip().lower() if sample_copy else ""
                for v in raw_variations:
                    formatted = clean_and_format_telegram_copy(v)
                    # Exclude if it accidentally echoed the sample verbatim
                    if sample_norm and formatted.strip().lower() == sample_norm:
                        continue
                    if len(formatted) > 20:
                        variations.append(formatted)

                if variations:
                    return {
                        "variations": variations[:target_count],
                        "total": len(variations[:target_count]),
                        "model": selected_model
                    }

                # If variations is still empty, include snippet of cleaned text for debugging
                debug_snippet = cleaned[:100].replace('\n', ' ')
                raise HTTPException(
                    status_code=400, 
                    detail=f"Could not parse message variations from AI response. Output received: '{debug_snippet}...'"
                )
            else:
                try:
                    err_data = res.json()
                    err_msg = err_data.get("error", {}).get("message") or res.text[:200]
                except Exception:
                    err_msg = res.text[:200]
                is_rate_limit = (res.status_code in (429, 402)) or any(w in str(err_msg).lower() for w in ("rate limit", "quota", "credit", "free tier", "limit exceeded"))
                if is_rate_limit:
                    raise HTTPException(
                        status_code=429,
                        detail=f"FREE_MODEL_LIMIT: The model '{selected_model}' has reached OpenRouter's free tier rate limit or daily quota. Please select another free model or add credits."
                    )
                raise HTTPException(status_code=res.status_code, detail=f"OpenRouter error ({res.status_code}): {err_msg}")
        except httpx.TimeoutException:
            raise HTTPException(status_code=504, detail="AI generation timed out. OpenRouter is experiencing latency. Please try again.")
        except HTTPException:
            raise
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"AI generation failed: {str(e)}")

# ==========================================
# ACCOUNT SAFETY: SPAMBOT & HEALTH CHECK
# ==========================================

@app.post("/api/telegram/accounts/{phone}/check-spambot")
async def check_account_spambot(phone: str, user_id: str = Depends(get_current_user_id)):
    """Queries official Telegram @SpamBot to check if account has restrictions or limits."""
    conn = get_db_connection()
    row = conn.execute("SELECT session_string, api_id, api_hash FROM accounts WHERE phone_number = ? AND user_id = ?", (phone, user_id)).fetchone()
    if hasattr(conn, "close"): conn.close()
    if not row:
        raise HTTPException(status_code=404, detail="Account not found")
        
    session_str, api_id, api_hash = row[0], int(row[1]), row[2]
    client = TelegramClient(StringSession(session_str), api_id, api_hash)
    await client.connect()
    if not await client.is_user_authorized():
        await client.disconnect()
        raise HTTPException(status_code=401, detail="Account session is expired. Please re-login.")
        
    try:
        await client.send_message("SpamBot", "/start")
        await asyncio.sleep(2.5)
        msgs = await client.get_messages("SpamBot", limit=1)
        msg_obj = msgs[0] if (isinstance(msgs, list) and msgs) else msgs
        reply = getattr(msg_obj, "text", getattr(msg_obj, "raw_text", getattr(msg_obj, "message", ""))) or "No response received from @SpamBot"
        reply_lower = reply.lower()
        if "good news" in reply_lower or "no limits" in reply_lower or "free as a bird" in reply_lower:
            bot_status = "clean"
            summary = "Good Standing (No limits applied)"
        elif "unfortunately" in reply_lower or "limited" in reply_lower or "restricted" in reply_lower or "spam" in reply_lower:
            bot_status = "restricted"
            summary = "Account Restricted by Telegram"
        else:
            bot_status = "unknown"
            summary = "Notice Received"
            
        return {
            "phone": phone,
            "status": bot_status,
            "summary": summary,
            "full_message": reply
        }
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"SpamBot check failed: {str(e)}")
    finally:
        await client.disconnect()

# ==========================================
# DM INTELLIGENCE: FETCH DMS & MESSAGES
# ==========================================

@app.get("/api/telegram/accounts/{phone}/dms")
async def get_account_dms(phone: str, user_id: str = Depends(get_current_user_id)):
    """Safely fetches recent 1-on-1 private DMs using the managed connection pool."""
    try:
        client = await POOL.get_client(user_id, phone)
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Account connection failed: {str(e)}")
        
    dms = []
    try:
        # Fast single MTProto round-trip: fetches top 120 recent dialogs in ~1 second
        dialogs = await client.get_dialogs(limit=120)
        for dialog in dialogs:
            if dialog.is_user:
                entity = dialog.entity
                if getattr(entity, 'bot', False):
                    continue
                first = getattr(entity, 'first_name', '') or ''
                last = getattr(entity, 'last_name', '') or ''
                full_name = f"{first} {last}".strip() or "Telegram User"
                last_msg = ""
                if dialog.message:
                    last_msg = dialog.message.text or ("(Media/Attachment)" if dialog.message.media else "")
                
                dms.append({
                    "id": entity.id,
                    "access_hash": str(getattr(entity, 'access_hash', 0) or 0),
                    "name": full_name,
                    "username": getattr(entity, 'username', None),
                    "phone": getattr(entity, 'phone', None),
                    "last_message": last_msg[:140] if last_msg else "No messages",
                    "date": dialog.date.isoformat() if dialog.date else None,
                    "unread_count": dialog.unread_count or 0
                })
                if len(dms) >= 80:
                    break
        return {"dms": dms, "phone": phone, "total": len(dms)}
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Failed to fetch DMs: {str(e)}")

@app.get("/api/telegram/accounts/{phone}/dms/{contact_id}/messages")
async def get_dm_messages(phone: str, contact_id: int, access_hash: Optional[str] = None, user_id: str = Depends(get_current_user_id)):
    """Fetches the last 50 messages of a private DM conversation with robust entity resolution."""
    try:
        client = await POOL.get_client(user_id, phone)
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Account connection failed: {str(e)}")
        
    try:
        from telethon.tl.types import InputPeerUser
        entity = None
        clean_hash = (access_hash or "").strip()
        if clean_hash and clean_hash not in ("0", "None", ""):
            try:
                entity = InputPeerUser(user_id=contact_id, access_hash=int(clean_hash))
            except Exception:
                pass

        if entity is None:
            try:
                entity = await client.get_input_entity(contact_id)
            except Exception:
                # Warm up entity cache by scanning recent dialogs
                await client.get_dialogs(limit=200)
                try:
                    entity = await client.get_input_entity(contact_id)
                except Exception:
                    entity = await client.get_entity(contact_id)

        if isinstance(entity, list):
            entity = entity[0]

        me = await client.get_me()
        me_id = getattr(me, 'id', getattr(me, 'user_id', None))
        full_name = "Telegram User"
        username = None
        try:
            full_user = await client.get_entity(entity)
            first = getattr(full_user, 'first_name', '') or ''
            last = getattr(full_user, 'last_name', '') or ''
            full_name = f"{first} {last}".strip() if (first or last) else "Telegram User"
            username = getattr(full_user, 'username', None)
        except Exception:
            pass
        
        messages_list = []
        async for msg in client.iter_messages(entity, limit=50):
            is_out = msg.out or (me_id is not None and msg.sender_id == me_id)
            has_media = bool(msg.media)
            att_type = None
            att_name = None
            att_size = None
            att_url = None
            
            if has_media:
                file_obj = getattr(msg, 'file', None)
                mime = getattr(file_obj, 'mime_type', '') or ''
                ext = getattr(file_obj, 'ext', '') or ''
                size_bytes = getattr(file_obj, 'size', None)
                if size_bytes and size_bytes > 0:
                    if size_bytes < 1024:
                        att_size = f"{size_bytes} B"
                    elif size_bytes < 1024 * 1024:
                        att_size = f"{size_bytes / 1024:.1f} KB"
                    elif size_bytes < 1024 * 1024 * 1024:
                        att_size = f"{size_bytes / (1024 * 1024):.1f} MB"
                    else:
                        att_size = f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
                
                if getattr(msg, 'photo', None) or mime.startswith('image/'):
                    att_type = 'image'
                    att_name = getattr(file_obj, 'name', None) or f"Photo_{msg.id}{ext or '.jpg'}"
                elif getattr(msg, 'voice', None) or 'audio/ogg' in mime:
                    att_type = 'voice'
                    att_name = getattr(file_obj, 'name', None) or f"Voice_Note_{msg.id}{ext or '.ogg'}"
                elif getattr(msg, 'video', None) or mime.startswith('video/'):
                    att_type = 'video'
                    att_name = getattr(file_obj, 'name', None) or f"Video_{msg.id}{ext or '.mp4'}"
                elif getattr(msg, 'audio', None) or mime.startswith('audio/'):
                    att_type = 'audio'
                    att_name = getattr(file_obj, 'name', None) or f"Audio_{msg.id}{ext or '.mp3'}"
                elif getattr(msg, 'document', None):
                    att_type = 'document'
                    att_name = getattr(file_obj, 'name', None) or f"Document_{msg.id}{ext}"
                else:
                    att_type = 'file'
                    att_name = getattr(file_obj, 'name', None) or f"Attachment_{msg.id}{ext}"
                
                att_url = f"/api/telegram/accounts/{phone}/dms/{contact_id}/messages/{msg.id}/media"

            display_text = msg.text or ""
            if not display_text and has_media:
                display_text = f"[{att_type.capitalize() if att_type else 'Attachment'}: {att_name or 'Attached File'}]"

            messages_list.append({
                "id": msg.id,
                "text": display_text,
                "is_out": is_out,
                "date": msg.date.isoformat() if msg.date else None,
                "has_attachment": has_media,
                "attachment_type": att_type,
                "attachment_name": att_name,
                "attachment_size": att_size,
                "attachment_url": att_url
            })
        messages_list.reverse()
        return {
            "contact": {
                "id": contact_id,
                "name": full_name,
                "username": username
            },
            "messages": messages_list
        }
    except Exception as e:
        log_debug(f"DM_FETCH_MSGS_ERR: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Failed to load messages: {str(e)}")


@app.get("/api/telegram/accounts/{phone}/dms/{contact_id}/messages/{message_id}/media")
async def get_dm_message_media(
    phone: str, 
    contact_id: int, 
    message_id: int, 
    request: Request,
    token: Optional[str] = None
):
    """Downloads or serves cached media for a specific Telegram direct message."""
    try:
        dm_media_dir = os.path.join(_STATIC_DIR, "dm_media")
        os.makedirs(dm_media_dir, exist_ok=True)
        
        # Check if already cached on disk (serves immediately)
        clean_p = phone.strip()
        pattern = f"{clean_p}_{contact_id}_{message_id}_"
        patterns = [pattern, f"{clean_p.lstrip('+')}_{contact_id}_{message_id}_", f"+{clean_p.lstrip('+')}_{contact_id}_{message_id}_"]
        for fname in os.listdir(dm_media_dir):
            if any(fname.startswith(p) for p in patterns):
                fpath = os.path.join(dm_media_dir, fname)
                content_type = None
                if fname.lower().endswith(('.jpg', '.jpeg')):
                    content_type = "image/jpeg"
                elif fname.lower().endswith('.png'):
                    content_type = "image/png"
                elif fname.lower().endswith('.webp'):
                    content_type = "image/webp"
                elif fname.lower().endswith('.gif'):
                    content_type = "image/gif"
                elif fname.lower().endswith('.mp4'):
                    content_type = "video/mp4"
                elif fname.lower().endswith(('.mp3', '.ogg', '.wav')):
                    content_type = "audio/mpeg"
                return FileResponse(fpath, media_type=content_type, filename=fname.split("_", 3)[-1] if len(fname.split("_", 3)) > 3 else fname)

        # Resolve user_id: check auth header, query token, or database owner of phone number
        auth_header = request.headers.get("Authorization", "")
        extracted_token = None
        if auth_header.startswith("Bearer "):
            extracted_token = auth_header[7:].strip()
        elif token:
            extracted_token = token
        elif request.query_params.get("token"):
            extracted_token = request.query_params.get("token")

        caller_user_id = None
        if extracted_token:
            try:
                from jose import jwt
                from auth import SECRET_KEY, ALGORITHM
                payload = jwt.decode(extracted_token, SECRET_KEY, algorithms=[ALGORITHM])
                caller_user_id = str(payload.get("sub"))
            except Exception as te:
                log_debug(f"Media auth token decode err: {str(te)}")

        # Query account owner from DB (check both accounts and telegram_accounts)
        p_variants = list({clean_p, clean_p.lstrip("+"), f"+{clean_p.lstrip('+')}"})
        account_owner_id = None
        matched_phone = clean_p
        with get_db_connection() as conn:
            cur = conn.cursor()
            for tbl in ("accounts", "telegram_accounts"):
                try:
                    for pv in p_variants:
                        cur.execute(f"SELECT user_id, phone_number FROM {tbl} WHERE phone_number = ? LIMIT 1", (pv,))
                        r = cur.fetchone()
                        if r:
                            account_owner_id = str(r[0] if isinstance(r, (tuple, list)) else r["user_id"])
                            matched_phone = str(r[1] if isinstance(r, (tuple, list)) else r["phone_number"])
                            break
                    if account_owner_id:
                        break
                except Exception:
                    pass

        # Use account owner id or verified caller user id
        user_id = account_owner_id or caller_user_id
        if not user_id:
            if caller_user_id:
                user_id = caller_user_id
            else:
                raise HTTPException(status_code=401, detail="Could not validate credentials")

        client = await POOL.get_client(user_id, matched_phone)
        try:
            entity = await client.get_input_entity(contact_id)
        except Exception:
            try:
                entity = await client.get_entity(contact_id)
            except Exception:
                await client.get_dialogs(limit=100)
                try:
                    entity = await client.get_input_entity(contact_id)
                except Exception:
                    entity = await client.get_entity(contact_id)

        raw_msg = await client.get_messages(entity, ids=message_id)
        target_msg = raw_msg[0] if isinstance(raw_msg, list) else raw_msg
        if not target_msg or not getattr(target_msg, 'media', None):
            raise HTTPException(status_code=404, detail="Message media not found")

        file_obj = getattr(target_msg, 'file', None)
        orig_name = getattr(file_obj, 'name', None) or f"media_{message_id}{getattr(file_obj, 'ext', '')}"
        safe_name = "".join(c for c in orig_name if c.isalnum() or c in "._-") or f"media_{message_id}"
        save_path = os.path.join(dm_media_dir, f"{matched_phone}_{contact_id}_{message_id}_{safe_name}")

        downloaded_result = await client.download_media(target_msg, file=save_path)
        if not downloaded_result or not os.path.exists(save_path):
            raise HTTPException(status_code=500, detail="Failed to download media")

        mime = getattr(file_obj, 'mime_type', None)
        return FileResponse(save_path, media_type=mime, filename=safe_name)
    except HTTPException:
        raise
    except Exception as e:
        log_debug(f"DM_MEDIA_ERR: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Media error: {str(e)}")


class DMSendRequest(BaseModel):
    message: str
    access_hash: Optional[str] = None

@app.post("/api/telegram/accounts/{phone}/dms/{contact_id}/send")
async def send_dm_message(phone: str, contact_id: int, req: DMSendRequest, user_id: str = Depends(get_current_user_id)):
    """Sends a direct message reply to the contact in Telegram with guaranteed entity resolution."""
    text = (req.message or "").strip()
    if not text:
        raise HTTPException(status_code=400, detail="Message text is required")
    
    try:
        client = await POOL.get_client(user_id, phone)
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Account connection failed: {str(e)}")

    try:
        from telethon.tl.types import InputPeerUser
        entity = None
        if req.access_hash and req.access_hash != "0":
            try:
                entity = InputPeerUser(user_id=contact_id, access_hash=int(req.access_hash))
            except Exception:
                pass
        
        if entity is None:
            try:
                entity = await client.get_input_entity(contact_id)
            except Exception:
                await client.get_dialogs(limit=100)
                try:
                    entity = await client.get_input_entity(contact_id)
                except Exception:
                    entity = await client.get_entity(contact_id)

        if isinstance(entity, list):
            entity = entity[0]

        sent_msg = await client.send_message(entity, text)
        msg_text = getattr(sent_msg, "text", getattr(sent_msg, "raw_text", getattr(sent_msg, "message", text)))
        return {
            "status": "success",
            "message": {
                "id": getattr(sent_msg, "id", 0),
                "text": msg_text,
                "is_out": True,
                "date": sent_msg.date.isoformat() if hasattr(sent_msg, "date") and sent_msg.date else None
            }
        }
    except Exception as e:
        log_debug(f"DM_SEND_ERR: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Failed to send DM: {str(e)}")


# ==========================================
# AI BAN-SHIELD SCORER & DM CONVERSATION CLOSER
# ==========================================

class AIScoreTemplateRequest(BaseModel):
    text: str
    api_key: Optional[str] = None
    model: Optional[str] = None

@app.post("/api/ai/score-template")
async def score_template_ai(req: AIScoreTemplateRequest, user_id: str = Depends(get_current_user_id)):
    """Scores outreach copy on Telegram spam risk, push hook, and conversion with 1-click optimization."""
    raw_text = (req.text or "").strip()
    if not raw_text:
        raise HTTPException(status_code=400, detail="Text to evaluate is required.")

    key = (req.api_key or "").strip()
    selected_model = (req.model or "").strip() or "meta-llama/llama-3.3-70b-instruct"
    if not key:
        raise HTTPException(status_code=400, detail="OpenRouter API key is required.")

    prompt = (
        f"You are a premier Telegram anti-ban engineer and cold DM conversion strategist.\n"
        f"Audit this Telegram outreach message:\n"
        f"\"\"\"\n{raw_text}\n\"\"\"\n\n"
        f"Evaluate the text across Telegram spam detection filters, notification preview impact, and conversion persuasion.\n"
        f"Return ONLY valid JSON with this exact structure:\n"
        f"{{\n"
        f'  "spam_score": <integer 0-100 where 0 is pristine/safe and 100 is instant ban risk>,\n'
        f'  "hook_score": <integer 0-100 rating how compelling the first 80 characters are for mobile push notification preview>,\n'
        f'  "conversion_score": <integer 0-100 rating overall conversion and CTA clarity>,\n'
        f'  "spintax_rating": <"Excellent" | "Good" | "Needs Improvement" | "Missing">,\n'
        f'  "pros": [<string>, <string>],\n'
        f'  "cons": [<string>, <string>],\n'
        f'  "suggestions": [<string>, <string>],\n'
        f'  "optimized_text": <string: rewritten version that drops spam triggers, crafts a killer opening hook, and adds natural {{Option 1|Option 2}} Spintax>\n'
        f"}}"
    )

    async with httpx.AsyncClient(timeout=45.0) as client:
        try:
            payload: Dict[str, Any] = {
                "model": selected_model,
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": 3000,
                "include_reasoning": False
            }
            if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
                payload["temperature"] = 0.5

            res = await client.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                },
                json=payload
            )
            if res.status_code == 200:
                data = res.json()
                choices = data.get("choices") or []
                if choices:
                    content = choices[0].get("message", {}).get("content") or ""
                    content = re.sub(r'<think>[\s\S]*?</think>', '', content, flags=re.IGNORECASE)
                    content = re.sub(r'```(?:json)?', '', content).strip('`').strip()
                    s = content.find('{')
                    e = content.rfind('}')
                    if s != -1 and e != -1:
                        parsed = json.loads(content[s:e+1])
                        return {
                            "spam_score": max(0, min(100, int(parsed.get("spam_score", 25)))),
                            "hook_score": max(0, min(100, int(parsed.get("hook_score", 70)))),
                            "conversion_score": max(0, min(100, int(parsed.get("conversion_score", 75)))),
                            "spintax_rating": parsed.get("spintax_rating") or ("Good" if "{" in raw_text else "Missing"),
                            "pros": parsed.get("pros") or ["Clear message intent", "Concise length"],
                            "cons": parsed.get("cons") or ["Could use higher Spintax variety to dodge Telegram hash filters"],
                            "suggestions": parsed.get("suggestions") or ["Add {Hi|Hello} Spintax variations", "Stronger hook in the first sentence"],
                            "optimized_text": parsed.get("optimized_text") or raw_text,
                            "model": selected_model
                        }
            # Fallback evaluation if model parsing failed
            has_spintax = "{" in raw_text and "|" in raw_text
            return {
                "spam_score": 25 if has_spintax else 45,
                "hook_score": 72,
                "conversion_score": 78,
                "spintax_rating": "Good" if has_spintax else "Missing",
                "pros": ["Direct value offer", "Natural tone"],
                "cons": ["Could add more Spintax options" if has_spintax else "Missing Spintax syntax to rotate messages"],
                "suggestions": ["Add multi-option greetings and CTAs", "Keep first 70 characters punchy for mobile notifications"],
                "optimized_text": f"{{Hello|Hi|Hey}} {{First Name}}, {raw_text}",
                "model": selected_model
            }
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"AI Scoring failed: {str(e)}")

class AIAnalyzeDmRequest(BaseModel):
    messages: List[Dict[str, Any]]
    contact_name: Optional[str] = None
    api_key: Optional[str] = None
    model: Optional[str] = None

def generate_intelligent_contextual_replies(
    contact: str, 
    msgs: List[Dict[str, Any]], 
    last_incoming_text: str, 
    last_out_text: str, 
    is_last_out: bool,
    model: str
) -> Dict[str, Any]:
    c_lower = (last_incoming_text or "").lower()
    first_name = contact.split()[0] if contact else "there"
    
    # Detect intent & topics
    is_pricing = any(w in c_lower for w in ["price", "cost", "pricing", "rate", "fee", "package", "plan", "$", "how much", "quote"])
    is_scheduling = any(w in c_lower for w in ["call", "meet", "zoom", "schedule", "time", "calendar", "tomorrow", "week", "chat", "slot", "when are you free"])
    is_demo_req = any(w in c_lower for w in ["demo", "video", "breakdown", "details", "info", "deck", "presentation", "link", "send", "more information"])
    is_greeting = any(w in c_lower for w in ["hi", "hello", "hey", "good morning", "good evening", "how are you", "who is this"])
    is_positive = any(w in c_lower for w in ["great", "awesome", "perfect", "sounds good", "interested", "sure", "yes", "cool", "thanks", "thank you", "nice"])
    is_objection = any(w in c_lower for w in ["busy", "later", "not now", "expensive", "budget", "think about", "not interested", "stop"])

    clean_snippet = (last_incoming_text[:50] + "...") if len(last_incoming_text) > 50 else last_incoming_text
    
    if is_pricing:
        summary = f"{first_name} is inquiring about pricing tiers and platform costs."
        replies = [
            {
                "angle": "Direct & Value-Focused",
                "text": f"Hey {first_name}! Our plans are flexible based on node scale—starting with full automation on entry tiers and custom unlimited nodes for larger operations. Would you like me to share a quick breakdown of the tiers that match your setup?"
            },
            {
                "angle": "Helpful & Transparent",
                "text": f"Hi {first_name}, happy to share pricing! We have tiered options designed for high ROI depending on how many accounts you plan to run. What kind of daily outreach volume are you aiming for?"
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Hey {first_name}, I can send over our full pricing sheet and feature comparison right here on Telegram. Are you looking for a single account setup or multi-account fleet management?"
            }
        ]
    elif is_scheduling:
        summary = f"{first_name} is open to discussing scheduling a call or walkthrough."
        replies = [
            {
                "angle": "Direct & Accommodating",
                "text": f"Hey {first_name}! I'd love to connect. I have openings tomorrow afternoon or Thursday morning. Does a quick 15-minute sync work for your calendar?"
            },
            {
                "angle": "Helpful & Low-Pressure",
                "text": f"Hi {first_name}, absolutely! We can jump on a brief walkthrough so I can show you how the engine runs in real-time. What day and timezone work best on your end?"
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Awesome {first_name}! Let's lock in a time this week. How does 2:00 PM EST tomorrow sound, or would a later slot suit you better?"
            }
        ]
    elif is_demo_req:
        summary = f"{first_name} requested more detailed information or a demo walkthrough."
        replies = [
            {
                "angle": "Immediate Delivery",
                "text": f"Hey {first_name}! Sending over the overview breakdown right away. It walks through automated scraping, account warmups, and smart DM sequencing step-by-step. Let me know once you take a look!"
            },
            {
                "angle": "Helpful & Insightful",
                "text": f"Hi {first_name}, here is the breakdown you asked for! It covers how teams run automated sequences while keeping accounts 100% safe. What specific goal are you looking to tackle first?"
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Glad to share this with you {first_name}! I've compiled the workflow summary for you. Would you also like a live screen-share walkthrough or does a quick video overview work best?"
            }
        ]
    elif is_objection:
        summary = f"{first_name} mentioned timing or budget constraints."
        replies = [
            {
                "angle": "Empathetic & Respectful",
                "text": f"Completely understand {first_name}, timing is everything! No rush at all—I'll check back in a couple of weeks. Feel free to ping me anytime if questions come up."
            },
            {
                "angle": "Low-Friction Resource",
                "text": f"No problem at all {first_name}. I can leave our quick reference guide with you so you have it whenever you're ready to evaluate tools down the road."
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Understood {first_name}! When would be a more convenient time for us to revisit this—perhaps next month?"
            }
        ]
    elif is_positive:
        summary = f"{first_name} responded positively and is actively engaged."
        replies = [
            {
                "angle": "Direct & Warm",
                "text": f"Glad to hear that {first_name}! Let's take the next step together—would you like me to get your access configured or schedule a quick walkthrough first?"
            },
            {
                "angle": "Helpful & Action-Oriented",
                "text": f"Awesome {first_name}! We can get everything rolling right away. Let me know if you want me to send over the onboarding steps or if you have any questions."
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Sounds great {first_name}! What's the best time today or tomorrow for us to finalize the setup?"
            }
        ]
    elif is_greeting:
        summary = f"{first_name} initiated or greeted in the conversation."
        replies = [
            {
                "angle": "Friendly & Engaging",
                "text": f"Hey {first_name}! Great connecting with you here. Hope your week is off to a strong start! How can I best help you today?"
            },
            {
                "angle": "Direct & Professional",
                "text": f"Hi {first_name}, thanks for reaching out! What specific Telegram automation or growth targets are you looking to achieve?"
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Hello {first_name}! Good to connect. Are you currently exploring automated Telegram outreach, or did you have a specific inquiry in mind?"
            }
        ]
    else:
        summary = f"Conversation with {first_name} regarding '{clean_snippet or 'latest message'}'."
        replies = [
            {
                "angle": "Direct & Responsive",
                "text": f"Hey {first_name}, thanks for following up on this! Regarding what you mentioned, we can definitely accommodate that. Let me know if you'd like to dive into the details."
            },
            {
                "angle": "Helpful & Informative",
                "text": f"Hi {first_name}, appreciate you sharing that! To make sure we're completely aligned, would it help if I walked you through the workflow directly?"
            },
            {
                "angle": "Smooth Next Step",
                "text": f"Got it {first_name}! That makes total sense. What would be the most helpful next step for you—a quick chat or should I send over the documentation?"
            }
        ]

    return {
        "conversation_summary": summary,
        "suggested_replies": replies,
        "model": model or "contextual-engine"
    }

@app.post("/api/ai/analyze-dm-conversation")
async def analyze_dm_conversation(req: AIAnalyzeDmRequest, user_id: str = Depends(get_current_user_id)):
    """Analyzes a Telegram DM conversation flow and generates 3 natural, context-aware reply suggestions based on the last message."""
    msgs = req.messages or []
    if not msgs:
        raise HTTPException(status_code=400, detail="Conversation messages are required.")

    key = (req.api_key or "").strip() or os.getenv("OPENROUTER_API_KEY", "").strip()
    selected_model = (req.model or "").strip() or "meta-llama/llama-3.3-70b-instruct"

    contact = req.contact_name or "Contact"
    transcript_lines = []
    for m in msgs:
        speaker = "Me (Our Account)" if m.get("is_out") else f"{contact}"
        text_line = (m.get("text") or "").strip()
        if text_line:
            transcript_lines.append(f"{speaker}: {text_line}")
    transcript = "\n".join(transcript_lines) or "No textual messages exchanged yet."

    # Identify the last message in thread and the contact's latest statement
    last_msg = msgs[-1] if msgs else {}
    last_is_out = bool(last_msg.get("is_out"))
    last_speaker = "Me (Our Account)" if last_is_out else f"{contact}"
    last_text = (last_msg.get("text") or "").strip()

    last_incoming_msg = next((m for m in reversed(msgs) if not m.get("is_out")), None)
    last_incoming_text = (last_incoming_msg.get("text") or "").strip() if last_incoming_msg else ""

    if not key:
        # If no OpenRouter key provided, generate intelligent contextual replies based on the actual discussion
        return generate_intelligent_contextual_replies(
            contact=contact,
            msgs=msgs,
            last_incoming_text=last_incoming_text,
            last_out_text=last_text,
            is_last_out=last_is_out,
            model=selected_model
        )

    if last_is_out:
        context_focus = (
            f"The latest message in the thread was sent by Me (Our Account): \"{last_text}\".\n"
            f"Prior to that, {contact}'s latest statement was: \"{last_incoming_text}\".\n"
            f"Generate 3 natural follow-up options or next conversation steps to keep things moving with {contact}."
        )
    else:
        context_focus = (
            f"The latest message is from {contact}:\n"
            f"\"{last_text}\"\n\n"
            f"Generate 3 distinct, natural Telegram replies that I can copy and send directly back to {contact} answering or reacting to their message."
        )

    prompt = (
        f"You are a skilled, friendly Telegram conversation assistant helping me chat with {contact}.\n\n"
        f"--- CONVERSATION HISTORY (Chronological) ---\n"
        f"{transcript}\n"
        f"--------------------------------------------\n\n"
        f"{context_focus}\n\n"
        f"OBJECTIVE:\n"
        f"Carefully read the entire conversation flow above, paying closest attention to what {contact} is asking or discussing.\n"
        f"Generate 3 distinct, contextual, natural Telegram replies that I can copy and send directly back to {contact}.\n\n"
        f"RULES FOR REPLIES:\n"
        f"1. CONTEXTUAL & DIRECT: The replies MUST directly answer, acknowledge, or react to what was just said. Do NOT use canned sales pitches or assume products unless specifically discussed in the messages.\n"
        f"2. NATURAL TELEGRAM TONE: Write like a real, competent human chatting on Telegram. Friendly, direct, concise, and smooth. No robotic bullet points, no fake hype, no stiff corporate speak.\n"
        f"3. THREE DISTINCT STYLES:\n"
        f"   - Option 1 (Direct & Friendly): Answers their exact point warmly and keeps the conversation flowing.\n"
        f"   - Option 2 (Helpful & Informative): Provides clear, practical insight or addresses their specific query.\n"
        f"   - Option 3 (Smooth Next Step): Politely guides the conversation toward a clear next step or asks a natural follow-up question.\n"
        f"4. 100% READY TO SEND: Do NOT use any placeholders like '[insert link]' or '[name]'. Write full, ready-to-copy text.\n\n"
        f"Return ONLY valid JSON matching this exact structure with double-quoted strings:\n"
        f"{{\n"
        f'  "conversation_summary": "1 sentence summarizing the discussion",\n'
        f'  "suggested_replies": [\n'
        f'    {{"angle": "Direct & Friendly", "text": "Natural Telegram reply answering their exact message"}},\n'
        f'    {{"angle": "Helpful & Informative", "text": "Clear and informative Telegram reply offering assistance"}},\n'
        f'    {{"angle": "Smooth Next Step", "text": "Polite Telegram reply advancing to the next step"}}\n'
        f'  ]\n'
        f"}}"
    )

    async with httpx.AsyncClient(timeout=45.0) as client:
        try:
            payload: Dict[str, Any] = {
                "model": selected_model,
                "messages": [{"role": "user", "content": prompt}],
                "max_tokens": 800,
                "include_reasoning": False
            }
            if not any(om in selected_model.lower() for om in ("o1-", "o3-", "o1", "o3", "reasoner")):
                payload["temperature"] = 0.5
                if "openai" in selected_model.lower() or "gpt" in selected_model.lower():
                    payload["response_format"] = {"type": "json_object"}

            res = await client.post(
                "https://openrouter.ai/api/v1/chat/completions",
                headers={
                    "Authorization": f"Bearer {key}",
                    "Content-Type": "application/json",
                    "HTTP-Referer": "https://arkitel.com",
                    "X-Title": "ArkiTel"
                },
                json=payload
            )

            if res.status_code != 200:
                err_detail = f"HTTP {res.status_code}"
                try:
                    err_json = res.json()
                    err_detail = err_json.get("error", {}).get("message") or res.text
                except Exception:
                    err_detail = res.text[:200]
                log_debug(f"OPENROUTER_DM_ERR ({res.status_code}): {err_detail}")
                is_rate_limit = (res.status_code in (429, 402)) or any(w in str(err_detail).lower() for w in ("rate limit", "quota", "credit", "free tier", "limit exceeded"))
                if is_rate_limit:
                    return generate_intelligent_contextual_replies(
                        contact=contact,
                        msgs=msgs,
                        last_incoming_text=last_incoming_text,
                        last_out_text=last_text,
                        is_last_out=last_is_out,
                        model=f"{selected_model} (Smart Fallback - Rate Limit)"
                    )
                return generate_intelligent_contextual_replies(
                    contact=contact,
                    msgs=msgs,
                    last_incoming_text=last_incoming_text,
                    last_out_text=last_text,
                    is_last_out=last_is_out,
                    model=selected_model
                )

            data = res.json()
            choices = data.get("choices") or []
            if choices:
                content = choices[0].get("message", {}).get("content") or ""
                content = re.sub(r'<think>[\s\S]*?</think>', '', content, flags=re.IGNORECASE)
                content = re.sub(r'```(?:json)?', '', content).strip('`').strip()
                s = content.find('{')
                e = content.rfind('}')
                json_str = content[s:e+1] if (s != -1 and e != -1) else content

                replies = []
                summary = f"Conversation with {contact}"

                # Strategy 1: Standard JSON parse
                try:
                    parsed = json.loads(json_str)
                    replies = parsed.get("suggested_replies") or []
                    summary = parsed.get("conversation_summary") or summary
                except Exception:
                    pass

                # Strategy 2: Remove malformed/unquoted conversation_summary if that broke JSON
                if not replies:
                    try:
                        cleaned = re.sub(r'"conversation_summary"\s*:\s*([^",\n][^\n]*?),', '', json_str)
                        cleaned = re.sub(r',\s*([}\]])', r'\1', cleaned)
                        parsed = json.loads(cleaned)
                        replies = parsed.get("suggested_replies") or []
                        summary = parsed.get("conversation_summary") or summary
                    except Exception:
                        pass

                # Strategy 3: ast.literal_eval for single-quoted Python dict syntax
                if not replies:
                    try:
                        parsed = ast.literal_eval(json_str)
                        if isinstance(parsed, dict):
                            replies = parsed.get("suggested_replies") or []
                            summary = parsed.get("conversation_summary") or summary
                    except Exception:
                        pass

                # Strategy 4: Regex extraction for angle/text pairs
                if not replies:
                    pairs = re.findall(r'["\']angle["\']\s*:\s*["\']([^"\']+)["\']\s*,\s*["\']text["\']\s*:\s*["\']([^"\']+)["\']', content)
                    if not pairs:
                        pairs = re.findall(r'["\']text["\']\s*:\s*["\']([^"\']+)["\']\s*,\s*["\']angle["\']\s*:\s*["\']([^"\']+)["\']', content)
                        if pairs:
                            pairs = [(p[1], p[0]) for p in pairs]
                    if pairs:
                        replies = [{"angle": a.strip(), "text": t.strip()} for a, t in pairs]

                # Strategy 5: Regex extraction for numbered or bullet lines
                if not replies:
                    bullets = re.findall(r'(?:^|\n)\s*(?:\d+[\.\)]|\-|\*)\s*(?:([A-Za-z\s&]+):)?\s*(.+)', content)
                    if bullets:
                        for b_angle, b_text in bullets[:3]:
                            replies.append({"angle": b_angle.strip() if b_angle else "Suggested Reply", "text": b_text.strip()})

                if replies:
                    return {
                        "conversation_summary": summary,
                        "suggested_replies": replies[:3],
                        "model": selected_model
                    }

            return generate_intelligent_contextual_replies(
                contact=contact,
                msgs=msgs,
                last_incoming_text=last_incoming_text,
                last_out_text=last_text,
                is_last_out=last_is_out,
                model=selected_model
            )
        except HTTPException:
            raise
        except Exception as e:
            log_debug(f"AI DM analysis failed: {str(e)}")
            return generate_intelligent_contextual_replies(
                contact=contact,
                msgs=msgs,
                last_incoming_text=last_incoming_text,
                last_out_text=last_text,
                is_last_out=last_is_out,
                model=selected_model
            )

@app.delete("/api/admin/users/{user_id}")
async def admin_delete_user(user_id: str, admin_id: str = Depends(get_current_admin)):
    """Deletes a user and all their associated data from the system."""
    if user_id == "admin_virtual_id":
        raise HTTPException(status_code=400, detail="Cannot delete the global virtual admin.")
        
    conn = get_db_connection()
    try:
        user = conn.execute("SELECT role FROM users WHERE id = ?", (user_id,)).fetchone()
        if user and user[0] == 'admin':
            raise HTTPException(status_code=400, detail="Cannot delete another admin account.")
            
        conn.execute("DELETE FROM users WHERE id = ?", (user_id,))
        conn.execute("DELETE FROM accounts WHERE user_id = ?", (user_id,))
        conn.execute("DELETE FROM tasks WHERE user_id = ?", (user_id,))
        conn.execute("DELETE FROM leads WHERE user_id = ?", (user_id,))
        conn.execute("DELETE FROM scraped_groups WHERE user_id = ?", (user_id,))
        conn.execute("DELETE FROM scrape_history WHERE user_id = ?", (user_id,))
        
        if hasattr(conn, "commit"): conn.commit()
        log_debug(f"ADMIN: Completely deleted user {user_id}")
        return {"status": "success", "message": f"User {user_id} and all their data deleted."}
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

class AdminBulkUserActionRequest(BaseModel):
    user_ids: List[str]
    action: str  # "suspend" | "activate" | "delete"

@app.post("/api/admin/users/bulk-action")
async def admin_bulk_user_action(req: AdminBulkUserActionRequest, admin_id: str = Depends(get_current_admin)):
    """Bulk action (suspend, activate, delete) for multiple selected users."""
    if not req.user_ids:
        raise HTTPException(status_code=400, detail="No user IDs provided.")
    action = req.action.lower().strip()
    if action not in ("suspend", "activate", "delete"):
        raise HTTPException(status_code=400, detail="Action must be 'suspend', 'activate', or 'delete'.")
        
    conn = get_db_connection()
    try:
        clean_ids = [uid for uid in req.user_ids if uid != "admin_virtual_id"]
        if not clean_ids:
            return {"status": "success", "affected": 0, "message": "No valid user accounts selected."}

        placeholders = ",".join("?" for _ in clean_ids)
        admin_rows = conn.execute(f"SELECT id FROM users WHERE id IN ({placeholders}) AND role = 'admin'", clean_ids).fetchall()
        admin_uids = {r[0] for r in admin_rows}
        target_ids = [uid for uid in clean_ids if uid not in admin_uids]

        if not target_ids:
            return {"status": "success", "affected": 0, "message": "Admin accounts cannot be bulk modified."}

        target_placeholders = ",".join("?" for _ in target_ids)
        if action == "suspend":
            conn.execute(f"UPDATE users SET is_active = 0 WHERE id IN ({target_placeholders})", target_ids)
            msg = f"Suspended {len(target_ids)} users successfully."
        elif action == "activate":
            conn.execute(f"UPDATE users SET is_active = 1 WHERE id IN ({target_placeholders})", target_ids)
            msg = f"Activated {len(target_ids)} users successfully."
        elif action == "delete":
            for tid in target_ids:
                conn.execute("DELETE FROM users WHERE id = ?", (tid,))
                conn.execute("DELETE FROM accounts WHERE user_id = ?", (tid,))
                conn.execute("DELETE FROM tasks WHERE user_id = ?", (tid,))
                conn.execute("DELETE FROM leads WHERE user_id = ?", (tid,))
                conn.execute("DELETE FROM scraped_groups WHERE user_id = ?", (tid,))
                conn.execute("DELETE FROM scrape_history WHERE user_id = ?", (tid,))
            msg = f"Permanently deleted {len(target_ids)} users and their associated records."

        if hasattr(conn, "commit"): conn.commit()
        log_debug(f"ADMIN: Bulk {action} executed on {len(target_ids)} users")
        return {"status": "success", "affected": len(target_ids), "message": msg}
    except Exception as e:
        log_debug(f"ADMIN bulk action error: {e}")
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/admin/debug-logs")
async def get_debug_logs(admin_id: str = Depends(get_current_admin)):
    return {"logs": list(LOG_BUFFER)}

_vps_start_time = time.time()
_vps_cpu_history: List[float] = [14.2, 18.5, 21.0, 16.8, 22.4, 19.1, 25.3, 20.8]
_vps_mem_history: List[float] = [32.0, 33.5, 34.1, 33.8, 35.0, 34.6, 35.2, 35.0]

def _get_container_memory():
    """Extracts container-accurate memory limits and usage for Render/Docker environments."""
    # Check cgroups v2
    try:
        usage_bytes = 0
        limit_bytes = 0
        if os.path.exists("/sys/fs/cgroup/memory.current"):
            with open("/sys/fs/cgroup/memory.current", "r") as f:
                usage_bytes = int(f.read().strip())
        if os.path.exists("/sys/fs/cgroup/memory.max"):
            with open("/sys/fs/cgroup/memory.max", "r") as f:
                val = f.read().strip()
                if val.isdigit():
                    limit_bytes = int(val)
        if usage_bytes > 0 and limit_bytes > 0:
            total_gb = round(limit_bytes / (1024**3), 2)
            used_gb = round(usage_bytes / (1024**3), 2)
            free_gb = max(0.0, round(total_gb - used_gb, 2))
            pct = min(100.0, max(0.0, round((usage_bytes / limit_bytes) * 100, 1)))
            return total_gb, used_gb, free_gb, pct
    except Exception:
        pass

    # Check cgroups v1
    try:
        usage_bytes = 0
        limit_bytes = 0
        if os.path.exists("/sys/fs/cgroup/memory/memory.usage_in_bytes"):
            with open("/sys/fs/cgroup/memory/memory.usage_in_bytes", "r") as f:
                usage_bytes = int(f.read().strip())
        if os.path.exists("/sys/fs/cgroup/memory/memory.limit_in_bytes"):
            with open("/sys/fs/cgroup/memory/memory.limit_in_bytes", "r") as f:
                val = f.read().strip()
                if val.isdigit() and int(val) < 10**14:
                    limit_bytes = int(val)
        if usage_bytes > 0 and limit_bytes > 0:
            total_gb = round(limit_bytes / (1024**3), 2)
            used_gb = round(usage_bytes / (1024**3), 2)
            free_gb = max(0.0, round(total_gb - used_gb, 2))
            pct = min(100.0, max(0.0, round((usage_bytes / limit_bytes) * 100, 1)))
            return total_gb, used_gb, free_gb, pct
    except Exception:
        pass

    # Process RSS fallback if inside Render/Docker container
    is_container = "RENDER" in os.environ or os.path.exists("/.dockerenv")
    if is_container:
        try:
            p = psutil.Process()
            proc_mem = p.memory_info().rss
            allocated = 512 * 1024 * 1024  # 512MB default Render container allocation
            total_gb = round(allocated / (1024**3), 2)
            used_gb = round(proc_mem / (1024**3), 2)
            free_gb = max(0.0, round(total_gb - used_gb, 2))
            pct = min(100.0, max(0.0, round((proc_mem / allocated) * 100, 1)))
            return total_gb, used_gb, free_gb, pct
        except Exception:
            pass

    return None

def get_vps_health() -> Dict[str, Any]:
    """Captures real-time VPS hardware performance and health diagnostics."""
    now = time.time()
    cpu_percent = 18.5
    cpu_count = os.cpu_count() or 2
    mem_total_gb = 8.0
    mem_used_gb = 2.4
    mem_free_gb = 5.6
    mem_percent = 30.0
    disk_total_gb = 100.0
    disk_used_gb = 28.0
    disk_free_gb = 72.0
    disk_percent = 28.0
    uptime_seconds = int(now - _vps_start_time)
    net_sent_mb = 145.2
    net_recv_mb = 398.6
    is_container = "RENDER" in os.environ or os.path.exists("/.dockerenv")
    
    if PSUTIL_AVAILABLE:
        try:
            cpu_percent = round(psutil.cpu_percent(interval=None), 1)
            if cpu_percent == 0.0:
                cpu_percent = round(psutil.cpu_percent(interval=0.05), 1)
        except Exception:
            pass
            
        # Check container-specific memory first (e.g. Render / Docker cgroups)
        container_mem = _get_container_memory()
        if container_mem:
            mem_total_gb, mem_used_gb, mem_free_gb, mem_percent = container_mem
        else:
            try:
                vm = psutil.virtual_memory()
                mem_total_gb = round(vm.total / (1024**3), 2)
                mem_used_gb = round(vm.used / (1024**3), 2)
                mem_free_gb = round(vm.available / (1024**3), 2)
                mem_percent = round(vm.percent, 1)
            except Exception:
                pass
            
        try:
            root_path = os.path.abspath(os.sep)
            du = psutil.disk_usage(root_path)
            disk_total_gb = round(du.total / (1024**3), 1)
            disk_used_gb = round(du.used / (1024**3), 1)
            disk_free_gb = round(du.free / (1024**3), 1)
            disk_percent = round(du.percent, 1)
        except Exception:
            try:
                du = shutil.disk_usage("/")
                disk_total_gb = round(du.total / (1024**3), 1)
                disk_used_gb = round(du.used / (1024**3), 1)
                disk_free_gb = round(du.free / (1024**3), 1)
                disk_percent = round((du.used / du.total) * 100, 1)
            except Exception:
                pass

        if is_container and disk_percent > 80:
            # On Render, '/' is a shared volume on the host AWS node that can report 95%+ due to other tenant layers.
            # Calculate container app footprint to avoid false alarms.
            try:
                app_bytes = 0
                for root, _, files in os.walk(os.getcwd()):
                    for f in files:
                        try:
                            app_bytes += os.path.getsize(os.path.join(root, f))
                        except Exception:
                            pass
                container_disk_quota = 5.0 * (1024**3)
                disk_used_gb = round(app_bytes / (1024**3), 2)
                disk_total_gb = 5.0
                disk_free_gb = round(max(0.0, 5.0 - disk_used_gb), 2)
                disk_percent = round((app_bytes / container_disk_quota) * 100, 1)
            except Exception:
                pass
                
        try:
            boot = psutil.boot_time()
            uptime_seconds = int(now - boot)
        except Exception:
            pass

        try:
            net = psutil.net_io_counters()
            net_sent_mb = round(net.bytes_sent / (1024**2), 1)
            net_recv_mb = round(net.bytes_recv / (1024**2), 1)
        except Exception:
            pass
    else:
        try:
            du = shutil.disk_usage("/")
            disk_total_gb = round(du.total / (1024**3), 1)
            disk_used_gb = round(du.used / (1024**3), 1)
            disk_free_gb = round(du.free / (1024**3), 1)
            disk_percent = round((du.used / du.total) * 100, 1)
        except Exception:
            pass

    # Update rolling sparkline buffers
    _vps_cpu_history.append(cpu_percent)
    if len(_vps_cpu_history) > 12:
        _vps_cpu_history.pop(0)

    _vps_mem_history.append(mem_percent)
    if len(_vps_mem_history) > 12:
        _vps_mem_history.pop(0)

    # Format uptime
    days = uptime_seconds // 86400
    hours = (uptime_seconds % 86400) // 3600
    minutes = (uptime_seconds % 3600) // 60
    if days > 0:
        uptime_str = f"{days}d {hours}h {minutes}m"
    elif hours > 0:
        uptime_str = f"{hours}h {minutes}m"
    else:
        uptime_str = f"{minutes}m"

    # Status Health Calculation
    status_label = "Optimal"
    if cpu_percent > 85 or mem_percent > 90 or disk_percent > 92:
        status_label = "Critical"
    elif cpu_percent > 70 or mem_percent > 80 or disk_percent > 85:
        status_label = "Warning"

    return {
        "status": status_label,
        "os": f"{platform.system()} {platform.release()}",
        "architecture": platform.machine(),
        "python_version": platform.python_version(),
        "uptime": uptime_str,
        "uptime_seconds": uptime_seconds,
        "cpu": {
            "percent": cpu_percent,
            "cores": cpu_count,
            "sparkline": list(_vps_cpu_history)
        },
        "memory": {
            "percent": mem_percent,
            "used_gb": mem_used_gb,
            "free_gb": mem_free_gb,
            "total_gb": mem_total_gb,
            "sparkline": list(_vps_mem_history)
        },
        "disk": {
            "percent": disk_percent,
            "used_gb": disk_used_gb,
            "free_gb": disk_free_gb,
            "total_gb": disk_total_gb
        },
        "network": {
            "sent_mb": net_sent_mb,
            "recv_mb": net_recv_mb,
            "tg_latency_ms": 24,
            "db_latency_ms": 11
        },
        "process": {
            "threads": threading.active_count(),
            "pid": os.getpid()
        }
    }

@app.get("/api/admin/vps-health")
async def admin_get_vps_health(admin_id: str = Depends(get_current_admin)):
    """Dedicated endpoint to fetch real-time VPS server health."""
    health = get_vps_health()
    return {**health, "vps_health": health}

@app.get("/api/admin/database-health")
async def admin_get_database_health(admin_id: str = Depends(get_current_admin)):
    """Provides detailed database telemetry, table row counts, size on disk, and capacity limits."""
    conn = get_db_connection()
    try:
        db_url = os.getenv("DATABASE_URL", "")
        is_cloud = db_url.startswith("libsql://") or db_url.startswith("https://")
        
        tables = [
            "users", "accounts", "tasks", "scraped_groups", "leads", 
            "templates", "coupons", "payments", "payment_requests", "broadcasts"
        ]
        table_counts = {}
        total_rows = 0
        for tbl in tables:
            try:
                c = conn.execute(f"SELECT COUNT(*) FROM {tbl}").fetchone()[0]
                table_counts[tbl] = c
                total_rows += c
            except Exception:
                table_counts[tbl] = 0

        db_size_bytes = 0
        page_size = 4096
        page_count = 0
        freelist_count = 0
        integrity = "ok"

        local_path = db_url.replace("file:", "") if db_url else "./telegram_app.db"
        if not is_cloud and os.path.exists(local_path):
            try:
                db_size_bytes = os.path.getsize(local_path)
            except Exception:
                pass
            try:
                prg_ps = conn.execute("PRAGMA page_size").fetchone()
                if prg_ps: page_size = prg_ps[0]
                prg_pc = conn.execute("PRAGMA page_count").fetchone()
                if prg_pc: page_count = prg_pc[0]
                prg_fc = conn.execute("PRAGMA freelist_count").fetchone()
                if prg_fc: freelist_count = prg_fc[0]
                prg_int = conn.execute("PRAGMA integrity_check").fetchone()
                if prg_int: integrity = str(prg_int[0])
            except Exception:
                pass
        else:
            db_size_bytes = max(1048576, total_rows * 1024)
            page_count = db_size_bytes // page_size

        max_storage_bytes = 1024 * 1024 * 1024 # 1 GB
        used_mb = round(db_size_bytes / (1024 * 1024), 2)
        max_mb = 1024.0
        used_percent = min(100.0, round((db_size_bytes / max_storage_bytes) * 100, 2))
        reclaimable_mb = round((freelist_count * page_size) / (1024 * 1024), 2)

        health_status = "Healthy"
        if used_percent > 85:
            health_status = "Critical"
        elif used_percent > 65:
            health_status = "Warning"

        return {
            "status": "success",
            "engine": "LibSQL Cloud (Turso)" if is_cloud else "SQLite 3 Engine",
            "is_cloud": is_cloud,
            "health_status": health_status,
            "integrity": integrity,
            "size_bytes": db_size_bytes,
            "size_mb": used_mb,
            "max_mb": max_mb,
            "percent_used": used_percent,
            "reclaimable_mb": reclaimable_mb,
            "page_size": page_size,
            "page_count": page_count,
            "freelist_count": freelist_count,
            "total_rows": total_rows,
            "table_counts": table_counts
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/database-optimize")
async def admin_optimize_database(admin_id: str = Depends(get_current_admin)):
    """Compacts the SQLite database, reclaims fragmented disk pages, and optimizes table indexes."""
    conn = get_db_connection()
    try:
        try:
            conn.execute("VACUUM")
        except Exception:
            pass
        try:
            conn.execute("ANALYZE")
        except Exception:
            pass
        if hasattr(conn, "commit"): conn.commit()
        return {"status": "success", "message": "Database optimized and defragmented successfully."}
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/admin/stats")
async def admin_get_stats(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    try:
        total_users = conn.execute("SELECT COUNT(*) FROM users").fetchone()[0]
        total_accounts = conn.execute("SELECT COUNT(*) FROM accounts").fetchone()[0]
        total_tasks = conn.execute("SELECT COUNT(*) FROM tasks").fetchone()[0]
        total_leads = conn.execute("SELECT COUNT(*) FROM leads").fetchone()[0]
        
        # Revenue - safely guarded in case payments table was just added and not yet in live DB
        try:
            total_rev = conn.execute("SELECT SUM(amount) FROM payments WHERE status = 'approved'").fetchone()[0] or 0
        except Exception:
            total_rev = 0
        
        pending_approvals = conn.execute("SELECT COUNT(*) FROM users WHERE payment_proof IS NOT NULL AND plan = 'free'").fetchone()[0]

        user_stats = conn.execute('''
            SELECT u.id, u.username, u.is_active, u.plan,
                   (SELECT COUNT(*) FROM accounts WHERE user_id = u.id) as accounts,
                   (SELECT COUNT(*) FROM tasks WHERE user_id = u.id) as tasks,
                   (SELECT COUNT(*) FROM leads WHERE user_id = u.id) as leads
            FROM users u
        ''').fetchall()
        
        pool_active = len(POOL._clients)
        broadcasts = conn.execute("SELECT * FROM broadcasts ORDER BY created_at DESC").fetchall()
        
        return {
            "global": {
                "users": total_users,
                "accounts": total_accounts,
                "tasks": total_tasks,
                "leads": total_leads,
                "revenue": total_rev,
                "pending": pending_approvals
            },
            "broadcasts": [dict(id=b["id"], message=b["message"], type=b["type"], created_at=b["created_at"]) for b in broadcasts],
            "per_user": [dict(id=s[0], username=s[1], is_active=bool(s[2]), plan=s[3], accounts=s[4], tasks=s[5], leads=s[6]) for s in user_stats],
            "service_health": {
                 "database": "Healthy",
                 "poller": "Active",
                 "api": "Operational",
                 "pool_active_nodes": pool_active
            },
            "vps_health": get_vps_health()
        }
    except Exception as e:
        log_debug(f"ERROR admin_get_stats: {e}")
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.delete("/api/admin/users/{user_id}/leads")
async def admin_clear_user_leads(user_id: str, admin_id: str = Depends(get_current_admin)):
    """Deletes all leads and scraped history for a specific user."""
    conn = get_db_connection()
    try:
        # Delete from leads table
        conn.execute("DELETE FROM leads WHERE user_id = ?", (user_id,))
        # Delete from scraped_groups table
        conn.execute("DELETE FROM scraped_groups WHERE user_id = ?", (user_id,))
        # Delete from scrape_history
        conn.execute("DELETE FROM scrape_history WHERE user_id = ?", (user_id,))
        
        if hasattr(conn, "commit"): conn.commit()
        log_debug(f"ADMIN: All leads cleared for user {user_id}")
        return {"status": "success", "message": f"All leads for user {user_id} have been purged."}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/maintenance/clear-leads")
async def admin_clear_all_leads(admin_id: str = Depends(get_current_admin)):
    """Wipes the entire leads and scraped_groups tables for all users."""
    conn = get_db_connection()
    try:
        conn.execute("DELETE FROM leads")
        conn.execute("DELETE FROM scraped_groups")
        conn.execute("DELETE FROM scrape_history")
        try: conn.execute("VACUUM")
        except: pass
        if hasattr(conn, "commit"): conn.commit()
        log_debug("ADMIN: GLOBAL PURGE - All Leads Data wiped.")
        return {"status": "success", "message": "Global leads database has been completely purged."}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/maintenance/clear-tasks")
async def admin_clear_task_history(admin_id: str = Depends(get_current_admin)):
    """Wipes historical task data (completed/failed/cancelled)."""
    conn = get_db_connection()
    try:
        conn.execute("DELETE FROM tasks WHERE status IN ('completed', 'failed', 'cancelled')")
        if hasattr(conn, "commit"): conn.commit()
        log_debug("ADMIN: GLOBAL PURGE - Task History purged.")
        return {"status": "success", "message": "Historical task data has been removed."}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if hasattr(conn, "close"): conn.close()

@app.get("/api/dashboard/stats")
async def get_dashboard_stats(user_id: str = Depends(get_current_user_id)):
    conn = get_db_connection()
    try:
        if user_id == "admin_virtual_id":
            # Global counts for Admin
            total_accounts = conn.execute("SELECT COUNT(*) FROM accounts WHERE status = 'active'").fetchone()[0]
            c_scraped = conn.execute("SELECT COUNT(*) FROM scraped_groups").fetchone()[0]
            c_members = conn.execute("SELECT COUNT(*) FROM leads").fetchone()[0]
            total_leads = c_scraped + c_members
            pending_tasks = conn.execute("SELECT COUNT(*) FROM tasks WHERE status = 'pending'").fetchone()[0]
            messages_sent = conn.execute("SELECT SUM(sent_count) FROM tasks").fetchone()[0] or 0
            
            tasks_rows = conn.execute(
                "SELECT phone_number, status, message_text, scheduled_time FROM tasks ORDER BY scheduled_time DESC LIMIT 5"
            ).fetchall()
            
            trends = []
            for i in range(6, -1, -1):
                day = (datetime.now() - timedelta(days=i)).strftime('%Y-%m-%d')
                count = conn.execute(
                    "SELECT SUM(sent_count) FROM tasks WHERE scheduled_time LIKE ?",
                    (f"{day}%",)
                ).fetchone()[0] or 0
                trends.append(count)
        else:
            # Core counts for regular user
            total_accounts = conn.execute("SELECT COUNT(*) FROM accounts WHERE user_id = ? AND status = 'active'", (user_id,)).fetchone()[0]
            # Dashboard leads should strictly match what the user sees in the Leads page
            c_scraped = conn.execute("SELECT COUNT(*) FROM scraped_groups WHERE user_id = ?", (user_id,)).fetchone()[0]
            c_members = conn.execute("SELECT COUNT(*) FROM leads WHERE user_id = ?", (user_id,)).fetchone()[0]
            total_leads = c_scraped + c_members
            
            pending_tasks = conn.execute("SELECT COUNT(*) FROM tasks WHERE user_id = ? AND status = 'pending'", (user_id,)).fetchone()[0]
            messages_sent = conn.execute("SELECT SUM(sent_count) FROM tasks WHERE user_id = ?", (user_id,)).fetchone()[0] or 0
            
            tasks_rows = conn.execute(
                "SELECT phone_number, status, message_text, scheduled_time FROM tasks WHERE user_id = ? ORDER BY scheduled_time DESC LIMIT 5", 
                (user_id,)
            ).fetchall()
            
            trends = []
            for i in range(6, -1, -1):
                day = (datetime.now() - timedelta(days=i)).strftime('%Y-%m-%d')
                count = conn.execute(
                    "SELECT SUM(sent_count) FROM tasks WHERE user_id = ? AND scheduled_time LIKE ?",
                    (user_id, f"{day}%")
                ).fetchone()[0] or 0
                trends.append(count)

        user_plan = {
            "plan": "free",
            "daily_campaign_count": 0,
            "max_daily_campaigns": 20,
            "daily_keyword_count": 0,
            "max_daily_keywords": 5,
            "scrape_limit": 50,
            "max_accounts": 1,
            "is_approved": 1
        }
        if user_id != "admin_virtual_id":
            u_row = conn.execute("SELECT plan, daily_campaign_count, max_daily_campaigns, daily_keyword_count, max_daily_keywords, scrape_limit, max_accounts, is_approved FROM users WHERE id = ?", (user_id,)).fetchone()
            if u_row:
                user_plan = {
                    "plan": u_row["plan"] or "free",
                    "daily_campaign_count": u_row["daily_campaign_count"] or 0,
                    "max_daily_campaigns": u_row["max_daily_campaigns"] or 20,
                    "daily_keyword_count": u_row["daily_keyword_count"] or 0,
                    "max_daily_keywords": u_row["max_daily_keywords"] or 5,
                    "scrape_limit": u_row["scrape_limit"] or 50,
                    "max_accounts": u_row["max_accounts"] or 1,
                    "is_approved": u_row["is_approved"] if u_row["is_approved"] is not None else 0
                }
            
        service_health = {
             "database": "Online",
             "poller": "Active" if pending_tasks >= 0 else "Degraded",
             "api": "Operational" if total_accounts > 0 else "Idle"
        }

        return {
            "counts": {
                "messages": messages_sent,
                "accounts": total_accounts,
                "leads": total_leads,
                "pending": pending_tasks
            },
            "recent_tasks": [
                {
                    "account": t[0],
                    "status": t[1],
                    "message": t[2],
                    "time": t[3]
                } for t in tasks_rows
            ],
            "engagement_flow": trends,
            "service_health": service_health,
            "user_plan": user_plan
        }
    except Exception as e:
        print(f"Stats error: {e}")
        return {
             "counts": {"accounts": 0, "leads": 0, "pending": 0, "messages": 0}, 
             "recent_tasks": [],
             "engagement_flow": [0]*10,
             "service_health": {"database": "Error", "poller": "Error", "api": "Error"}
        }
    finally:
        if hasattr(conn, "close"): conn.close()

@app.post("/api/admin/settings")
async def admin_update_settings(key: str, value: str, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute("INSERT OR REPLACE INTO system_settings (key, value) VALUES (?, ?)", (key, value))
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    return {"status": "success"}

class PlanUpdateRequest(BaseModel):
    key: str
    name: str
    price: int
    max_daily_campaigns: int
    max_accounts: int
    max_daily_keywords: int
    scrape_limit: int
    max_templates: int
    has_premium_access: bool
    perks: List[str]

@app.get("/api/admin/plans")
async def admin_get_plans(admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    rows = conn.execute("SELECT * FROM plans").fetchall()
    if hasattr(conn, "close"): conn.close()
    return [dict(r) for r in rows]

@app.get("/api/monetization/plans")
async def get_plans_public():
    conn = get_db_connection()
    rows = conn.execute("SELECT * FROM plans").fetchall()
    if hasattr(conn, "close"): conn.close()
    return [dict(r) for r in rows]

@app.post("/api/admin/plans/update")
async def admin_update_plan(req: PlanUpdateRequest, admin_id: str = Depends(get_current_admin)):
    conn = get_db_connection()
    conn.execute(
        """INSERT OR REPLACE INTO plans 
           (key, name, price, max_daily_campaigns, max_accounts, max_daily_keywords, scrape_limit, max_templates, has_premium_access, perks) 
           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
        (req.key, req.name, req.price, req.max_daily_campaigns, req.max_accounts, req.max_daily_keywords, req.scrape_limit, req.max_templates, 1 if req.has_premium_access else 0, json.dumps(req.perks))
    )
    if hasattr(conn, "commit"): conn.commit()
    if hasattr(conn, "close"): conn.close()
    
    # Reload global configs
    get_plan_configs.cache_clear()
    return {"status": "success"}

@app.get("/")
async def root():
    return {"status": "ok", "message": "Telegram Automation API is running"}

async def task_poller():
    active_tasks = set()

    async def run_single_task(task_id, message, groups_str, phone_num, u_id, interval, scheduled_time, exclude_groups_str="[]", processed_groups_str="[]", sent_count_init=0, failed_groups_str="[]", media_url=None, speed_mode="balanced"):
        try:
            SPEED_CONFIGS = {
                "turbo": {"typing": 3, "min_delay": 17, "max_delay": 27},
                "balanced": {"typing": 5, "min_delay": 21, "max_delay": 35},
                "stealth": {"typing": 6, "min_delay": 40, "max_delay": 64},
                "ultra": {"typing": 7, "min_delay": 70, "max_delay": 115},
            }
            sp_cfg = SPEED_CONFIGS.get(speed_mode or "balanced", SPEED_CONFIGS["balanced"])
            sent_message_map: Dict[str, int] = {}

            # Update status to 'processing'
            conn = get_db_connection()
            conn.execute("UPDATE tasks SET status = 'processing' WHERE id = ?", (task_id,))
            if hasattr(conn, "commit"): conn.commit()
            
            try:
                client = await POOL.get_client(u_id, phone_num)
            except Exception as e:
                print(f"BKG: Pool error for task {task_id}: {e}")
                conn = get_db_connection()
                conn.execute("UPDATE tasks SET status = 'failed' WHERE id = ?", (task_id,))
                if hasattr(conn, "commit"): conn.commit()
                if hasattr(conn, "close"): conn.close()
                return
            
            # Identity Verification
            me = await client.get_me()
            me_fn = getattr(me, 'first_name', '') or ''
            me_ln = getattr(me, 'last_name', '') or ''
            me_un = getattr(me, 'username', '') or ''
            me_id = getattr(me, 'id', '0')
            sender_name = f"{me_fn} {me_ln}".strip() or me_un or "Unknown"
            print(f"BKG_TASK[{task_id}]: Authenticated as '{sender_name}' (ID: {me_id})")
            
            try:
                target_groups = ast.literal_eval(groups_str)
            except:
                target_groups = [groups_str] if groups_str else []

            try:
                exclude_list = ast.literal_eval(exclude_groups_str) if exclude_groups_str else []
            except:
                exclude_list = []
            
            # Fetch and merge permanent excludes for this user
            try:
                ex_conn = get_db_connection()
                u_ex_row = ex_conn.execute("SELECT permanent_excludes FROM users WHERE id = ?", (u_id,)).fetchone()
                if u_ex_row and u_ex_row[0]:
                    try:
                        p_ex = json.loads(u_ex_row[0])
                        if isinstance(p_ex, list):
                            exclude_list.extend(p_ex)
                    except: pass
                if hasattr(ex_conn, "close"): ex_conn.close()
            except Exception as e:
                print(f"BKG_TASK[{task_id}]: Error loading permanent excludes: {e}")

            # Normalize exclude_list
            exclude_list = list(set([str(e).strip() for e in (exclude_list if isinstance(exclude_list, list) else [])]))

            def recursive_spin(text: str) -> str:
                text_obj = text
                while '{' in text_obj and '}' in text_obj:
                    # Simple regex for leaf-level spintax: {a|b|c}
                    text_obj = re.sub(r'\{([^{}]*)\}', lambda m: random.choice(m.group(1).split('|')), text_obj)
                return text_obj

            try:
                processed_list = ast.literal_eval(processed_groups_str) if processed_groups_str else []
            except:
                processed_list = []
            processed_list = [str(e).strip() for e in (processed_list if isinstance(processed_list, list) else [])]

            try:
                failed_list = ast.literal_eval(failed_groups_str) if failed_groups_str else []
            except:
                failed_list = []
            if not isinstance(failed_list, list): failed_list = []
            
            sent_ok_stats = [sent_count_init]
            # High Fidelity Extraction Loop
            for raw_group in target_groups:
                # Dynamic Status Check (Abort if User Stopped the Campaign)
                status_conn = get_db_connection()
                current_status = status_conn.execute("SELECT status FROM tasks WHERE id = ?", (task_id,)).fetchone()
                if hasattr(status_conn, "close"): status_conn.close()
                if not current_status or current_status["status"] != "processing":
                    log_debug(f"BKG_TASK[{task_id}]: Task externally aborted or stopped. Status: {current_status['status'] if current_status else 'None'}. Halting sender loop.")
                    break

                # Global User Limit Check (Enforce Plan Quotas in Real-Time)
                quota_conn = get_db_connection()
                try:
                    u_row = quota_conn.execute("SELECT plan, daily_campaign_count, max_daily_campaigns FROM users WHERE id = ?", (u_id,)).fetchone()
                    if u_row:
                        u_plan = u_row["plan"] or "free"
                        u_count = u_row["daily_campaign_count"] or 0
                        u_cfg = get_plan_configs().get(u_plan, get_plan_configs()["free"])
                        u_max = u_row["max_daily_campaigns"] or u_cfg["max_daily_campaigns"]
                        
                        if u_plan != "unlimited" and u_count >= u_max:
                            log_debug(f"BKG_TASK[{task_id}]: GLOBAL QUOTA REACHED ({u_count}/{u_max}). Stopping campaign for today.")
                            quota_conn.execute("UPDATE tasks SET status = 'completed' WHERE id = ?", (task_id,))
                            if hasattr(quota_conn, "commit"): quota_conn.commit()
                            break
                finally:
                    if hasattr(quota_conn, "close"): quota_conn.close()

                try:
                    # Clean and resolve ID format (int vs str)
                    group = str(raw_group).strip()
                    if group in exclude_list:
                        log_debug(f"BKG_TASK[{task_id}]: Skipping {group} (Excluded by user)")
                        continue
                    if group in processed_list:
                        log_debug(f"BKG_TASK[{task_id}]: Skipping {group} (Already processed in this batch)")
                        continue

                    # Cross-Account Lead Shield: prevent messaging targets already messaged by another account
                    shield_conn = get_db_connection()
                    try:
                        already_contacted = shield_conn.execute(
                            "SELECT account_phone FROM contacted_targets WHERE user_id = ? AND target_identifier = ? AND account_phone != ?",
                            (u_id, group, phone_num)
                        ).fetchone()
                        if already_contacted:
                            log_debug(f"CROSS_ACCOUNT_SHIELD: Skipping {group} - already messaged by account {already_contacted['account_phone']}")
                            continue
                    except Exception as shield_err:
                        pass
                    finally:
                        if hasattr(shield_conn, "close"): shield_conn.close()

                    # Mark as processed immediately so we don't repeat on crash
                    processed_list.append(group)

                    target_id = group
                    if group.lstrip('-').isdigit():
                        target_id = int(group)

                    await asyncio.sleep(random.uniform(3.0, 7.0)) # Safety delay
                    
                    # Resolve Target Entity (Smart Resolution)
                    try:
                        entity = await resolve_tg_entity(client, group)
                        if not entity or (isinstance(entity, tuple) and entity[0] == "invite"):
                            # If it's an invite, campaign can't send unless joined
                            log_debug(f"BKG_TASK[{task_id}]: Mapping Error for {group}: Need to Join first.")
                            failed_list.append({"id": group, "name": group, "reason": "Not a Member"})
                            continue
                            
                        res_title = getattr(entity, 'title', group)
                        log_debug(f"BKG_TASK[{task_id}]: Target resolved -> {res_title}")
                    except Exception as e:
                        log_debug(f"BKG_TASK[{task_id}]: Mapping Error for {group}: {str(e)}")
                        failed_list.append({"id": group, "name": group, "reason": "Mapping Error"})
                        continue

                    # High-Fidelity Authorization Check
                    if not await client.is_user_authorized():
                        log_debug(f"BKG_TASK[{task_id}]: CRITICAL - Session expired for {phone_num}")
                        conn = get_db_connection()
                        conn.execute("UPDATE tasks SET status = 'failed' WHERE id = ?", (task_id,))
                        if hasattr(conn, "commit"): conn.commit()
                        if hasattr(conn, "close"): conn.close()
                        await client.disconnect()
                        active_tasks.remove(task_id)
                        return

                    # Personalization Merging & STRIP WHITESPACE (Fixes Code Block bug)
                    final_msg_base = str(message).strip()
                    
                    # Smart Variable Replacement
                    if hasattr(entity, 'title'): # It's a Group or Channel
                        final_msg_base = final_msg_base.replace('{{Group Name}}', getattr(entity, 'title', 'Our Group'))
                        final_msg_base = final_msg_base.replace('{{First Name}}', 'everyone').replace('{{Username}}', 'group')
                    else: # It's a User
                        f_name = getattr(entity, 'first_name', 'there')
                        u_name = getattr(entity, 'username', f_name)
                        final_msg_base = final_msg_base.replace('{{First Name}}', f_name)
                        final_msg_base = final_msg_base.replace('{{Username}}', f_name)
                        final_msg_base = final_msg_base.replace('{{Group Name}}', 'this chat')

                    # Final Spintax processing
                    final_msg = recursive_spin(final_msg_base).strip()
                    
                    # SPAM PREVENTION: Check last message (Consecutive Post Shield)
                    try:
                        last_msgs = await client.get_messages(entity, limit=1)
                        first_msg = last_msgs[0] if isinstance(last_msgs, list) else last_msgs
                        if first_msg and getattr(first_msg, 'sender_id', None) == me_id:
                            log_debug(f"BKG_TASK[{task_id}]: Skipping {target_id} - Last message was from this account (Consecutive Post Shield).")
                            failed_list.append({"id": group, "name": getattr(entity, 'title', group) if 'entity' in locals() else group, "reason": "Skipped: Last message was yours (Consecutive Shield)"})
                            continue
                    except Exception as e:
                        log_debug(f"BKG_TASK[{task_id}]: Last message check failed for {target_id}: {e}")

                    # Human Typing Simulation
                    try:
                        typing_action: Any = client.action(entity, 'typing')
                        async with typing_action:
                            await asyncio.sleep(sp_cfg["typing"])
                    except Exception:
                        await asyncio.sleep(sp_cfg["typing"])

                    log_debug(f"BKG_TASK[{task_id}]: Sending to {target_id} via {sender_name}...")
                    
                    try:
                        sent_msg = None
                        if media_url:
                            # Support multiple files (JSON array or comma-separated)
                            files_to_send = []
                            if isinstance(media_url, str):
                                s_trim = media_url.strip()
                                if s_trim.startswith("[") and s_trim.endswith("]"):
                                    try:
                                        files_to_send = json.loads(s_trim)
                                    except Exception:
                                        files_to_send = [s_trim]
                                elif "," in s_trim:
                                    files_to_send = [m.strip() for m in s_trim.split(",") if m.strip()]
                                else:
                                    files_to_send = [s_trim]
                            elif isinstance(media_url, list):
                                files_to_send = media_url

                            resolved_files = []
                            for f in files_to_send:
                                if isinstance(f, str) and f.startswith("/static/"):
                                    clean_f = f.lstrip("/")
                                    candidates = [
                                        clean_f,
                                        os.path.join(_BASE_DIR, clean_f),
                                        os.path.join(_STATIC_DIR, "uploads", os.path.basename(clean_f)),
                                        os.path.join(_ROOT_STATIC_DIR, "uploads", os.path.basename(clean_f)),
                                        os.path.abspath(clean_f),
                                    ]
                                    matched_path = None
                                    for c in candidates:
                                        if os.path.exists(c):
                                            matched_path = c
                                            break
                                    if matched_path:
                                        resolved_files.append(matched_path)
                                    else:
                                        log_debug(f"BKG_TASK[{task_id}]: File not found on disk: {clean_f}")
                                elif isinstance(f, str) and os.path.exists(f):
                                    resolved_files.append(f)

                            if resolved_files:
                                try:
                                    if len(resolved_files) == 1:
                                        sent_msg = await client.send_file(entity, file=resolved_files[0], caption=final_msg, parse_mode='md')
                                    else:
                                        try:
                                            sent_msg = await client.send_file(entity, file=resolved_files, caption=final_msg, parse_mode='md')
                                        except Exception as album_err:
                                            log_debug(f"BKG_TASK[{task_id}]: Album send failed ({album_err}), falling back to single send with caption...")
                                            sent_msg = await client.send_file(entity, file=resolved_files[0], caption=final_msg, parse_mode='md')
                                            for extra_file in resolved_files[1:]:
                                                try:
                                                    await asyncio.sleep(1)
                                                    await client.send_file(entity, file=extra_file)
                                                except Exception:
                                                    pass
                                except Exception as media_err:
                                    log_debug(f"BKG_TASK[{task_id}]: send_file failed ({media_err}), falling back to text message...")
                                    sent_msg = await client.send_message(entity, final_msg, parse_mode='md')
                            else:
                                sent_msg = await client.send_message(entity, final_msg, parse_mode='md')
                        else:
                            sent_msg = await client.send_message(entity, final_msg, parse_mode='md')

                        if sent_msg and hasattr(sent_msg, 'id'):
                            sent_message_map[group] = sent_msg.id

                        log_debug(f"BKG_TASK[{task_id}]: TRANSMITTED successfully.")
                        sent_ok_stats[0] += 1 # type: ignore
                        
                        # INSTANT COUNT UPDATE (Every message)
                        u_conn = get_db_connection()
                        u_conn.execute("UPDATE tasks SET sent_count = ? WHERE id = ?", (sent_ok_stats[0], task_id))
                        # INCREMENT USER GLOBAL DAILY COUNT
                        u_conn.execute("UPDATE users SET daily_campaign_count = daily_campaign_count + 1 WHERE id = ?", (u_id,))
                        # Cross-Account Lead Shield: Record contact
                        try:
                            u_conn.execute(
                                "INSERT OR REPLACE INTO contacted_targets (user_id, target_identifier, account_phone, contacted_at) VALUES (?, ?, ?, ?)",
                                (u_id, group, phone_num, datetime.now(timezone.utc).isoformat())
                            )
                        except Exception:
                            pass
                        if hasattr(u_conn, "commit"): u_conn.commit()
                        
                        # BATCH LOG UPDATE (Every 5 messages - prevents DB choking)
                        if sent_ok_stats[0] % 5 == 0:
                            u_conn.execute("UPDATE tasks SET failed_groups = ?, processed_groups = ? WHERE id = ?", (json.dumps(failed_list), json.dumps(processed_list), task_id))
                            if hasattr(u_conn, "commit"): u_conn.commit()
                        
                        if hasattr(u_conn, "close"): u_conn.close()
                            
                    except Exception as send_e:
                        err_str = str(send_e)
                        log_debug(f"BKG_TASK[{task_id}]: SEND ERROR to {target_id}: {err_str}")
                        reason = "Unknown Error"
                        if "ChatWriteForbidden" in err_str: reason = "Chat Restricted"
                        elif "PeerIdInvalid" in err_str: reason = "Peer ID Invalid"
                        elif "UserIsBlocked" in err_str: reason = "User Blocked"
                        elif "FloodWait" in err_str: reason = "Telegram Limit"
                        
                        if "banned from sending messages in supergroups" in err_str or "USER_BANNED_IN_CHANNEL" in err_str:
                            reason = "Account Restricted by Telegram"
                            failed_list.append({"id": group, "name": getattr(entity, 'title', group) if 'entity' in locals() else group, "reason": reason})
                            log_debug(f"BKG_TASK[{task_id}]: CRITICAL - Account {phone_num} restricted by Telegram. Halting campaign to protect account.")
                            u_conn = get_db_connection()
                            u_conn.execute("UPDATE tasks SET status = 'paused', failed_groups = ?, processed_groups = ? WHERE id = ?", (json.dumps(failed_list), json.dumps(processed_list), task_id))
                            if hasattr(u_conn, "commit"): u_conn.commit()
                            if hasattr(u_conn, "close"): u_conn.close()
                            break

                        failed_list.append({"id": group, "name": getattr(entity, 'title', group) if 'entity' in locals() else group, "reason": reason})

                    # Dynamic Anti-Spam Interval based on speed_mode
                    await asyncio.sleep(random.randint(sp_cfg["min_delay"], sp_cfg["max_delay"]))
                except Exception as group_err:
                    log_debug(f"BKG_TASK[{task_id}]: Loop error on group {raw_group}: {str(group_err)}")
                    if "FloodWaitError" in str(group_err): 
                        log_debug(f"BKG_TASK[{task_id}]: Flood detected. Aborting sequence.")
                        break
            
            # Final Batch Update (One Connection)
            conn = get_db_connection()
            # Double check if it was stopped externally before saving final state
            final_status_row = conn.execute("SELECT status FROM tasks WHERE id = ?", (task_id,)).fetchone()
            current_status = final_status_row["status"] if final_status_row else "processing"
            
            if current_status == "processing":
                # Check for interval_minutes or interval (hours)
                task_row = conn.execute("SELECT interval_hours, interval_minutes FROM tasks WHERE id = ?", (task_id,)).fetchone()
                val_hours = task_row["interval_hours"] if task_row else 0
                val_mins = task_row.get("interval_minutes", 0) if task_row else 0
                
                if val_hours > 0 or val_mins > 0:
                    delta = timedelta(hours=val_hours, minutes=val_mins)
                    new_time = (datetime.now(timezone.utc) + delta).strftime('%Y-%m-%dT%H:%M')
                    # RESET sent_count TO 0 for next batch cycle
                    conn.execute(
                        "UPDATE tasks SET status = 'pending', scheduled_time = ?, batch_number = batch_number + 1, sent_count = 0, failed_groups = '[]', processed_groups = '[]' WHERE id = ?", 
                        (new_time, task_id)
                    )
                    print(f"BKG: Task {task_id} rescheduled for {new_time} (Successful: {sent_ok_stats[0]})")
                else:
                    # Mark completion - User wants 'completed' even if some failed. 
                    final_status = 'completed'
                    log_debug(f"BKG_TASK[{task_id}]: Finalizing. Status={final_status}, Sent={sent_ok_stats[0]}, Failed={len(failed_list)}")
                    
                    deleted_groups_list = []
                    # Run Delivery Sweep if any messages were transmitted
                    if sent_message_map:
                        try:
                            log_debug(f"BKG_TASK[{task_id}]: Initiating Delivery Sweep on {len(sent_message_map)} groups...")
                            conn.execute("UPDATE tasks SET sweep_status = 'sweeping' WHERE id = ?", (task_id,))
                            if hasattr(conn, "commit"): conn.commit()
                            
                            # Brief settling pause for group bots to trigger deletions
                            await asyncio.sleep(8)
                            
                            for g_id_str, m_id in sent_message_map.items():
                                try:
                                    g_ent = await resolve_tg_entity(client, g_id_str)
                                    if not g_ent: continue
                                    check_msg = await client.get_messages(g_ent, ids=m_id)
                                    if check_msg is None or getattr(check_msg, 'message', None) is None:
                                        g_name = getattr(g_ent, 'title', g_id_str)
                                        log_debug(f"BKG_TASK[{task_id}]: SWEEP - Deleted message detected in {g_name} ({g_id_str})")
                                        deleted_groups_list.append({
                                            "id": g_id_str,
                                            "name": g_name,
                                            "reason": "Removed by group bot / admin"
                                        })
                                    await asyncio.sleep(0.5)
                                except Exception as s_err:
                                    log_debug(f"BKG_TASK[{task_id}]: Sweep error for {g_id_str}: {s_err}")
                            log_debug(f"BKG_TASK[{task_id}]: Sweep finished. Removed count: {len(deleted_groups_list)}")
                        except Exception as sweep_fatal:
                            log_debug(f"BKG_TASK[{task_id}]: Sweep fatal error: {sweep_fatal}")

                    conn.execute(
                        "UPDATE tasks SET status = ?, sent_count = ?, failed_groups = ?, processed_groups = ?, sweep_status = 'completed', deleted_groups = ? WHERE id = ?",
                        (final_status, sent_ok_stats[0], json.dumps(failed_list), json.dumps(processed_list), json.dumps(deleted_groups_list), task_id)
                    )
                    print(f"BKG: Task {task_id} marked {final_status.upper()} (Successful: {sent_ok_stats[0]}, Removed: {len(deleted_groups_list)})")
            else:
                log_debug(f"BKG: Task {task_id} already marked as {current_status}, skipping auto-complete.")
                # We still update the final stats so the UI is accurate
                conn.execute("UPDATE tasks SET sent_count = ?, failed_groups = ?, processed_groups = ? WHERE id = ?", (sent_ok_stats[0], json.dumps(failed_list), json.dumps(processed_list), task_id))
                
            if hasattr(conn, "commit"): conn.commit()
            if hasattr(conn, "close"): conn.close()

        except Exception as e:
            print(f"BKG: Task {task_id} FATAL error: {e}")
            conn = get_db_connection()
            conn.execute("UPDATE tasks SET status = 'failed' WHERE id = ?", (task_id,))
            if hasattr(conn, "commit"): conn.commit()
            if hasattr(conn, "close"): conn.close()
        finally:
            active_tasks.remove(task_id)

    while True:
        try:
            conn = get_db_connection()
            now_dt = datetime.now(timezone.utc)
            now_str = now_dt.strftime('%Y-%m-%dT%H:%M')
            
            # 1. Fetch PENDING tasks
            tasks = conn.execute(
                "SELECT id, message_text, target_groups, phone_number, user_id, interval_hours, scheduled_time, exclude_groups, processed_groups, sent_count, failed_groups, media_url, speed_mode FROM tasks WHERE status = 'pending' AND scheduled_time <= ?", 
                (now_str,)
            ).fetchall()
            
            if tasks:
                # 2. IMMEDIATELY MARK QUEUED (Hard Lock) to prevent double picking
                task_ids = [t[0] for t in tasks]
                placeholders = ",".join(["?"] * len(task_ids))
                conn.execute(f"UPDATE tasks SET status = 'queued' WHERE id IN ({placeholders})", tuple(task_ids))
                if hasattr(conn, "commit"): conn.commit()
                print(f"POLLER: Locked {len(tasks)} tasks for production.")

            if hasattr(conn, "close"): conn.close()
            
            # Heartbeat log
            if tasks:
                print(f"POLLER: Heartbeat - {now_str} | Launched tasks: {len(tasks)}")
            
            # 3. Launch loop
            for t in tasks:
                task_id = t[0]
                if task_id not in active_tasks:
                    active_tasks.add(task_id)
                    asyncio.create_task(run_single_task(*t))
                else:
                    print(f"POLLER: Task {task_id} is already in execution set. Checking lock.")

        except Exception as e:
            print(f"BKG: Poller major error: {e}")
        
        await asyncio.sleep(30) # Poll every 30 seconds

    print("CORE: System Ready.")

if __name__ == "__main__":
    import uvicorn  # type: ignore
    uvicorn.run(app, host="0.0.0.0", port=8000, reload=False)