File size: 154,268 Bytes
7f6c4f8
d221625
7f6c4f8
 
 
 
a581495
fa615e3
 
d221625
a40ecea
 
d221625
a581495
7f6c4f8
66fb4d9
 
a40ecea
7f6c4f8
d221625
2e5dd6d
7f6c4f8
 
 
 
 
 
69abd0a
d221625
a40ecea
 
 
69abd0a
7f6c4f8
2e5dd6d
 
 
a40ecea
7f6c4f8
 
764ca25
d221625
a40ecea
 
 
 
 
 
 
 
d221625
 
764ca25
7f6c4f8
 
d221625
 
 
 
 
 
 
 
 
7f6c4f8
fa615e3
 
 
 
d221625
 
 
 
 
 
 
 
7f6c4f8
2e5dd6d
d221625
2e5dd6d
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
a581495
69abd0a
a40ecea
69abd0a
 
a40ecea
 
 
69abd0a
 
 
a581495
 
 
 
2e5dd6d
66fb4d9
d221625
 
 
a581495
 
 
 
 
 
 
 
 
2e5dd6d
 
 
a581495
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
d221625
 
a581495
 
 
 
 
 
 
2e5dd6d
 
 
 
a581495
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a40ecea
 
 
d221625
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
 
d0a8ec0
 
655220c
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ef9510
 
 
d221625
 
 
7ef9510
 
d221625
 
 
66fb4d9
 
 
d221625
 
 
66fb4d9
d221625
 
d0a8ec0
 
d221625
 
d0a8ec0
 
 
 
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a581495
69abd0a
 
 
a40ecea
 
a581495
fa615e3
 
 
d221625
2e5dd6d
66fb4d9
2e5dd6d
a581495
 
 
 
69abd0a
 
fa615e3
a581495
d221625
 
69abd0a
 
 
d221625
69abd0a
 
 
a581495
7f6c4f8
d221625
fa615e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
2e5dd6d
e6051e5
2e5dd6d
 
66fb4d9
 
 
 
d221625
66fb4d9
2e5dd6d
d221625
69abd0a
 
fa615e3
69abd0a
d221625
2e5dd6d
e6051e5
2e5dd6d
 
7f6c4f8
 
2e5dd6d
 
7f6c4f8
d221625
7f6c4f8
2e5dd6d
 
 
 
 
66fb4d9
2e5dd6d
7f6c4f8
d221625
2e5dd6d
e6051e5
2e5dd6d
d221625
7f6c4f8
2e5dd6d
69abd0a
 
 
 
 
 
 
66fb4d9
2e5dd6d
7f6c4f8
d221625
7f6c4f8
 
 
 
d221625
7f6c4f8
66fb4d9
7f6c4f8
d221625
2e5dd6d
66fb4d9
2e5dd6d
 
 
 
7f6c4f8
 
69abd0a
a40ecea
69abd0a
 
66fb4d9
 
69abd0a
 
 
 
66fb4d9
 
2e5dd6d
66fb4d9
 
7f6c4f8
d221625
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
d0a8ec0
 
 
 
 
 
 
 
 
d221625
d0a8ec0
d221625
 
d0a8ec0
 
d221625
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
d0a8ec0
 
 
d221625
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
d0a8ec0
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
 
 
 
d221625
 
 
 
 
 
 
655220c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
d0a8ec0
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
d221625
 
 
d0a8ec0
 
 
d221625
 
 
655220c
d0a8ec0
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
655220c
 
d221625
d0a8ec0
 
 
655220c
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
d0a8ec0
 
 
 
d221625
 
 
d0a8ec0
d221625
d0a8ec0
 
d221625
 
 
 
 
 
 
d0a8ec0
 
 
 
d221625
 
 
 
 
 
 
d0a8ec0
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
 
 
 
 
d221625
 
7f6c4f8
2e5dd6d
69abd0a
 
 
 
 
 
 
7f6c4f8
a581495
66fb4d9
2e5dd6d
66fb4d9
a581495
 
2e5dd6d
d221625
 
 
2e5dd6d
a581495
d221625
 
a581495
69abd0a
 
 
 
d221625
69abd0a
 
 
 
 
 
a581495
7f6c4f8
 
d221625
2e5dd6d
66fb4d9
7f6c4f8
a581495
66fb4d9
2e5dd6d
66fb4d9
 
a581495
69abd0a
 
 
 
d221625
a581495
69abd0a
e6051e5
 
 
 
 
 
 
 
 
 
a581495
7f6c4f8
 
d221625
2e5dd6d
 
 
69abd0a
 
2e5dd6d
 
d221625
2e5dd6d
 
 
 
d221625
 
69abd0a
 
 
 
2e5dd6d
69abd0a
d221625
 
 
2e5dd6d
 
 
a581495
d221625
2e5dd6d
66fb4d9
2e5dd6d
 
 
 
69abd0a
 
2e5dd6d
 
d221625
2e5dd6d
69abd0a
a40ecea
 
 
 
69abd0a
 
2e5dd6d
69abd0a
 
2e5dd6d
 
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3811bb
 
 
 
a40ecea
e3811bb
 
2e5dd6d
 
 
7f6c4f8
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
69abd0a
 
 
 
2e5dd6d
69abd0a
 
fa615e3
 
d221625
fa615e3
 
d221625
fa615e3
 
d221625
 
fa615e3
 
 
 
 
 
 
d221625
 
 
 
 
 
fa615e3
 
 
d221625
 
 
 
 
 
 
 
 
 
 
a40ecea
 
 
 
fa615e3
 
 
7f6c4f8
a581495
69abd0a
 
 
 
a581495
69abd0a
a581495
 
e3811bb
 
 
 
 
 
 
 
 
 
a581495
7f6c4f8
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
 
 
 
d221625
 
655220c
d0a8ec0
 
d221625
 
 
 
 
 
d0a8ec0
d221625
 
 
 
d0a8ec0
 
d221625
 
 
655220c
 
 
d221625
 
655220c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
655220c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
d221625
 
 
 
 
655220c
 
d221625
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
d0a8ec0
 
 
 
655220c
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
 
d0a8ec0
655220c
 
d0a8ec0
 
 
 
 
 
655220c
d0a8ec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88a99f6
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88a99f6
 
a40ecea
88a99f6
 
 
 
a40ecea
88a99f6
a40ecea
 
 
 
 
 
 
 
88a99f6
 
 
 
a40ecea
 
 
 
 
 
 
 
88a99f6
 
 
 
a40ecea
 
 
 
88a99f6
a40ecea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
 
 
 
d221625
d0a8ec0
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
fa615e3
2e5dd6d
66fb4d9
7f6c4f8
d221625
2e5dd6d
e6051e5
 
 
 
 
 
 
 
 
 
2e5dd6d
69abd0a
 
 
fa615e3
 
d221625
 
 
 
 
 
 
 
 
 
 
fa615e3
 
7f6c4f8
fa615e3
 
e6051e5
fa615e3
69abd0a
 
 
 
fa615e3
69abd0a
 
e6051e5
69abd0a
2e5dd6d
69abd0a
 
d221625
 
 
 
69abd0a
fa615e3
 
 
 
 
 
 
69abd0a
fa615e3
 
 
2e5dd6d
fa615e3
 
 
 
 
 
 
 
 
 
 
 
d221625
fa615e3
 
 
d221625
a40ecea
 
 
 
 
 
fa615e3
 
 
 
 
69abd0a
 
 
 
 
 
 
 
 
 
fa615e3
69abd0a
 
7f6c4f8
d221625
2e5dd6d
fa615e3
2e5dd6d
 
69abd0a
 
 
 
 
35f7916
 
69abd0a
 
d221625
69abd0a
fa615e3
a40ecea
 
 
 
d0a8ec0
 
 
a40ecea
 
 
 
d0a8ec0
a40ecea
 
 
 
 
 
 
 
 
 
 
 
d0a8ec0
 
 
7f6c4f8
d221625
 
 
2e5dd6d
69abd0a
 
 
 
fa615e3
7f6c4f8
66fb4d9
fa615e3
 
d221625
 
 
fa615e3
 
 
 
 
 
 
 
69abd0a
fa615e3
 
 
d221625
fa615e3
69abd0a
7f6c4f8
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
66fb4d9
2e5dd6d
7f6c4f8
 
66fb4d9
 
d221625
 
66fb4d9
 
d221625
 
 
 
 
 
 
 
 
66fb4d9
 
 
 
 
 
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
66fb4d9
 
69abd0a
 
2e5dd6d
 
66fb4d9
2e5dd6d
 
66fb4d9
2e5dd6d
69abd0a
d221625
 
 
 
 
 
69abd0a
 
66fb4d9
e3811bb
 
 
 
 
 
 
 
 
 
 
2e5dd6d
66fb4d9
7f6c4f8
d221625
 
 
 
 
 
 
 
 
 
 
7f6c4f8
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
66fb4d9
2e5dd6d
7f6c4f8
66fb4d9
2e5dd6d
7f6c4f8
66fb4d9
7f6c4f8
66fb4d9
 
2e5dd6d
 
 
66fb4d9
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fb4d9
 
2e5dd6d
e6051e5
 
 
 
d221625
 
 
2e5dd6d
e6051e5
2e5dd6d
e6051e5
 
 
 
 
 
66fb4d9
 
7f6c4f8
 
66fb4d9
 
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655220c
 
d221625
 
 
 
 
 
655220c
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fb4d9
d221625
 
 
 
 
 
 
2e5dd6d
d221625
7f6c4f8
 
 
2e5dd6d
66fb4d9
2e5dd6d
d221625
 
2e5dd6d
d221625
 
 
2e5dd6d
66fb4d9
7f6c4f8
a581495
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
69abd0a
 
d221625
 
 
 
 
a581495
66fb4d9
a581495
69abd0a
66fb4d9
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6051e5
d221625
 
 
 
 
 
66fb4d9
d221625
 
 
66fb4d9
d221625
7f6c4f8
a581495
d221625
 
 
 
2e5dd6d
66fb4d9
 
 
d221625
69abd0a
d221625
66fb4d9
2e5dd6d
66fb4d9
 
 
d221625
69abd0a
d221625
66fb4d9
69abd0a
66fb4d9
 
d221625
 
e6051e5
d221625
66fb4d9
e6051e5
 
d221625
e6051e5
 
 
 
a581495
 
d221625
66fb4d9
d221625
 
66fb4d9
d221625
e6051e5
 
7f6c4f8
a581495
e6051e5
 
 
 
d221625
 
 
 
 
66fb4d9
e6051e5
 
 
 
2e5dd6d
69abd0a
d221625
e6051e5
 
d221625
e6051e5
d221625
 
 
 
 
a581495
7f6c4f8
d221625
66fb4d9
d221625
 
66fb4d9
e6051e5
 
7f6c4f8
a581495
e6051e5
 
 
 
d221625
 
 
 
2e5dd6d
d221625
e6051e5
d221625
69abd0a
e6051e5
66fb4d9
 
2e5dd6d
69abd0a
d221625
 
 
 
 
 
 
 
 
a581495
7f6c4f8
d221625
66fb4d9
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6051e5
d221625
 
66fb4d9
d221625
e6051e5
 
2e5dd6d
 
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fb4d9
d221625
 
 
 
2e5dd6d
d221625
 
 
 
e6051e5
d221625
 
 
e6051e5
d221625
 
 
 
 
 
 
 
 
2e5dd6d
69abd0a
 
d221625
 
66fb4d9
e6051e5
 
 
d221625
 
 
 
 
 
 
 
e6051e5
d221625
2e5dd6d
 
d221625
66fb4d9
d221625
 
e6051e5
 
 
 
d221625
66fb4d9
7f6c4f8
 
a581495
d221625
7f6c4f8
e3811bb
 
66fb4d9
e3811bb
d221625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e5dd6d
 
 
a40ecea
 
 
 
 
 
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
import os
import re
import json
import time
import uuid
import base64
import logging
import tempfile
import shutil
import asyncio
import secrets
import threading
from typing import Optional, Dict, List, Any, Tuple
from contextlib import asynccontextmanager

import aiosqlite

from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect, Header, Query, UploadFile, File, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, StreamingResponse
from fastapi.middleware.cors import CORSMiddleware

from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend

from storage_handler import (
    store_file, retrieve_file, delete_file,
    download_database, upload_database, start_db_sync,
    list_backups, create_timestamped_backup, restore_backup, delete_backup,
    start_backup_loop, StorageUnavailableError, _OFFLINE, LOCAL_STORAGE_DIR
)

# ------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------
APP_VERSION = "3.0.0"
DATABASE_URL = os.environ.get("DATABASE_URL", "/data/infinitychat.db")
MESSAGE_KEY_B64 = os.environ.get("SECRET_KEY", None)
FILE_ENCRYPTION_KEY_B64 = os.environ.get("FILE_ENCRYPTION_KEY", None)

# Admin console credentials. These are intentionally plain-text secrets supplied
# by the host via secret/environment configuration (ADMIN_USERNAME/ADMIN_PASSWORD).
ADMIN_USERNAME = (os.environ.get("ADMIN_USERNAME", "") or "").strip()
ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "") or ""
ADMIN_SESSION_TTL = 12 * 60 * 60
ADMIN_SESSION_LOCK = threading.Lock()
ADMIN_SESSIONS: Dict[str, Dict[str, Any]] = {}

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("InfinityChat")

if MESSAGE_KEY_B64 is None:
    MESSAGE_KEY_B64 = base64.urlsafe_b64encode(os.urandom(32)).decode()
    logger.warning(
        "SECRET_KEY not set - generated a random key for this run. "
        "Existing messages will be unreadable next restart unless you persist it!"
    )
try:
    MESSAGE_KEY = base64.urlsafe_b64decode(MESSAGE_KEY_B64)
except Exception:
    raise ValueError("SECRET_KEY is not valid base64")
assert len(MESSAGE_KEY) == 32, "SECRET_KEY must decode to exactly 32 bytes"

# Temp directory for chunk assembly - uses disk not RAM
TEMP_DIR = os.environ.get("TEMP_DIR", "/tmp/infinitychat_uploads")
os.makedirs(TEMP_DIR, exist_ok=True)

# ------------------------------------------------------------------------
# App constants
# ------------------------------------------------------------------------
GLOBAL_CONVERSATION_ID = 1          # id of the public lobby room
MAX_PRIVATE_CHATS_PER_PAIR = 3      # up to 3 private chats between two people
MAX_MESSAGE_LENGTH = 10000
MAX_FILE_SIZE = 500 * 1024 * 1024   # 500MB upload cap
DOWNLOAD_CHUNK_SIZE = 1024 * 1024   # 1MB streaming chunks

# ------------------------------------------------------------------------
# Database initialization + additive migrations (v1 -> v2)
# ------------------------------------------------------------------------
async def _table_columns(db: aiosqlite.Connection, table: str) -> set:
    cursor = await db.execute(f"PRAGMA table_info({table})")
    rows = await cursor.fetchall()
    return {r["name"] for r in rows}


async def _ensure_column(db: aiosqlite.Connection, table: str, column: str, ddl: str):
    """Add a column if it doesn't exist yet (safe, non-destructive upgrade)."""
    cols = await _table_columns(db, table)
    if column not in cols:
        await db.execute(f"ALTER TABLE {table} ADD COLUMN {column} {ddl}")
        logger.info(f"🛠  Migration: added column {table}.{column}")


async def init_database():
    os.makedirs(os.path.dirname(DATABASE_URL), exist_ok=True)
    local_exists = os.path.exists(DATABASE_URL)
    db_existed = download_database(DATABASE_URL)
    if db_existed:
        logger.info("✅ Restored database from storage")
    elif local_exists:
        logger.info("📁 Using existing local database")
    else:
        logger.info("🆕 Starting with fresh database")

    async with aiosqlite.connect(DATABASE_URL) as db:
        db.row_factory = aiosqlite.Row
        await db.execute("PRAGMA journal_mode=WAL;")
        await db.execute("PRAGMA foreign_keys=ON;")
        await db.execute("PRAGMA synchronous=NORMAL;")
        await db.execute("PRAGMA cache_size=-20000;")
        await db.execute("PRAGMA busy_timeout=10000;")

        # --- Base tables (v1 schema, unchanged shapes so old DBs keep working) ---
        await db.execute("""
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT UNIQUE NOT NULL,
                display_name TEXT NOT NULL DEFAULT '',
                password_hash TEXT NOT NULL,
                salt TEXT NOT NULL,
                avatar_path TEXT,
                token TEXT UNIQUE,
                created_at INTEGER DEFAULT (strftime('%s','now')),
                last_seen INTEGER DEFAULT (strftime('%s','now')),
                status TEXT DEFAULT 'offline'
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS messages (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                sender_id INTEGER NOT NULL,
                encrypted_content TEXT NOT NULL,
                timestamp_ms INTEGER NOT NULL,
                reply_to_id INTEGER,
                is_edited INTEGER DEFAULT 0,
                is_deleted INTEGER DEFAULT 0,
                file_path TEXT,
                file_type TEXT,
                file_name TEXT,
                file_size INTEGER DEFAULT 0,
                conversation_id INTEGER NOT NULL DEFAULT 1,
                client_id TEXT,
                FOREIGN KEY(sender_id) REFERENCES users(id)
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS read_receipts (
                user_id INTEGER NOT NULL,
                message_id INTEGER NOT NULL,
                read_at INTEGER DEFAULT (strftime('%s','now')),
                PRIMARY KEY (user_id, message_id),
                FOREIGN KEY(user_id) REFERENCES users(id),
                FOREIGN KEY(message_id) REFERENCES messages(id)
            )
        """)

        # --- v2 tables ---
        await db.execute("""
            CREATE TABLE IF NOT EXISTS conversations (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                type TEXT NOT NULL DEFAULT 'global' CHECK(type IN ('global','dm')),
                title TEXT,
                created_by INTEGER,
                user_low_id INTEGER,
                user_high_id INTEGER,
                created_at INTEGER DEFAULT (strftime('%s','now'))
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS conversation_members (
                conversation_id INTEGER NOT NULL,
                user_id INTEGER NOT NULL,
                joined_at INTEGER DEFAULT (strftime('%s','now')),
                last_read_message_id INTEGER NOT NULL DEFAULT 0,
                PRIMARY KEY (conversation_id, user_id),
                FOREIGN KEY(conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
                FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)

        # Blocking: one-way rows (A blocks B). Chat creation/access is restricted
        # when either user in a pair has blocked the other.
        await db.execute("""
            CREATE TABLE IF NOT EXISTS user_blocks (
                blocker_id INTEGER NOT NULL,
                blocked_id INTEGER NOT NULL,
                created_at INTEGER DEFAULT (strftime('%s','now')),
                PRIMARY KEY (blocker_id, blocked_id),
                FOREIGN KEY(blocker_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY(blocked_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)

        # --- v3: social feed / Twitter-like features ---
        await db.execute("""
            CREATE TABLE IF NOT EXISTS social_posts (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                author_id INTEGER NOT NULL,
                body TEXT NOT NULL DEFAULT '',
                media_json TEXT,
                reply_to_id INTEGER,
                quote_id INTEGER,
                is_deleted INTEGER NOT NULL DEFAULT 0,
                created_at_ms INTEGER NOT NULL,
                edited_at_ms INTEGER,
                FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY(reply_to_id) REFERENCES social_posts(id) ON DELETE SET NULL,
                FOREIGN KEY(quote_id) REFERENCES social_posts(id) ON DELETE SET NULL
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS social_likes (
                post_id INTEGER NOT NULL,
                user_id INTEGER NOT NULL,
                created_at_ms INTEGER NOT NULL,
                PRIMARY KEY (post_id, user_id),
                FOREIGN KEY(post_id) REFERENCES social_posts(id) ON DELETE CASCADE,
                FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS social_reposts (
                post_id INTEGER NOT NULL,
                user_id INTEGER NOT NULL,
                created_at_ms INTEGER NOT NULL,
                PRIMARY KEY (post_id, user_id),
                FOREIGN KEY(post_id) REFERENCES social_posts(id) ON DELETE CASCADE,
                FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS social_bookmarks (
                post_id INTEGER NOT NULL,
                user_id INTEGER NOT NULL,
                created_at_ms INTEGER NOT NULL,
                PRIMARY KEY (post_id, user_id),
                FOREIGN KEY(post_id) REFERENCES social_posts(id) ON DELETE CASCADE,
                FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS follows (
                follower_id INTEGER NOT NULL,
                following_id INTEGER NOT NULL,
                created_at_ms INTEGER NOT NULL,
                PRIMARY KEY (follower_id, following_id),
                FOREIGN KEY(follower_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY(following_id) REFERENCES users(id) ON DELETE CASCADE
            )
        """)
        await db.execute("""
            CREATE TABLE IF NOT EXISTS social_notifications (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER NOT NULL,
                actor_id INTEGER NOT NULL,
                type TEXT NOT NULL,
                post_id INTEGER,
                created_at_ms INTEGER NOT NULL,
                read INTEGER NOT NULL DEFAULT 0,
                FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY(actor_id) REFERENCES users(id) ON DELETE CASCADE,
                FOREIGN KEY(post_id) REFERENCES social_posts(id) ON DELETE CASCADE
            )
        """)

        # --- Additive migrations for databases created before v2 ---
        msg_cols = await _table_columns(db, "messages")
        if "conversation_id" not in msg_cols:
            await _ensure_column(db, "messages", "conversation_id",
                                 "INTEGER NOT NULL DEFAULT 1")
        if "client_id" not in msg_cols:
            await _ensure_column(db, "messages", "client_id", "TEXT")
        if "is_edited" not in msg_cols:
            await _ensure_column(db, "messages", "is_edited", "INTEGER DEFAULT 0")
        if "is_deleted" not in msg_cols:
            await _ensure_column(db, "messages", "is_deleted", "INTEGER DEFAULT 0")

        usr_cols = await _table_columns(db, "users")
        for col, ddl in {
            "display_name": "TEXT NOT NULL DEFAULT ''",
            "avatar_path": "TEXT",
            "last_seen": "INTEGER DEFAULT (strftime('%s','now'))",
            "status": "TEXT DEFAULT 'offline'",
            "bio": "TEXT NOT NULL DEFAULT ''",
            "location": "TEXT NOT NULL DEFAULT ''",
            "website": "TEXT NOT NULL DEFAULT ''",
        }.items():
            if col not in usr_cols:
                await _ensure_column(db, "users", col, ddl)

        conv_cols = await _table_columns(db, "conversations")
        for col, ddl in {
            "is_group": "INTEGER NOT NULL DEFAULT 0",
            "custom_name": "TEXT",
        }.items():
            if col not in conv_cols:
                await _ensure_column(db, "conversations", col, ddl)

        cm_cols = await _table_columns(db, "conversation_members")
        for col, ddl in {
            "status": "TEXT NOT NULL DEFAULT 'accepted'",
            "role": "TEXT NOT NULL DEFAULT 'member'",
        }.items():
            if col not in cm_cols:
                await _ensure_column(db, "conversation_members", col, ddl)

        # Existing memberships predate invites/roles: only null/empty get the
        # default. A real 'pending' invite keeps its pending state across
        # restarts so DMs/groups still require the invitee to accept.
        await db.execute(
            "UPDATE conversation_members SET status = 'accepted' "
            "WHERE status IS NULL OR status = ''"
        )
        await db.execute(
            "UPDATE conversation_members SET role = 'member' "
            "WHERE role IS NULL OR role = ''"
        )
        # Group owner is the first accepted member of each group with no owner.
        await db.execute("""
            UPDATE conversation_members
            SET role = 'owner'
            WHERE conversation_id IN (
                SELECT id FROM conversations WHERE is_group = 1
            )
              AND user_id = (
                  SELECT user_id FROM conversation_members cm
                  WHERE cm.conversation_id = conversation_members.conversation_id
                    AND cm.status = 'accepted'
                  ORDER BY cm.joined_at ASC, cm.user_id ASC LIMIT 1
              )
        """)

        rc_cols = await _table_columns(db, "read_receipts")
        if "read_at" not in rc_cols:
            await _ensure_column(db, "read_receipts", "read_at",
                                 "INTEGER DEFAULT (strftime('%s','now'))")
        # Old receipts stored seconds - upgrade them to milliseconds
        await db.execute(
            "UPDATE read_receipts SET read_at = read_at * 1000 WHERE read_at < 100000000000"
        )

        # Seed the global lobby conversation (id 1 stays stable across versions)
        await db.execute(
            "INSERT OR IGNORE INTO conversations (id, type, title, created_by) "
            "VALUES (?, 'global', 'Global Chat', NULL)",
            (GLOBAL_CONVERSATION_ID,)
        )

        # Backfill: any message that somehow predates conversations gets the lobby.
        # Only do this when there is no matching conversation id at all (NULL or
        # missing) — never rewrite valid DM/group messages back to Global.
        await db.execute(
            "UPDATE messages SET conversation_id = ? "
            "WHERE conversation_id IS NULL OR conversation_id NOT IN "
            "(SELECT id FROM conversations)",
            (GLOBAL_CONVERSATION_ID,)
        )

        # --- Indexes ---
        await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_sender_time ON messages(sender_id, timestamp_ms)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_time_id ON messages(timestamp_ms, id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_not_deleted ON messages(is_deleted, id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_messages_conversation ON messages(conversation_id, id)")
        await db.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_messages_client_dedupe "
                         "ON messages(sender_id, client_id) WHERE client_id IS NOT NULL")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_receipts_user_msg ON read_receipts(user_id, message_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_receipts_message ON read_receipts(message_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_members_user ON conversation_members(user_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_members_status "
                         "ON conversation_members(user_id, status)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_conv_dm_pair "
                         "ON conversations(type, user_low_id, user_high_id) WHERE type = 'dm'")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_blocks_blocked "
                         "ON user_blocks(blocked_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_blocks_blocker "
                         "ON user_blocks(blocker_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_posts_author_time "
                         "ON social_posts(author_id, created_at_ms)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_posts_time "
                         "ON social_posts(created_at_ms)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_posts_reply "
                         "ON social_posts(reply_to_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_follows_following "
                         "ON follows(following_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_follows_follower "
                         "ON follows(follower_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_likes_post "
                         "ON social_likes(post_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_reposts_post "
                         "ON social_reposts(post_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_bookmarks_user "
                         "ON social_bookmarks(user_id, post_id)")
        await db.execute("CREATE INDEX IF NOT EXISTS idx_notifications_user "
                         "ON social_notifications(user_id, read, created_at_ms)")
        await db.commit()

    upload_database(DATABASE_URL)
    start_db_sync(DATABASE_URL)
    start_backup_loop(DATABASE_URL)
    logger.info("✅ Database initialized successfully (schema v3)")

    # Clean up any leftover temp upload dirs from previous runs
    _cleanup_temp_dir()


# ------------------------------------------------------------------------
# FastAPI lifespan
# ------------------------------------------------------------------------
@asynccontextmanager
async def lifespan(app: FastAPI):
    await init_database()
    yield
    logger.info("🔄 Final database sync on shutdown...")
    upload_database(DATABASE_URL)
    _cleanup_temp_dir()


app = FastAPI(title="InfinityChat", version=APP_VERSION, lifespan=lifespan)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,     # auth travels via header/query, never cookies
    allow_methods=["*"],
    allow_headers=["*"]
)
app.mount("/static", StaticFiles(directory="static"), name="static")


# ------------------------------------------------------------------------
# Temp directory cleanup
# ------------------------------------------------------------------------
def _cleanup_temp_dir():
    """Remove all leftover temp chunk dirs older than 2 hours."""
    try:
        now = time.time()
        for entry in os.scandir(TEMP_DIR):
            if entry.is_dir():
                age = now - entry.stat().st_mtime
                if age > 7200:  # 2 hours
                    shutil.rmtree(entry.path, ignore_errors=True)
                    logger.debug(f"🧹 Cleaned up old temp dir: {entry.path}")
    except Exception as e:
        logger.warning(f"Temp cleanup error: {e}")


# ------------------------------------------------------------------------
# Database helper
# ------------------------------------------------------------------------
async def get_db() -> aiosqlite.Connection:
    conn = await aiosqlite.connect(DATABASE_URL)
    conn.row_factory = aiosqlite.Row
    await conn.execute("PRAGMA journal_mode=WAL")
    await conn.execute("PRAGMA foreign_keys=ON")
    await conn.execute("PRAGMA busy_timeout=10000")
    return conn


def schedule_db_sync():
    import threading
    threading.Thread(target=upload_database, args=(DATABASE_URL,), daemon=True).start()


# ------------------------------------------------------------------------
# Encryption helpers (AES-256-GCM)
# ------------------------------------------------------------------------
def encrypt_message(plaintext: str) -> str:
    aesgcm = AESGCM(MESSAGE_KEY)
    nonce = os.urandom(12)
    ciphertext = aesgcm.encrypt(nonce, plaintext.encode('utf-8'), None)
    return base64.urlsafe_b64encode(nonce + ciphertext).decode('utf-8')


def decrypt_message(encrypted_b64: str) -> str:
    try:
        raw = base64.urlsafe_b64decode(encrypted_b64)
        nonce, ciphertext = raw[:12], raw[12:]
        aesgcm = AESGCM(MESSAGE_KEY)
        return aesgcm.decrypt(nonce, ciphertext, None).decode('utf-8')
    except Exception:
        return "[Decryption Error]"


# ------------------------------------------------------------------------
# Password hashing (PBKDF2)
# ------------------------------------------------------------------------
def hash_password(password: str, salt: Optional[str] = None) -> Tuple[str, str]:
    if salt is None:
        salt = os.urandom(32).hex()
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt.encode(),
        iterations=600000,
        backend=default_backend()
    )
    key = base64.urlsafe_b64encode(kdf.derive(password.encode())).decode()
    return key, salt


def verify_password(password: str, salt: str, stored_hash: str) -> bool:
    key, _ = hash_password(password, salt)
    return key == stored_hash


def generate_token() -> str:
    return base64.urlsafe_b64encode(os.urandom(32)).decode()


# ------------------------------------------------------------------------
# Authentication
# ------------------------------------------------------------------------
async def authenticate_user(token: str) -> Optional[dict]:
    if not token or token in ('null', 'undefined'):
        return None
    db = await get_db()
    try:
        cursor = await db.execute(
            "SELECT id, username, display_name, avatar_path, status, bio, location, website FROM users WHERE token = ?",
            (token,)
        )
        user = await cursor.fetchone()
        if user:
            await db.execute(
                "UPDATE users SET last_seen = ? WHERE id = ?",
                (int(time.time()), user['id'])
            )
            await db.commit()
            return dict(user)
        return None
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Conversation helpers
# ------------------------------------------------------------------------
async def _block_exists(db: aiosqlite.Connection, a: int, b: int) -> bool:
    """True when blocking exists in either direction (mutual restriction)."""
    if a == b:
        return False
    cursor = await db.execute(
        "SELECT 1 FROM user_blocks WHERE (blocker_id = ? AND blocked_id = ?) "
        "OR (blocker_id = ? AND blocked_id = ?) LIMIT 1",
        (a, b, b, a)
    )
    return await cursor.fetchone() is not None


async def _conversation_blocked(db: aiosqlite.Connection, cid: int, uid: int) -> bool:
    """A non-global conversation is hidden when the user blocks (or is blocked by)
    any other accepted member."""
    if cid == GLOBAL_CONVERSATION_ID:
        return False
    member_ids = await _raw_conversation_member_ids(db, cid)
    for mid in member_ids:
        if mid != uid and await _block_exists(db, uid, mid):
            return True
    return False


async def _membership(db: aiosqlite.Connection, cid: int, uid: int) -> Optional[dict]:
    cursor = await db.execute(
        "SELECT * FROM conversation_members WHERE conversation_id = ? AND user_id = ?",
        (cid, uid)
    )
    row = await cursor.fetchone()
    return dict(row) if row else None


async def user_in_conversation(db: aiosqlite.Connection, uid: int, cid: int) -> bool:
    """Everyone is in the global lobby; other chats require an accepted membership
    and no active block with another member."""
    if cid == GLOBAL_CONVERSATION_ID:
        return True
    member = await _membership(db, cid, uid)
    if not member or member.get("status") != "accepted":
        return False
    if await _conversation_blocked(db, cid, uid):
        return False
    return True


async def _raw_conversation_member_ids(db: aiosqlite.Connection, cid: int) -> List[int]:
    if cid == GLOBAL_CONVERSATION_ID:
        cursor = await db.execute("SELECT id FROM users")
        return [r["id"] for r in await cursor.fetchall()]
    cursor = await db.execute(
        "SELECT user_id FROM conversation_members "
        "WHERE conversation_id = ? AND status = 'accepted'",
        (cid,)
    )
    return [r["user_id"] for r in await cursor.fetchall()]


async def _blocked_member_ids(db: aiosqlite.Connection, cid: int) -> set:
    """Members of a non-global conversation who are in a mutual block pair with
    another accepted member. Those members are excluded from live broadcasts so
    hidden/blocked chats never leak messages."""
    if cid == GLOBAL_CONVERSATION_ID:
        return set()
    member_ids = await _raw_conversation_member_ids(db, cid)
    blocked = set()
    for mid in member_ids:
        for other in member_ids:
            if mid != other and await _block_exists(db, mid, other):
                blocked.add(mid)
                blocked.add(other)
    return blocked


async def conversation_member_ids(db: aiosqlite.Connection, cid: int) -> List[int]:
    """Accepted member ids of a conversation (for scoped broadcasts).

    Global broadcasts include everyone. Non-global broadcasts exclude users who
    are in a block pair so hidden chats never leak messages."""
    member_ids = await _raw_conversation_member_ids(db, cid)
    if cid == GLOBAL_CONVERSATION_ID:
        return member_ids
    blocked = await _blocked_member_ids(db, cid)
    return [mid for mid in member_ids if mid not in blocked]


async def _group_update_recipient_ids(db: aiosqlite.Connection, cid: int) -> List[int]:
    """Recipients for group summary/update/deleted events.

    Includes accepted members and pending invitees, but still excludes anyone
    who has an active block with another member of the group so hidden chats
    never leak through live events."""
    if cid == GLOBAL_CONVERSATION_ID:
        return []
    ids = await _raw_conversation_member_ids(db, cid)
    cursor = await db.execute(
        "SELECT user_id FROM conversation_members WHERE conversation_id = ? AND status = 'pending'",
        (cid,)
    )
    ids += [r["user_id"] for r in await cursor.fetchall()]
    return [mid for mid in ids if not await _conversation_blocked(db, cid, mid)]


async def _conversation_member_rows(db: aiosqlite.Connection, cid: int, include_pending: bool = False) -> List[dict]:
    cond = "" if include_pending else "AND cm.status = 'accepted'"
    cursor = await db.execute(
        f"""SELECT u.id, u.username, u.display_name, u.avatar_path,
                   cm.status, cm.role, cm.joined_at, u.status AS user_status,
                   u.last_seen
            FROM conversation_members cm JOIN users u ON u.id = cm.user_id
            WHERE cm.conversation_id = ? {cond}
            ORDER BY cm.joined_at ASC, u.id ASC""",
        (cid,)
    )
    return [dict(r) for r in await cursor.fetchall()]


async def _ensure_global_member(db: aiosqlite.Connection, uid: int):
    """First sighting of a user: mark the whole existing lobby as already-read."""
    cursor = await db.execute(
        "SELECT MAX(id) AS max_id FROM messages WHERE conversation_id = ?",
        (GLOBAL_CONVERSATION_ID,)
    )
    row = await cursor.fetchone()
    max_id = row["max_id"] or 0
    await db.execute(
        "INSERT OR IGNORE INTO conversation_members "
        "(conversation_id, user_id, last_read_message_id, status, role) "
        "VALUES (?, ?, ?, 'accepted', 'member')",
        (GLOBAL_CONVERSATION_ID, uid, max_id)
    )


def _row_to_public_user(row) -> dict:
    return {
        "id": row["id"],
        "username": row["username"],
        "display_name": row["display_name"],
        "avatar_path": row["avatar_path"],
    }


def _preview_for(content: str, file_type: str, file_name: str) -> str:
    content = (content or "").strip()
    if content:
        # mirror client-side markdown so previews read like the real message
        text = re.sub(r'<img\b[^>]*>', ' ', content)
        text = re.sub(r'```', ' ', text)
        text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text)
        text = re.sub(r'`([^`]+)`', r'\1', text)
        text = re.sub(r'([\s(>]|^)\*([^*\n]+)\*(?=[\s.,!?;:)\n]|$)', r'\1\2', text)
        text = re.sub(r'\s+', ' ', text).strip()
        return text[:90]
    if file_type and file_type.startswith("image/"):
        return "📷 Photo"
    if file_type and file_type.startswith("video/"):
        return "🎬 Video"
    if file_type and file_type.startswith("audio/"):
        return "🎵 Audio"
    return f"📎 {file_name or 'File'}"


async def _peer_for(db: aiosqlite.Connection, cid: int, uid: int) -> Optional[dict]:
    """The other person inside a DM conversation.

    Falls back to the conversation's pair columns so the initiator still sees
    the intended recipient's name even after that recipient rejected or left.
    """
    cursor = await db.execute(
        """SELECT u.id, u.username, u.display_name, u.avatar_path, u.status, u.last_seen
           FROM conversation_members cm JOIN users u ON u.id = cm.user_id
           WHERE cm.conversation_id = ? AND cm.user_id != ? LIMIT 1""",
        (cid, uid)
    )
    row = await cursor.fetchone()
    if row:
        return dict(row)
    cursor = await db.execute(
        "SELECT user_low_id, user_high_id FROM conversations WHERE id = ?", (cid,)
    )
    conv = await cursor.fetchone()
    if not conv:
        return None
    other_id = conv["user_high_id"] if conv["user_low_id"] == uid else conv["user_low_id"]
    if not other_id or other_id == uid:
        return None
    cursor = await db.execute(
        """SELECT id, username, display_name, avatar_path, status, last_seen
           FROM users WHERE id = ?""",
        (other_id,)
    )
    row = await cursor.fetchone()
    return dict(row) if row else None


async def _dm_number(db: aiosqlite.Connection, cid: int) -> int:
    cursor = await db.execute(
        """SELECT id FROM conversations
           WHERE type = 'dm' AND is_group = 0 AND user_low_id = (
               SELECT user_low_id FROM conversations WHERE id = ?
           ) AND user_high_id = (
               SELECT user_high_id FROM conversations WHERE id = ?
           ) ORDER BY id""",
        (cid, cid)
    )
    ids = [r["id"] for r in await cursor.fetchall()]
    try:
        return ids.index(cid) + 1
    except ValueError:
        return 1


async def _build_conversation_summary(
    db: aiosqlite.Connection, cid: int, uid: int, online_ids: set = None
) -> dict:
    """One conversation as sent to the client (sidebar row)."""
    cursor = await db.execute(
        "SELECT * FROM conversations WHERE id = ?", (cid,)
    )
    conv = await cursor.fetchone()
    if not conv:
        raise HTTPException(404, "Conversation not found")

    online_ids = online_ids if online_ids is not None else set()
    membership = await _membership(db, cid, uid) or {}
    is_group = bool(conv["is_group"] or 0) if "is_group" in conv.keys() else False

    # Display title: custom rename wins for DMs/groups, otherwise group title
    # or the name of the other person in a DM.
    title = conv["title"] or "Chat"
    custom_name = conv["custom_name"] if "custom_name" in conv.keys() else None
    dm_number = 1
    if custom_name:
        title = custom_name

    summary = {
        "id": conv["id"],
        "type": conv["type"],
        "is_group": is_group,
        "title": title,
        "custom_name": custom_name,
        "created_at": conv["created_at"],
        "peer": None,
        "peer_online": False,
        "peer_removed": False,
        "dm_number": dm_number,
        "status": membership.get("status", "accepted"),
        "role": membership.get("role", "member"),
        "is_owner": membership.get("role") == "owner",
        "is_pending": membership.get("status") == "pending",
        "member_count": 0,
        "pending_member_count": 0,
        "members": [],
        "last_message_id": None,
        "last_message_preview": "",
        "last_message_ts": None,
        "last_sender_name": "",
        "last_sender_id": None,
        "last_message_type": None,
        "unread_count": 0,
    }

    members = await _conversation_member_rows(db, cid, include_pending=is_group)
    summary["member_count"] = sum(1 for m in members if m["status"] == "accepted")
    summary["pending_member_count"] = sum(1 for m in members if m["status"] == "pending")
    public_members = []
    for m in members:
        public_members.append({
            "id": m["id"],
            "username": m["username"],
            "display_name": m["display_name"],
            "avatar_path": m["avatar_path"],
            "online": m["id"] in online_ids,
            "last_seen": m["last_seen"],
            "status": m["status"],
            "role": m["role"],
        })
    summary["members"] = public_members

    if not is_group and conv["type"] == "dm":
        peer = await _peer_for(db, cid, uid)
        if peer:
            summary["peer"] = {
                "id": peer["id"],
                "username": peer["username"],
                "display_name": peer["display_name"],
                "avatar_path": peer["avatar_path"],
                "online": peer["id"] in online_ids,
                "last_seen": peer["last_seen"],
            }
            summary["peer_online"] = peer["id"] in online_ids
            peer_membership = await _membership(db, cid, peer["id"])
            summary["peer_removed"] = bool(peer_membership and peer_membership.get("status") == "left")
            summary["dm_number"] = await _dm_number(db, cid)
            if not custom_name:
                title = peer["display_name"] or peer["username"]
                summary["title"] = title
    # If no custom name and no peer (unlikely), keep generic title.

    # Newest visible message (encrypted in DB - decrypt only what we preview)
    cursor = await db.execute(
        """SELECT m.id, m.sender_id, m.encrypted_content, m.timestamp_ms,
                  m.file_path, m.file_type, m.file_name, u.username, u.display_name
           FROM messages m JOIN users u ON u.id = m.sender_id
           WHERE m.conversation_id = ? AND m.is_deleted = 0
           ORDER BY m.id DESC LIMIT 1""",
        (cid,)
    )
    last = await cursor.fetchone()
    if last:
        plain = decrypt_message(last["encrypted_content"])
        summary["last_message_id"] = last["id"]
        summary["last_message_preview"] = _preview_for(plain, last["file_type"], last["file_name"])
        summary["last_message_ts"] = last["timestamp_ms"]
        summary["last_sender_id"] = last["sender_id"]
        summary["last_sender_name"] = last["display_name"] or last["username"]
        summary["last_message_type"] = last["file_type"] or "text"

    # Unread = messages from other people past the point the user last read to.
    # Pending invites do not count as unread messages.
    if summary["is_pending"]:
        summary["unread_count"] = 0
    else:
        cursor = await db.execute(
            """SELECT COUNT(*) AS cnt FROM messages m
               WHERE m.conversation_id = ? AND m.sender_id != ? AND m.is_deleted = 0
                 AND m.id > (SELECT COALESCE(last_read_message_id, 0)
                             FROM conversation_members
                             WHERE conversation_id = ? AND user_id = ?)""",
            (cid, uid, cid, uid)
        )
        unread = await cursor.fetchone()
        summary["unread_count"] = unread["cnt"] or 0
    return summary


async def _conversations_for_user(db: aiosqlite.Connection, uid: int, online_ids=None) -> List[dict]:
    """Global lobby first, then the user's private chats + groups newest-activity first.

    Pending group invites are included so clients can accept/reject them. Active
    blocks hide the conversation entirely (but leave the underlying rows intact)."""
    cursor = await db.execute(
        """SELECT c.id FROM conversations c
           WHERE c.id = ?
              OR EXISTS (
                     SELECT 1 FROM conversation_members cm
                     WHERE cm.conversation_id = c.id AND cm.user_id = ?
                       AND cm.status IN ('accepted', 'pending'))
           ORDER BY c.id""",
        (GLOBAL_CONVERSATION_ID, uid)
    )
    ids = [r["id"] for r in await cursor.fetchall()]
    summaries = []
    for cid in ids:
        try:
            # Hide chats when either user has blocked the other; never delete them.
            if _conversation_blocked is not None and cid != GLOBAL_CONVERSATION_ID \
                    and await _conversation_blocked(db, cid, uid):
                continue
            summaries.append(await _build_conversation_summary(db, cid, uid, online_ids))
        except HTTPException:
            continue

    def sort_key(s):
        if s["id"] == GLOBAL_CONVERSATION_ID:
            return (0, 0)
        # Pending invites float to the top so they are easy to accept.
        if s.get("is_pending"):
            return (0, 1)
        return (1, -(s["last_message_ts"] or 0))

    return sorted(summaries, key=sort_key)


# ------------------------------------------------------------------------
# Message serialization
# ------------------------------------------------------------------------
async def _attach_receipt_info(db: aiosqlite.Connection, msgs: List[dict], uid: int):
    """
    For the current user's own messages, attach who has read them
    (readers + reader_count). Used by the clickable read-receipt tick.
    """
    own = [m["id"] for m in msgs if m["sender_id"] == uid]
    if not own:
        return msgs
    placeholders = ",".join("?" * len(own))
    reader_map: Dict[int, List[dict]] = {mid: [] for mid in own}
    count_map: Dict[int, int] = {mid: 0 for mid in own}

    cursor = await db.execute(
        f"""SELECT r.message_id, r.read_at, u.id, u.username, u.display_name, u.avatar_path
            FROM read_receipts r JOIN users u ON u.id = r.user_id
            WHERE r.message_id IN ({placeholders})
            ORDER BY r.read_at ASC""",
        tuple(own)
    )
    for row in await cursor.fetchall():
        reader_map.setdefault(row["message_id"], []).append({
            "user_id": row["id"],
            "username": row["username"],
            "display_name": row["display_name"],
            "avatar_path": row["avatar_path"],
            "read_at": row["read_at"],
        })
        count_map[row["message_id"]] = count_map.get(row["message_id"], 0) + 1

    cursor = await db.execute(
        f"""SELECT message_id, COUNT(*) AS cnt FROM read_receipts
            WHERE message_id IN ({placeholders}) GROUP BY message_id""",
        tuple(own)
    )
    for row in await cursor.fetchall():
        count_map[row["message_id"]] = row["cnt"]

    for m in msgs:
        if m["sender_id"] == uid:
            m["readers"] = reader_map.get(m["id"], [])[:12]
            m["reader_count"] = count_map.get(m["id"], 0)
            m["status"] = "read" if m["reader_count"] > 0 else "sent"
        else:
            m["readers"] = []
            m["reader_count"] = 0
    return msgs


def _row_to_message(row, content: str) -> dict:
    return {
        "id": row["id"],
        "conversation_id": row["conversation_id"],
        "sender_id": row["sender_id"],
        "sender_username": row["username"],
        "sender_display_name": row["display_name"],
        "sender_avatar_path": row["avatar_path"],
        "content": content,
        "timestamp_ms": row["timestamp_ms"],
        "reply_to_id": row["reply_to_id"],
        "is_edited": bool(row["is_edited"]),
        "is_deleted": False,
        "file_path": row["file_path"],
        "file_type": row["file_type"],
        "file_name": row["file_name"],
        "file_size": row["file_size"],
        "status": "sent",
        "readers": [],
        "reader_count": 0,
    }


# ------------------------------------------------------------------------
# HTTP Endpoints
# ------------------------------------------------------------------------
@app.get("/api/health")
async def health_check():
    return {"status": "healthy", "timestamp": int(time.time()), "version": APP_VERSION}


@app.post("/api/auth/signup")
async def signup(
    username: str = Query(..., min_length=3, max_length=30),
    password: str = Query(..., min_length=6),
    display_name: str = Query(None, max_length=50)
):
    if not username.replace('_', '').isalnum():
        raise HTTPException(400, "Invalid username - only letters, numbers and underscores allowed")
    db = await get_db()
    try:
        async with db.execute("SELECT id FROM users WHERE username = ?", (username.lower(),)) as cursor:
            if await cursor.fetchone():
                raise HTTPException(409, "Username taken")
        pwd_hash, salt = hash_password(password)
        token = generate_token()
        display = display_name or username
        cursor = await db.execute(
            "INSERT INTO users (username, display_name, password_hash, salt, token, status) "
            "VALUES (?,?,?,?,?,'online')",
            (username.lower(), display, pwd_hash, salt, token)
        )
        new_uid = cursor.lastrowid
        await _ensure_global_member(db, new_uid)
        await db.commit()
        schedule_db_sync()
        return {
            "token": token,
            "user": {
                "id": new_uid,
                "username": username.lower(),
                "display_name": display,
                "avatar_path": None,
                "status": "online"
            }
        }
    finally:
        await db.close()


@app.post("/api/auth/login")
async def login(username: str = Query(...), password: str = Query(...)):
    db = await get_db()
    try:
        async with db.execute("SELECT * FROM users WHERE username = ?", (username.lower(),)) as cursor:
            user = await cursor.fetchone()
        if not user or not verify_password(password, user['salt'], user['password_hash']):
            raise HTTPException(401, "Invalid credentials")
        token = generate_token()
        await db.execute(
            "UPDATE users SET token = ?, last_seen = ?, status = 'online' WHERE id = ?",
            (token, int(time.time()), user['id'])
        )
        await _ensure_global_member(db, user['id'])
        await db.commit()
        schedule_db_sync()
        return {
            "token": token,
            "user": {
                "id": user['id'],
                "username": user['username'],
                "display_name": user['display_name'],
                "avatar_path": user['avatar_path'],
                "status": "online"
            }
        }
    finally:
        await db.close()


@app.get("/api/auth/verify")
async def verify_token(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    return {"user": user}


@app.post("/api/auth/logout")
async def logout(token: str = Header(..., alias="X-Auth-Token")):
    db = await get_db()
    try:
        cursor = await db.execute("SELECT id FROM users WHERE token = ?", (token,))
        row = await cursor.fetchone()
        await db.execute(
            "UPDATE users SET token = NULL, status = 'offline', last_seen = ? WHERE token = ?",
            (int(time.time()), token)
        )
        await db.commit()
        schedule_db_sync()
        if row:
            # a logged-out device/tab must not keep receiving messages
            await manager.force_close_user(row["id"])
        return {"status": "logged_out"}
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Profile
# ------------------------------------------------------------------------
@app.get("/api/profile")
async def get_profile(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    return {"user": user}


@app.patch("/api/profile")
async def update_profile(
    display_name: str = Query(None, max_length=50),
    bio: str = Query(None, max_length=200),
    location: str = Query(None, max_length=80),
    website: str = Query(None, max_length=160),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        updates = []
        args = []
        if display_name is not None:
            updates.append("display_name = ?")
            args.append(display_name.strip() or user['username'])
        if bio is not None:
            updates.append("bio = ?")
            args.append(bio.strip()[:200])
        if location is not None:
            updates.append("location = ?")
            args.append(location.strip()[:80])
        if website is not None:
            website = (website or "").strip()
            if website and not re.match(r"^https?://", website, re.I):
                website = "https://" + website
            updates.append("website = ?")
            args.append(website[:160])
        if updates:
            args.append(user['id'])
            await db.execute(f"UPDATE users SET {', '.join(updates)} WHERE id = ?", args)
            await db.commit()
            schedule_db_sync()
            user = await authenticate_user(token)
        else:
            raise HTTPException(400, "No profile fields supplied")
        manager.update_profile(user['id'], display_name=user.get('display_name'),
                               avatar_path=user.get('avatar_path'))
        await manager.broadcast({
            "type": "profile_updated",
            "user_id": user['id'],
            "username": user['username'],
            "display_name": user.get('display_name'),
            "avatar_path": user.get('avatar_path'),
        })
        return {"user": user}
    finally:
        await db.close()


@app.post("/api/profile/password")
async def change_password(
    current_password: str = Query(...),
    new_password: str = Query(..., min_length=6, max_length=200),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        async with db.execute(
            "SELECT password_hash, salt FROM users WHERE id = ?", (user['id'],)
        ) as cursor:
            row = await cursor.fetchone()
        if not row or not verify_password(current_password, row['salt'], row['password_hash']):
            raise HTTPException(400, "Current password is incorrect")
        pwd_hash, salt = hash_password(new_password)
        await db.execute(
            "UPDATE users SET password_hash = ?, salt = ? WHERE id = ?",
            (pwd_hash, salt, user['id'])
        )
        await db.commit()
        schedule_db_sync()
        return {"status": "ok"}
    finally:
        await db.close()


_MAGIC_TO_TYPE = [
    (b"\xff\xd8\xff", "image/jpeg"),
    (b"\x89PNG\r\n\x1a\n", "image/png"),
    (b"GIF87a", "image/gif"),
    (b"GIF89a", "image/gif"),
    (b"RIFF", "image/webp"),
    (b"\x00\x00\x01\x00", "image/x-icon"),
    (b"BM", "image/bmp"),
]


def sniff_image_type(data: bytes) -> Optional[str]:
    for magic, mime in _MAGIC_TO_TYPE:
        if data.startswith(magic):
            if mime == "image/webp" and len(data) > 12:
                if data[8:12] != b"WEBP":
                    continue
            return mime
    return None


@app.post("/api/profile/avatar")
async def upload_avatar(
    file: UploadFile = File(...),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)

    # Write to temp file on disk first, not RAM
    with tempfile.NamedTemporaryFile(delete=False, suffix=".img", dir=TEMP_DIR) as tmp:
        tmp_path = tmp.name
        size = 0
        head = b""
        chunk = await file.read(65536)
        while chunk:
            if size < 32:
                head += chunk[:32]
            size += len(chunk)
            if size > 5 * 1024 * 1024:
                os.unlink(tmp_path)
                raise HTTPException(400, "Image too large (max 5MB)")
            tmp.write(chunk)
            chunk = await file.read(65536)

    # Only actual image bytes are accepted (magic-number sniffing)
    detected = sniff_image_type(head) if head else None
    if not detected:
        os.unlink(tmp_path)
        raise HTTPException(400, "Invalid image file - must be PNG, JPEG, GIF, WebP or BMP")

    try:
        with open(tmp_path, 'rb') as f:
            data = f.read()
        ext = {".jpg": "", ".png": "", ".gif": "", ".webp": "", ".ico": "", ".bmp": ""}.get(
            os.path.splitext(file.filename or "")[1].lower(), "")
        if detected == "image/jpeg":
            ext = ".jpg"
        elif detected == "image/png":
            ext = ".png"
        elif detected == "image/gif":
            ext = ".gif"
        elif detected == "image/webp":
            ext = ".webp"
        remote_path = f"avatars/{user['username']}_{uuid.uuid4().hex}{ext}"
        try:
            await asyncio.to_thread(store_file, remote_path, data)
        except StorageUnavailableError:
            raise HTTPException(503, "File storage is unavailable")
    finally:
        os.unlink(tmp_path)

    db = await get_db()
    try:
        await db.execute(
            "UPDATE users SET avatar_path = ? WHERE id = ?",
            (remote_path, user['id'])
        )
        await db.commit()
        schedule_db_sync()
    finally:
        await db.close()

    # Live presence uses in-memory state; keep the online-users list fresh
    manager.update_profile(user['id'], avatar_path=remote_path)
    await manager.broadcast({
        "type": "profile_updated",
        "user_id": user['id'],
        "username": user['username'],
        "display_name": user['display_name'],
        "avatar_path": remote_path,
    })
    return {"avatar_path": remote_path}


# ------------------------------------------------------------------------
# Conversations (REST fallbacks - primary channel is WebSocket)
# ------------------------------------------------------------------------
@app.get("/api/conversations")
async def list_conversations(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        # REST fallback must seed the global watermark too (it normally happens
        # on first WS connect / signup), otherwise unread counts would be wrong.
        await _ensure_global_member(db, user['id'])
        await db.commit()
        online = {u["id"] for u in manager.get_online_users()}
        return {"conversations": await _conversations_for_user(db, user['id'], online)}
    finally:
        await db.close()


_dm_create_lock = asyncio.Lock()


async def _create_dm(db: aiosqlite.Connection, uid: int, target_id: int,
                     manager_ref=None) -> Optional[dict]:
    """Create (or re-invite) a private chat. Returns summary or None if invalid.

    Like group invites, every newly created DM is pending until the other person
    accepts. Re-inviting a conversation the user previously hid (or that the
    recipient rejected/left) also requires the recipient to accept again.

    Up to MAX_PRIVATE_CHATS_PER_PAIR private chats are allowed per pair of users.
    Hidden ('left') chats do not consume that slot and are restored on re-invite.
    Blocked users cannot create or be added to a private chat; the global room
    remains the only shared conversation.
    """
    if target_id == uid:
        raise HTTPException(400, "You can't start a private chat with yourself")

    async with _dm_create_lock:
        cursor = await db.execute(
            "SELECT id, username, display_name FROM users WHERE id = ?", (target_id,)
        )
        target = await cursor.fetchone()
        if not target:
            raise HTTPException(404, "User not found")
        if await _block_exists(db, uid, target_id):
            raise HTTPException(403, "You can't start a private chat with this user")

        low, high = sorted([uid, target_id])
        cursor = await db.execute(
            """SELECT id FROM conversations
               WHERE type = 'dm' AND is_group = 0 AND user_low_id = ? AND user_high_id = ?
               ORDER BY id ASC""",
            (low, high)
        )
        dm_ids = [r["id"] for r in await cursor.fetchall()]

        # An active chat is one both sides still participate in (either the
        # recipient accepted, or is currently holding a pending invite). A chat
        # the recipient rejected/left (or that this user hid) does not consume a
        # slot and can be re-invited without creating an extra conversation.
        active_count = 0
        reusable_id = None
        for cid in dm_ids:
            mine = await _membership(db, cid, uid)
            other = await _membership(db, cid, target_id)
            status = mine.get("status") if mine else None
            other_status = other.get("status") if other else None
            if status in ("accepted", "pending") and other_status in ("accepted", "pending"):
                active_count += 1
            elif ((status == "accepted" and (other is None or other_status == "left"))
                  or (status == "left" and other is not None)) and reusable_id is None:
                # Restore the oldest inactive chat. The recipient must accept
                # again (set to pending below) before the chat is usable.
                reusable_id = cid

        if active_count >= MAX_PRIVATE_CHATS_PER_PAIR:
            raise HTTPException(
                400,
                f"You already have {MAX_PRIVATE_CHATS_PER_PAIR} private chats with "
                f"{target['username']} - delete one to start another"
            )

        now = int(time.time())
        if reusable_id is not None:
            cid = reusable_id
            await db.execute(
                "UPDATE conversation_members SET status = 'accepted' "
                "WHERE conversation_id = ? AND user_id = ?",
                (cid, uid)
            )
            other = await _membership(db, cid, target_id)
            target_status = other.get("status") if other else None
            if not other:
                await db.execute(
                    "INSERT INTO conversation_members "
                    "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'pending', 'member')",
                    (cid, target_id, now)
                )
            elif target_status != "pending":
                # Re-invite: the recipient has to accept again even if they had
                # accepted before the chat was hidden/left.
                await db.execute(
                    "UPDATE conversation_members SET status = 'pending', role = 'member' "
                    "WHERE conversation_id = ? AND user_id = ?",
                    (cid, target_id)
                )
        else:
            cursor = await db.execute(
                """INSERT INTO conversations (type, title, created_by, user_low_id, user_high_id, is_group)
                   VALUES ('dm', '', ?, ?, ?, 0)""",
                (uid, low, high)
            )
            cid = cursor.lastrowid
            await db.execute(
                "INSERT INTO conversation_members "
                "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'accepted', 'member')",
                (cid, uid, now)
            )
            await db.execute(
                "INSERT INTO conversation_members "
                "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'pending', 'member')",
                (cid, target_id, now)
            )
        await db.commit()
        schedule_db_sync()

        online = {u["id"] for u in manager.get_online_users()} if manager_ref else set()
        return await _build_conversation_summary(db, cid, uid, online)


@app.post("/api/conversations/dm")
async def create_dm_rest(
    user_id: int = Query(...),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        summary = await _create_dm(db, user['id'], user_id, manager)
        if summary is None:
            raise HTTPException(400, "Could not create private chat")
        # Live-push the new conversation. The initiator gets it immediately; the
        # recipient gets a pending conversation_updated they must accept.
        online = {u["id"] for u in manager.get_online_users()}
        await manager.send_to_user(user['id'], {"type": "dm_created", "conversation": summary})
        peer = summary.get("peer") or {}
        if peer.get("id"):
            peer_summary = await _build_conversation_summary(db, summary["id"], peer["id"], online)
            await manager.send_to_user(peer["id"], {"type": "conversation_updated",
                                                    "conversation": peer_summary})
        return {"conversation": summary}
    finally:
        await db.close()


_group_create_lock = asyncio.Lock()
_MAX_GROUP_NAME = 60


async def _create_group(db: aiosqlite.Connection, creator_id: int, name: str,
                        member_ids: List[int]) -> dict:
    if not name or not name.strip():
        raise HTTPException(400, "Group name is required")
    name = name.strip()[:_MAX_GROUP_NAME]
    member_ids = sorted({int(m) for m in member_ids if int(m) != creator_id})
    if not member_ids:
        raise HTTPException(400, "Select at least one member")
    if len(member_ids) > 50:
        raise HTTPException(400, "A group can have at most 50 members")

    placeholders = ",".join("?" * len(member_ids))
    cursor = await db.execute(
        f"SELECT id, username, display_name FROM users WHERE id IN ({placeholders})",
        member_ids
    )
    users = {r["id"]: r for r in await cursor.fetchall()}
    for mid in member_ids:
        if mid not in users:
            raise HTTPException(404, "Could not find a member")
        if await _block_exists(db, creator_id, mid):
            raise HTTPException(403, f"You can't include a blocked user: {users[mid]['username']}")
    for i in range(len(member_ids)):
        for j in range(i + 1, len(member_ids)):
            if await _block_exists(db, member_ids[i], member_ids[j]):
                raise HTTPException(
                    403,
                    f"{users[member_ids[i]]['username']} and {users[member_ids[j]]['username']} "
                    "have a block; they cannot be in the same group"
                )

    async with _group_create_lock:
        cursor = await db.execute(
            """INSERT INTO conversations (type, title, created_by, is_group)
               VALUES ('dm', ?, ?, 1)""",
            (name, creator_id)
        )
        cid = cursor.lastrowid
        now = int(time.time())
        await db.execute(
            "INSERT INTO conversation_members "
            "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'accepted', 'owner')",
            (cid, creator_id, now)
        )
        await db.executemany(
            "INSERT OR IGNORE INTO conversation_members "
            "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'pending', 'member')",
            [(cid, mid, now) for mid in member_ids]
        )
        await db.commit()
        schedule_db_sync()

    online = {u["id"] for u in manager.get_online_users()}
    summary = await _build_conversation_summary(db, cid, creator_id, online)

    # Invitees see a pending conversation so they can accept or decline.
    for mid in member_ids:
        invite = await _build_conversation_summary(db, cid, mid, online)
        await manager.send_to_user(mid, {"type": "conversation_updated", "conversation": invite})
    return summary


@app.post("/api/conversations/group")
async def create_group_rest(
    name: str = Query(..., max_length=_MAX_GROUP_NAME),
    member_ids: str = Query(...),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    ids = [int(x) for x in member_ids.split(",") if x.strip()]
    db = await get_db()
    try:
        summary = await _create_group(db, user['id'], name, ids)
        await manager.send_to_user(user['id'], {"type": "dm_created", "conversation": summary})
        return {"conversation": summary}
    finally:
        await db.close()


async def _accept_conversation_invite(db: aiosqlite.Connection, uid: int, cid: int) -> dict:
    member = await _membership(db, cid, uid)
    if not member or member.get("status") != "pending":
        raise HTTPException(404, "You don't have a pending invite to this conversation")
    if await _conversation_blocked(db, cid, uid):
        raise HTTPException(403, "This conversation is hidden because of a block")
    await db.execute(
        "UPDATE conversation_members SET status = 'accepted' WHERE conversation_id = ? AND user_id = ?",
        (cid, uid)
    )
    await db.commit()
    schedule_db_sync()
    return await _build_conversation_summary(db, cid, uid, {u["id"] for u in manager.get_online_users()})


async def _reject_conversation_invite(db: aiosqlite.Connection, uid: int, cid: int) -> bool:
    member = await _membership(db, cid, uid)
    if not member or member.get("status") != "pending":
        raise HTTPException(404, "You don't have a pending invite to this conversation")
    await db.execute("DELETE FROM conversation_members WHERE conversation_id = ? AND user_id = ?",
                     (cid, uid))
    await db.commit()
    schedule_db_sync()
    # Tell the people who remain (e.g. the initiator) that this invite was
    # declined so they are not left wondering why the chat is still pending.
    online = {u["id"] for u in manager.get_online_users()}
    for mid in await _group_update_recipient_ids(db, cid):
        await manager.send_to_user(mid, {
            "type": "conversation_updated",
            "conversation": await _build_conversation_summary(db, cid, mid, online),
            "invite_rejected": True,
            "rejected_user_id": uid,
        })
    return True


@app.post("/api/conversations/{conversation_id}/accept")
async def accept_conversation_invite_rest(
    conversation_id: int,
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        summary = await _accept_conversation_invite(db, user['id'], conversation_id)
        online = {u["id"] for u in manager.get_online_users()}
        members = await _group_update_recipient_ids(db, conversation_id)
        for mid in members:
            await manager.send_to_user(mid, {"type": "conversation_updated",
                                             "conversation": await _build_conversation_summary(
                                                 db, conversation_id, mid, online)})
        return {"conversation": summary}
    finally:
        await db.close()


@app.post("/api/conversations/{conversation_id}/reject")
async def reject_conversation_invite_rest(
    conversation_id: int,
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await _reject_conversation_invite(db, user['id'], conversation_id)
        return {"status": "rejected"}
    finally:
        await db.close()


@app.post("/api/conversations/{conversation_id}/members")
async def add_group_members_rest(
    conversation_id: int,
    member_ids: str = Query(...),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        member = await _membership(db, conversation_id, user['id'])
        conv = await db.execute("SELECT * FROM conversations WHERE id = ?", (conversation_id,))
        row = await conv.fetchone()
        if not row or not row["is_group"]:
            raise HTTPException(400, "Only groups can have members added")
        if not member or member.get("status") != "accepted":
            raise HTTPException(403, "Accept the group invite first")
        if not await user_in_conversation(db, user['id'], conversation_id):
            raise HTTPException(403, "You can no longer access this conversation")

        ids = [int(x) for x in member_ids.split(",") if x.strip()]
        existing = await _raw_conversation_member_ids(db, conversation_id)
        new_ids = [mid for mid in ids if mid != user['id']]
        if len(existing) + len(set(new_ids)) > 50:
            raise HTTPException(400, "A group can have at most 50 members")
        now = int(time.time())
        for i in range(len(ids)):
            for j in range(i + 1, len(ids)):
                if await _block_exists(db, ids[i], ids[j]):
                    raise HTTPException(403, "Selected users have a block between them")
        for mid in ids:
            if not mid or mid == user['id']:
                continue
            if await _block_exists(db, user['id'], mid):
                raise HTTPException(403, "You can't add a blocked user")
            for existing_id in existing:
                if await _block_exists(db, mid, existing_id):
                    raise HTTPException(
                        403, "That user has a block with someone already in this group")
            await db.execute(
                "INSERT OR IGNORE INTO conversation_members "
                "(conversation_id, user_id, joined_at, status, role) VALUES (?, ?, ?, 'pending', 'member')",
                (conversation_id, mid, now)
            )
        await db.commit()
        schedule_db_sync()
        online = {u["id"] for u in manager.get_online_users()}
        summary = await _build_conversation_summary(db, conversation_id, user['id'], online)
        members = await _group_update_recipient_ids(db, conversation_id)
        for mid in ids:
            if mid == user['id'] or await _conversation_blocked(db, conversation_id, mid):
                continue
            invite = await _build_conversation_summary(db, conversation_id, mid, online)
            await manager.send_to_user(mid, {"type": "conversation_updated", "conversation": invite})
        for mid in members:
            await manager.send_to_user(mid, {"type": "conversation_updated",
                                             "conversation": await _build_conversation_summary(
                                                 db, conversation_id, mid, online)})
        return {"conversation": summary}
    finally:
        await db.close()


@app.patch("/api/conversations/{conversation_id}")
async def rename_conversation_rest(
    conversation_id: int,
    name: str = Query(..., max_length=_MAX_GROUP_NAME),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    name = name.strip()
    if not name:
        raise HTTPException(400, "Name cannot be empty")
    db = await get_db()
    try:
        if not await user_in_conversation(db, user['id'], conversation_id):
            raise HTTPException(403, "You can't rename this conversation")
        await db.execute(
            "UPDATE conversations SET custom_name = ?, title = ? WHERE id = ?",
            (name, name, conversation_id)
        )
        await db.commit()
        schedule_db_sync()
        online = {u["id"] for u in manager.get_online_users()}
        summary = await _build_conversation_summary(db, conversation_id, user['id'], online)
        members = await _group_update_recipient_ids(db, conversation_id)
        for mid in members:
            await manager.send_to_user(mid, {"type": "conversation_updated",
                                             "conversation": await _build_conversation_summary(
                                                 db, conversation_id, mid, online)})
        return {"conversation": summary}
    finally:
        await db.close()


async def _hard_delete_conversation(db: aiosqlite.Connection, cid: int):
    """Permanently remove a conversation, its messages, receipts and members."""
    await db.execute(
        "DELETE FROM read_receipts WHERE message_id IN "
        "(SELECT id FROM messages WHERE conversation_id = ?)", (cid,)
    )
    await db.execute("DELETE FROM messages WHERE conversation_id = ?", (cid,))
    await db.execute("DELETE FROM conversation_members WHERE conversation_id = ?", (cid,))
    await db.execute("DELETE FROM conversations WHERE id = ?", (cid,))


async def _delete_dm_side(db: aiosqlite.Connection, cid: int, uid: int,
                          conv: dict) -> dict:
    """Hide a private chat for `uid`, or delete it for everyone when no accepted
    peer remains.

    Return the status to report back to the caller.
    """
    member = await _membership(db, cid, uid)
    if not member:
        raise HTTPException(404, "You are not part of this private chat")
    if member.get("status") == "left":
        return {"status": "hidden", "conversation_id": cid}
    if member.get("status") != "accepted":
        raise HTTPException(403, "Accept or reject the invite before deleting this chat")

    other_id = conv["user_high_id"] if conv["user_low_id"] == uid else conv["user_low_id"]
    other = await _membership(db, cid, other_id) if other_id else None
    other_active = other and other.get("status") == "accepted"
    online = {u["id"] for u in manager.get_online_users()}

    if not other_active:
        # No one else has actually accepted: cancel/delete the chat for everyone.
        await _hard_delete_conversation(db, cid)
        await db.commit()
        schedule_db_sync()
        notify = {uid}
        if other is not None:
            notify.add(other_id)
        for mid in notify:
            await manager.send_to_user(mid, {
                "type": "conversation_deleted",
                "conversation_id": cid,
            })
        return {"status": "deleted", "conversation_id": cid}

    # The other person is still in the chat: hide it only for the current user.
    await db.execute(
        "UPDATE conversation_members SET status = 'left' "
        "WHERE conversation_id = ? AND user_id = ?",
        (cid, uid)
    )
    await db.commit()
    schedule_db_sync()

    await manager.send_to_user(uid, {
        "type": "conversation_left",
        "conversation_id": cid,
        "user_id": uid,
        "reason": "deleted",
    })
    await manager.send_to_user(other_id, {
        "type": "conversation_left",
        "conversation_id": cid,
        "user_id": uid,
        "reason": "deleted",
        "conversation": await _build_conversation_summary(db, cid, other_id, online),
    })
    return {"status": "hidden", "conversation_id": cid}


@app.delete("/api/conversations/{conversation_id}")
async def delete_or_leave_conversation_rest(
    conversation_id: int,
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        member = await _membership(db, conversation_id, user['id'])
        cursor = await db.execute("SELECT * FROM conversations WHERE id = ?", (conversation_id,))
        conv = await cursor.fetchone()
        if not conv:
            raise HTTPException(404, "Conversation not found")

        # Private chats hide for the deleting user; they are only removed for
        # everyone when the other person also deletes (or never accepted).
        if conv["is_group"] != 1:
            return await _delete_dm_side(db, conversation_id, user['id'], conv)

        if not member or member.get("status") != "accepted":
            raise HTTPException(403, "You are not a member of this group")

        notify_ids = await _group_update_recipient_ids(db, conversation_id)
        # Owner deletes the group permanently; members leave the group.
        if member and member.get("role") == "owner" and member.get("status") == "accepted":
            await _hard_delete_conversation(db, conversation_id)
            await db.commit()
            schedule_db_sync()
            for mid in notify_ids:
                await manager.send_to_user(mid, {"type": "conversation_deleted",
                                                 "conversation_id": conversation_id})
            return {"status": "deleted", "conversation_id": conversation_id}

        # Non-owner: leave (removes membership, keeps the group + history).
        await db.execute("DELETE FROM conversation_members WHERE conversation_id = ? AND user_id = ?",
                         (conversation_id, user['id']))
        await db.commit()
        schedule_db_sync()
        # Only notify the members who remain in the group.
        remaining = await conversation_member_ids(db, conversation_id)
        for mid in remaining:
            await manager.send_to_user(mid, {"type": "conversation_left",
                                             "conversation_id": conversation_id,
                                             "user_id": user['id'],
                                             "conversation": await _build_conversation_summary(
                                                 db, conversation_id, mid,
                                                 {u["id"] for u in manager.get_online_users()})})
        return {"status": "left", "conversation_id": conversation_id}
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Users: search + blocking
# ------------------------------------------------------------------------
@app.get("/api/users")
async def list_users(
    query: str = Query("", max_length=100),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    q = query.strip().lower()
    db = await get_db()
    try:
        if q:
            cursor = await db.execute(
                """SELECT id, username, display_name, avatar_path, status
                   FROM users WHERE id != ? AND (lower(username) LIKE ? OR lower(display_name) LIKE ?)
                   ORDER BY CASE WHEN status = 'online' THEN 0 ELSE 1 END, username LIMIT 100""",
                (user['id'], f"%{q}%", f"%{q}%")
            )
        else:
            cursor = await db.execute(
                """SELECT id, username, display_name, avatar_path, status FROM users
                   WHERE id != ? ORDER BY CASE WHEN status = 'online' THEN 0 ELSE 1 END, username LIMIT 100""",
                (user['id'],)
            )
        out = []
        for r in await cursor.fetchall():
            out.append({
                "id": r["id"],
                "username": r["username"],
                "display_name": r["display_name"],
                "avatar_path": r["avatar_path"],
                "online": r["status"] == "online" and manager.is_online(r["id"]),
                "blocked": await _block_exists(db, user['id'], r["id"]),
            })
        return {"users": out}
    finally:
        await db.close()


@app.get("/api/users/blocked")
async def blocked_users(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        cursor = await db.execute(
            """SELECT u.id, u.username, u.display_name, u.avatar_path, b.created_at
               FROM user_blocks b JOIN users u ON u.id = b.blocked_id
               WHERE b.blocker_id = ? ORDER BY b.created_at DESC""",
            (user['id'],)
        )
        return {"users": [dict(r) for r in await cursor.fetchall()]}
    finally:
        await db.close()


@app.post("/api/users/{user_id}/block")
async def block_user_rest(user_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    if user_id == user['id']:
        raise HTTPException(400, "You can't block yourself")
    db = await get_db()
    try:
        cursor = await db.execute("SELECT id FROM users WHERE id = ?", (user_id,))
        if not await cursor.fetchone():
            raise HTTPException(404, "User not found")
        await db.execute(
            "INSERT OR IGNORE INTO user_blocks (blocker_id, blocked_id) VALUES (?, ?)",
            (user['id'], user_id)
        )
        await db.commit()
        schedule_db_sync()
        await manager.send_to_user(user_id, {
            "type": "block_changed",
            "blocker_id": user['id'],
            "blocked": True,
        })
        return {"status": "blocked", "user_id": user_id}
    finally:
        await db.close()


@app.delete("/api/users/{user_id}/block")
async def unblock_user_rest(user_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await db.execute(
            "DELETE FROM user_blocks WHERE blocker_id = ? AND blocked_id = ?",
            (user['id'], user_id)
        )
        await db.commit()
        schedule_db_sync()
        await manager.send_to_user(user_id, {
            "type": "block_changed",
            "blocker_id": user['id'],
            "blocked": False,
        })
        return {"status": "unblocked", "user_id": user_id}
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Social feed (Twitter/X-like)
# ------------------------------------------------------------------------
MAX_POST_LENGTH = 28000
MAX_POST_MEDIA = 4


async def _blocked_user_ids(db: aiosqlite.Connection, uid: int) -> set:
    """All users who are in a block relationship with uid (either direction)."""
    cursor = await db.execute(
        "SELECT blocker_id, blocked_id FROM user_blocks "
        "WHERE blocker_id = ? OR blocked_id = ?",
        (uid, uid)
    )
    ids = set()
    for row in await cursor.fetchall():
        ids.add(row["blocker_id"])
        ids.add(row["blocked_id"])
    ids.discard(uid)
    return ids


async def _social_user(db: aiosqlite.Connection, uid: int) -> dict:
    cursor = await db.execute(
        "SELECT id, username, display_name, avatar_path, bio, location, website "
        "FROM users WHERE id = ?", (uid,)
    )
    row = await cursor.fetchone()
    if not row:
        raise HTTPException(404, "User not found")
    return dict(row)


async def _toggle_row(db: aiosqlite.Connection, table: str, post_id: int, uid: int):
    """Insert/remove a (post_id,user_id) row; returns True when now present."""
    cursor = await db.execute(f"SELECT 1 FROM {table} WHERE post_id = ? AND user_id = ?",
                              (post_id, uid))
    exists = await cursor.fetchone() is not None
    if exists:
        await db.execute(f"DELETE FROM {table} WHERE post_id = ? AND user_id = ?", (post_id, uid))
        return False
    await db.execute(f"INSERT INTO {table} (post_id, user_id, created_at_ms) VALUES (?,?,?)",
                     (post_id, uid, int(time.time() * 1000)))
    return True


async def _count_post(db: aiosqlite.Connection, table: str, post_id: int) -> int:
    cursor = await db.execute(f"SELECT COUNT(*) AS c FROM {table} WHERE post_id = ?", (post_id,))
    row = await cursor.fetchone()
    return row["c"] or 0


async def _post_exists(db: aiosqlite.Connection, post_id: int, include_deleted: bool = False) -> bool:
    sql = "SELECT 1 FROM social_posts WHERE id = ?"
    args = [post_id]
    if not include_deleted:
        sql += " AND is_deleted = 0"
    cursor = await db.execute(sql, args)
    return await cursor.fetchone() is not None


async def _post_author_id(db: aiosqlite.Connection, post_id: int) -> Optional[int]:
    cur = await db.execute("SELECT author_id FROM social_posts WHERE id = ?", (post_id,))
    row = await cur.fetchone()
    return row["author_id"] if row else None


async def _ensure_interactable_post(db: aiosqlite.Connection, post_id: int, viewer_id: int) -> None:
    author = await _post_author_id(db, post_id)
    if author is None:
        raise HTTPException(404, "Post not found")
    if await _block_exists(db, viewer_id, author):
        raise HTTPException(403, "You cannot interact with this user")


async def _serialize_post(db: aiosqlite.Connection, row: dict, viewer_id: int,
                          reposter_id: int = None) -> dict:
    author = await _social_user(db, row["author_id"])
    media = []
    if row.get("media_json"):
        try:
            media = json.loads(row["media_json"])
        except Exception:
            media = []
    quote = None
    if row.get("quote_id"):
        qcur = await db.execute("SELECT * FROM social_posts WHERE id = ?", (row["quote_id"],))
        qrow = await qcur.fetchone()
        if qrow and not qrow["is_deleted"] and not await _block_exists(db, viewer_id, qrow["author_id"]):
            quote = await _serialize_post(db, dict(qrow), viewer_id)
    async def has(table):
        cur = await db.execute(f"SELECT 1 FROM {table} WHERE post_id = ? AND user_id = ?",
                               (row["id"], viewer_id))
        return await cur.fetchone() is not None
    reposter = None
    if reposter_id:
        ru = await _social_user(db, reposter_id)
        reposter = {
            "id": ru["id"], "username": ru["username"],
            "display_name": ru["display_name"], "avatar_path": ru["avatar_path"],
        }
    return {
        "id": row["id"],
        "author": {
            "id": author["id"],
            "username": author["username"],
            "display_name": author["display_name"],
            "avatar_path": author["avatar_path"],
        },
        "body": row["body"],
        "media": media,
        "reply_to_id": row["reply_to_id"],
        "quote_id": row["quote_id"],
        "quote": quote,
        "created_at_ms": row["created_at_ms"],
        "edited_at_ms": row["edited_at_ms"],
        "reposted_at_ms": row.get("reposted_at_ms"),
        "reposter_id": reposter_id,
        "reposter": reposter,
        "like_count": await _count_post(db, "social_likes", row["id"]),
        "repost_count": await _count_post(db, "social_reposts", row["id"]),
        "reply_count": await _count_replies(db, row["id"]),
        "bookmark_count": await _count_post(db, "social_bookmarks", row["id"]),
        "liked": await has("social_likes"),
        "reposted": await has("social_reposts"),
        "bookmarked": await has("social_bookmarks"),
    }


async def _count_replies(db: aiosqlite.Connection, post_id: int) -> int:
    cursor = await db.execute(
        "SELECT COUNT(*) AS c FROM social_posts WHERE reply_to_id = ? AND is_deleted = 0",
        (post_id,)
    )
    return (await cursor.fetchone())["c"] or 0


async def _serialize_post_rows(db: aiosqlite.Connection, rows: List[dict],
                               viewer_id: int, reposters: List[int] = None) -> List[dict]:
    out = []
    blocked = await _blocked_user_ids(db, viewer_id)
    for i, row in enumerate(rows):
        if row["author_id"] in blocked:
            continue
        if row.get("reposter_id") is not None and row["reposter_id"] in blocked:
            continue
        reposter = row.get("reposter_id") if row.get("reposter_id") is not None else ((reposters or [None])[i] if reposters else None)
        out.append(await _serialize_post(db, row, viewer_id, reposter))
    return out


async def _extract_mentions(body: str) -> List[str]:
    return list(dict.fromkeys(re.findall(r"@([A-Za-z0-9_]{1,30})", body or "")))


async def _extract_hashtags(body: str) -> List[str]:
    return list(dict.fromkeys(re.findall(r"#([A-Za-z0-9_]{1,100})", body or "")))


async def _notify_social(db: aiosqlite.Connection, user_id: int, actor_id: int,
                         ntype: str, post_id: int = None):
    if user_id == actor_id:
        return
    if await _block_exists(db, user_id, actor_id):
        return
    await db.execute(
        "INSERT INTO social_notifications (user_id, actor_id, type, post_id, created_at_ms) "
        "VALUES (?,?,?,?,?)",
        (user_id, actor_id, ntype, post_id, int(time.time() * 1000))
    )


async def _load_post(db: aiosqlite.Connection, post_id: int, viewer_id: int) -> dict:
    cur = await db.execute("SELECT * FROM social_posts WHERE id = ?", (post_id,))
    row = await cur.fetchone()
    if not row or row["is_deleted"]:
        raise HTTPException(404, "Post not found")
    if await _block_exists(db, viewer_id, row["author_id"]):
        raise HTTPException(403, "You cannot view posts from this user")
    return await _serialize_post(db, dict(row), viewer_id)


async def _social_author_ids(db: aiosqlite.Connection, uid: int) -> List[int]:
    ids = {uid}
    cursor = await db.execute("SELECT following_id FROM follows WHERE follower_id = ?", (uid,))
    for row in await cursor.fetchall():
        ids.add(row["following_id"])
    return list(ids)


_SOCIAL_MEDIA_EXTS = (".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".avif",
                       ".mp4", ".webm", ".mov", ".ogg", ".m4v", ".mpeg", ".mpg")


def _is_social_media(m: dict) -> bool:
    mtype = (str(m.get("file_type", "") or "")).lower()
    path = (str(m.get("file_path", "") or "")).lower()
    return mtype.startswith("image/") or mtype.startswith("video/") or path.endswith(_SOCIAL_MEDIA_EXTS)


@app.post("/api/social/posts")
async def create_social_post_rest(
    body: str = Query("", max_length=MAX_POST_LENGTH),
    media_json: str = Query(""),
    reply_to_id: int = Query(None),
    quote_id: int = Query(None),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    body = (body or "").strip()
    if not body and not media_json:
        raise HTTPException(400, "Post cannot be empty")
    media = []
    if media_json:
        try:
            media = json.loads(media_json)
        except Exception:
            raise HTTPException(400, "Invalid media payload")
        if not isinstance(media, list) or len(media) > MAX_POST_MEDIA:
            raise HTTPException(400, f"A post can contain at most {MAX_POST_MEDIA} media files")
        for m in media:
            path = m.get("file_path", "")
            if not str(path).startswith(f"uploads/{user['username']}/"):
                raise HTTPException(403, "You can only attach files you uploaded")
            if not _is_social_media(m):
                raise HTTPException(400, "Only images and videos can be attached to posts")
    db = await get_db()
    try:
        if reply_to_id:
            await _ensure_interactable_post(db, reply_to_id, user["id"])
        if quote_id:
            await _ensure_interactable_post(db, quote_id, user["id"])
        cur = await db.execute(
            "INSERT INTO social_posts (author_id, body, media_json, reply_to_id, quote_id, "
            "created_at_ms) VALUES (?,?,?,?,?,?)",
            (user["id"], body, media_json or None, reply_to_id, quote_id,
             int(time.time() * 1000))
        )
        post_id = cur.lastrowid
        await db.commit()
        schedule_db_sync()
        # Notify reply target + mentioned users.
        if reply_to_id:
            rt = await db.execute("SELECT author_id FROM social_posts WHERE id = ?", (reply_to_id,))
            rt_row = await rt.fetchone()
            if rt_row:
                await _notify_social(db, rt_row["author_id"], user["id"], "reply", post_id)
        for handle in await _extract_mentions(body):
            mc = await db.execute("SELECT id FROM users WHERE lower(username) = lower(?)", (handle,))
            mrow = await mc.fetchone()
            if mrow:
                await _notify_social(db, mrow["id"], user["id"], "mention", post_id)
        await db.commit()
        post = await _load_post(db, post_id, user["id"])
        return {"post": post}
    finally:
        await db.close()


@app.get("/api/social/posts/{post_id}")
async def get_post_rest(post_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        return {"post": await _load_post(db, post_id, user["id"])}
    finally:
        await db.close()


@app.get("/api/social/posts/{post_id}/replies")
async def get_replies_rest(post_id: int, limit: int = Query(50, le=100),
                           token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await _ensure_interactable_post(db, post_id, user["id"])
        cur = await db.execute(
            "SELECT * FROM social_posts WHERE reply_to_id = ? AND is_deleted = 0 "
            "ORDER BY created_at_ms ASC LIMIT ?", (post_id, limit))
        rows = [dict(r) for r in await cur.fetchall()]
        return {"replies": await _serialize_post_rows(db, rows, user["id"])}
    finally:
        await db.close()


@app.delete("/api/social/posts/{post_id}")
async def delete_social_post_rest(post_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        cur = await db.execute("SELECT author_id FROM social_posts WHERE id = ?", (post_id,))
        row = await cur.fetchone()
        if not row:
            raise HTTPException(404, "Post not found")
        if row["author_id"] != user["id"]:
            raise HTTPException(403, "You can only delete your own posts")
        await db.execute("UPDATE social_posts SET is_deleted = 1 WHERE id = ?", (post_id,))
        await db.commit()
        schedule_db_sync()
        return {"status": "deleted", "post_id": post_id}
    finally:
        await db.close()


@app.patch("/api/social/posts/{post_id}")
async def edit_social_post_rest(
    post_id: int,
    body: str = Query("", max_length=MAX_POST_LENGTH),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    body = (body or "").strip()
    if not body:
        raise HTTPException(400, "Post body cannot be empty")
    db = await get_db()
    try:
        cur = await db.execute("SELECT author_id, is_deleted FROM social_posts WHERE id = ?", (post_id,))
        row = await cur.fetchone()
        if not row or row["is_deleted"]:
            raise HTTPException(404, "Post not found")
        if row["author_id"] != user["id"]:
            raise HTTPException(403, "You can only edit your own posts")
        await db.execute(
            "UPDATE social_posts SET body = ?, edited_at_ms = ? WHERE id = ?",
            (body, int(time.time() * 1000), post_id)
        )
        await db.commit()
        schedule_db_sync()
        return {"post": await _load_post(db, post_id, user["id"])}
    finally:
        await db.close()


@app.post("/api/social/posts/{post_id}/like")
async def toggle_like_rest(post_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await _ensure_interactable_post(db, post_id, user["id"])
        liked = await _toggle_row(db, "social_likes", post_id, user["id"])
        await db.commit()
        if liked:
            cur = await db.execute("SELECT author_id FROM social_posts WHERE id = ?", (post_id,))
            row = await cur.fetchone()
            await _notify_social(db, row["author_id"], user["id"], "like", post_id)
            await db.commit()
        return {"liked": liked, "like_count": await _count_post(db, "social_likes", post_id)}
    finally:
        await db.close()


@app.post("/api/social/posts/{post_id}/repost")
async def toggle_repost_rest(post_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await _ensure_interactable_post(db, post_id, user["id"])
        reposted = await _toggle_row(db, "social_reposts", post_id, user["id"])
        await db.commit()
        if reposted:
            cur = await db.execute("SELECT author_id FROM social_posts WHERE id = ?", (post_id,))
            row = await cur.fetchone()
            await _notify_social(db, row["author_id"], user["id"], "repost", post_id)
            await db.commit()
        return {"reposted": reposted, "repost_count": await _count_post(db, "social_reposts", post_id)}
    finally:
        await db.close()


@app.post("/api/social/posts/{post_id}/bookmark")
async def toggle_bookmark_rest(post_id: int, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await _ensure_interactable_post(db, post_id, user["id"])
        bookmarked = await _toggle_row(db, "social_bookmarks", post_id, user["id"])
        await db.commit()
        return {"bookmarked": bookmarked}
    finally:
        await db.close()


@app.get("/api/social/feed")
async def get_social_feed_rest(
    feed: str = Query("home"),
    cursor: int = Query(-1),
    limit: int = Query(30, le=100),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        blocked = await _blocked_user_ids(db, user["id"])
        if feed == "home":
            # Home shows all public posts (so other users can see them) mixed
            # with reposts made by accounts the viewer follows / themself.
            author_ids = await _social_author_ids(db, user["id"])
            cur = await db.execute(
                "SELECT * FROM social_posts WHERE is_deleted = 0 "
                "ORDER BY created_at_ms DESC LIMIT ?", (limit * 3,))
            rows = [dict(r) for r in await cur.fetchall()]
            reposter_ids = list(author_ids)
            reposts = []
            if reposter_ids:
                ph = ",".join("?" * len(reposter_ids))
                cur = await db.execute(
                    f"SELECT p.*, r.user_id AS reposter_id, r.created_at_ms AS reposted_at_ms "
                    f"FROM social_reposts r JOIN social_posts p ON p.id = r.post_id "
                    f"WHERE r.user_id IN ({ph}) AND p.is_deleted = 0",
                    reposter_ids)
                reposts = [dict(r) for r in await cur.fetchall()]
                repost_ids = {r["id"] for r in reposts}
                # Prefer the repost entry when a followed account reposts a post;
                # the original post is still visible to everyone below when not reposted.
                rows = [r for r in rows if r["id"] not in repost_ids] + reposts
                rows = sorted(rows, key=lambda r: r.get("reposted_at_ms") or r["created_at_ms"], reverse=True)
        elif feed == "explore":
            cur = await db.execute(
                "SELECT * FROM social_posts WHERE is_deleted = 0 "
                "ORDER BY created_at_ms DESC LIMIT ?", (limit * 3,))
            rows = [dict(r) for r in await cur.fetchall()]
        else:
            raise HTTPException(400, "Unknown feed")
        # Never let blocked users crowd out visible posts before pagination.
        rows = [r for r in rows
                if r["author_id"] not in blocked
                and (r.get("reposter_id") is None or r["reposter_id"] not in blocked)]
        if cursor >= 0:
            rows = [r for r in rows if r["created_at_ms"] < cursor]
        rows = rows[:limit]
        posts = await _serialize_post_rows(db, rows, user["id"])
        posts.sort(key=lambda p: p.get("reposted_at_ms") or p["created_at_ms"], reverse=True)
        return {"posts": posts, "cursor": posts[-1]["created_at_ms"] if posts else None}
    finally:
        await db.close()


@app.get("/api/social/users/{username}/posts")
async def get_user_posts_rest(username: str, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        u = await db.execute("SELECT id FROM users WHERE lower(username) = lower(?)", (username,))
        urow = await u.fetchone()
        if not urow:
            raise HTTPException(404, "User not found")
        if await _block_exists(db, user["id"], urow["id"]):
            raise HTTPException(403, "Content is not available")
        cur = await db.execute(
            "SELECT * FROM social_posts WHERE author_id = ? AND is_deleted = 0 "
            "ORDER BY created_at_ms DESC LIMIT 100", (urow["id"],))
        rows = [dict(r) for r in await cur.fetchall()]
        return {"posts": await _serialize_post_rows(db, rows, user["id"])}
    finally:
        await db.close()


@app.get("/api/social/profile/{username}")
async def get_social_profile_rest(username: str, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        cur = await db.execute(
            "SELECT id FROM users WHERE lower(username) = lower(?)", (username,))
        urow = await cur.fetchone()
        if not urow:
            raise HTTPException(404, "User not found")
        target_id = urow["id"]
        blocked = await _block_exists(db, user["id"], target_id)
        if blocked:
            raise HTTPException(403, "Content is not available")
        prof = await _social_user(db, target_id)
        followers = await db.execute("SELECT COUNT(*) AS c FROM follows WHERE following_id = ?", (target_id,))
        following = await db.execute("SELECT COUNT(*) AS c FROM follows WHERE follower_id = ?", (target_id,))
        is_follow = await db.execute("SELECT 1 FROM follows WHERE follower_id = ? AND following_id = ?",
                                     (user["id"], target_id))
        posts = await db.execute("SELECT COUNT(*) AS c FROM social_posts WHERE author_id = ? AND is_deleted = 0",
                                 (target_id,))
        return {
            "profile": {
                **prof,
                "is_self": target_id == user["id"],
                "is_following": await is_follow.fetchone() is not None,
                "followers_count": (await followers.fetchone())["c"],
                "following_count": (await following.fetchone())["c"],
                "posts_count": (await posts.fetchone())["c"],
            }
        }
    finally:
        await db.close()


@app.post("/api/social/users/{username}/follow")
async def toggle_follow_rest(username: str, token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        cur = await db.execute("SELECT id FROM users WHERE lower(username) = lower(?)", (username,))
        row = await cur.fetchone()
        if not row:
            raise HTTPException(404, "User not found")
        target_id = row["id"]
        if target_id == user["id"]:
            raise HTTPException(400, "You cannot follow yourself")
        if await _block_exists(db, user["id"], target_id):
            raise HTTPException(403, "You cannot interact with this user")
        now = int(time.time() * 1000)
        existing = await db.execute("SELECT 1 FROM follows WHERE follower_id = ? AND following_id = ?",
                                    (user["id"], target_id))
        if await existing.fetchone():
            await db.execute("DELETE FROM follows WHERE follower_id = ? AND following_id = ?",
                             (user["id"], target_id))
            await db.commit()
            return {"following": False}
        await db.execute("INSERT INTO follows (follower_id, following_id, created_at_ms) VALUES (?,?,?)",
                         (user["id"], target_id, now))
        await _notify_social(db, target_id, user["id"], "follow")
        await db.commit()
        return {"following": True}
    finally:
        await db.close()


@app.get("/api/social/search")
async def social_search_rest(q: str = Query("", max_length=100),
                             token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    q = (q or "").strip()
    db = await get_db()
    try:
        blocked = await _blocked_user_ids(db, user["id"])
        users = []
        posts = []
        if q:
            like = f"%{q}%"
            ucur = await db.execute(
                "SELECT u.id, u.username, u.display_name, u.avatar_path, u.bio, "
                "(SELECT 1 FROM follows f WHERE f.follower_id = ? AND f.following_id = u.id) AS is_following "
                "FROM users u WHERE u.id != ? AND (lower(u.username) LIKE ? OR lower(u.display_name) LIKE ?) LIMIT 30",
                (user["id"], user["id"], like.lower(), like.lower()))
            for r in await ucur.fetchall():
                if r["id"] in blocked:
                    continue
                users.append({
                    "id": r["id"], "username": r["username"], "display_name": r["display_name"],
                    "avatar_path": r["avatar_path"], "bio": r["bio"],
                    "is_following": bool(r["is_following"]),
                })
            pcur = await db.execute(
                "SELECT * FROM social_posts WHERE is_deleted = 0 AND body LIKE ? "
                "ORDER BY created_at_ms DESC LIMIT 50", (like,))
            rows = [dict(r) for r in await pcur.fetchall() if r["author_id"] not in blocked]
            posts = await _serialize_post_rows(db, rows, user["id"])
        return {"users": users, "posts": posts}
    finally:
        await db.close()


@app.get("/api/social/suggestions")
async def social_suggestions_rest(limit: int = Query(5, le=20),
                                  token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        blocked = await _blocked_user_ids(db, user["id"])
        cur = await db.execute(
            "SELECT u.id, u.username, u.display_name, u.avatar_path, u.bio, "
            "(SELECT COUNT(*) FROM follows f WHERE f.following_id = u.id) AS followers_count "
            "FROM users u LEFT JOIN follows f ON f.follower_id = ? AND f.following_id = u.id "
            "WHERE u.id != ? AND u.id NOT IN (SELECT following_id FROM follows WHERE follower_id = ?) "
            "ORDER BY followers_count DESC, u.username LIMIT ?",
            (user["id"], user["id"], user["id"], limit)
        )
        out = []
        for r in await cur.fetchall():
            if r["id"] in blocked:
                continue
            out.append({
                "id": r["id"], "username": r["username"], "display_name": r["display_name"],
                "avatar_path": r["avatar_path"], "bio": r["bio"], "followers_count": r["followers_count"],
                "is_following": False,
            })
        return {"users": out}
    finally:
        await db.close()


@app.get("/api/social/trending")
async def social_trending_rest(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        since = int((time.time() - 7 * 24 * 3600) * 1000)
        cur = await db.execute(
            "SELECT body FROM social_posts WHERE is_deleted = 0 AND created_at_ms >= ?", (since,))
        counts = {}
        for row in await cur.fetchall():
            for tag in await _extract_hashtags(row["body"]):
                t = tag.lower()
                counts[t] = counts.get(t, 0) + 1
        top = [{"tag": k, "count": v} for k, v in counts.items()]
        top.sort(key=lambda x: (-x["count"], x["tag"]))
        return {"trending": top[:10]}
    finally:
        await db.close()


@app.get("/api/social/bookmarks")
async def social_bookmarks_rest(limit: int = Query(50, le=100),
                                token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        cur = await db.execute(
            "SELECT p.* FROM social_bookmarks b JOIN social_posts p ON p.id = b.post_id "
            "WHERE b.user_id = ? AND p.is_deleted = 0 ORDER BY b.created_at_ms DESC LIMIT ?",
            (user["id"], limit))
        rows = [dict(r) for r in await cur.fetchall()]
        return {"posts": await _serialize_post_rows(db, rows, user["id"])}
    finally:
        await db.close()


@app.get("/api/social/notifications")
async def social_notifications_rest(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        blocked = await _blocked_user_ids(db, user["id"])
        cur = await db.execute(
            "SELECT n.*, u.username AS actor_username, u.display_name AS actor_display_name, "
            "u.avatar_path AS actor_avatar_path FROM social_notifications n JOIN users u ON u.id = n.actor_id "
            "WHERE n.user_id = ? ORDER BY n.created_at_ms DESC LIMIT 100", (user["id"],))
        out = []
        for r in await cur.fetchall():
            if r["actor_id"] in blocked:
                continue
            out.append({
                "id": r["id"], "type": r["type"], "post_id": r["post_id"],
                "created_at_ms": r["created_at_ms"], "read": bool(r["read"]),
                "actor": {"id": r["actor_id"], "username": r["actor_username"],
                          "display_name": r["actor_display_name"], "avatar_path": r["actor_avatar_path"]},
            })
        return {"notifications": out}
    finally:
        await db.close()


@app.post("/api/social/notifications/read")
async def mark_notifications_read_rest(token: str = Header(..., alias="X-Auth-Token")):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        await db.execute("UPDATE social_notifications SET read = 1 WHERE user_id = ?", (user["id"],))
        await db.commit()
        return {"status": "read"}
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Admin console (separate /admin page)
# ------------------------------------------------------------------------
def _admin_configured() -> bool:
    return bool(ADMIN_USERNAME and ADMIN_PASSWORD)


def _require_admin(admin_token: Optional[str]) -> str:
    if not _admin_configured():
        raise HTTPException(503, "Admin login is not configured. Set ADMIN_USERNAME and ADMIN_PASSWORD.")
    if not admin_token:
        raise HTTPException(401, "Admin authentication required")
    now = time.time()
    with ADMIN_SESSION_LOCK:
        sess = ADMIN_SESSIONS.get(admin_token)
        if not sess or sess["expires"] < now:
            ADMIN_SESSIONS.pop(admin_token, None)
            raise HTTPException(401, "Admin session expired")
        return sess.get("username", "admin")


def _cleanup_admin_sessions(now: float = None) -> None:
    now = now or time.time()
    with ADMIN_SESSION_LOCK:
        for key, sess in list(ADMIN_SESSIONS.items()):
            if sess["expires"] < now:
                ADMIN_SESSIONS.pop(key, None)


@app.post("/api/admin/login")
async def admin_login(request: Request):
    if not _admin_configured():
        raise HTTPException(503, "Admin login is not configured. Set ADMIN_USERNAME and ADMIN_PASSWORD.")
    try:
        body = await request.json()
    except Exception:
        raise HTTPException(400, "Invalid JSON body")
    username = str(body.get("username", "") or "").strip()
    password = str(body.get("password", "") or "")
    if username != ADMIN_USERNAME or password != ADMIN_PASSWORD:
        raise HTTPException(401, "Invalid admin credentials")
    _cleanup_admin_sessions()
    token = secrets.token_urlsafe(32)
    now = time.time()
    with ADMIN_SESSION_LOCK:
        ADMIN_SESSIONS[token] = {
            "username": username,
            "created": now,
            "expires": now + ADMIN_SESSION_TTL,
        }
    return {"token": token, "username": username, "expires_in": ADMIN_SESSION_TTL}


@app.post("/api/admin/logout")
async def admin_logout(admin_token: str = Header(None, alias="X-Admin-Token")):
    with ADMIN_SESSION_LOCK:
        ADMIN_SESSIONS.pop(admin_token, None)
    return {"status": "logged_out"}


@app.get("/api/admin/status")
async def admin_status(admin_token: str = Header(None, alias="X-Admin-Token")):
    _require_admin(admin_token)
    backups = await asyncio.to_thread(list_backups)
    return {
        "app_version": APP_VERSION,
        "storage_mode": "local" if _OFFLINE else "bucket",
        "db_path": DATABASE_URL,
        "temp_dir": TEMP_DIR,
        "local_storage_dir": LOCAL_STORAGE_DIR,
        "backup_count": len(backups),
        "backup_interval_seconds": 3600,
        "admin_session_ttl_seconds": ADMIN_SESSION_TTL,
    }


@app.get("/api/admin/backups")
async def admin_backups(admin_token: str = Header(None, alias="X-Admin-Token")):
    _require_admin(admin_token)
    return {"backups": await asyncio.to_thread(list_backups), "offline": _OFFLINE}


@app.post("/api/admin/backups/create")
async def admin_create_backup(admin_token: str = Header(None, alias="X-Admin-Token")):
    _require_admin(admin_token)
    path = await asyncio.to_thread(create_timestamped_backup, DATABASE_URL, "manual")
    if not path:
        raise HTTPException(503, "Backup unavailable (storage offline or failure)")
    return {"status": "created", "path": path}


@app.post("/api/admin/backups/{backup_prefix:path}/restore")
async def admin_restore_backup(backup_prefix: str, admin_token: str = Header(None, alias="X-Admin-Token")):
    _require_admin(admin_token)
    try:
        count = await asyncio.to_thread(restore_backup, DATABASE_URL, backup_prefix)
    except StorageUnavailableError as e:
        raise HTTPException(503, str(e))
    except FileNotFoundError as e:
        raise HTTPException(404, str(e))
    except Exception as e:
        raise HTTPException(500, f"Restore failed: {e}")
    return {"status": "restored", "files": count, "restart_recommended": True}


@app.delete("/api/admin/backups/{backup_prefix:path}")
async def admin_delete_backup(backup_prefix: str, admin_token: str = Header(None, alias="X-Admin-Token")):
    _require_admin(admin_token)
    try:
        await asyncio.to_thread(delete_backup, backup_prefix)
    except ValueError as e:
        raise HTTPException(400, str(e))
    except StorageUnavailableError as e:
        raise HTTPException(503, str(e))
    except Exception as e:
        raise HTTPException(500, f"Delete failed: {e}")
    return {"status": "deleted", "path": backup_prefix}


# ------------------------------------------------------------------------
# Messages (REST fallbacks for reliable edit/delete/receipts)
# ------------------------------------------------------------------------
async def _get_message_row(db: aiosqlite.Connection, mid: int):
    cursor = await db.execute(
        """SELECT m.*, u.username, u.display_name, u.avatar_path
           FROM messages m JOIN users u ON u.id = m.sender_id
           WHERE m.id = ?""",
        (mid,)
    )
    return await cursor.fetchone()


@app.patch("/api/messages/{message_id}")
async def edit_message_rest(
    message_id: int,
    content: str = Query(..., max_length=MAX_MESSAGE_LENGTH),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    content = content.strip()
    if not content:
        raise HTTPException(400, "Message cannot be empty")
    db = await get_db()
    try:
        row = await _get_message_row(db, message_id)
        if not row or row["sender_id"] != user['id'] or row["is_deleted"]:
            raise HTTPException(404, "Message not found")
        if not await user_in_conversation(db, user['id'], row["conversation_id"]):
            raise HTTPException(403, "You can no longer access this conversation")
        new_enc = encrypt_message(content)
        await db.execute(
            "UPDATE messages SET encrypted_content = ?, is_edited = 1 WHERE id = ?",
            (new_enc, message_id)
        )
        await db.commit()
        schedule_db_sync()
        await _broadcast_to_conversation(db, row["conversation_id"], {
            "type": "message_edited",
            "message_id": message_id,
            "conversation_id": row["conversation_id"],
            "content": content,
            "editor_id": user['id'],
        }, exclude_user_id=None)   # include the author's other tabs
        members = await conversation_member_ids(db, row["conversation_id"])
        await _push_conversation_updates(user['id'], row["conversation_id"],
                                         [m for m in members if m != user['id']])
        return {"status": "ok", "message_id": message_id, "content": content}
    finally:
        await db.close()


@app.delete("/api/messages/{message_id}")
async def delete_message_rest(
    message_id: int,
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        row = await _get_message_row(db, message_id)
        if not row:
            # Idempotent: an already-removed message should not make the
            # client keep showing "delete failed" when a resend/reconnect
            # replays the request.
            return {"status": "already_deleted", "message_id": message_id}
        if row["sender_id"] != user['id'] or row["is_deleted"]:
            raise HTTPException(404, "Message not found")
        if not await user_in_conversation(db, user['id'], row["conversation_id"]):
            raise HTTPException(403, "You can no longer access this conversation")

        if row["file_path"]:
            try:
                await asyncio.to_thread(delete_file, row["file_path"])
            except Exception as e:
                logger.warning(f"Could not delete remote file {row['file_path']}: {e}")

        await db.execute("DELETE FROM messages WHERE id = ?", (message_id,))
        await db.execute("DELETE FROM read_receipts WHERE message_id = ?", (message_id,))
        await db.commit()
        schedule_db_sync()
        await _broadcast_to_conversation(db, row["conversation_id"], {
            "type": "message_deleted",
            "message_id": message_id,
            "conversation_id": row["conversation_id"],
            "deleted_by": user['id'],
        }, exclude_user_id=None)   # everyone gets the event; sender removes locally
        members = await conversation_member_ids(db, row["conversation_id"])
        await _push_conversation_updates(user['id'], row["conversation_id"],
                                         [m for m in members if m != user['id']])
        return {"status": "deleted", "message_id": message_id}
    finally:
        await db.close()


@app.get("/api/messages/{message_id}/read-receipts")
async def message_read_receipts(
    message_id: int,
    token: str = Header(..., alias="X-Auth-Token")
):
    """Who read this message and when (only visible to the message author)."""
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)
    db = await get_db()
    try:
        row = await _get_message_row(db, message_id)
        if not row or row["is_deleted"]:
            raise HTTPException(404, "Message not found")
        if row["sender_id"] != user['id']:
            raise HTTPException(403, "Only the author can view read receipts")
        if not await user_in_conversation(db, user['id'], row["conversation_id"]):
            raise HTTPException(403, "Not a member of this conversation")

        cursor = await db.execute(
            """SELECT r.read_at, u.id, u.username, u.display_name, u.avatar_path, u.status
               FROM read_receipts r JOIN users u ON u.id = r.user_id
               WHERE r.message_id = ? ORDER BY r.read_at ASC LIMIT 500""",
            (message_id,)
        )
        readers = []
        for r in await cursor.fetchall():
            readers.append({
                "user_id": r["id"],
                "username": r["username"],
                "display_name": r["display_name"],
                "avatar_path": r["avatar_path"],
                "read_at": r["read_at"],
                "online": r["status"] == "online" and manager.is_online(r["id"]),
            })

        cursor = await db.execute(
            "SELECT COUNT(*) AS cnt FROM read_receipts WHERE message_id = ?", (message_id,)
        )
        reader_total = (await cursor.fetchone())["cnt"]

        result = {
            "message_id": message_id,
            "conversation_id": row["conversation_id"],
            "readers": readers,
            "reader_count": reader_total,
            "readers_truncated": reader_total > len(readers),
            "is_dm": row["conversation_id"] != GLOBAL_CONVERSATION_ID,
            "not_read": [],
        }

        # DM: explicitly list the other person if they haven't read yet
        if result["is_dm"]:
            cursor = await db.execute(
                """SELECT u.id, u.username, u.display_name, u.avatar_path, u.status
                   FROM conversation_members cm JOIN users u ON u.id = cm.user_id
                   WHERE cm.conversation_id = ? AND u.id != ?""",
                (row["conversation_id"], user['id'])
            )
            for r in await cursor.fetchall():
                already = any(p["user_id"] == r["id"] for p in readers)
                if not already:
                    result["not_read"].append({
                        "user_id": r["id"],
                        "username": r["username"],
                        "display_name": r["display_name"],
                        "avatar_path": r["avatar_path"],
                        "online": r["status"] == "online" and manager.is_online(r["id"]),
                    })
        return result
    finally:
        await db.close()


# ------------------------------------------------------------------------
# Chunked Upload - assembles chunks on DISK not RAM
# ------------------------------------------------------------------------
upload_sessions: Dict[str, dict] = {}


@app.post("/api/upload/chunk")
async def upload_chunk(
    file: UploadFile = File(...),
    chunk_index: int = Query(...),
    total_chunks: int = Query(...),
    file_name: str = Query(...),
    file_type: str = Query(...),
    file_size: int = Query(...),
    upload_id: str = Query(...),
    token: str = Header(..., alias="X-Auth-Token")
):
    user = await authenticate_user(token)
    if not user:
        raise HTTPException(401)

    if file_size > MAX_FILE_SIZE:
        raise HTTPException(400, "File too large (max 500MB)")
    if len(file_name) > 300:
        raise HTTPException(400, "File name too long")
    if "\x00" in file_name or "/" in file_name or "\\" in file_name:
        raise HTTPException(400, "Invalid file name")

    # Sweep stale sessions (aborted uploads) so the dict can't grow forever
    if len(upload_sessions) > 64:
        stale_before = time.time() - 7200
        for sid, sess in list(upload_sessions.items()):
            if sess["created_at"] < stale_before:
                del upload_sessions[sid]

    # Create session with temp dir on disk
    if upload_id not in upload_sessions:
        session_dir = os.path.join(TEMP_DIR, f"upload_{upload_id}")
        os.makedirs(session_dir, exist_ok=True)
        upload_sessions[upload_id] = {
            "session_dir": session_dir,
            "filename": file_name,
            "file_type": file_type,
            "file_size": file_size,
            "total_chunks": total_chunks,
            "received_chunks": set(),
            "user_id": user['id'],
            "created_at": time.time()
        }

    session = upload_sessions[upload_id]
    if session['user_id'] != user['id']:
        raise HTTPException(403)
    if chunk_index < 0 or chunk_index >= total_chunks or total_chunks > 10000:
        raise HTTPException(400, "Invalid chunk parameters")
    if len(session['received_chunks']) >= total_chunks:
        raise HTTPException(400, "Upload already complete")

    # Write chunk directly to disk
    chunk_path = os.path.join(session['session_dir'], f"chunk_{chunk_index:06d}")
    with open(chunk_path, 'wb') as f:
        data = await file.read(65536)
        while data:
            f.write(data)
            data = await file.read(65536)

    session['received_chunks'].add(chunk_index)

    if len(session['received_chunks']) == total_chunks:
        remote_path = f"uploads/{user['username']}/{uuid.uuid4().hex}/{file_name}"
        session_dir = session['session_dir']

        try:
            assembled_path = os.path.join(session_dir, "assembled")
            with open(assembled_path, 'wb') as out_f:
                for i in range(total_chunks):
                    chunk_file = os.path.join(session_dir, f"chunk_{i:06d}")
                    with open(chunk_file, 'rb') as in_f:
                        buf = in_f.read(1024 * 1024)
                        while buf:
                            out_f.write(buf)
                            buf = in_f.read(1024 * 1024)
                    os.unlink(chunk_file)

            with open(assembled_path, 'rb') as f:
                full_data = f.read()
            # Blocking bucket write happens off the event loop
            try:
                await asyncio.to_thread(store_file, remote_path, full_data)
            except StorageUnavailableError:
                raise HTTPException(503, "File storage is unavailable")
            finally:
                del full_data

        finally:
            shutil.rmtree(session_dir, ignore_errors=True)
            del upload_sessions[upload_id]

        return {
            "status": "complete",
            "file_path": remote_path,
            "file_name": file_name,
            "file_type": file_type,
            "file_size": file_size
        }

    return {
        "status": "in_progress",
        "received": len(session['received_chunks']),
        "total": total_chunks
    }


# ------------------------------------------------------------------------
# File Download - streams from bucket to client in chunks
# ------------------------------------------------------------------------
@app.get("/api/download/{file_path:path}")
async def download_file(
    file_path: str,
    token: str = Header(None, alias="X-Auth-Token"),
    t: str = Query(None)
):
    auth_token = token or t
    user = await authenticate_user(auth_token)
    if not user:
        raise HTTPException(401)
    if '..' in file_path or '\\' in file_path:
        raise HTTPException(400, "Invalid path")

    # Avatars are public profile data. Social attachments are linked to a post.
    # Chat files belong to a conversation, so a user blocked out of it must not
    # be able to keep downloading those files.
    social_media = False
    if not file_path.startswith("avatars/"):
        db = await get_db()
        try:
            # Social-media attachment: any signed-in user may fetch a post's media.
            sp = await db.execute(
                "SELECT 1 FROM social_posts WHERE is_deleted = 0 AND media_json LIKE ? LIMIT 1",
                (f'%{file_path}%',)
            )
            if await sp.fetchone() is not None:
                social_media = True
            if not social_media:
                cursor = await db.execute(
                    "SELECT conversation_id FROM messages WHERE file_path = ? ORDER BY id DESC LIMIT 1",
                    (file_path,)
                )
                row = await cursor.fetchone()
                if not row:
                    raise HTTPException(404, "File not found")
                if not await user_in_conversation(db, user['id'], row["conversation_id"]):
                    raise HTTPException(403, "You can no longer access this file")
        finally:
            await db.close()

    try:
        # AES-GCM needs the whole ciphertext to verify its auth tag, so one full
        # read is required for security - the response is streamed in chunks.
        data = await asyncio.to_thread(retrieve_file, file_path)
    except FileNotFoundError:
        raise HTTPException(404, "File not found")
    except Exception as e:
        logger.error(f"Download error for {file_path}: {e}")
        raise HTTPException(500, "Failed to retrieve file")

    import mimetypes
    mt = mimetypes.guess_type(file_path)[0] or "application/octet-stream"
    file_size = len(data)

    # sanitize for the Content-Disposition header
    disp_name = os.path.basename(file_path).replace('"', "'").replace("\r", "").replace("\n", "")

    def iter_data():
        offset = 0
        while offset < len(data):
            yield data[offset:offset + DOWNLOAD_CHUNK_SIZE]
            offset += DOWNLOAD_CHUNK_SIZE

    return StreamingResponse(
        iter_data(),
        media_type=mt,
        headers={
            "Content-Length": str(file_size),
            "Cache-Control": "private, max-age=3600",
            "Content-Disposition": f'inline; filename="{disp_name}"'
        }
    )


# Lightweight per-user flood guards (in-memory only)
_send_history: Dict[int, List[float]] = {}
_typing_history: Dict[Tuple[int, int], float] = {}


def _allow_send(uid: int) -> bool:
    """Simple burst guard: max 20 sends per 5 seconds per user."""
    now = time.time()
    hist = _send_history.setdefault(uid, [])
    hist[:] = [t for t in hist if now - t < 5.0]
    if len(hist) >= 20:
        return False
    hist.append(now)
    return True


def _allow_typing(uid: int, cid: int) -> bool:
    now = time.time()
    key = (uid, cid)
    last = _typing_history.get(key, 0.0)
    if now - last < 0.8:
        return False
    _typing_history[key] = now
    return True


# ------------------------------------------------------------------------
# WebSocket Manager
# ------------------------------------------------------------------------
class ConnectionManager:
    def __init__(self):
        self.active: Dict[int, Dict[str, Any]] = {}

    async def connect(self, ws: WebSocket, user_id: int, username: str,
                      display_name: str = "", avatar_path: Optional[str] = None):
        await ws.accept()
        if user_id not in self.active:
            self.active[user_id] = {
                "username": username,
                "display_name": display_name or username,
                "avatar_path": avatar_path,
                "connections": set(),
            }
        else:
            self.active[user_id]["display_name"] = display_name or username
            self.active[user_id]["avatar_path"] = avatar_path or self.active[user_id]["avatar_path"]
        self.active[user_id]["connections"].add(ws)

    def disconnect(self, ws: WebSocket, user_id: int):
        if user_id in self.active:
            self.active[user_id]["connections"].discard(ws)
            if not self.active[user_id]["connections"]:
                del self.active[user_id]

    async def send_to_user(self, user_id: int, msg: dict):
        if user_id in self.active:
            for ws in list(self.active[user_id]["connections"]):
                try:
                    await ws.send_json(msg)
                except Exception:
                    self.disconnect(ws, user_id)

    async def send_to_users(self, user_ids: List[int], msg: dict, exclude_user_id=None):
        for uid in user_ids:
            if uid == exclude_user_id:
                continue
            await self.send_to_user(uid, msg)

    async def broadcast(self, msg: dict, exclude_user_id: Optional[int] = None):
        for uid, info in list(self.active.items()):
            if uid == exclude_user_id:
                continue
            for ws in list(info["connections"]):
                try:
                    await ws.send_json(msg)
                except Exception:
                    self.disconnect(ws, uid)

    def get_online_users(self) -> List[Dict]:
        return [
            {
                "id": uid,
                "username": info["username"],
                "display_name": info.get("display_name") or info["username"],
                "avatar_path": info.get("avatar_path"),
            }
            for uid, info in self.active.items()
        ]

    def update_profile(self, user_id: int, display_name: Optional[str] = None,
                       avatar_path: Optional[str] = None):
        """Keep the in-memory presence record in sync with profile changes."""
        if user_id not in self.active:
            return
        info = self.active[user_id]
        if display_name is not None and display_name != "":
            info["display_name"] = display_name
        if avatar_path is not None:
            info["avatar_path"] = avatar_path

    def is_online(self, user_id: int) -> bool:
        return user_id in self.active

    async def force_close_user(self, user_id: int, code: int = 4001, reason: str = ""):
        """Close every live socket of a user (e.g. after logging out)."""
        if user_id not in self.active:
            return
        for ws in list(self.active[user_id]["connections"]):
            try:
                await ws.close(code=code, reason=reason)
            except Exception:
                pass


manager = ConnectionManager()


async def _broadcast_to_conversation(db, cid: int, msg: dict, exclude_user_id=None):
    """Send a WS event to every online member of a conversation."""
    try:
        members = await conversation_member_ids(db, cid)
    except Exception as e:
        logger.error(f"Membership lookup failed for conv {cid}: {e}")
        return
    await manager.send_to_users(members, msg, exclude_user_id=exclude_user_id)


async def _push_conversation_updates(uid: int, cid: int, other_member_ids: List[int]):
    """Refreshed sidebar summaries after activity in a conversation."""
    db = await get_db()
    try:
        online = {u["id"] for u in manager.get_online_users()}
        summary = await _build_conversation_summary(db, cid, uid, online)
        await manager.send_to_user(uid, {"type": "conversation_updated", "conversation": summary})
        if other_member_ids:
            for mid in other_member_ids:
                other_summary = await _build_conversation_summary(db, cid, mid, online)
                await manager.send_to_user(mid, {"type": "conversation_updated", "conversation": other_summary})
    except Exception as e:
        logger.error(f"conversation update push failed: {e}")
    finally:
        await db.close()


# ------------------------------------------------------------------------
# WebSocket endpoint
# ------------------------------------------------------------------------
@app.websocket("/ws")
async def ws_endpoint(ws: WebSocket, token: str = Query(...)):
    user = await authenticate_user(token)
    if not user:
        await ws.close(code=4001, reason="Invalid token")
        return

    uid = user['id']
    username = user['username']
    display_name = user['display_name']
    avatar_path = user['avatar_path']

    await manager.connect(ws, uid, username, display_name, avatar_path)

    online_ids = {u["id"] for u in manager.get_online_users()}

    db = await get_db()
    try:
        await _ensure_global_member(db, uid)
        # persist "online" so REST presence fields (e.g. receipts modal) agree
        await db.execute(
            "UPDATE users SET status = 'online', last_seen = ? WHERE id = ?",
            (int(time.time()), uid)
        )
        await db.commit()
        conversations = await _conversations_for_user(db, uid, online_ids)
    finally:
        await db.close()

    await ws.send_json({
        "type": "connection_established",
        "user_id": uid,
        "username": username,
        "display_name": display_name,
        "avatar_path": avatar_path,
        "online_users": manager.get_online_users(),
        "conversations": conversations,
        "server_time": int(time.time() * 1000),
    })

    await manager.broadcast({
        "type": "user_status",
        "user_id": uid,
        "username": username,
        "display_name": display_name,
        "avatar_path": avatar_path,
        "status": "online"
    }, exclude_user_id=uid)

    try:
        while True:
            data = await ws.receive_json()
            mtype = data.get("type")

            # ---- keepalive ----
            if mtype == "ping":
                await ws.send_json({"type": "pong", "t": data.get("t")})
                continue

            # ---- create private chat ----
            if mtype == "create_dm":
                target_id = data.get("user_id")
                if not target_id:
                    continue
                db = await get_db()
                try:
                    try:
                        summary = await _create_dm(db, uid, int(target_id), manager)
                        if summary is None:
                            continue
                    except HTTPException as e:
                        await ws.send_json({
                            "type": "error", "code": "DM_FAILED",
                            "message": e.detail
                        })
                        continue
                    await ws.send_json({
                        "type": "dm_created",
                        "conversation": summary,
                    })
                    peer = summary.get("peer") or {}
                    online = {u["id"] for u in manager.get_online_users()}
                    # The recipient gets a pending invite (must accept), not a
                    # ready-to-use chat.
                    if peer.get("id"):
                        db2 = await get_db()
                        try:
                            peer_summary = await _build_conversation_summary(
                                db2, summary["id"], peer["id"], online)
                            await manager.send_to_user(peer["id"], {
                                "type": "conversation_updated",
                                "conversation": peer_summary,
                            })
                        finally:
                            await db2.close()
                finally:
                    await db.close()
                continue

            # ---- request conversations ----
            if mtype == "request_conversations":
                online = {u["id"] for u in manager.get_online_users()}
                db = await get_db()
                try:
                    conversations = await _conversations_for_user(db, uid, online)
                finally:
                    await db.close()
                await ws.send_json({"type": "conversations", "conversations": conversations})
                continue

            # ---- send message ----
            if mtype == "send_message":
                if not _allow_send(uid):
                    await ws.send_json({"type": "error", "code": "RATE_LIMIT",
                                        "message": "You're sending too fast - slow down",
                                        "client_id": data.get("client_id")})
                    continue
                content = (data.get("content") or "").strip()
                conversation_id = int(data.get("conversation_id") or GLOBAL_CONVERSATION_ID)
                reply_to_id = data.get("reply_to_id")
                client_id = data.get("client_id")
                file_path = data.get("file_path")
                file_type = data.get("file_type")
                file_name = data.get("file_name")
                file_size = data.get("file_size", 0)

                if not content and not file_path:
                    await ws.send_json({"type": "error", "code": "EMPTY", "message": "Message cannot be empty",
                                        "client_id": client_id})
                    continue
                if len(content) > MAX_MESSAGE_LENGTH:
                    await ws.send_json({"type": "error", "code": "TOO_LONG", "message": "Message too long",
                                        "client_id": client_id})
                    continue

                db = await get_db()
                try:
                    if not await user_in_conversation(db, uid, conversation_id):
                        await ws.send_json({"type": "error", "code": "FORBIDDEN",
                                            "message": "You are not in this conversation",
                                            "client_id": client_id})
                        continue

                    # Idempotent insert: replaying a send after reconnect won't duplicate
                    if client_id:
                        cursor = await db.execute(
                            "SELECT id FROM messages WHERE sender_id = ? AND client_id = ?",
                            (uid, client_id)
                        )
                        existing = await cursor.fetchone()
                        if existing:
                            await ws.send_json({
                                "type": "error", "code": "DUPLICATE",
                                "message": "duplicate_message", "message_id": existing["id"],
                                "client_id": client_id
                            })
                            continue

                    encrypted = encrypt_message(content)
                    ts = int(time.time() * 1000)
                    cursor = await db.execute(
                        """INSERT INTO messages
                           (sender_id, encrypted_content, timestamp_ms, reply_to_id,
                            file_path, file_type, file_name, file_size,
                            conversation_id, client_id)
                           VALUES (?,?,?,?,?,?,?,?,?,?)""",
                        (uid, encrypted, ts, reply_to_id, file_path, file_type,
                         file_name, file_size, conversation_id, client_id or None)
                    )
                    mid = cursor.lastrowid
                    await db.commit()
                    schedule_db_sync()

                    sender = {
                        "sender_username": username,
                        "sender_display_name": display_name,
                        "sender_avatar_path": avatar_path,
                    }
                    msg_obj = {
                        "id": mid,
                        "conversation_id": conversation_id,
                        "sender_id": uid,
                        **sender,
                        "content": content,
                        "timestamp_ms": ts,
                        "reply_to_id": reply_to_id,
                        "file_path": file_path,
                        "file_type": file_type,
                        "file_name": file_name,
                        "file_size": file_size,
                        "is_edited": False,
                        "is_deleted": False,
                        "status": "sent",
                        "readers": [],
                        "reader_count": 0,
                    }

                    members = await conversation_member_ids(db, conversation_id)
                    for member_id in members:
                        # send to every connection of the member (multi-tab safe).
                        # the originator gets client_id so it can ack its pending send.
                        await manager.send_to_user(member_id, {
                            "type": "new_message",
                            "message": msg_obj,
                            "client_id": client_id if member_id == uid else None,
                        })

                    for member_id in members:
                        if member_id != uid:
                            await _push_conversation_updates(member_id, conversation_id, [])
                finally:
                    await db.close()
                continue

            # ---- load messages (cursor pagination) ----
            if mtype == "load_messages":
                conversation_id = int(data.get("conversation_id") or GLOBAL_CONVERSATION_ID)
                cursor_id = data.get("cursor")
                limit = min(int(data.get("limit", 50) or 50), 100)
                db = await get_db()
                try:
                    if not await user_in_conversation(db, uid, conversation_id):
                        await ws.send_json({"type": "error", "code": "FORBIDDEN",
                                            "message": "Not in conversation"})
                        continue
                    if cursor_id:
                        rows = await db.execute(
                            """SELECT m.*, u.username, u.display_name, u.avatar_path
                               FROM messages m JOIN users u ON m.sender_id = u.id
                               WHERE m.conversation_id = ? AND m.is_deleted = 0 AND m.id < ?
                               ORDER BY m.id DESC LIMIT ?""",
                            (conversation_id, cursor_id, limit)
                        )
                    else:
                        rows = await db.execute(
                            """SELECT m.*, u.username, u.display_name, u.avatar_path
                               FROM messages m JOIN users u ON m.sender_id = u.id
                               WHERE m.conversation_id = ? AND m.is_deleted = 0
                               ORDER BY m.id DESC LIMIT ?""",
                            (conversation_id, limit)
                        )

                    msgs = []
                    for row in reversed(await rows.fetchall()):
                        decrypted = decrypt_message(row['encrypted_content'])
                        msgs.append(_row_to_message(row, decrypted))

                    await _attach_receipt_info(db, msgs, uid)
                    next_cursor = msgs[0]['id'] if msgs else None
                    await ws.send_json({
                        "type": "messages_loaded",
                        "conversation_id": conversation_id,
                        "messages": msgs,
                        "next_cursor": next_cursor,
                        "has_more": len(msgs) == limit
                    })
                finally:
                    await db.close()
                continue

            # ---- edit (via REST normally, kept for legacy clients) ----
            if mtype == "edit_message":
                mid = data.get("message_id")
                new_content = (data.get("content") or "").strip()
                if not mid or not new_content:
                    continue
                db = await get_db()
                try:
                    async with db.execute(
                        "SELECT * FROM messages WHERE id = ? AND sender_id = ? AND is_deleted = 0",
                        (mid, uid)
                    ) as cursor:
                        row = await cursor.fetchone()
                    if not row:
                        await ws.send_json({"type": "error", "code": "NOT_FOUND",
                                            "message": "Message not found"})
                        continue
                    new_enc = encrypt_message(new_content)
                    await db.execute(
                        "UPDATE messages SET encrypted_content = ?, is_edited = 1 WHERE id = ?",
                        (new_enc, mid)
                    )
                    await db.commit()
                    schedule_db_sync()
                    await _broadcast_to_conversation(db, row['conversation_id'], {
                        "type": "message_edited",
                        "message_id": mid,
                        "conversation_id": row['conversation_id'],
                        "content": new_content,
                        "editor_id": uid,
                    }, exclude_user_id=uid)
                    members = await conversation_member_ids(db, row['conversation_id'])
                    await _push_conversation_updates(uid, row['conversation_id'],
                                                     [m for m in members if m != uid])
                finally:
                    await db.close()
                continue

            # ---- delete (via REST normally, kept for legacy clients) ----
            if mtype == "delete_message":
                mid = data.get("message_id")
                if not mid:
                    continue
                db = await get_db()
                try:
                    async with db.execute(
                        "SELECT * FROM messages WHERE id = ? AND sender_id = ?",
                        (mid, uid)
                    ) as cursor:
                        row = await cursor.fetchone()
                    if not row:
                        await ws.send_json({"type": "error", "code": "NOT_FOUND",
                                            "message": "Message not found"})
                        continue
                    if row['file_path']:
                        try:
                            await asyncio.to_thread(delete_file, row['file_path'])
                        except Exception:
                            pass
                    await db.execute("DELETE FROM messages WHERE id = ?", (mid,))
                    await db.execute("DELETE FROM read_receipts WHERE message_id = ?", (mid,))
                    await db.commit()
                    schedule_db_sync()
                    await _broadcast_to_conversation(db, row['conversation_id'], {
                        "type": "message_deleted",
                        "message_id": mid,
                        "conversation_id": row['conversation_id'],
                        "deleted_by": uid,
                    })
                    members = await conversation_member_ids(db, row['conversation_id'])
                    await _push_conversation_updates(uid, row['conversation_id'],
                                                     [m for m in members if m != uid])
                finally:
                    await db.close()
                continue

            # ---- typing (scoped to one conversation) ----
            if mtype == "typing":
                conversation_id = int(data.get("conversation_id") or GLOBAL_CONVERSATION_ID)
                if bool(data.get("is_typing", False)) and not _allow_typing(uid, conversation_id):
                    continue
                db = await get_db()
                try:
                    await _broadcast_to_conversation(db, conversation_id, {
                        "type": "typing_indicator",
                        "user_id": uid,
                        "username": username,
                        "display_name": display_name,
                        "is_typing": bool(data.get("is_typing", False)),
                        "conversation_id": conversation_id,
                    }, exclude_user_id=uid)
                finally:
                    await db.close()
                continue

            # ---- mark read: instant receipts the moment the tab is open ----
            if mtype == "mark_read":
                up_to = data.get("up_to_message_id")
                conversation_id = int(data.get("conversation_id") or GLOBAL_CONVERSATION_ID)
                if not up_to:
                    continue
                db = await get_db()
                try:
                    if not await user_in_conversation(db, uid, conversation_id):
                        continue

                    # Keep the per-user watermark monotonic
                    cursor = await db.execute(
                        """SELECT last_read_message_id FROM conversation_members
                           WHERE conversation_id = ? AND user_id = ?""",
                        (conversation_id, uid)
                    )
                    row = await cursor.fetchone()
                    watermark = row["last_read_message_id"] if row else 0
                    if int(up_to) <= watermark:
                        continue

                    now_ms = int(time.time() * 1000)
                    cursor = await db.execute(
                        """SELECT m.id, m.sender_id FROM messages m
                           WHERE m.conversation_id = ? AND m.id > ? AND m.id <= ?
                             AND m.sender_id != ? AND m.is_deleted = 0
                           ORDER BY m.id ASC""",
                        (conversation_id, watermark, up_to, uid)
                    )
                    rows = await cursor.fetchall()
                    # Watermark still advances to up_to, but only the newest batch
                    # needs per-message receipts (bounds write + notification cost).
                    for r in rows[-5000:]:
                        await db.execute(
                            "INSERT OR IGNORE INTO read_receipts (user_id, message_id, read_at) "
                            "VALUES (?,?,?)",
                            (uid, r['id'], now_ms)
                        )
                    new_reads = rows[-5000:]

                    await db.execute(
                        """INSERT INTO conversation_members (conversation_id, user_id, last_read_message_id)
                           VALUES (?,?,?)
                           ON CONFLICT(conversation_id, user_id)
                           DO UPDATE SET last_read_message_id = excluded.last_read_message_id""",
                        (conversation_id, uid, int(up_to))
                    )
                    await db.commit()
                    if new_reads:
                        schedule_db_sync()

                    # Notify each author the instant one of their messages is read
                    for r in new_reads:
                        await manager.send_to_user(r['sender_id'], {
                            "type": "message_read",
                            "message_id": r['id'],
                            "conversation_id": conversation_id,
                            "reader": {
                                "user_id": uid,
                                "username": username,
                                "display_name": display_name,
                                "avatar_path": avatar_path,
                            },
                            "read_at": now_ms,
                        })
                    await _push_conversation_updates(uid, conversation_id, [])
                finally:
                    await db.close()
                continue

            # ---- online users ----
            if mtype == "get_online_users":
                await ws.send_json({
                    "type": "online_users",
                    "users": manager.get_online_users()
                })
                continue

    except WebSocketDisconnect:
        pass
    except Exception as e:
        logger.error(f"WS error for {username}: {e}", exc_info=True)
    finally:
        # Remove this socket first. If the user still has another connection
        # (multi-tab), they stay online; otherwise they are now fully offline.
        manager.disconnect(ws, uid)
        if uid not in manager.active:
            db = await get_db()
            try:
                await db.execute(
                    "UPDATE users SET status = 'offline', last_seen = ? WHERE id = ?",
                    (int(time.time()), uid)
                )
                await db.commit()
                schedule_db_sync()
            finally:
                await db.close()
            await manager.broadcast({
                "type": "user_status",
                "user_id": uid,
                "username": username,
                "display_name": display_name,
                "avatar_path": avatar_path,
                "status": "offline"
            })


@app.get("/")
async def root():
    return FileResponse("static/index.html", headers={"Cache-Control": "no-cache"})


@app.get("/admin", include_in_schema=False)
async def admin_page():
    return FileResponse("static/admin.html", headers={"Cache-Control": "no-cache"})