File size: 163,303 Bytes
78b41d5
16eedb2
 
 
78b41d5
 
 
40d3e55
78b41d5
 
2178c57
1b52ac7
78b41d5
 
e386cb0
d36d309
 
 
 
78b41d5
 
e386cb0
8be6be7
5bf08af
ce5b13f
 
 
d8fc2e3
0d792fe
ce5b13f
 
0801a37
78b41d5
d36d309
 
 
9d0f66c
d36d309
6949f45
 
97d1f30
d36d309
0a25f0d
 
 
ca484f2
 
 
f25ebdc
 
 
a960618
 
 
3889732
 
 
78b41d5
 
 
 
ef5ce88
 
 
76b1cf2
 
 
 
 
 
 
 
 
78b41d5
 
 
76b1cf2
 
 
 
 
 
78b41d5
 
 
 
 
 
 
 
e386cb0
 
 
 
 
09e3435
 
 
 
 
e386cb0
 
09e3435
 
 
 
 
 
 
21ed919
 
 
09e3435
21ed919
09e3435
e386cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09e3435
 
e386cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09e3435
e386cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09e3435
 
 
 
 
 
e386cb0
09e3435
 
 
 
e386cb0
09e3435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e386cb0
 
09e3435
 
 
 
 
 
d0f5f64
 
09e3435
 
 
 
 
21ed919
09e3435
 
 
 
e9eb4f9
21ed919
09e3435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e386cb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b41d5
16eedb2
0cb9f26
f49a89f
78b41d5
 
 
 
 
 
 
 
 
 
 
40d3e55
78b41d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
370af6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b52ac7
 
 
 
 
ce5b13f
 
b0bd2b7
 
 
 
 
 
 
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
5b38521
 
 
 
 
 
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
1b52ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b41d5
 
 
 
 
16eedb2
0cb9f26
16eedb2
 
78b41d5
 
 
 
 
 
 
 
 
16eedb2
0cb9f26
78b41d5
 
 
 
 
 
76b1cf2
78b41d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
78b41d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
78b41d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
78b41d5
 
 
 
 
 
40d3e55
 
78b41d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370af6d
 
76b1cf2
370af6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
370af6d
 
adccf05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d792fe
 
 
 
d8fc2e3
 
 
 
 
 
 
 
 
 
 
 
 
0d792fe
 
 
 
 
 
 
 
 
 
 
 
370af6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b13f
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5bedf7
 
ce5b13f
 
 
 
 
 
 
f5bedf7
 
 
 
ce5b13f
 
 
 
f5bedf7
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5bedf7
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d792fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
0d792fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b13f
b6b7e9f
 
 
 
ce5b13f
 
 
76b1cf2
ce5b13f
 
 
b6b7e9f
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d792fe
 
 
ce5b13f
 
0d792fe
 
 
 
ce5b13f
 
0d792fe
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5bedf7
ce5b13f
 
 
 
 
 
 
 
f5bedf7
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
ce5b13f
 
 
 
 
 
d36d309
 
 
 
 
 
ce5b13f
d36d309
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5bedf7
ce5b13f
 
f5bedf7
 
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5bedf7
ce5b13f
 
f5bedf7
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d3e55
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d36d309
 
 
 
 
 
 
 
 
ce5b13f
 
 
d36d309
 
 
74e945d
 
 
d36d309
74e945d
d36d309
 
 
 
9d0f66c
74e945d
d36d309
 
9d0f66c
 
74e945d
9d0f66c
 
d36d309
 
74e945d
d36d309
 
6949f45
 
 
 
 
 
 
 
 
 
ce5b13f
d36d309
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
d36d309
 
 
 
 
ce5b13f
 
 
 
 
 
 
d36d309
 
 
 
 
9d0f66c
6949f45
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
1b52ac7
ce5b13f
 
 
1b52ac7
 
 
 
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b52ac7
 
 
 
 
 
 
 
 
 
 
ce5b13f
 
 
 
1b52ac7
ce5b13f
 
 
 
 
 
 
 
 
 
5b38521
 
76b1cf2
5b38521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0bd2b7
 
76b1cf2
b0bd2b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce5b13f
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ce5b13f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62988c6
 
 
76b1cf2
62988c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
62988c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56953ae
76b1cf2
56953ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b3025
 
 
 
 
 
 
 
 
76b1cf2
61b3025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8be6be7
 
 
 
 
 
 
 
 
76b1cf2
8be6be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
8be6be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf08af
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
5bf08af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c005d
5bf08af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
5bf08af
 
 
 
 
 
 
 
 
 
 
27c005d
5bf08af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
5bf08af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1208b15
 
 
 
 
 
 
 
76b1cf2
1208b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
1208b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d81a1d
 
 
 
 
 
1208b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
1208b15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97d1f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
97d1f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0801a37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0801a37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c005d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a25f0d
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27c005d
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
0a25f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca484f2
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
ca484f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f25ebdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
f25ebdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a960618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
a960618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
a960618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3889732
 
2178c57
3889732
 
 
 
 
 
2178c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3889732
 
 
76b1cf2
3889732
 
 
 
2178c57
3889732
2178c57
3889732
 
2178c57
 
3889732
 
 
2178c57
3889732
 
 
 
 
76b1cf2
3889732
 
 
 
2178c57
3889732
 
2178c57
3889732
2178c57
3889732
 
 
cf8055d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
cf8055d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
cf8055d
 
 
 
 
 
 
 
 
 
 
 
 
 
76b1cf2
cf8055d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b41d5
 
d36d309
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
"""
🔮 AstrologyBazi API - Complete Eastern Mysticism Platform
BaZi (八字) | Nine Flying Stars (九宫飞星) | 28 Lunar Mansions (二十八星宿) | Chinese Calendar (农历)
Powering https://astrologybazi.com
🔒 Protected with API Key Authentication
"""

from birth_time_context import BirthTimeRequest
from fastapi import FastAPI, HTTPException, Header, Depends
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, field_validator
from typing import Optional, Dict, List, Any
import logging
import os
from datetime import date, datetime, timedelta
from dotenv import load_dotenv

# 🔧 Load environment variables from .env file
load_dotenv()

from bazi_calculator import BaZiCalculator
from lunar_python import Solar, Lunar
from nine_stars import NineStarsCalculator
from constellation_28 import Constellation28Calculator
from shensha_analyzer import ShenshaAnalyzer
from genju_analyzer import GenjuAnalyzer
from dayun_analyzer import DayunAnalyzer
from evidence import build_evidence, build_timing, build_meta, select_dayun_score_preview
from dayun_dynamic_assessment import build_dayun_dynamic_assessment
from ai_interpreter import AIInterpreter
from time_fortune_analyzer import TimeFortuneAnalyzer
from fortune_score_calculator import FortuneScoreCalculator

# 🎯 Phase 3 新增模块 (2025-12-24)
from marriage_analyzer import MarriageAnalyzer
from career_analyzer import CareerAnalyzer
from wealth_analyzer import WealthAnalyzer
from health_analyzer import HealthAnalyzer
from official_analyzer import OfficialAnalyzer      # 官职分析
from legal_risk_analyzer import LegalRiskAnalyzer  # 法律风险分析
from marriage_compatibility_analyzer import MarriageCompatibilityAnalyzer  # 八字合婚

# 🌟 Rituals Service (2026-01-25)
from rituals_service import rituals_service

# 🖐️ Body Reading Analyzer (2026-02-16)
from body_reading_analyzer import analyze_body_reading

# 💕 Love Clarity Analyzer (2026-02-28)
from love_clarity_analyzer import LoveClarityAnalyzer

# 🌀 Qimen Dunjia Calculator (2026-04-14)
from qimen_calculator import QimenCalculator

# 🔮 Liu Yao Calculator (2026-04-17)
from liuyao_calculator import calculate_liuyao

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# 🔐 API Key Configuration
# Support both naming conventions:
# - API_KEY: For HuggingFace Space settings (simple, since only one API key)
# - SISI_HF_API_KEY: For Cloudflare (clear distinction among many API keys)
# Fail closed: no hardcoded fallback — if neither env var is set, all
# protected endpoints refuse service instead of accepting a default key.
API_KEY = os.environ.get("API_KEY") or os.environ.get("SISI_HF_API_KEY")

if not API_KEY:
    logger.error(
        "🔒 API key is NOT configured (set API_KEY or SISI_HF_API_KEY). "
        "All protected endpoints will return 503 until a key is set."
    )


def verify_api_key(x_api_key: Optional[str] = Header(None)):
    """Verify API key from request header (fail closed when unconfigured)"""
    if not API_KEY:
        raise HTTPException(
            status_code=503,
            detail="🔒 API key is not configured on the server. Service unavailable."
        )
    if x_api_key != API_KEY:
        raise HTTPException(
            status_code=403,
            detail="🔒 Invalid or missing API key. Please contact SisiTheFox for access."
        )
    return x_api_key


# ==================== Chinese Calendar Service ====================

class ChineseCalendarService:
    """Service for Chinese lunar calendar operations"""

    # 地支到生肖的映射
    ZHI_TO_ZODIAC = {
        '子': '鼠', '丑': '牛', '寅': '虎', '卯': '兔',
        '辰': '龙', '巳': '蛇', '午': '马', '未': '羊',
        '申': '猴', '酉': '鸡', '戌': '狗', '亥': '猪'
    }

    # 生肖emoji映射
    ZODIAC_EMOJI = {
        '鼠': '🐀', '牛': '🐂', '虎': '🐅', '兔': '🐇',
        '龙': '🐉', '蛇': '🐍', '马': '🐎', '羊': '🐑',
        '猴': '🐒', '鸡': '🐓', '狗': '🐕', '猪': '🐖'
    }

    # 天神吉凶分类(传统六黄道六黑道)
    # 六黄道(吉):青龙、明堂、金匮、天德、玉堂、司命
    # 六黑道(凶):白虎、天刑、朱雀、天牢、玄武、勾陈
    JI_SHEN_LIST = ["青龙", "明堂", "金匮", "天德", "玉堂", "司命"]
    XIONG_SHEN_LIST = ["天刑", "白虎", "朱雀", "天牢", "玄武", "勾陈"]

    @staticmethod
    def get_today_calendar() -> Dict:
        """Get today's Chinese lunar calendar information"""
        today = datetime.now().date()
        return ChineseCalendarService.get_calendar_by_date(today)

    @staticmethod
    def get_calendar_by_date(target_date: date) -> Dict:
        """Get Chinese lunar calendar information for a specific date"""
        try:
            solar = Solar.fromYmd(target_date.year, target_date.month, target_date.day)
            lunar = solar.getLunar()

            lunar_year = lunar.getYear()
            lunar_month = lunar.getMonth()
            lunar_day = lunar.getDay()

            lunar_year_cn = lunar.getYearInChinese()
            lunar_month_cn = lunar.getMonthInChinese()
            lunar_day_cn = lunar.getDayInChinese()

            year_ganzhi = lunar.getYearInGanZhi()
            month_ganzhi = lunar.getMonthInGanZhi()
            day_ganzhi = lunar.getDayInGanZhi()

            zodiac = lunar.getYearShengXiao()

            weekday_num = target_date.weekday()
            weekdays_cn = ['一', '二', '三', '四', '五', '六', '日']
            weekday_cn = f"星期{weekdays_cn[weekday_num]}"

            almanac = ChineseCalendarService._get_almanac_for_date(lunar)
            auspicious_hours = ChineseCalendarService._get_auspicious_hours(lunar)
            solar_term = ChineseCalendarService._get_solar_term(lunar)
            festival = ChineseCalendarService._get_festival(target_date, lunar)

            return {
                "success": True,
                "date": str(target_date),
                "weekday": weekday_cn,
                "solar": {
                    "year": target_date.year,
                    "month": target_date.month,
                    "day": target_date.day,
                    "date_string": f"{target_date.year}年{target_date.month}月{target_date.day}日"
                },
                "lunar": {
                    "year": lunar_year,
                    "month": lunar_month,
                    "day": lunar_day,
                    "year_cn": lunar_year_cn,
                    "month_cn": lunar_month_cn,
                    "day_cn": lunar_day_cn,
                    "date_string": f"{lunar_year_cn}{lunar_month_cn}{lunar_day_cn}",
                    "is_leap": lunar.getMonth() < 0
                },
                "ganzhi": {
                    "year": year_ganzhi,
                    "month": month_ganzhi,
                    "day": day_ganzhi,
                    "full": f"{year_ganzhi}年 {month_ganzhi}月 {day_ganzhi}日"
                },
                "zodiac": {
                    "animal": zodiac,
                    "animal_cn": zodiac
                },
                "almanac": almanac,
                "auspicious_hours": auspicious_hours,
                "solar_term": solar_term,
                "festival": festival,
                "timestamp": datetime.now().isoformat()
            }

        except Exception as e:
            logger.error(f"Error getting calendar for {target_date}: {str(e)}")
            return {
                "success": False,
                "error": str(e),
                "date": str(target_date)
            }

    @staticmethod
    def get_calendar_range(start_date: date, end_date: date) -> List[Dict]:
        """Get calendar information for a date range"""
        if (end_date - start_date).days > 365:
            raise ValueError("Date range cannot exceed 365 days")

        calendars = []
        current_date = start_date

        while current_date <= end_date:
            calendar = ChineseCalendarService.get_calendar_by_date(current_date)
            calendars.append(calendar)
            current_date += timedelta(days=1)

        return calendars

    @staticmethod
    def _get_almanac_for_date(lunar: Lunar) -> Dict:
        """Get authentic almanac data (宜忌) from lunar-python"""
        try:
            # 获取真实的宜忌
            yi = lunar.getDayYi()
            ji = lunar.getDayJi()

            # 获取冲煞信息
            chong_gan = lunar.getChongGan()
            chong_zhi = lunar.getChong()
            sha = lunar.getSha()

            # 转换冲的地支为生肖
            chong_zodiac = ChineseCalendarService.ZHI_TO_ZODIAC.get(chong_zhi, chong_zhi)
            chong_emoji = ChineseCalendarService.ZODIAC_EMOJI.get(chong_zodiac, '')

            # 组合完整的冲煞格式:"冲🐑羊(丁未)煞东"
            chong_ganzhi = chong_gan + chong_zhi
            chong_sha_full = f"冲{chong_emoji}{chong_zodiac}({chong_ganzhi})煞{sha}"

            # 获取吉神宜趋和凶煞宜忌
            ji_shen = lunar.getDayJiShen()
            xiong_sha = lunar.getDayXiongSha()

            # 获取彭祖百忌
            pengzu_gan = lunar.getPengZuGan()
            pengzu_zhi = lunar.getPengZuZhi()

            return {
                "appropriate": yi,
                "avoid": ji,
                "chong": chong_zhi,  # 保留原始地支供兼容
                "sha": sha,  # 保留原始煞向供兼容
                "chong_sha": chong_sha_full,  # 新增:完整格式"冲🐑羊(丁未)煞东"
                "ji_shen": ji_shen,  # 吉神宜趋
                "xiong_sha": xiong_sha,  # 凶煞宜忌
                "pengzu": {
                    "gan": pengzu_gan,
                    "zhi": pengzu_zhi
                }
            }

        except Exception as e:
            logger.error(f"Error getting almanac data: {e}")
            # 返回空数据而不是报错
            return {
                "appropriate": [],
                "avoid": [],
                "chong": "",
                "sha": "",
                "chong_sha": "",
                "ji_shen": [],
                "xiong_sha": [],
                "pengzu": {"gan": "", "zhi": ""}
            }

    @staticmethod
    def _get_auspicious_hours(lunar: Lunar) -> List[Dict]:
        """Get 12-hour fortune analysis (十二时辰吉凶)"""
        try:
            times = lunar.getTimes()
            hours = []

            # 只取前12个时辰(lunar-python可能返回13个,包含下一天的子时)
            for i, t in enumerate(times[:12]):
                time_ganzhi = t.getGanZhi()
                tian_shen = t.getTianShen()
                chong = t.getChong()
                sha = t.getSha()

                # 判断吉凶(六黄道为吉,六黑道为凶)
                if tian_shen in ChineseCalendarService.JI_SHEN_LIST:
                    fortune = "吉"
                elif tian_shen in ChineseCalendarService.XIONG_SHEN_LIST:
                    fortune = "凶"
                else:
                    fortune = "凶"  # 未知天神默认为凶

                # 时间段
                start_hour = i * 2
                end_hour = i * 2 + 1
                period = f"{start_hour:02d}:00-{end_hour:02d}:59"

                hours.append({
                    "time": f"{time_ganzhi}时",
                    "period": period,
                    "ganzhi": time_ganzhi,
                    "tian_shen": tian_shen,
                    "fortune": fortune,
                    "chong": chong,
                    "sha": sha
                })

            return hours

        except Exception as e:
            logger.error(f"Error getting auspicious hours: {e}")
            return []

    @staticmethod
    def _get_solar_term(lunar: Lunar) -> Optional[str]:
        """Get solar term (节气)"""
        try:
            jieqi = lunar.getJieQi()
            return jieqi if jieqi else None
        except:
            return None

    @staticmethod
    def _get_festival(target_date: date, lunar: Lunar) -> Optional[str]:
        """Get festival name"""
        try:
            solar_festivals = lunar.getFestivals()
            if solar_festivals:
                return ", ".join(solar_festivals)

            lunar_festivals = lunar.getOtherFestivals()
            if lunar_festivals:
                return ", ".join(lunar_festivals)

            return None
        except:
            return None


app = FastAPI(
    title="AstrologyBazi API",
    version="2.1.1",
    description="🔮 Complete Eastern Mysticism API - BaZi Fortune Analysis (Phases 1-5), Nine Flying Stars, 28 Lunar Mansions, Chinese Calendar"
)

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


class BaZiRequest(BirthTimeRequest):
    """Request model for BaZi calculation"""
    birth_date: str = Field(
        ...,
        description="Birth date in YYYY-MM-DD format (e.g., '1990-01-15')",
        example="1990-01-15"
    )
    birth_time: str = Field(
        ...,
        description="Birth time in HH:MM format (e.g., '08:30')",
        example="08:30"
    )
    gender: str = Field(
        default="male",
        description="Gender: 'male' or 'female'",
        example="male"
    )
    is_lunar: bool = Field(
        default=False,
        description="True if birth_date is lunar calendar, False for solar/Gregorian",
        example=False
    )


class BaZiCompleteRequest(BirthTimeRequest):
    """Request model for complete BaZi calculation with 大运/流年"""
    birth_date: str = Field(
        ...,
        description="Birth date in YYYY-MM-DD format",
        example="1990-01-15"
    )
    birth_time: str = Field(
        ...,
        description="Birth time in HH:MM format",
        example="08:30"
    )
    gender: str = Field(
        default="male",
        description="Gender: 'male' or 'female'",
        example="male"
    )
    is_lunar: bool = Field(
        default=False,
        description="True if lunar calendar",
        example=False
    )
    num_dayun: int = Field(
        default=12,
        description="Number of 大运 periods (each 10 years, default 12 = 120 years)",
        example=12
    )


class BaZiInterpretationRequest(BirthTimeRequest):
    """Request model for AI interpretation of BaZi"""
    birth_date: str = Field(
        ...,
        description="Birth date in YYYY-MM-DD format",
        example="1990-01-15"
    )
    birth_time: str = Field(
        ...,
        description="Birth time in HH:MM format",
        example="08:30"
    )
    gender: str = Field(
        default="male",
        description="Gender: 'male' or 'female'",
        example="male"
    )
    is_lunar: bool = Field(
        default=False,
        description="True if lunar calendar",
        example=False
    )
    interpretation_level: str = Field(
        default="basic",
        description="Interpretation depth: 'basic' (3000 words, $29), 'professional' (8000 words, $99), 'master' (15000 words, $199)",
        example="basic"
    )
    language: str = Field(
        default="zh-CN",
        description="Language: 'zh-CN', 'en', 'zh-TW'",
        example="zh-CN"
    )
    num_dayun: int = Field(
        default=12,
        description="Number of 大运 periods for complete analysis",
        example=12
    )


class MonthlyFortuneRequest(BaseModel):
    """Request model for monthly fortune analysis"""
    birth_date: str = Field(..., description="Birth date in YYYY-MM-DD format", example="1990-01-15")
    birth_time: str = Field(..., description="Birth time in HH:MM format", example="08:30")
    gender: str = Field(default="male", description="Gender: 'male' or 'female'", example="male")
    is_lunar: bool = Field(default=False, description="True if lunar calendar", example=False)
    target_year: int = Field(..., description="Target year", example=2026)
    target_month: int = Field(..., description="Target month (1-12)", example=1, ge=1, le=12)
    language: str = Field(
        default="zh",
        description="Output language: 'zh' for Chinese, 'en' for cold-reading English",
        example="zh"
    )


class YearlyFortuneRequest(BaseModel):
    """Request model for yearly fortune analysis (all 12 months)"""
    birth_date: str = Field(..., description="Birth date in YYYY-MM-DD format", example="1990-01-15")
    birth_time: str = Field(..., description="Birth time in HH:MM format", example="08:30")
    gender: str = Field(default="male", description="Gender: 'male' or 'female'", example="male")
    is_lunar: bool = Field(default=False, description="True if lunar calendar", example=False)
    target_year: int = Field(default=None, description="Target year (defaults to current year)", example=2026)
    language: str = Field(default="zh", description="Output language: 'zh' or 'en'", example="zh")


class DailyFortuneRequest(BaseModel):
    """Request model for daily fortune analysis"""
    birth_date: str = Field(..., description="Birth date in YYYY-MM-DD format", example="1990-01-15")
    birth_time: str = Field(..., description="Birth time in HH:MM format", example="08:30")
    gender: str = Field(default="male", description="Gender: 'male' or 'female'", example="male")
    is_lunar: bool = Field(default=False, description="True if lunar calendar", example=False)
    target_date: str = Field(..., description="Target date in YYYY-MM-DD format", example="2026-01-01")
    activities: List[str] = Field(default=[], description="Activities to analyze (e.g., ['嫁娶', '开业'])", example=["嫁娶"])


class CareerMonthlyFortuneRequest(BaseModel):
    """Request model for career-specific monthly fortune analysis"""
    birth_date: str = Field(..., description="Birth date in YYYY-MM-DD format", example="1990-01-15")
    birth_time: str = Field(..., description="Birth time in HH:MM format", example="08:30")
    gender: str = Field(default="male", description="Gender: 'male' or 'female'", example="male")
    is_lunar: bool = Field(default=False, description="True if lunar calendar", example=False)
    target_year: int = Field(..., description="Target year to analyze (e.g., 2026)", example=2026)


class FindAuspiciousDatesRequest(BaseModel):
    """Request model for finding auspicious dates"""
    birth_date: str = Field(..., description="Birth date in YYYY-MM-DD format", example="1990-01-15")
    birth_time: str = Field(..., description="Birth time in HH:MM format", example="08:30")
    gender: str = Field(default="male", description="Gender: 'male' or 'female'", example="male")
    is_lunar: bool = Field(default=False, description="True if lunar calendar", example=False)
    start_date: str = Field(..., description="Start date in YYYY-MM-DD format", example="2026-01-01")
    end_date: str = Field(..., description="End date in YYYY-MM-DD format", example="2026-01-31")
    activity: str = Field(..., description="Activity type", example="嫁娶")
    min_score: int = Field(default=70, description="Minimum score requirement (0-100)", example=70, ge=0, le=100)


def _get_ordinal(n: int) -> str:
    """Convert number to ordinal string (1 -> 1st, 2 -> 2nd, etc.)"""
    if 10 <= n % 100 <= 20:
        suffix = 'th'
    else:
        suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')
    return f"{n}{suffix}"


def apply_cold_reading_english(monthly_fortune: Dict[str, Any], year: int, month: int) -> Dict[str, Any]:
    """
    Transform Chinese fortune data into Barnum Effect English
    NOT simple translation - apply psychological triggers

    Core Principles:
    - Specific numbers: "3 days", "the 15th", "2 weeks"
    - Sensory details: "that sinking feeling", "you've noticed"
    - Emotional triggers: "frustrated", "hopeful", "relieved"
    - Universal specifics: "the conversation you've been avoiding"
    - Time anchors: "Next Tuesday", "before the month ends"

    Args:
        monthly_fortune: Chinese fortune analysis data
        year: Target year
        month: Target month

    Returns:
        Cold-reading English version with Barnum Effect triggers
    """
    import random

    score = monthly_fortune['score']
    quality_cn = monthly_fortune['quality_cn']

    # Map Chinese quality to cold-reading English with Barnum triggers
    quality_map = {
        "大吉": "Peak Performance Window",
        "顺遂": "Favorable Momentum",
        "平稳": "Strategic Recalibration",
        "波折": "Protective Defense Phase",
        "困难": "Strategic Pause Period"
    }

    quality_en = quality_map.get(quality_cn, "Transition Phase")

    # Generate specific peak days based on score patterns
    if score >= 80:
        peak_days = [3, 8, 15, 22]
        caution_days = [18, 27]
    elif score >= 65:
        peak_days = [5, 12, 19, 24]
        caution_days = [9, 21]
    elif score >= 50:
        peak_days = [7, 14, 21]
        caution_days = [4, 11, 25]
    else:
        peak_days = [6, 16]
        caution_days = [3, 10, 20, 28]

    # Cold-reading matters based on score range
    matters_templates = []

    if score >= 80:  # 大吉
        matters_templates = [
            f"That work project you've been overthinking? Your instincts were right. Start it before the {peak_days[0]}th.",
            f"You'll see an unexpected opportunity around the {peak_days[1]}th. The one that feels 'too good to be true'? Trust it this time.",
            f"Someone's been watching your work quietly. Around the {peak_days[2]}th, they speak up. It's the validation you needed.",
            f"That financial decision you've been postponing? The window opens between the {peak_days[0]}th and {peak_days[2]}th. After that, conditions shift."
        ]
    elif score >= 65:  # 顺遂
        matters_templates = [
            f"The progress you've been making feels slow, but others notice. Around the {peak_days[0]}th, you'll see proof.",
            f"That side project or idea you keep dismissing? Between the {peak_days[1]}th and {peak_days[2]}th, test it quietly.",
            f"A relationship (work or personal) that's felt stuck will shift around the {peak_days[2]}th. Stay open.",
            f"Financial stability improves, but not dramatically. The {peak_days[0]}th brings a small win that matters more than you think."
        ]
    elif score >= 50:  # 平稳
        matters_templates = [
            f"This month rewards patience over action. The urge to force things around the {caution_days[0]}th? Resist it.",
            f"Something you started weeks ago finally stabilizes around the {peak_days[1]}th. Not exciting, but necessary.",
            f"That person who's been distant lately? Around the {peak_days[0]}th, you'll understand why. Don't take it personally.",
            f"Financially, hold steady. The temptation to spend around the {caution_days[1]}th looks harmless but isn't."
        ]
    elif score >= 35:  # 波折
        matters_templates = [
            f"That situation that keeps you up at 3am? Around the {caution_days[0]}th, it gets worse before it gets better.",
            f"Someone will test your boundaries between the {caution_days[1]}th and {caution_days[2]}th. This is when you draw the line.",
            f"A plan you've been counting on might delay around the {caution_days[0]}th. Have a backup ready before the {peak_days[0]}th.",
            f"Financially, protect what you have. The 'opportunity' around the {caution_days[1]}th is actually a trap."
        ]
    else:  # 困难
        matters_templates = [
            f"This month is about defense, not offense. Between the {caution_days[0]}th and {caution_days[2]}th, just hold ground.",
            f"That relationship that's been rocky? Around the {caution_days[1]}th, you'll need to decide: fix it or walk away.",
            f"The financial pressure you're feeling peaks around the {caution_days[0]}th. Don't make desperate moves - it passes by the {peak_days[0]}th.",
            f"Someone will offer a solution that sounds perfect around the {caution_days[2]}th. It's not. Wait until next month."
        ]

    # Select appropriate number of matters based on score
    num_matters = 3 if score >= 65 else 2
    matters_en = random.sample(matters_templates, num_matters)

    # Cold-reading advice with emotional triggers
    if score >= 80:
        advice_en = f"You know that idea you dismissed last week as 'not the right time'? THIS month IS the time. But don't broadcast it - skeptics drain energy you'll need. Start quietly between the {peak_days[0]}th and {peak_days[1]}th, refine as you go. The urge to perfect before starting? That's fear talking. Ignore it."
        warning_en = f"Around the {caution_days[0]}th-{caution_days[1]}th: someone offers advice that sounds wise but feels off in your gut. Trust the feeling, not their credentials."
    elif score >= 65:
        advice_en = f"This month rewards steady progress over big moves. That thing you've been doing consistently? It compounds between the {peak_days[0]}th and {peak_days[2]}th. The results won't be dramatic, but they're building to something significant next month. Stay the course."
        warning_en = f"The {peak_days[3]}th feels urgent but isn't. What feels like a deadline is actually just anxiety. Take your time."
    elif score >= 50:
        advice_en = f"Hold your position this month. The itch to make changes around the {caution_days[0]}th is restlessness, not intuition. Use this time to observe, not act. Around the {peak_days[1]}th, you'll see why waiting was right. Trust the pause."
        warning_en = f"Someone pushes you to decide quickly around the {caution_days[1]}th. Anyone rushing you doesn't have your best interests. Delay until you're sure."
    elif score >= 35:
        advice_en = f"This is a testing month. Between the {caution_days[0]}th and {caution_days[2]}th, protect your energy like it's money. Say no more than yes. The thing that looks like a failure around the {caution_days[1]}th? It's actually filtering out what wasn't meant for you. Trust that."
        warning_en = f"Around the {caution_days[1]}th-{caution_days[2]}th: someone will gaslight you about a decision you made. You weren't wrong. They're projecting their regret."
    else:
        advice_en = f"This month is about survival, not success. Between the {caution_days[0]}th and {caution_days[2]}th, just keep breathing. The pressure you're feeling peaks around the {caution_days[1]}th - and then it breaks. Don't make permanent decisions in temporary darkness. The {peak_days[0]}th brings the first sign of relief."
        warning_en = f"Someone offers a quick fix around the {caution_days[2]}th. It will create more problems than it solves. Decline politely, weather the storm alone."

    # Format peak days string
    peak_days_str = ", ".join([_get_ordinal(d) for d in peak_days[:3]])
    if len(peak_days) > 3:
        peak_days_str += f" (high energy)"

    caution_days_str = ", ".join([_get_ordinal(d) for d in caution_days[:2]])

    return {
        'year': year,
        'month': month,
        'score': score,
        'quality': quality_en,
        'matters': matters_en,
        'advice': advice_en,
        'peak_days': peak_days_str,
        'caution_days': caution_days_str,
        'warning': warning_en,
        'overall_energy': f"This month's energy is {quality_en}. Trust what you feel in your body, not what looks logical on paper."
    }


@app.get("/")
def root():
    """Root endpoint"""
    return {
        "status": "online",
        "service": "AstrologyBazi API",
        "version": "2.1.1",
        "documentation": "/docs",
        "website": "https://astrologybazi.com"
    }


@app.get("/health")
@app.head("/health")
def health_check():
    """Health check endpoint for monitoring services"""
    return {
        "status": "healthy",
        "service": "AstrologyBazi API",
        "version": "2.1.1",
        "uptime": "ok"
    }


@app.post("/calculate-bazi")
@app.post("/api/calculate-bazi")  # Alternative endpoint
def calculate_bazi(req: BaZiRequest, api_key: str = Depends(verify_api_key)):
    """
    Calculate BaZi (八字) chart from birth information

    🔒 Requires API Key authentication via x-api-key header

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)

    **Response:**
    - `bazi_chart`: Four Pillars (Year, Month, Day, Hour) with Gan/Zhi
    - `day_master`: Day stem (日主) - the most important element
    - `five_elements`: Distribution of Wood, Fire, Earth, Metal, Water
    - `ten_gods`: Ten Gods (十神) relationships
    - `hidden_stems`: Hidden stems (藏干) in Earthly Branches

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false
    }
    ```
    """
    try:
        logger.info(f"🔮 Calculating BaZi for: {req.birth_date} {req.birth_time}")

        # Validate date format
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        # Validate time format
        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        # Validate gender
        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        # Calculate BaZi
        result = BaZiCalculator.calculate_bazi(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            **req.time_options()
        )

        logger.info(f"✅ BaZi calculation successful")
        return {
            "success": True,
            "data": result
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Calculation Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"BaZi calculation failed: {str(e)}")


@app.post("/calculate-bazi-simple")
def calculate_bazi_simple(req: BaZiRequest, api_key: str = Depends(verify_api_key)):
    """
    Simplified BaZi calculation - returns only essential data

    🔒 Requires API Key authentication via x-api-key header

    Perfect for quick lookups and mobile apps.
    Returns only: Four Pillars, Day Master, Five Elements
    """
    try:
        logger.info(f"🔮 Simple BaZi for: {req.birth_date} {req.birth_time}")

        result = BaZiCalculator.calculate_bazi(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            **req.time_options()
        )

        # Return simplified response
        simplified = {
            "bazi_chart": result['bazi_chart'],
            "day_master": result['day_master'],
            "five_elements": result['five_elements'],
            "time_context": result['time_context']
        }

        logger.info(f"✅ Simple BaZi calculation successful")
        return {
            "success": True,
            "data": simplified
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Calculation Error: {e}")
        raise HTTPException(status_code=500, detail=f"BaZi calculation failed: {str(e)}")


@app.post("/calculate-bazi-complete")
@app.post("/api/calculate-bazi-complete")  # Alternative endpoint
def calculate_bazi_complete(req: BaZiCompleteRequest, api_key: str = Depends(verify_api_key)):
    """
    Calculate complete BaZi with 大运 (Major Cycles) and 流年 (Yearly Cycles)

    🔒 Requires API Key authentication via x-api-key header

    **Premium Feature** - Returns comprehensive 120-year fortune analysis:

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)
    - `num_dayun`: Number of 大运 periods (default: 12, each 10 years = 120 years total)

    **Response:**
    - All basic BaZi data (四柱八字, 日主, 五行, 十神, 藏干)
    - `dayun`: List of major cycles, each containing:
      - `start_age`, `end_age`: Age range for this cycle
      - `ganzhi`: Heavenly Stem + Earthly Branch (干支)
      - `elements`: Five Elements distribution
      - `ten_gods`: Ten Gods relationships
      - `nayin`: 60 Jia Zi element (纳音)
      - `shens`: Spiritual forces (神煞) active in this period
      - `liunian`: 10 yearly cycles within this major cycle
    - `yun_direction`: 'forward' (顺) or 'backward' (逆)
    - `yun_start_age`: Age when cycles begin

    **Perfect for:**
    - 10-year fortune predictions
    - Career timing analysis
    - Wealth opportunity windows
    - Investment decision support
    - Life path planning

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false,
      "num_dayun": 12
    }
    ```
    """
    try:
        logger.info(f"🔮 Complete BaZi calculation for: {req.birth_date} {req.birth_time}")

        # Validate date format
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        # Validate time format
        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        # Validate gender
        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        # Calculate complete BaZi with dayun/liunian
        result = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=req.num_dayun,
            **req.time_options()
        )

        # 2026-07-12 Phase 3: 追加 meta/strength/timing/evidence 四层字段,旧字段一个不改。
        # 格局分析(genju)在这里只用来丰富evidence,失败不影响这个endpoint本身能不能用——
        # 这是新加的能力,不能让它的问题拖垮已经在跑的老功能。
        current_age = None
        try:
            birth_year = int(req.birth_date.split('-')[0])
            current_age = datetime.now().year - birth_year
        except (ValueError, IndexError):
            pass

        if current_age is not None:
            for d in result.get('dayun', []):
                d['is_current'] = d.get('start_age', 0) <= current_age <= d.get('end_age', -1)

        genju_analysis = None
        try:
            genju_analysis = GenjuAnalyzer(result).analyze_genju()
        except Exception as e:
            logger.warning(f"⚠️ Genju analysis skipped for evidence block (non-fatal): {e}")

        result['meta'] = build_meta()
        result['strength'] = result.get('wuxing_strength')
        result['timing'] = build_timing(
            result, result['bazi_chart']['day']['zhi'], result['bazi_chart']['month']['zhi']
        )
        result['evidence'] = build_evidence(result, genju_analysis, current_age)
        if genju_analysis:
            result['genju_analysis'] = genju_analysis

            # Deterministic, redacted teaser for the free trend chart. The
            # selector prefers the broad support-aware trend_score; the full
            # structure score and reasons/events/advice remain exclusive to the
            # paid endpoint. No LLM call is involved here.
            try:
                preview_analysis = DayunAnalyzer(
                    result,
                    genju_analysis,
                    current_age=current_age,
                ).analyze_all_dayun()
                result['dayun_score_preview'] = select_dayun_score_preview(
                    result.get('dayun', []),
                    preview_analysis.get('dayun_analysis', []),
                )
            except Exception as e:
                logger.warning(f"⚠️ Dayun score preview skipped (non-fatal): {e}")


        # New free, evidence-first assessment. It deliberately has no total
        # score and remains available even when optional Genju enrichment fails.
        try:
            result['dayun_dynamics'] = build_dayun_dynamic_assessment(
                result,
                genju_analysis,
                current_age=current_age,
            )
        except Exception as e:
            logger.warning(f"⚠️ Dayun dynamic assessment skipped (non-fatal): {e}")

        logger.info(f"✅ Complete BaZi calculation successful with {req.num_dayun} dayun periods")
        return {
            "success": True,
            "data": result
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Calculation Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Complete BaZi calculation failed: {str(e)}")


# ==================== Shensha Analysis API (神煞分析) ====================

@app.post("/analyze-shensha")
@app.post("/api/analyze-shensha")  # Alternative endpoint
def analyze_shensha(req: BaZiRequest, api_key: str = Depends(verify_api_key)):
    """
    🔮 Analyze Spiritual Forces (神煞分析) - Advanced Feature

    🔒 Requires API Key authentication via x-api-key header

    **Premium Feature** - Comprehensive analysis of 47 spiritual forces:

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)

    **Response:**
    - `bazi_chart`: Basic BaZi chart
    - `shensha_analysis`: Complete spiritual forces analysis:
      - `auspicious_gods`: List of beneficial spiritual forces (吉神)
        - Tianyi Nobleman (天乙贵人) - Most auspicious
        - Red Phoenix (红鸾) - Marriage fortune
        - Heavenly Joy (天喜) - Joyful events
        - Wealth God (禄神) - Financial fortune
        - Fortune Star (福星) - General luck
        - Golden Chariot (金舆) - Wealth & status
        - And more... (10 total)
      - `inauspicious_gods`: List of challenging forces (凶煞)
        - Sheep Blade (羊刃) - Strong character
        - Emptiness (空亡) - Void & loss
        - Disaster God (灾煞) - Accidents
        - White Tiger (白虎) - Bloodshed
        - Loneliness (孤辰) - Solitude
        - Widowhood (寡宿) - Isolation
        - And more... (15+ total)
      - `peach_blossom_system`: Detailed romance analysis
        - Inner peach blossom (墙内桃花) - Harmonious marriage
        - Outer peach blossom (墙外桃花) - Affairs risk
      - `traveling_horse_system`: Movement & career analysis
        - Wealth horse (禄马同乡) - Fortune through travel
        - Broken foot horse (折足马) - Travel obstacles
        - And more... (12 types total)
      - `overall_judgment`: Comprehensive fortune rating
        - Auspicious/Inauspicious scores
        - Fortune level (大吉/中吉/平/不利)
        - Remedy suggestions

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false
    }
    ```

    **Response Example:**
    ```json
    {
      "success": true,
      "data": {
        "bazi_chart": {...},
        "shensha_analysis": {
          "auspicious_gods": [
            {
              "name": "天乙贵人",
              "name_en": "Tianyi Nobleman",
              "position": "year",
              "strength": 10,
              "level": 10,
              "effect": "逢凶化吉、遇难呈祥、贵人相助",
              "description": "命中最吉之神,主一生多得贵人扶持..."
            }
          ],
          "inauspicious_gods": [...],
          "peach_blossom_system": {
            "has_peach_blossom": true,
            "count": 1,
            "details": [...]
          },
          "traveling_horse_system": {...},
          "overall_judgment": {
            "overall_judgment": "moderately_auspicious",
            "overall_judgment_cn": "中吉",
            "auspicious_score": 85.2,
            "inauspicious_score": 32.5,
            "message": "吉多凶少,总体平顺,稳中向好"
          }
        }
      }
    }
    ```

    **Perfect for:**
    - Fortune telling services ($49-$99)
    - Detailed life analysis
    - Marriage compatibility
    - Career guidance
    - Timing decisions (when to start business, get married, etc.)
    """
    try:
        logger.info(f"🔮 Analyzing Shenshas for: {req.birth_date} {req.birth_time}")

        # Validate inputs
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        # Step 1: Calculate basic BaZi chart
        bazi_result = BaZiCalculator.calculate_bazi(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            **req.time_options()
        )

        # Step 2: Analyze spiritual forces
        analyzer = ShenshaAnalyzer(bazi_result)
        shensha_analysis = analyzer.analyze_all_shenshas()

        logger.info(f"✅ Shensha analysis successful: {shensha_analysis['overall_judgment']['overall_judgment_cn']}")

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_result,
                "shensha_analysis": shensha_analysis
            }
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Shensha Analysis Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Shensha analysis failed: {str(e)}")


# ==================== Genju Pattern Analysis API (格局分析) ====================

@app.post("/analyze-genju")
@app.post("/api/analyze-genju")  # Alternative endpoint
def analyze_genju(req: BaZiRequest, api_key: str = Depends(verify_api_key)):
    """
    🎯 Analyze Life Pattern (格局分析) - Core Premium Feature

    🔒 Requires API Key authentication via x-api-key header

    **Premium Feature** - Comprehensive analysis of life patterns and destiny structure:

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)

    **Response:**
    - `bazi_chart`: Basic BaZi chart
    - `genju_analysis`: Complete pattern analysis:
      - `primary_pattern`: Main life pattern (格局)
        - **Six Proper Patterns (六大正格)**:
          - 正官格 (Proper Officer) - Government, authority
          - 七杀格 (Seven Killings) - Military, leadership
          - 正财格 (Proper Wealth) - Business, stable income
          - 偏财格 (Indirect Wealth) - Investment, windfall
          - 正印格 (Proper Seal) - Education, wisdom
          - 偏印格 (Indirect Seal) - Arts, metaphysics
        - **Special Patterns (特殊格局)**:
          - 从财格/从杀格/从儿格 (Following patterns) - Resource-following structure; validate carrying capacity by phase
          - 化气格 (Transformation) - Rare structural configuration; validate with current timing
          - 专旺格 (Specialized prosperity) - Single element dominance
      - `yongshen_analysis`: **Yongshen (用神) - MOST CRITICAL CONCEPT**
        - 用神: The key element needed for balance
        - 喜神: Supporting elements
        - 忌神: Harmful elements
        - 仇神: Enemy elements
        - 闲神: Neutral elements
      - `pattern_level`: Pattern quality and current-stage capacity reading
        - level/score: structural strength indicators, not a lifetime wealth grade
        - description: stage-aware explanation tied to the chart structure
        - nobility: traditional pattern signal kept separate from financial claims
      - `career_suggestions`: Career recommendations by pattern
        - Primary careers (most suitable)
        - Secondary careers (acceptable)
        - Careers to avoid
      - `wealth_analysis`: Current wealth-channel and carrying-capacity analysis
        - Wealth star and pattern signals
        - Current-cycle wealth opportunities and risks
        - Actionable cash-flow and support checks
      - `marriage_analysis`: Marriage quality analysis
        - Marriage quality (excellent/good/moderate/challenging)
        - Spouse characteristics
        - Marriage timing (early/normal/late)

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false
    }
    ```

    **Response Example:**
    ```json
    {
      "success": true,
      "data": {
        "bazi_chart": {...},
        "genju_analysis": {
          "primary_pattern": {
            "pattern_name": "正官格",
            "pattern_type": "proper",
            "is_established": true,
            "strength": 85,
            "description": "月令正官透干,为官贵之命...",
            "favorable_elements": ["正印", "食神", "财星"],
            "unfavorable_elements": ["七杀", "伤官"]
          },
          "yongshen_analysis": {
            "用神": "正印",
            "喜神": ["比劫", "正印"],
            "忌神": ["伤官", "七杀"],
            "仇神": ["偏财"],
            "闲神": ["食神"],
            "method": "扶抑法",
            "explanation": "日主偏弱,需印比帮扶..."
          },
          "pattern_level": {
            "level": "上等",
            "score": 85,
            "nobility": "high",
            "explanation": "格局清纯,用神有力,当前阶段承载力较稳"
          },
          "career_suggestions": {
            "primary": ["公务员", "政府官员", "企业管理层"],
            "secondary": ["教师", "军官", "警官"],
            "avoid": ["自由职业", "销售", "投资"],
            "best_industry": "政府机构、国企、教育系统"
          },
          "wealth_analysis": {
            "wealth_potential": "high",
            "wealth_source": "salary",
            "best_period": "35-55岁",
            "investment_advice": "保守稳健型投资为主"
          },
          "marriage_analysis": {
            "quality": "stable",
            "spouse_characteristics": "贤惠、稳重、有责任心",
            "marriage_timing": "normal",
            "compatibility_advice": "适合门当户对,重视家庭"
          }
        }
      }
    }
    ```

    **Perfect for:**
    - Fortune telling services ($99-$299)
    - Career consulting ($199-$499)
    - Life planning guidance ($299-$999)
    - Investment timing analysis
    - Marriage compatibility consulting
    """
    try:
        logger.info(f"🎯 Analyzing Genju Pattern for: {req.birth_date} {req.birth_time}")

        # Validate inputs
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        # Step 1: Calculate basic BaZi chart
        bazi_result = BaZiCalculator.calculate_bazi(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            **req.time_options()
        )

        # Step 2: Analyze life pattern (格局)
        analyzer = GenjuAnalyzer(bazi_result)
        genju_analysis = analyzer.analyze_genju()

        logger.info(f"✅ Genju analysis successful: {genju_analysis['primary_pattern']['pattern_name']} - {genju_analysis['pattern_level']['level']}")

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_result,
                "genju_analysis": genju_analysis
            }
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Genju Analysis Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Genju pattern analysis failed: {str(e)}")


# ==================== Evidence-first Dayun Dynamics API ====================

@app.post("/analyze-dayun-dynamics")
@app.post("/api/analyze-dayun-dynamics")
def analyze_dayun_dynamics(req: BaZiCompleteRequest, api_key: str = Depends(verify_api_key)):
    """
    Return deterministic, role-aware Da Yun dynamics.

    This endpoint separates the broad Five-Element climate from the way a
    Da Yun interacts with the natal chart.  It reports evidence, counter-
    evidence, transformation gates, and uncertainty instead of a single
    fortune score.  It does not call an LLM.
    """
    try:
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )
        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )
        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )
        if req.num_dayun < 1 or req.num_dayun > 12:
            raise HTTPException(
                status_code=400,
                detail="num_dayun must be between 1 and 12"
            )

        bazi_complete = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=req.num_dayun,
            **req.time_options()
        )
        genju_analysis = GenjuAnalyzer(bazi_complete).analyze_genju()
        current_age = None
        try:
            current_age = datetime.now().year - int(req.birth_date.split('-')[0])
        except (ValueError, IndexError):
            pass

        dynamics = build_dayun_dynamic_assessment(
            bazi_complete,
            genju_analysis,
            current_age=current_age,
        )
        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_complete.get("bazi_chart"),
                "day_master": bazi_complete.get("day_master"),
                "yun_direction": bazi_complete.get("yun_direction"),
                "yun_start_age": bazi_complete.get("yun_start_age"),
                "dayun_dynamics": dynamics,
            },
        }
    except HTTPException:
        raise
    except ValueError as e:
        logger.error(f"❌ Dayun dynamics validation error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Dayun dynamics calculation error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Dayun dynamics calculation failed: {str(e)}")


# ==================== Dayun (Major Cycles) Analysis API (大运分析) ====================
# ⚠️ DEPRECATED (2026-07): 2026-07-12全仓库排查确认astrologybazi前端零调用此endpoint
# (唯一出现处是tianji-oracle的proxy allowlist字符串,从未被实际发起请求)。
# 保留仅为兼容,不删除代码——见 API_UNIFICATION_PLAN.md §1.1/§6。
# 大运数据现在应从 /calculate-bazi-complete 的 timing/evidence 字段获取。

@app.post("/analyze-dayun")
@app.post("/api/analyze-dayun")  # Alternative endpoint
def analyze_dayun(req: BaZiCompleteRequest, api_key: str = Depends(verify_api_key)):
    """
    📅 Analyze Major Cycles (大运分析) - Premium Fortune Prediction

    ⚠️ DEPRECATED: 零前端调用,详见函数上方模块级注释。

    🔒 Requires API Key authentication via x-api-key header

    **Premium Feature** - Comprehensive 120-year fortune prediction and life planning:

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)
    - `num_dayun`: Number of major cycles (default: 12, each 10 years = 120 years)

    **Response:**
    - `bazi_chart`: Complete BaZi chart with Dayun data
    - `genju_analysis`: Pattern analysis with Yongshen (用神)
    - `dayun_analysis`: Comprehensive Dayun fortune analysis:
      - `dayun_analysis`: List of all major cycles with:
        - Fortune quality (大吉/顺遂/平稳/波折/困难)
        - Detailed score and reasons
        - Event predictions (career/wealth/marriage/health/study)
        - Life advice for each period
        - Critical years identification
      - `turning_points`: Major life turning points
        - Age and Ganzhi of turning point
        - Direction (转吉/转凶/重大变动)
        - Score changes
      - `life_stages`: Life stage analysis
        - Early years (0-30): Average score and judgment
        - Middle years (30-60): Average score and judgment
        - Late years (60+): Average score and judgment
        - Best life stage identification
      - `overall_summary`: Complete life fortune summary
        - Total average score
        - Quality distribution
        - Best and worst Dayun periods
        - Overall judgment

    **Fortune Prediction Features:**
    1. **Yongshen-based Analysis** (用神判断法)
       - The returned 0–100 value is an explainable heuristic signal.
       - It is not a probability, percentile, or historically calibrated forecast.
       - `judgment.score_method` and `judgment.score_confidence` identify its semantics.

    2. **Special Combinations** (特殊组合)
       - Relationship adjustments are calculated once by the central
         XingChongHeHeCalculator.
       - Sui Yun Bing Lin (岁运并临) is a year-level event only when the full
         annual GanZhi equals the Dayun GanZhi; it is not a fixed decade penalty.

    3. **Auspicious Spirits** (吉神)
       - Tianyi Nobleman (天乙贵人), Yi Ma (驿马), and Peach Blossom (桃花)
         remain bounded heuristic signals, not guaranteed outcomes.

    4. **Event Predictions** (事件预测)
       - Career: Promotion, job change, peak period
       - Wealth: Investment timing, income increase, financial risks
       - Marriage: Wedding timing, relationship quality, crisis periods
       - Health: Peak health years, illness risks, prevention advice
       - Study: Academic success, exam luck, learning opportunities

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false,
      "num_dayun": 12
    }
    ```

    **Response Example:**
    ```json
    {
      "success": true,
      "data": {
        "bazi_chart": {...},
        "genju_analysis": {...},
        "dayun_analysis": {
          "dayun_analysis": [
            {
              "ganzhi": "戊寅",
              "age_range": "7-16岁",
              "judgment": {
                "quality": "good",
                "quality_cn": "顺遂平安",
                "label": "✅ 良好",
                "score": 65,
                "reasons": [
                  "✅ 大运为用神五行,有力助身",
                  "✅ 大运逢驿马,利于走动变动"
                ]
              },
              "events": {
                "career": ["7-10岁:学业进步快"],
                "wealth": ["..."],
                "marriage": ["..."],
                "health": ["..."],
                "study": ["7-16岁:学业进步快,考试运佳"]
              },
              "advice": [
                "✅ 7-16岁运势向好,适合稳健发展",
                "抓住学习机会,提升知识能力"
              ],
              "critical_years": [
                {
                  "year": 2011,
                  "age": 22,
                  "ganzhi": "辛卯",
                  "importance": 3,
                  "reasons": ["流年与日柱天克地冲"]
                }
              ]
            }
          ],
          "turning_points": [
            {
              "age": 37,
              "from_dayun": "己卯",
              "to_dayun": "庚辰",
              "direction": "转吉",
              "score_change": 35,
              "description": "37岁进入庚辰大运,运势转吉"
            }
          ],
          "life_stages": {
            "early_years": {
              "age_range": "0-30岁(早年运)",
              "avg_score": 58.5,
              "judgment": "运势良好,平稳向上"
            },
            "middle_years": {
              "age_range": "30-60岁(中年运)",
              "avg_score": 72.3,
              "judgment": "运势极佳,大吉大利"
            },
            "late_years": {
              "age_range": "60岁以上阶段",
              "avg_score": 45.2,
              "judgment": "运势平稳,中规中矩"
            },
            "best_stage": "middle"
          },
          "overall_summary": {
            "total_dayun_count": 12,
            "avg_score": 58.7,
            "overall_judgment": "已计算周期整体偏稳,仍需按当前窗口调整节奏",
            "quality_distribution": {
              "excellent": 2,
              "good": 5,
              "average": 3,
              "challenging": 2,
              "difficult": 0
            },
            "best_dayun": {
              "ganzhi": "庚辰",
              "age_range": "37-46岁",
              "score": 85
            },
            "worst_dayun": {
              "ganzhi": "丙子",
              "age_range": "67-76岁",
              "score": 32
            }
          }
        }
      }
    }
    ```

    **Perfect for:**
    - 10-year fortune predictions ($199-$399)
    - Life planning consultation ($299-$799)
    - Career timing analysis ($399-$999)
    - Investment decision support ($499-$1,499)
    - Marriage timing consultation ($199-$499)
    - Comprehensive destiny report ($499-$1,999)

    **Business Value:**
    - Subscription model: $29-49/month for ongoing Dayun updates
    - One-time reports: $199-$1,999 depending on depth
    - Monthly revenue potential: $2,500-$5,000
    """
    try:
        logger.info(f"📅 Analyzing Dayun for: {req.birth_date} {req.birth_time}")

        # Validate inputs
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        # Step 1: Calculate complete BaZi with Dayun
        bazi_complete = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=req.num_dayun,
            **req.time_options()
        )

        # Step 2: Analyze life pattern (格局) to get Yongshen
        genju_analyzer = GenjuAnalyzer(bazi_complete)
        genju_analysis = genju_analyzer.analyze_genju()

        # 🎯 计算当前年龄(用于标记当前大运)
        from datetime import datetime
        birth_year = int(req.birth_date.split('-')[0])
        current_year = datetime.now().year
        current_age = current_year - birth_year

        # Step 3: Analyze Dayun (major cycles)
        dayun_analyzer = DayunAnalyzer(bazi_complete, genju_analysis, current_age=current_age)
        dayun_analysis = dayun_analyzer.analyze_all_dayun()

        logger.info(f"✅ Dayun analysis successful: {len(dayun_analysis['dayun_analysis'])} major cycles analyzed")
        logger.info(f"   Overall judgment: {dayun_analysis['overall_summary']['overall_judgment']}")

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_complete,
                "genju_analysis": genju_analysis,
                "dayun_analysis": dayun_analysis
            }
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Dayun Analysis Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Dayun analysis failed: {str(e)}")


# ==================== AI Interpretation API (AI解读引擎) ====================

@app.post("/ai-interpretation")
@app.post("/api/ai-interpretation")  # Alternative endpoint
def ai_interpretation(req: BaZiInterpretationRequest, api_key: str = Depends(verify_api_key)):
    """
    🤖 AI BaZi Interpretation - Transform Technical Analysis into Natural Language

    🔒 Requires API Key authentication via x-api-key header

    **Premium Feature** - Professional natural language interpretation of BaZi destiny analysis:

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format (e.g., '1990-01-15')
    - `birth_time`: Birth time in HH:MM format (e.g., '08:30')
    - `gender`: 'male' or 'female' (default: 'male')
    - `is_lunar`: True for lunar calendar, False for solar/Gregorian (default: False)
    - `interpretation_level`: Depth of interpretation
      - **'basic'**: 3,000 words - Essential interpretation ($29)
        - Personality, Career, Wealth, Marriage, Health, Overall
      - **'professional'**: 8,000 words - Detailed analysis ($99)
        - Basic sections + Dayun overview + Life stages analysis
      - **'master'**: 15,000 words - Complete life planning ($199)
        - Professional sections + Turning points + All Dayun details + Spiritual forces + Life planning
    - `language`: Output language 'zh-CN' (Chinese), 'en' (English), 'zh-TW' (Traditional Chinese)
    - `num_dayun`: Number of major cycles to analyze (default: 12 = 120 years)

    **Response:**
    - `bazi_chart`: Complete BaZi chart with all technical data
    - `shensha_analysis`: Spiritual forces analysis (47 forces)
    - `genju_analysis`: Pattern analysis with Yongshen (用神)
    - `dayun_analysis`: Major cycles fortune prediction
    - `ai_interpretation`: **Natural language interpretation** 🌟
      - `level`: Interpretation depth ('basic'/'professional'/'master')
      - `word_count`: Total word count
      - `sections`: Natural language sections:
        - **personality**: Who you are (based on Day Master + Pattern)
          - Example: "你是甲木日主,就像大树一样,天性正直向上..."
        - **career**: Career recommendations with specific industries
          - Example: "你的八字显示你适合在有组织、有制度的环境中工作,如公务员..."
        - **wealth**: Current wealth-channel and carrying-capacity signals
        - **marriage**: Marriage quality and spouse characteristics
        - **health**: Health advice based on Five Elements
        - **overall**: Structural summary and current-phase suggestions
        - **(Professional/Master only)** **dayun_overview**: Current and next cycle signals
        - **(Professional/Master only)** **life_stages**: Current-window calibration
        - **(Master only)** **turning_points**: Turning points near the current window
        - **(Master only)** **ten_year_fortunes**: Current and next cycle details
        - **(Master only)** **auspicious_spirits**: Beneficial forces interpretation
        - **(Master only)** **inauspicious_spirits**: Challenging forces interpretation
        - **(Master only)** **life_planning**: Comprehensive life roadmap

    **Key Transformation Examples:**

    | Technical Term | Natural Language |
    |----------------|------------------|
    | 甲木日主 + 正官格 | "你是一个正直向上、有责任心的人,适合在政府或大企业工作" |
    | 用神为正印 | "学习是你的贵人,通过教育和知识提升可以改变命运" |
    | 大运走喜神运 | "未来10年运势极佳,是创业或升职的黄金时期" |
    | 羊刃 + 七杀 | "性格刚强果断,适合军警或竞争性行业" |
    | 桃花 + 红鸾 | "异性缘好,容易遇到心仪对象" |

    **Example:**
    ```json
    {
      "birth_date": "1990-01-15",
      "birth_time": "08:30",
      "gender": "male",
      "is_lunar": false,
      "interpretation_level": "professional",
      "language": "zh-CN",
      "num_dayun": 12
    }
    ```

    **Response Example:**
    ```json
    {
      "success": true,
      "data": {
        "bazi_chart": {...},
        "shensha_analysis": {...},
        "genju_analysis": {...},
        "dayun_analysis": {...},
        "ai_interpretation": {
          "level": "professional",
          "word_count": 8247,
          "sections": {
            "personality": "你是甲木日主,就像大树一样,天性正直向上、积极进取。你有很强的责任心...",
            "career": "你的八字显示你适合在有组织、有制度的环境中工作,如公务员、政府官员...",
            "wealth": "当前财路与承载力信号:先核现金流、支持条件和可重复交付...",
            "marriage": "你的婚姻质量良好,配偶贤惠、稳重、有责任心...",
            "health": "作为木性日主,你需要特别注意肝胆的健康...",
            "overall": "当前结构提示资源与职责如何协同;请结合当前窗口的现实反馈...",
            "dayun_overview": "当前与下一大运窗口的结构信号与可执行建议...",
            "life_stages": "当前阶段校准:把机会当作工作假设,用现金流和结果复核..."
          }
        }
      }
    }
    ```

    **Perfect for:**
    - BaZi consulting services ($29-$199 per reading)
    - Automated fortune-telling platforms
    - Mobile apps with human-readable reports
    - Life coaching and career consulting
    - Subscription-based destiny analysis services

    **Business Value:**
    - **Basic tier**: $29 × 100 users/month = $2,900/month
    - **Professional tier**: $99 × 50 users/month = $4,950/month
    - **Master tier**: $199 × 25 users/month = $4,975/month
    - **Total potential**: $12,825/month ($153,900/year)

    **Monthly Revenue Projection:**
    - Conservative (50 users): $2,500/month
    - Moderate (200 users): $10,000/month
    - Optimistic (500 users): $20,000+/month
    """
    try:
        logger.info(f"🤖 AI Interpretation for: {req.birth_date} {req.birth_time}, level: {req.interpretation_level}")

        # Validate inputs
        if len(req.birth_date.split('-')) != 3:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_date format. Use YYYY-MM-DD (e.g., '1990-01-15')"
            )

        if len(req.birth_time.split(':')) != 2:
            raise HTTPException(
                status_code=400,
                detail="Invalid birth_time format. Use HH:MM (e.g., '08:30')"
            )

        if req.gender not in ['male', 'female']:
            raise HTTPException(
                status_code=400,
                detail="Invalid gender. Use 'male' or 'female'"
            )

        if req.interpretation_level not in ['basic', 'professional', 'master']:
            raise HTTPException(
                status_code=400,
                detail="Invalid interpretation_level. Use 'basic', 'professional', or 'master'"
            )

        if req.language not in ['zh-CN', 'en', 'zh-TW']:
            raise HTTPException(
                status_code=400,
                detail="Invalid language. Use 'zh-CN', 'en', or 'zh-TW'"
            )

        # Step 1: Calculate complete BaZi with Dayun
        logger.info("  Step 1/5: Calculating complete BaZi with Dayun...")
        bazi_complete = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=req.num_dayun,
            **req.time_options()
        )
        logger.info(f"    ✅ BaZi calculated: {bazi_complete['day_master']['gan']}日主")

        # Step 2: Analyze spiritual forces (Shensha)
        logger.info("  Step 2/5: Analyzing spiritual forces (Shensha)...")
        shensha_analyzer = ShenshaAnalyzer(bazi_complete)
        shensha_analysis = shensha_analyzer.analyze_all_shenshas()
        logger.info(f"    ✅ Shensha analyzed: {len(shensha_analysis['auspicious_gods'])} auspicious, {len(shensha_analysis['inauspicious_gods'])} inauspicious")

        # Step 3: Analyze life pattern (Genju) to get Yongshen
        logger.info("  Step 3/5: Analyzing life pattern (Genju) for Yongshen...")
        genju_analyzer = GenjuAnalyzer(bazi_complete)
        genju_analysis = genju_analyzer.analyze_genju()
        logger.info(f"    ✅ Genju analyzed: {genju_analysis['primary_pattern']['pattern_name']}, 用神: {genju_analysis['yongshen_analysis']['用神']}")

        # Step 4: Analyze Dayun (major cycles)
        logger.info("  Step 4/5: Analyzing Dayun (major cycles)...")

        # 🎯 计算当前年龄(用于标记当前大运)
        from datetime import datetime
        birth_year = int(req.birth_date.split('-')[0])
        current_year = datetime.now().year
        current_age = current_year - birth_year
        logger.info(f"    📍 Current age: {current_age} years (born {birth_year})")

        dayun_analyzer = DayunAnalyzer(bazi_complete, genju_analysis, current_age=current_age)
        dayun_analysis = dayun_analyzer.analyze_all_dayun()
        logger.info(f"    ✅ Dayun analyzed: {len(dayun_analysis['dayun_analysis'])} periods, avg score: {dayun_analysis['overall_summary']['avg_score']}")

        # 🎯 Phase 3: Advanced Analysis Modules (2025-12-24)
        logger.info("  Step 4.5/6: Running Phase 3 advanced analysis modules...")

        # 🌐 Map API language to analyzer language ('zh-CN'/'zh-TW' → 'zh', 'en' → 'en')
        analyzer_lang = 'en' if req.language == 'en' else 'zh'

        # 婚姻分析
        marriage_analyzer = MarriageAnalyzer(bazi_complete, dayun_analysis, language=analyzer_lang)
        marriage_analysis = marriage_analyzer.analyze_marriage()
        logger.info(f"    ✅ Marriage analyzed: Quality {marriage_analysis['marriage_quality']['score']}/100")

        # 事业分析
        career_analyzer = CareerAnalyzer(bazi_complete, genju_analysis, dayun_analysis)  # 🔧 Bug fix: 添加dayun_analysis
        career_analysis = career_analyzer.analyze_career(language=analyzer_lang)
        logger.info(f"    ✅ Career analyzed: {len(career_analysis['recommended_careers'])} careers recommended")

        # 🆕 财运分析
        wealth_analyzer = WealthAnalyzer(bazi_complete, dayun_analysis)
        wealth_analysis = wealth_analyzer.analyze_wealth(language=analyzer_lang)
        logger.info(f"    ✅ Wealth analyzed: Score {wealth_analysis['wealth_quality']['score']}/100")

        # 健康分析
        health_analyzer = HealthAnalyzer(bazi_complete)
        health_analysis = health_analyzer.analyze_health(language=analyzer_lang)
        logger.info(f"    ✅ Health analyzed: {len(health_analysis['health_risks'])} risks identified")

        # 🆕 官职分析 (2025-12-25)
        official_analyzer = OfficialAnalyzer(bazi_complete, dayun_analysis)
        official_analysis = official_analyzer.analyze_official()
        logger.info(f"    ✅ Official analyzed: Level {official_analysis.get('official_level', 'N/A')}")

        # 🆕 法律风险分析 (2025-12-25)
        legal_risk_analyzer = LegalRiskAnalyzer(bazi_complete, dayun_analysis)
        legal_risk_analysis = legal_risk_analyzer.analyze_legal_risk()
        logger.info(f"    ✅ Legal risk analyzed: Score {legal_risk_analysis.get('risk_score', 0)}/100")

        # Step 5: Generate AI interpretation
        logger.info(f"  Step 5/6: Generating {req.interpretation_level} level AI interpretation...")
        interpreter = AIInterpreter(
            bazi_chart=bazi_complete,
            shensha_analysis=shensha_analysis,
            genju_analysis=genju_analysis,
            dayun_analysis=dayun_analysis
        )
        ai_interpretation = interpreter.generate_interpretation(
            level=req.interpretation_level,
            language=req.language
        )
        logger.info(f"    ✅ Interpretation generated: {ai_interpretation['word_count']} words, {len(ai_interpretation['sections'])} sections")

        logger.info(f"✅ Complete AI interpretation successful for {req.interpretation_level} level")

        # 🎯 Extract life curve data (1-100 age fortune table with scores)
        life_curve_data = None
        if hasattr(interpreter, 'life_curve_data') and interpreter.life_curve_data:
            life_curve_data = interpreter.life_curve_data

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_complete,
                "shensha_analysis": shensha_analysis,
                "genju_analysis": genju_analysis,
                "dayun_analysis": dayun_analysis,
                "ai_interpretation": ai_interpretation,
                "life_curve_data": life_curve_data,  # Phase 2: 1-100岁完整运势数据
                # 🎯 Phase 3: Advanced Analysis (2025-12-24)
                "marriage_analysis": marriage_analysis,  # 婚姻分析
                "career_analysis": career_analysis,      # 事业分析
                "health_analysis": health_analysis,      # 健康分析
                "wealth_analysis": wealth_analysis,      # 财运分析
                # 🆕 Phase 3.5: Official & Legal Analysis (2025-12-25)
                "official_analysis": official_analysis,      # 官职分析
                "legal_risk_analysis": legal_risk_analysis   # 法律风险分析
            }
        }

    except ValueError as e:
        logger.error(f"❌ Validation Error: {e}")
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ AI Interpretation Error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"AI interpretation failed: {str(e)}")


# ==================== Time Fortune Prediction API (时间预测API) ====================

@app.post("/monthly-fortune")
@app.post("/api/monthly-fortune")  # Alternative endpoint
def analyze_monthly_fortune_api(req: MonthlyFortuneRequest, api_key: str = Depends(verify_api_key)):
    """
    📅 Monthly Fortune Analysis (流月运势分析)

    🔒 Requires API Key authentication

    **Premium Feature** - Analyze fortune for a specific month ($9.9/month subscription)

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `target_year`: Year to analyze (e.g., 2026)
    - `target_month`: Month to analyze (1-12)
    - `language`: 'zh' (Chinese, default) or 'en' (cold-reading English with Barnum Effect)

    **Response:**
    - Monthly fortune score (0-100)
    - Quality assessment (Chinese: 大吉/顺遂/平稳/波折/困难, English: Peak Performance Window/Favorable Momentum/etc.)
    - **English mode**: Cold-reading format with specific dates, emotional triggers, Barnum Effect
    - **Chinese mode**: Traditional fortune analysis with reasons and advice
    - Suitable/avoid activities (Chinese mode only)

    **Business Value**: $9.9/month subscription × 200 users = $1,980/month
    """
    try:
        logger.info(f"📅 Analyzing monthly fortune for {req.target_year}-{req.target_month}")

        # Calculate BaZi
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=12
        )

        # Analyze Genju for Yongshen
        genju_analyzer = GenjuAnalyzer(bazi_chart)
        genju_analysis = genju_analyzer.analyze_genju()

        # Analyze Monthly Fortune
        fortune_analyzer = TimeFortuneAnalyzer(bazi_chart, genju_analysis, None)
        monthly_fortune = fortune_analyzer.analyze_monthly_fortune(req.target_year, req.target_month)

        logger.info(f"✅ Monthly fortune analyzed: {monthly_fortune['quality_cn']} ({monthly_fortune['score']}/100)")

        # Apply cold-reading transformation for English
        if req.language == "en":
            monthly_fortune_output = apply_cold_reading_english(
                monthly_fortune,
                req.target_year,
                req.target_month
            )
            logger.info(f"🔮 Applied cold-reading English transformation")
        else:
            monthly_fortune_output = monthly_fortune

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_chart,
                "monthly_fortune": monthly_fortune_output
            }
        }

    except Exception as e:
        logger.error(f"❌ Monthly fortune analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Monthly fortune analysis failed: {str(e)}")


@app.post("/career-monthly-fortune")
@app.post("/api/career-monthly-fortune")  # Alternative endpoint
def analyze_career_monthly_fortune_api(req: CareerMonthlyFortuneRequest, api_key: str = Depends(verify_api_key)):
    """
    💼 Career-Specific Monthly Fortune Analysis (事业月运分析)

    🔒 Requires API Key authentication

    **Career Forecast Feature** - Analyze 12 months of career fortune for a specific year

    ⚠️ **Product Differentiation**: This endpoint provides CAREER-ONLY monthly analysis.
    It is different from Tianji Oracle's comprehensive monthly fortune (which includes health, relationships, wealth).
    This prevents content duplication if users purchase both products.

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `target_year`: Year to analyze (e.g., 2026)

    **Response:**
    - Array of 12 months with:
      - `month`: "YYYY-MM" format
      - `month_ganzhi`: Monthly celestial stem and branch (干支)
      - `career_score`: Career fortune score (0-100)
      - `quality_cn`: Quality assessment (大吉/顺遂/平稳/波折/困难)
      - `opportunities`: List of career opportunities this month
      - `risks`: List of career risks to watch out for
      - `advice`: Actionable career advice for the month

    **Business Value**: Part of Career Forecast product ($19.99)
    """
    try:
        logger.info(f"💼 Analyzing career monthly fortune for year {req.target_year}")

        # Calculate BaZi
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=False,  # Don't need Dayun for this
            num_dayun=0
        )

        # Initialize CareerAnalyzer
        career_analyzer = CareerAnalyzer(bazi_chart)

        # Analyze career monthly fortune
        monthly_fortune = career_analyzer._analyze_career_monthly_fortune(req.target_year)

        logger.info(f"✅ Career monthly fortune analyzed: {len(monthly_fortune)} months")

        return {
            "success": True,
            "data": {
                "target_year": req.target_year,
                "monthly_fortune": monthly_fortune,
                "note": "Career-only analysis. For comprehensive fortune including health/relationships/wealth, see Tianji Oracle product."
            }
        }

    except Exception as e:
        logger.error(f"❌ Career monthly fortune analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Career monthly fortune analysis failed: {str(e)}")


@app.post("/yearly-fortune")
@app.post("/api/yearly-fortune")  # Alternative endpoint
def analyze_yearly_fortune_api(req: YearlyFortuneRequest, api_key: str = Depends(verify_api_key)):
    """
    🎯 Yearly Fortune Analysis - 12 Month Complete Forecast (流年12个月运势)

    🔒 Requires API Key authentication

    **Premium Feature** - Get complete yearly fortune prediction for all 12 months

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `target_year`: Year to analyze (defaults to current year)
    - `language`: 'zh' or 'en' for output language

    **Response:**
    - Complete BaZi chart with Five Elements analysis
    - 12-month fortune array with scores, quality, reasons, advice
    - Yearly overview with best/worst months
    - Feng Shui recommendations per month

    **Business Value**: Powers Tianji Oracle Premium ($19.99/reading)
    """
    try:
        # Default to current year if not specified
        target_year = req.target_year or datetime.now().year
        logger.info(f"🎯 Analyzing yearly fortune for {target_year}, birth: {req.birth_date}")

        # 1. Calculate BaZi chart (only once!)
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=12
        )

        # 2. Analyze Genju for Yongshen/Jishen
        genju_analyzer = GenjuAnalyzer(bazi_chart)
        genju_analysis = genju_analyzer.analyze_genju()

        # 3. Initialize fortune analyzer once
        fortune_analyzer = TimeFortuneAnalyzer(bazi_chart, genju_analysis, None)

        # 4. Analyze all 12 months
        monthly_fortunes = []
        total_score = 0
        best_month = {"month": 1, "score": 0}
        worst_month = {"month": 1, "score": 100}

        for month in range(1, 13):
            monthly_fortune = fortune_analyzer.analyze_monthly_fortune(target_year, month)

            # Track best/worst
            score = monthly_fortune.get('score', 50)
            total_score += score

            if score > best_month["score"]:
                best_month = {"month": month, "score": score, "quality": monthly_fortune.get('quality_cn', '平稳')}
            if score < worst_month["score"]:
                worst_month = {"month": month, "score": score, "quality": monthly_fortune.get('quality_cn', '平稳')}

            monthly_fortunes.append({
                "month": month,
                "month_name_zh": f"{month}月",
                "month_name_en": datetime(target_year, month, 1).strftime("%B"),
                **monthly_fortune
            })

        # 5. Calculate yearly overview
        avg_score = round(total_score / 12)
        yearly_quality = (
            "大吉" if avg_score >= 80 else
            "顺遂" if avg_score >= 65 else
            "平稳" if avg_score >= 50 else
            "波折" if avg_score >= 35 else
            "困难"
        )

        # 6. Extract key info for frontend
        day_master = bazi_chart.get('day_master', {})
        five_elements = bazi_chart.get('five_elements', {})

        logger.info(f"✅ Yearly fortune analyzed: avg={avg_score}, best=M{best_month['month']}, worst=M{worst_month['month']}")

        return {
            "success": True,
            "data": {
                # BaZi summary (not full chart to reduce payload)
                "bazi_summary": {
                    "day_master": day_master,
                    "five_elements": five_elements,
                    "year_pillar": bazi_chart.get('bazi_chart', {}).get('year', {}),
                    "month_pillar": bazi_chart.get('bazi_chart', {}).get('month', {}),
                    "day_pillar": bazi_chart.get('bazi_chart', {}).get('day', {}),
                    "hour_pillar": bazi_chart.get('bazi_chart', {}).get('hour', {}),
                },
                # Yongshen/Jishen
                "genju_summary": {
                    "yongshen": genju_analysis.get('yongshen', ''),
                    "jishen": genju_analysis.get('jishen', []),
                    "pattern": genju_analysis.get('pattern', ''),
                },
                # Yearly overview
                "yearly_overview": {
                    "year": target_year,
                    "average_score": avg_score,
                    "quality": yearly_quality,
                    "best_month": best_month,
                    "worst_month": worst_month,
                },
                # All 12 months
                "monthly_fortunes": monthly_fortunes,
            }
        }

    except Exception as e:
        logger.error(f"❌ Yearly fortune analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Yearly fortune analysis failed: {str(e)}")


@app.post("/daily-fortune")
@app.post("/api/daily-fortune")  # Alternative endpoint
def analyze_daily_fortune_api(req: DailyFortuneRequest, api_key: str = Depends(verify_api_key)):
    """
    🗓️ Daily Fortune & Date Selection (流日择日分析)

    🔒 Requires API Key authentication

    **Premium Feature** - Analyze if a specific date is suitable for certain activities ($19/reading)

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `target_date`: Date to analyze in YYYY-MM-DD format
    - `activities`: Activities to check (e.g., ['嫁娶', '开业', '搬家'])

    **Response:**
    - Daily fortune score (0-100)
    - Overall verdict (大吉/吉/平/凶/大凶)
    - Activity-specific analysis with scores
    - Almanac (宜忌) from traditional calendar

    **Common Activities:**
    - 嫁娶 (Marriage), 开业 (Business Opening), 搬家 (Moving)
    - 动土 (Construction), 出行 (Travel), 签约 (Contract Signing)
    - 求财 (Wealth Seeking), 祭祀 (Worship), 安葬 (Burial)

    **Business Value**: $19/reading × 100 users = $1,900/month
    """
    try:
        logger.info(f"🗓️ Analyzing daily fortune for {req.target_date}")

        # Parse target date
        try:
            year, month, day = map(int, req.target_date.split('-'))
            target_date_obj = date(year, month, day)
        except ValueError:
            raise HTTPException(status_code=400, detail="Invalid target_date format. Use YYYY-MM-DD")

        # Calculate BaZi
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=12
        )

        # Analyze Genju for Yongshen
        genju_analyzer = GenjuAnalyzer(bazi_chart)
        genju_analysis = genju_analyzer.analyze_genju()

        # Analyze Daily Fortune
        fortune_analyzer = TimeFortuneAnalyzer(bazi_chart, genju_analysis, None)
        daily_fortune = fortune_analyzer.analyze_daily_fortune(target_date_obj, req.activities)

        logger.info(f"✅ Daily fortune analyzed: {daily_fortune['quality_cn']} ({daily_fortune['score']}/100)")

        return {
            "success": True,
            "data": {
                "bazi_chart": bazi_chart,
                "daily_fortune": daily_fortune
            }
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Daily fortune analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Daily fortune analysis failed: {str(e)}")


@app.post("/hourly-fortune")
@app.post("/api/hourly-fortune")  # Alternative endpoint
def analyze_hourly_fortune_api(req: DailyFortuneRequest, api_key: str = Depends(verify_api_key)):
    """
    ⏰ Hourly Fortune Analysis (时辰吉凶分析)

    🔒 Requires API Key authentication

    **Premium Feature** - Analyze fortune for all 12 hours of a day

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `target_date`: Date to analyze in YYYY-MM-DD format

    **Response:**
    - 12 hourly fortunes with scores (0-100 each)
    - Tian Shen (天神) for each hour
    - Suitable activities for each hour
    - Recommendations

    **Use Cases:**
    - Find best time for important meetings
    - Schedule medical appointments
    - Plan business negotiations
    - Choose wedding ceremony time
    """
    try:
        logger.info(f"⏰ Analyzing hourly fortune for {req.target_date}")

        # Parse target date
        try:
            year, month, day = map(int, req.target_date.split('-'))
            target_date_obj = date(year, month, day)
        except ValueError:
            raise HTTPException(status_code=400, detail="Invalid target_date format. Use YYYY-MM-DD")

        # Calculate BaZi
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=12
        )

        # Analyze Genju for Yongshen
        genju_analyzer = GenjuAnalyzer(bazi_chart)
        genju_analysis = genju_analyzer.analyze_genju()

        # Analyze Hourly Fortune
        fortune_analyzer = TimeFortuneAnalyzer(bazi_chart, genju_analysis, None)
        hourly_fortunes = fortune_analyzer.analyze_hourly_fortune(target_date_obj)

        logger.info(f"✅ Hourly fortune analyzed: 12 hours")

        return {
            "success": True,
            "data": {
                "date": str(target_date_obj),
                "hourly_fortunes": hourly_fortunes
            }
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Hourly fortune analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Hourly fortune analysis failed: {str(e)}")


@app.post("/find-auspicious-dates")
@app.post("/api/find-auspicious-dates")  # Alternative endpoint
def find_auspicious_dates_api(req: FindAuspiciousDatesRequest, api_key: str = Depends(verify_api_key)):
    """
    🔍 Find Auspicious Dates (寻找吉日)

    🔒 Requires API Key authentication

    **Premium Feature** - Find best dates for specific activities within a date range

    **Request Body:**
    - `birth_date`: Birth date in YYYY-MM-DD format
    - `birth_time`: Birth time in HH:MM format
    - `gender`: 'male' or 'female'
    - `is_lunar`: True if lunar calendar
    - `start_date`: Range start date in YYYY-MM-DD format
    - `end_date`: Range end date in YYYY-MM-DD format (max 90 days)
    - `activity`: Activity type (e.g., '嫁娶', '开业', '搬家')
    - `min_score`: Minimum score requirement (default: 70)

    **Response:**
    - List of auspicious dates sorted by score
    - Each date includes:
      - Overall score and activity-specific score
      - Verdict (大吉/吉/平)
      - Detailed reasons
      - Weekday information

    **Common Use Cases:**
    - 🎊 Wedding date selection (嫁娶)
    - 🏢 Business opening dates (开业)
    - 🏠 Moving dates (搬家)
    - ✍️ Contract signing dates (签约)
    - ✈️ Travel start dates (出行)

    **Business Value**: Part of $19 date selection service
    """
    try:
        logger.info(f"🔍 Finding auspicious dates for {req.activity} from {req.start_date} to {req.end_date}")

        # Parse dates
        try:
            start_year, start_month, start_day = map(int, req.start_date.split('-'))
            end_year, end_month, end_day = map(int, req.end_date.split('-'))
            start_date_obj = date(start_year, start_month, start_day)
            end_date_obj = date(end_year, end_month, end_day)
        except ValueError:
            raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")

        # Validate date range
        if end_date_obj <= start_date_obj:
            raise HTTPException(status_code=400, detail="end_date must be after start_date")

        if (end_date_obj - start_date_obj).days > 90:
            raise HTTPException(status_code=400, detail="Date range cannot exceed 90 days")

        # Calculate BaZi
        bazi_chart = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            include_dayun=True,
            num_dayun=12
        )

        # Analyze Genju for Yongshen
        genju_analyzer = GenjuAnalyzer(bazi_chart)
        genju_analysis = genju_analyzer.analyze_genju()

        # Find Auspicious Dates
        fortune_analyzer = TimeFortuneAnalyzer(bazi_chart, genju_analysis, None)
        auspicious_dates = fortune_analyzer.find_auspicious_dates(
            start_date_obj,
            end_date_obj,
            req.activity,
            req.min_score
        )

        logger.info(f"✅ Found {len(auspicious_dates)} auspicious dates for {req.activity}")

        return {
            "success": True,
            "data": {
                "activity": req.activity,
                "date_range": {
                    "start": req.start_date,
                    "end": req.end_date
                },
                "min_score": req.min_score,
                "found_count": len(auspicious_dates),
                "auspicious_dates": auspicious_dates[:30]  # Limit to 30 results
            }
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Find auspicious dates error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Find auspicious dates failed: {str(e)}")


# ==================== Chinese Calendar API ====================

@app.get("/api/calendar/today")
def get_today_calendar():
    """
    Get today's Chinese lunar calendar information

    📅 Public endpoint - No API key required

    **Response:**
    - Solar calendar date
    - Lunar calendar date (农历)
    - Gan Zhi (干支) for year, month, day
    - Zodiac animal (生肖)
    - Almanac data (宜忌)
    - Solar term (节气) if applicable
    - Festivals if applicable

    **Example Response:**
    ```json
    {
      "success": true,
      "date": "2025-12-10",
      "weekday": "星期三",
      "solar": {
        "year": 2025,
        "month": 12,
        "day": 10,
        "date_string": "2025年12月10日"
      },
      "lunar": {
        "year": 2025,
        "month": 11,
        "day": 10,
        "year_cn": "乙巳年",
        "month_cn": "冬月",
        "day_cn": "初十",
        "date_string": "乙巳年冬月初十"
      },
      "ganzhi": {
        "year": "乙巳",
        "month": "戊子",
        "day": "癸未"
      },
      "zodiac": {
        "animal": "蛇"
      },
      "almanac": {
        "appropriate": ["祭祀", "祈福", "求嗣"],
        "avoid": ["嫁娶", "动土", "破土"]
      }
    }
    ```
    """
    try:
        logger.info("📅 Fetching today's Chinese calendar")
        result = ChineseCalendarService.get_today_calendar()
        logger.info(f"✅ Today's calendar retrieved: {result['date']}")
        return result
    except Exception as e:
        logger.error(f"❌ Error getting today's calendar: {e}")
        raise HTTPException(status_code=500, detail=f"Calendar service error: {str(e)}")


@app.get("/api/calendar/range")
def get_calendar_range(start: str, end: str):
    """
    Get Chinese lunar calendar information for a date range

    📅 Public endpoint - No API key required

    **Query Parameters:**
    - `start`: Start date in YYYY-MM-DD format
    - `end`: End date in YYYY-MM-DD format

    **Note:** Maximum range is 365 days

    **Response:**
    List of calendar objects for each date in the range

    **Example:**
    ```
    GET /api/calendar/range?start=2025-12-01&end=2025-12-31
    ```
    """
    try:
        logger.info(f"📅 Fetching calendar range: {start} to {end}")

        # Parse dates
        try:
            start_year, start_month, start_day = map(int, start.split('-'))
            end_year, end_month, end_day = map(int, end.split('-'))
            start_date = date(start_year, start_month, start_day)
            end_date = date(end_year, end_month, end_day)
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD"
            )

        # Validate date range
        if end_date < start_date:
            raise HTTPException(
                status_code=400,
                detail="End date must be after start date"
            )

        result = ChineseCalendarService.get_calendar_range(start_date, end_date)
        logger.info(f"✅ Calendar range retrieved: {len(result)} days")
        return {
            "success": True,
            "start_date": start,
            "end_date": end,
            "total_days": len(result),
            "calendars": result
        }

    except HTTPException:
        raise
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Error getting calendar range: {e}")
        raise HTTPException(status_code=500, detail=f"Calendar service error: {str(e)}")


@app.get("/api/calendar/{date_str}")
def get_calendar_by_date(date_str: str):
    """
    Get Chinese lunar calendar information for a specific date

    📅 Public endpoint - No API key required

    **Path Parameter:**
    - `date_str`: Date in YYYY-MM-DD format (e.g., '2025-12-10')

    **Response:**
    Same as `/api/calendar/today` but for the specified date

    **Example:**
    ```
    GET /api/calendar/2025-12-10
    ```
    """
    try:
        logger.info(f"📅 Fetching calendar for: {date_str}")

        # Parse date string
        try:
            year, month, day = map(int, date_str.split('-'))
            target_date = date(year, month, day)
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD (e.g., '2025-12-10')"
            )

        result = ChineseCalendarService.get_calendar_by_date(target_date)
        logger.info(f"✅ Calendar retrieved for: {date_str}")
        return result

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Error getting calendar for {date_str}: {e}")
        raise HTTPException(status_code=500, detail=f"Calendar service error: {str(e)}")


class LunarToSolarRequest(BaseModel):
    """Request model for Lunar to Solar conversion"""
    lunar_year: int = Field(..., description="Lunar year (e.g., 2025)", ge=1900, le=2100, example=2025)
    lunar_month: int = Field(..., description="Lunar month (1-12)", ge=1, le=12, example=1)
    lunar_day: int = Field(..., description="Lunar day (1-30)", ge=1, le=30, example=1)
    is_leap_month: bool = Field(False, description="Whether it's a leap month (闰月)", example=False)


@app.post("/api/calendar/convert-lunar-to-solar")
def convert_lunar_to_solar(request: LunarToSolarRequest):
    """
    Convert Chinese Lunar calendar date to Gregorian (Solar) calendar date

    🌙 农历转公历 | Lunar to Solar Conversion
    📅 Public endpoint - No API key required

    **Request Body:**
    ```json
    {
      "lunar_year": 2025,
      "lunar_month": 1,
      "lunar_day": 1,
      "is_leap_month": false
    }
    ```

    **Response:**
    ```json
    {
      "success": true,
      "solar_date": "2025-01-29",
      "lunar_date": "2025-01-01",
      "solar_date_string": "2025年1月29日",
      "lunar_date_string": "2025年正月初一",
      "weekday": "星期三",
      "ganzhi": "己巳年 丁丑月 甲子日",
      "zodiac": "蛇"
    }
    ```

    **Example:**
    ```bash
    curl -X POST "https://sisithefox-astrologybazi-api.hf.space/api/calendar/convert-lunar-to-solar" \\
      -H "Content-Type: application/json" \\
      -d '{"lunar_year": 2025, "lunar_month": 1, "lunar_day": 1, "is_leap_month": false}'
    ```
    """
    try:
        logger.info(f"🌙 Converting lunar date: {request.lunar_year}-{request.lunar_month}-{request.lunar_day} (leap: {request.is_leap_month})")

        # Create Lunar object
        # Note: For leap month, use negative month value
        month_value = -request.lunar_month if request.is_leap_month else request.lunar_month
        lunar = Lunar.fromYmd(request.lunar_year, month_value, request.lunar_day)

        # Convert to Solar
        solar = lunar.getSolar()

        # Get calendar info for the converted solar date
        target_date = date(solar.getYear(), solar.getMonth(), solar.getDay())
        calendar_info = ChineseCalendarService.get_calendar_by_date(target_date)

        # Build response
        response = {
            "success": True,
            "solar_date": solar.toYmd(),
            "lunar_date": f"{request.lunar_year}-{request.lunar_month:02d}-{request.lunar_day:02d}",
            "solar_date_string": f"{solar.getYear()}年{solar.getMonth()}月{solar.getDay()}日",
            "lunar_date_string": calendar_info.get("lunar", {}).get("date_string", ""),
            "weekday": calendar_info.get("weekday", ""),
            "ganzhi": calendar_info.get("ganzhi", {}).get("full", ""),
            "zodiac": calendar_info.get("zodiac", {}).get("animal", ""),
            "is_leap_month": request.is_leap_month
        }

        logger.info(f"✅ Conversion successful: {response['lunar_date']} -> {response['solar_date']}")
        return response

    except Exception as e:
        logger.error(f"❌ Lunar to solar conversion failed: {e}")
        raise HTTPException(
            status_code=400,
            detail=f"Invalid lunar date or conversion error: {str(e)}"
        )


# ==================== Nine Flying Stars API ====================

class NineStarsRequest(BaseModel):
    """Request model for Nine Flying Stars analysis"""
    date: str = Field(..., description="Target date in YYYY-MM-DD format", example="2025-12-11")
    hour: int = Field(12, description="Hour of the day (0-23), default 12 (noon)", ge=0, le=23)


@app.post("/api/nine-stars/analyze")
def analyze_nine_stars(
    req: NineStarsRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    🌟 Calculate and analyze Nine Flying Stars (九宫飞星)

    🔒 Protected endpoint - Requires API key

    Based on traditional Xuan Kong Feng Shui (玄空风水) theory:
    - 《钦定协纪辨方书》
    - 《紫白赋》
    - 《沈氏玄空学》

    **Parameters:**
    - `date`: Target date in YYYY-MM-DD format (e.g., "2025-12-11")
    - `hour`: Hour of the day (0-23), default 12 (noon)

    **Response:**
    - Yearly/Monthly/Daily/Hourly flying stars (年月日时飞星)
    - Nine palace distribution (九宫分布)
    - Fortune analysis (吉凶分析)
    - Lucky/Unlucky directions (吉凶方位)

    **Example Response:**
    ```json
    {
      "success": true,
      "data": {
        "date": "2025-12-11",
        "hour": 12,
        "yearly_star": {
          "number": 2,
          "name": "二黑巨门星",
          "fortune": "凶"
        },
        "monthly_star": {
          "number": 8,
          "name": "八白左辅星",
          "fortune": "大吉"
        },
        "daily_star": {
          "number": 6,
          "name": "六白武曲星",
          "fortune": "吉"
        },
        "hourly_star": {
          "number": 3,
          "name": "三碧禄存星",
          "fortune": "凶"
        },
        "palace_distribution": {
          "1": 6, "2": 7, "3": 8, "4": 9, "5": 1,
          "6": 2, "7": 3, "8": 4, "9": 5
        },
        "palace_details": {...},
        "fortune_analysis": {
          "overall_score": 6.5,
          "overall_fortune": "吉",
          "description": "运势平稳向上,谨慎行事可获成功。",
          "lucky_directions": ["西北", "南", "东"],
          "avoid_directions": ["西南", "西", "东北"]
        }
      }
    }
    ```
    """
    try:
        logger.info(f"🌟 Analyzing Nine Flying Stars for: {req.date} at hour {req.hour}")

        # Parse and validate date
        try:
            target_date = datetime.strptime(req.date, "%Y-%m-%d").date()
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD (e.g., '2025-12-11')"
            )

        # Validate hour
        if not 0 <= req.hour <= 23:
            raise HTTPException(
                status_code=400,
                detail="Invalid hour. Must be between 0 and 23"
            )

        # Calculate Nine Flying Stars
        result = NineStarsCalculator.get_complete_analysis(target_date, req.hour)

        logger.info(f"✅ Nine Stars analysis successful: {result['fortune_analysis']['overall_fortune']}")
        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Nine Stars calculation error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Nine Stars analysis failed: {str(e)}")


@app.get("/api/nine-stars/today")
def get_today_nine_stars(
    hour: int = 12,
    api_key: str = Depends(verify_api_key)
):
    """
    🌟 Get today's Nine Flying Stars analysis

    🔒 Protected endpoint - Requires API key

    **Query Parameters:**
    - `hour`: Hour of the day (0-23), default 12 (noon)

    **Returns:** Same as `/api/nine-stars/analyze` but for today's date
    """
    try:
        today = datetime.now().date()
        logger.info(f"🌟 Fetching today's Nine Flying Stars at hour {hour}")

        result = NineStarsCalculator.get_complete_analysis(today, hour)

        logger.info(f"✅ Today's Nine Stars retrieved: {result['fortune_analysis']['overall_fortune']}")
        return {
            "success": True,
            "data": result
        }

    except Exception as e:
        logger.error(f"❌ Error getting today's Nine Stars: {e}")
        raise HTTPException(status_code=500, detail=f"Nine Stars service error: {str(e)}")


# ==================== 28 Lunar Mansions API ====================

class Constellation28Request(BaseModel):
    """Request model for 28 Constellations analysis"""
    date: str = Field(..., description="Target date in YYYY-MM-DD format", example="2025-12-11")
    activities: List[str] = Field(
        default=[],
        description="Activities to analyze (e.g., ['嫁娶', '开市', '安葬'])",
        example=["嫁娶", "开市"]
    )


@app.post("/api/28-constellations/analyze")
def analyze_28_constellations(
    req: Constellation28Request,
    api_key: str = Depends(verify_api_key)
):
    """
    ⭐ Analyze 28 Lunar Mansions (二十八星宿) for date selection

    🔒 Protected endpoint - Requires API key

    Based on traditional Chinese astronomy and almanac selection:
    - 《钦定协纪辨方书》
    - 《星宿择日法》

    **Parameters:**
    - `date`: Target date in YYYY-MM-DD format
    - `activities`: List of activities to check (optional)

    **Response:**
    - Current constellation (当日星宿)
    - Four Symbols classification (四象)
    - Fortune level (吉凶等级 1-5)
    - Suitable/Avoid activities (宜忌)
    - Activity-specific recommendations (择日建议)

    **Example Response:**
    ```json
    {
      "success": true,
      "data": {
        "date": "2025-12-11",
        "constellation": {
          "name": "角",
          "pinyin": "Jiao",
          "symbol": "东方青龙",
          "animal": "蛟龙",
          "element": "木",
          "fortune": "吉",
          "level": 4,
          "description": "龙角,主功名、文书、升迁"
        },
        "suitable_activities": ["祭祀", "嫁娶", "纳采", "开市"],
        "avoid_activities": ["安葬", "动土"],
        "fortune_analysis": {
          "recommendation": "今日角宿值日,大吉之日",
          "activity_analysis": [
            {
              "activity": "嫁娶",
              "verdict": "大吉",
              "reason": "角宿宜嫁娶"
            }
          ]
        }
      }
    }
    ```
    """
    try:
        logger.info(f"⭐ Analyzing 28 Constellations for: {req.date}")

        # Parse date
        try:
            target_date = datetime.strptime(req.date, "%Y-%m-%d").date()
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD"
            )

        # Get constellation for the date
        constellation_data = Constellation28Calculator.get_constellation(target_date)

        # Prepare response
        result = {
            "date": str(target_date),
            "constellation": constellation_data,
            "suitable_activities": constellation_data['suitable'],
            "avoid_activities": constellation_data['avoid']
        }

        # Add fortune analysis if activities provided
        if req.activities:
            analysis = Constellation28Calculator.get_fortune_analysis(
                target_date,
                req.activities
            )
            result['fortune_analysis'] = analysis

        logger.info(f"✅ 28 Constellations analysis successful: {constellation_data['name']}宿")
        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ 28 Constellations error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"28 Constellations analysis failed: {str(e)}")


@app.get("/api/28-constellations/today")
def get_today_constellation(api_key: str = Depends(verify_api_key)):
    """
    ⭐ Get today's 28 Lunar Mansions constellation

    🔒 Protected endpoint - Requires API key

    **Returns:** Today's constellation info with suitable/avoid activities
    """
    try:
        today = datetime.now().date()
        logger.info(f"⭐ Fetching today's constellation")

        constellation_data = Constellation28Calculator.get_constellation(today)

        result = {
            "date": str(today),
            "constellation": constellation_data,
            "suitable_activities": constellation_data['suitable'],
            "avoid_activities": constellation_data['avoid']
        }

        logger.info(f"✅ Today's constellation: {constellation_data['name']}宿")
        return {
            "success": True,
            "data": result
        }

    except Exception as e:
        logger.error(f"❌ Error getting today's constellation: {e}")
        raise HTTPException(status_code=500, detail=f"Constellation service error: {str(e)}")


@app.post("/api/28-constellations/find-auspicious")
def find_auspicious_dates(
    activity: str,
    start_date: str,
    end_date: str,
    min_level: int = 3,
    api_key: str = Depends(verify_api_key)
):
    """
    ⭐ Find auspicious dates for specific activity (寻找吉日)

    🔒 Protected endpoint - Requires API key

    **Query Parameters:**
    - `activity`: Activity name (e.g., "嫁娶", "开市", "安葬")
    - `start_date`: Start date (YYYY-MM-DD)
    - `end_date`: End date (YYYY-MM-DD)
    - `min_level`: Minimum fortune level (1-5), default 3

    **Returns:** List of auspicious dates ranked by fortune level

    **Example:**
    ```
    POST /api/28-constellations/find-auspicious
    {
      "activity": "嫁娶",
      "start_date": "2025-12-11",
      "end_date": "2026-01-11",
      "min_level": 4
    }
    ```
    """
    try:
        logger.info(f"⭐ Finding auspicious dates for: {activity}")

        # Parse dates
        try:
            start = datetime.strptime(start_date, "%Y-%m-%d").date()
            end = datetime.strptime(end_date, "%Y-%m-%d").date()
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD"
            )

        # Validate date range
        if end <= start:
            raise HTTPException(
                status_code=400,
                detail="end_date must be after start_date"
            )

        if (end - start).days > 365:
            raise HTTPException(
                status_code=400,
                detail="Date range cannot exceed 1 year"
            )

        # Find auspicious dates
        auspicious_dates = Constellation28Calculator.find_auspicious_dates(
            start, end, activity, min_level
        )

        logger.info(f"✅ Found {len(auspicious_dates)} auspicious dates")
        return {
            "success": True,
            "data": {
                "activity": activity,
                "date_range": {
                    "start": str(start),
                    "end": str(end)
                },
                "min_level": min_level,
                "found_count": len(auspicious_dates),
                "auspicious_dates": auspicious_dates[:30]  # Limit to 30 results
            }
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Error finding auspicious dates: {e}")
        raise HTTPException(status_code=500, detail=f"Auspicious date search failed: {str(e)}")


# ========================================
# 🐉 Zodiac Fortune Endpoints (生肖运程)
# ========================================

from zodiac_fortune_service import ZodiacFortuneService


@app.get("/api/zodiac-fortune/today/{zodiac}")
def get_today_zodiac_fortune(zodiac: str):
    """
    🐉 Get today's fortune for a specific zodiac (今日生肖运程)

    **Path Parameters:**
    - `zodiac`: Chinese zodiac animal (鼠/牛/虎/兔/龙/蛇/马/羊/猴/鸡/狗/猪)

    **Returns:** Complete fortune analysis for today

    **Example:**
    ```bash
    curl https://sisithefox-astrologybazi-api.hf.space/api/zodiac-fortune/today/鼠
    ```

    **Response:**
    ```json
    {
      "success": true,
      "date": "2025-12-11",
      "zodiac": "鼠",
      "overall": {
        "score": 9,
        "description": "今日与值日地支相合,贵人相助,万事顺遂"
      },
      "career": {"score": 9, "advice": "贵人相助,可开展新业务"},
      "wealth": {"score": 8, "advice": "财运亨通,可适当投资"},
      "love": {"score": 9, "advice": "桃花运旺,单身者可主动出击"},
      "health": {"score": 8, "advice": "精力充沛,身体健康"},
      "lucky_hours": [...],
      "lucky_color": ["黑色", "蓝色"],
      "lucky_number": [1, 2]
    }
    ```
    """
    try:
        logger.info(f"🐉 Getting today's fortune for zodiac: {zodiac}")

        today = datetime.now().date()
        fortune = ZodiacFortuneService.generate_daily_fortune(zodiac, today)

        return fortune

    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Error getting zodiac fortune: {e}")
        raise HTTPException(status_code=500, detail=f"Zodiac fortune service error: {str(e)}")


@app.get("/api/zodiac-fortune/{zodiac}/{date_str}")
def get_zodiac_fortune_by_date(zodiac: str, date_str: str):
    """
    🐉 Get fortune for a specific zodiac on a specific date (指定日期生肖运程)

    **Path Parameters:**
    - `zodiac`: Chinese zodiac animal (鼠/牛/虎/兔/龙/蛇/马/羊/猴/鸡/狗/猪)
    - `date_str`: Date in YYYY-MM-DD format

    **Returns:** Complete fortune analysis for the specified date

    **Example:**
    ```bash
    curl https://sisithefox-astrologybazi-api.hf.space/api/zodiac-fortune/龙/2026-01-01
    ```

    **Response:** Same format as `/api/zodiac-fortune/today/{zodiac}`
    """
    try:
        # The generic two-segment route is registered before the historical
        # all-zodiac route. Delegate here so /all/today and /all/YYYY-MM-DD
        # cannot be mistaken for a single zodiac named "all".
        if zodiac == 'all':
            return get_all_zodiac_fortune(date_str)

        logger.info(f"🐉 Getting fortune for {zodiac} on {date_str}")

        # Parse date
        try:
            target_date = datetime.strptime(date_str, "%Y-%m-%d").date()
        except ValueError:
            raise HTTPException(
                status_code=400,
                detail="Invalid date format. Use YYYY-MM-DD"
            )

        fortune = ZodiacFortuneService.generate_daily_fortune(zodiac, target_date)

        return fortune

    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Error getting zodiac fortune: {e}")
        raise HTTPException(status_code=500, detail=f"Zodiac fortune service error: {str(e)}")


@app.get("/api/zodiac-fortune/all/{date_str}")
def get_all_zodiac_fortune(date_str: str):
    """
    🐉 Get fortune for ALL 12 zodiacs on a specific date (12生肖运程)

    **Path Parameters:**
    - `date_str`: Date in YYYY-MM-DD format or "today"

    **Returns:** Fortune for all 12 Chinese zodiacs

    **Example:**
    ```bash
    curl https://sisithefox-astrologybazi-api.hf.space/api/zodiac-fortune/all/2026-01-01
    curl https://sisithefox-astrologybazi-api.hf.space/api/zodiac-fortune/all/today
    ```

    **Response:**
    ```json
    {
      "success": true,
      "date": "2026-01-01",
      "fortunes": {
        "鼠": {...},
        "牛": {...},
        ...
      }
    }
    ```
    """
    try:
        logger.info(f"🐉 Getting fortune for all zodiacs on {date_str}")

        # Parse date
        if date_str.lower() == "today":
            target_date = datetime.now().date()
        else:
            try:
                target_date = datetime.strptime(date_str, "%Y-%m-%d").date()
            except ValueError:
                raise HTTPException(
                    status_code=400,
                    detail="Invalid date format. Use YYYY-MM-DD or 'today'"
                )

        # Generate fortune for all 12 zodiacs
        zodiacs = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪']
        fortunes = {}

        for zodiac in zodiacs:
            try:
                fortunes[zodiac] = ZodiacFortuneService.generate_daily_fortune(zodiac, target_date)
            except Exception as e:
                logger.error(f"Error generating fortune for {zodiac}: {e}")
                fortunes[zodiac] = {"success": False, "error": str(e)}

        return {
            "success": True,
            "date": str(target_date),
            "fortunes": fortunes
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Error getting all zodiac fortunes: {e}")
        raise HTTPException(status_code=500, detail=f"Zodiac fortune service error: {str(e)}")


# ==================== 八字合婚 API ====================

class MarriageCompatibilityRequest(BaseModel):
    """Request model for marriage compatibility analysis (八字合婚)"""
    male_birth_date: str = Field(
        ...,
        description="Male birth date in YYYY-MM-DD format",
        example="1990-01-15"
    )
    male_birth_time: str = Field(
        ...,
        description="Male birth time in HH:MM format",
        example="08:30"
    )
    male_is_lunar: bool = Field(
        default=False,
        description="True if male birth date is lunar calendar"
    )
    female_birth_date: str = Field(
        ...,
        description="Female birth date in YYYY-MM-DD format",
        example="1992-06-20"
    )
    female_birth_time: str = Field(
        ...,
        description="Female birth time in HH:MM format",
        example="14:00"
    )
    female_is_lunar: bool = Field(
        default=False,
        description="True if female birth date is lunar calendar"
    )


@app.post("/api/marriage-compatibility")
@app.post("/marriage-compatibility")
def analyze_marriage_compatibility(
    req: MarriageCompatibilityRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    💍 八字合婚分析 (Marriage Compatibility Analysis)

    基于《男女能結合的奧祕》五要则评分系统:
    1. 八字相似 (25%): 两人八字中相同、相生、相合之字
    2. 禍福相同 (30%): 夫妻命运走向是否一致
    3. 子女同步 (15%): 头胎子女性别是否一致
    4. 年支同氣 (15%): 年支是否同为一方之气
    5. 日干舒配 (15%): 日干五行配合

    🔒 Protected endpoint - Requires API key

    **Request Body:**
    - `male_birth_date`: 男方出生日期 (YYYY-MM-DD)
    - `male_birth_time`: 男方出生时间 (HH:MM)
    - `female_birth_date`: 女方出生日期 (YYYY-MM-DD)
    - `female_birth_time`: 女方出生时间 (HH:MM)

    **Returns:**
    - 总分 (0-100)
    - 评级 (天作之合/上等婚配/中上婚配/中等婚配/中下婚配/下等婚配/不宜婚配)
    - 五要则详细分析
    - 冲克分析
    - 合婚建议

    **Example:**
    ```json
    {
      "male_birth_date": "1990-01-15",
      "male_birth_time": "08:30",
      "female_birth_date": "1992-06-20",
      "female_birth_time": "14:00"
    }
    ```
    """
    try:
        logger.info(f"💍 Marriage compatibility analysis: Male={req.male_birth_date} Female={req.female_birth_date}")

        # 计算男方八字
        male_bazi = BaZiCalculator.calculate_bazi(
            req.male_birth_date,
            req.male_birth_time,
            gender='male',
            is_lunar=req.male_is_lunar
        )

        # 计算女方八字
        female_bazi = BaZiCalculator.calculate_bazi(
            req.female_birth_date,
            req.female_birth_time,
            gender='female',
            is_lunar=req.female_is_lunar
        )

        # 合婚分析
        analyzer = MarriageCompatibilityAnalyzer(male_bazi, female_bazi)
        result = analyzer.analyze_compatibility()

        # 添加八字信息到结果
        result['male_bazi'] = {
            'birth_date': req.male_birth_date,
            'birth_time': req.male_birth_time,
            'bazi_chart': male_bazi.get('bazi_chart', {})
        }
        result['female_bazi'] = {
            'birth_date': req.female_birth_date,
            'birth_time': req.female_birth_time,
            'bazi_chart': female_bazi.get('bazi_chart', {})
        }

        logger.info(f"✅ Marriage compatibility analysis complete: {result['total_score']}分 ({result['rating']})")

        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Marriage compatibility error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Marriage compatibility analysis failed: {str(e)}")


# ==================== Comprehensive Fortune Score API ====================

class FortuneScoreRequest(BaseModel):
    """Request model for comprehensive fortune score (0-120 scoring system)"""
    birth_date: str = Field(
        ...,
        description="Birth date in YYYY-MM-DD format",
        example="1998-03-15"
    )
    birth_time: str = Field(
        ...,
        description="Birth time in HH:MM format",
        example="10:30"
    )
    gender: str = Field(
        ...,
        description="Gender: 'male' or 'female'",
        example="female"
    )
    is_lunar: bool = Field(
        default=False,
        description="True if birth date is lunar calendar"
    )
    target_date: Optional[str] = Field(
        default=None,
        description="Target date for fortune analysis in YYYY-MM-DD format (default: today)",
        example="2026-06-15"
    )
    include_western: bool = Field(
        default=True,
        description="Include Western astrology (Saturn/Jupiter Return) in scoring"
    )


@app.post("/api/fortune-score")
@app.post("/fortune-score")
def calculate_comprehensive_fortune_score(
    req: FortuneScoreRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    🔮 综合运势评分 (Comprehensive Fortune Score)

    算法融合东西方命理系统:
    - 东方 (70%): 八字身财关系 + 大运成败 + 流年吉凶 + 十神组合
    - 西方 (30%): 土星回归 + 木星回归 + 行星过境 + 星体力量

    评分体系:
    - 0-120分综合评分
    - 5级评级:excellent (90+), good (70-89), average (50-69), challenging (30-49), difficult (<30)

    特色功能:
    - ✨ 木星回归(12年周期):机会期加分
    - ⚠️ 土星回归(29.5年周期):压力期减分
    - 🎯 十神组合检测:10种关键吉凶组合
    - 📊 精确身财关系:4步量化法(0-607分)
    """
    try:
        logger.info(f"📊 Fortune score request: {req.birth_date} {req.birth_time} ({req.gender})")

        # 1. Calculate BaZi
        bazi_result = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            num_dayun=12
        )

        # 2. Analyze Genju (格局)
        genju_analyzer = GenjuAnalyzer(bazi_result)
        genju_analysis = genju_analyzer.analyze_genju()

        # 3. Analyze Dayun (大运)
        dayun_analyzer = DayunAnalyzer(bazi_result, genju_analysis)
        dayun_analysis = dayun_analyzer.analyze_all_dayun()

        # 4. Calculate comprehensive fortune score
        calculator = FortuneScoreCalculator(
            bazi_chart=bazi_result,
            genju_analysis=genju_analysis,
            dayun_analysis=dayun_analysis
        )

        # Parse target date
        if req.target_date:
            target = datetime.strptime(req.target_date, '%Y-%m-%d').date()
        else:
            target = datetime.now().date()

        # Calculate score
        result = calculator.calculate_comprehensive_score(
            target_date=target,
            include_western=req.include_western
        )

        logger.info(f"✅ Fortune score complete: {result['total_score']}/120 ({result['rating']})")

        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Fortune score error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Fortune score calculation failed: {str(e)}")


# ============================================================================
# 🌟 RITUALS API - Energy Rituals & Spiritual Practices (2026-01-25)
# ============================================================================

class RitualSearchRequest(BaseModel):
    """仪式搜索请求"""
    query: Optional[str] = Field(None, description="关键词搜索")
    category: Optional[List[str]] = Field(None, description="类别筛选")
    difficulty: Optional[int] = Field(None, ge=1, le=5, description="难度筛选 (1-5)")
    ritual_type: Optional[str] = Field(None, description="类型筛选")
    max_time: Optional[int] = Field(None, description="最大时长(分钟)")
    include_premium: Optional[bool] = Field(True, description="是否包含Premium内容")


class RitualRecommendRequest(BaseModel):
    """仪式推荐请求"""
    user_need: str = Field(..., description="用户需求")
    limit: Optional[int] = Field(5, ge=1, le=20, description="返回数量")
    birth_date: Optional[str] = Field(None, description="出生日期(可选,用于更精准推荐)")
    birth_time: Optional[str] = Field(None, description="出生时间")
    gender: Optional[str] = Field(None, description="性别")


@app.get("/rituals")
@app.get("/api/rituals")  # 备用路由
def get_all_rituals(api_key: str = Depends(verify_api_key)):
    """
    获取所有能量仪式

    Usage:
        GET /rituals
        Headers: X-API-Key: your_api_key

    Returns:
        {
            "success": true,
            "data": {
                "rituals": [...],
                "total": 100,
                "categories": [...],
                "statistics": {...}
            }
        }
    """
    try:
        logger.info("🌟 Fetching all rituals")
        rituals = rituals_service.get_all_rituals()

        logger.info(f"✅ Retrieved {len(rituals)} rituals")
        return {
            "success": True,
            "data": {
                "rituals": rituals,
                "total": len(rituals),
                "categories": rituals_service.get_categories(),
                "statistics": rituals_service.get_statistics()
            }
        }
    except Exception as e:
        logger.error(f"❌ Get all rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch rituals: {str(e)}")


@app.get("/rituals/statistics")
@app.get("/api/rituals/statistics")  # 备用路由
def get_rituals_statistics(api_key: str = Depends(verify_api_key)):
    """
    获取仪式统计信息

    Usage:
        GET /rituals/statistics
        Headers: X-API-Key: your_api_key

    Returns:
        {
            "success": true,
            "data": {
                "total_rituals": 100,
                "by_category": {...},
                "by_difficulty": {...},
                "by_type": {...},
                "average_duration": 30,
                "premium_count": 20,
                "free_count": 80
            }
        }
    """
    try:
        logger.info("🌟 Fetching rituals statistics")

        stats = rituals_service.get_statistics()

        logger.info(f"✅ Retrieved statistics for {stats['totalRituals']} rituals")
        return {
            "success": True,
            "data": stats
        }
    except Exception as e:
        logger.error(f"❌ Get statistics error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch statistics: {str(e)}")


# NOTE: /rituals/statistics is registered above this dynamic route on purpose —
# FastAPI matches routes in registration order, and /rituals/{ritual_id} would
# otherwise greedily swallow "statistics" as a ritual_id and 404.
@app.get("/rituals/{ritual_id}")
@app.get("/api/rituals/{ritual_id}")  # 备用路由
def get_ritual_by_id(ritual_id: str, api_key: str = Depends(verify_api_key)):
    """
    获取单个仪式详情

    Usage:
        GET /rituals/new_moon_abundance
        Headers: X-API-Key: your_api_key

    Parameters:
        ritual_id: 仪式ID

    Returns:
        {
            "success": true,
            "data": {
                "ritual": {...},
                "related_rituals": [...]
            }
        }
    """
    try:
        logger.info(f"🌟 Fetching ritual: {ritual_id}")
        ritual = rituals_service.get_ritual_by_id(ritual_id)

        if not ritual:
            raise HTTPException(status_code=404, detail=f"Ritual not found: {ritual_id}")

        # 获取相关仪式
        related = rituals_service.get_related_rituals(ritual_id, limit=5)

        logger.info(f"✅ Retrieved ritual: {ritual.get('title_en')}")
        return {
            "success": True,
            "data": {
                "ritual": ritual,
                "related_rituals": related
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Get ritual error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch ritual: {str(e)}")


@app.post("/rituals/search")
@app.post("/api/rituals/search")  # 备用路由
def search_rituals(request: RitualSearchRequest, api_key: str = Depends(verify_api_key)):
    """
    搜索仪式

    Usage:
        POST /rituals/search
        Headers: X-API-Key: your_api_key
        Body: {
            "query": "abundance",
            "category": ["prosperity", "healing"],
            "difficulty": 3,
            "max_time": 30,
            "include_premium": true
        }

    Parameters:
        query: 关键词搜索(可选)
        category: 类别筛选(可选)
        difficulty: 难度筛选 1-5(可选)
        ritual_type: 类型筛选(可选)
        max_time: 最大时长分钟数(可选)
        include_premium: 是否包含Premium内容(默认true)

    Returns:
        {
            "success": true,
            "data": {
                "rituals": [...],
                "total": 25,
                "filters_applied": {...}
            }
        }
    """
    try:
        logger.info(f"🌟 Searching rituals with query: {request.query}")

        rituals = rituals_service.search_rituals(
            query=request.query,
            category=request.category,
            difficulty=request.difficulty,
            ritual_type=request.ritual_type,
            max_time=request.max_time,
            include_premium=request.include_premium
        )

        logger.info(f"✅ Found {len(rituals)} matching rituals")
        return {
            "success": True,
            "data": {
                "rituals": rituals,
                "total": len(rituals),
                "filters_applied": {
                    "query": request.query,
                    "category": request.category,
                    "difficulty": request.difficulty,
                    "ritual_type": request.ritual_type,
                    "max_time": request.max_time,
                    "include_premium": request.include_premium
                }
            }
        }
    except Exception as e:
        logger.error(f"❌ Search rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to search rituals: {str(e)}")


@app.get("/rituals/category/{category}")
@app.get("/api/rituals/category/{category}")  # 备用路由
def get_rituals_by_category(category: str, api_key: str = Depends(verify_api_key)):
    """
    按类别获取仪式

    Usage:
        GET /rituals/category/prosperity
        Headers: X-API-Key: your_api_key

    Parameters:
        category: 类别名称(prosperity, healing, protection, love, career, spiritual_growth)

    Returns:
        {
            "success": true,
            "data": {
                "category": "prosperity",
                "rituals": [...],
                "total": 15
            }
        }
    """
    try:
        logger.info(f"🌟 Fetching rituals for category: {category}")

        rituals = rituals_service.search_rituals(category=[category])

        logger.info(f"✅ Found {len(rituals)} rituals in category: {category}")
        return {
            "success": True,
            "data": {
                "category": category,
                "rituals": rituals,
                "total": len(rituals)
            }
        }
    except Exception as e:
        logger.error(f"❌ Get category rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch category rituals: {str(e)}")


@app.get("/rituals/moon-phase/current")
@app.get("/api/rituals/moon-phase/current")  # 备用路由
def get_current_moon_phase_rituals(api_key: str = Depends(verify_api_key)):
    """
    获取当前月相适合的仪式

    Usage:
        GET /rituals/moon-phase/current
        Headers: X-API-Key: your_api_key

    Returns:
        {
            "success": true,
            "data": {
                "moon_phase": "waxing_crescent",
                "rituals": [...],
                "total": 8,
                "description": "适合显化和吸引的时期"
            }
        }
    """
    try:
        logger.info("🌟 Fetching current moon phase rituals")

        result = rituals_service.get_current_moon_phase_rituals()

        logger.info(f"✅ Retrieved moon phase rituals: {result['currentPhase']}")
        return {
            "success": True,
            "data": result
        }
    except Exception as e:
        logger.error(f"❌ Get moon phase rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch moon phase rituals: {str(e)}")


@app.post("/rituals/recommend")
@app.post("/api/rituals/recommend")  # 备用路由
def recommend_rituals(request: RitualRecommendRequest, api_key: str = Depends(verify_api_key)):
    """
    个性化仪式推荐

    Usage:
        POST /rituals/recommend
        Headers: X-API-Key: your_api_key
        Body: {
            "user_need": "I need more abundance in my life",
            "limit": 5,
            "birth_date": "1990-05-15",
            "birth_time": "14:30",
            "gender": "female"
        }

    Parameters:
        user_need: 用户需求描述(必填)
        limit: 返回数量 1-20(默认5)
        birth_date: 出生日期(可选,用于更精准推荐)
        birth_time: 出生时间(可选)
        gender: 性别(可选)

    Returns:
        {
            "success": true,
            "data": {
                "recommendations": [...],
                "total": 5,
                "user_need": "...",
                "personalized": true
            }
        }
    """
    try:
        logger.info(f"🌟 Recommending rituals for need: {request.user_need}")

        recommendations = rituals_service.recommend_rituals(
            user_need=request.user_need,
            limit=request.limit,
            birth_date=request.birth_date,
            birth_time=request.birth_time,
            gender=request.gender
        )

        is_personalized = bool(request.birth_date)

        logger.info(f"✅ Generated {len(recommendations)} recommendations (personalized: {is_personalized})")
        return {
            "success": True,
            "data": {
                "recommendations": recommendations,
                "total": len(recommendations),
                "user_need": request.user_need,
                "personalized": is_personalized
            }
        }
    except Exception as e:
        logger.error(f"❌ Recommend rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to recommend rituals: {str(e)}")


@app.get("/rituals/related/{ritual_id}")
@app.get("/api/rituals/related/{ritual_id}")  # 备用路由
def get_related_rituals(ritual_id: str, limit: int = 5, api_key: str = Depends(verify_api_key)):
    """
    获取相关仪式

    Usage:
        GET /rituals/related/new_moon_abundance?limit=5
        Headers: X-API-Key: your_api_key

    Parameters:
        ritual_id: 仪式ID
        limit: 返回数量(默认5)

    Returns:
        {
            "success": true,
            "data": {
                "ritual_id": "new_moon_abundance",
                "related_rituals": [...],
                "total": 5
            }
        }
    """
    try:
        logger.info(f"🌟 Fetching related rituals for: {ritual_id}")

        related = rituals_service.get_related_rituals(ritual_id, limit=limit)

        logger.info(f"✅ Found {len(related)} related rituals")
        return {
            "success": True,
            "data": {
                "ritual_id": ritual_id,
                "related_rituals": related,
                "total": len(related)
            }
        }
    except Exception as e:
        logger.error(f"❌ Get related rituals error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Failed to fetch related rituals: {str(e)}")


# ============================================================================
# 🖐️ BODY READING API - Five Elements Body/Hand/Face Prediction (2026-02-16)
# ============================================================================

class BodyReadingRequest(BaseModel):
    """体相分析请求"""
    birth_date: str = Field(..., description="出生日期 YYYY-MM-DD", example="1990-01-15")
    birth_time: str = Field(default="12:00", description="出生时间 HH:MM", example="08:30")
    gender: str = Field(default="male", description="性别 male/female")
    is_lunar: bool = Field(default=False, description="是否农历")


@app.post("/analyze-body-reading")
@app.post("/api/analyze-body-reading")
def analyze_body_reading_endpoint(
    req: BodyReadingRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    🖐️ 五行体相预测 (Five Elements Body Reading Prediction)

    基于BaZi五行规则系统,预测手型/面相/体型/性格/健康
    纯规则匹配(免费),不调用AI

    用途:作为手相/面相付费分析的免费钩子
    - 免费:五行体相预测(本API)
    - 付费:AI实拍掌纹/面相分析(vision AI)
    """
    try:
        logger.info(f"🖐️ Body reading request: {req.birth_date} {req.birth_time} ({req.gender})")

        # 1. Calculate BaZi
        bazi_result = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            num_dayun=3
        )

        # 2. Analyze body reading
        result = analyze_body_reading(bazi_result)

        # 3. Add BaZi summary info
        chart = bazi_result.get('bazi_chart', {})
        result['bazi_summary'] = {
            'four_pillars': {
                'year': {'stem': chart.get('year', {}).get('gan', ''), 'branch': chart.get('year', {}).get('zhi', '')},
                'month': {'stem': chart.get('month', {}).get('gan', ''), 'branch': chart.get('month', {}).get('zhi', '')},
                'day': {'stem': chart.get('day', {}).get('gan', ''), 'branch': chart.get('day', {}).get('zhi', '')},
                'hour': {'stem': chart.get('hour', {}).get('gan', ''), 'branch': chart.get('hour', {}).get('zhi', '')},
            },
            'day_master': bazi_result.get('day_master', {}),
        }

        logger.info(f"✅ Body reading complete: dominant={result['dominant_element']}")

        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Body reading error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Body reading analysis failed: {str(e)}")


# ============================================================================
# 💕 LOVE CLARITY - Marriage Destiny Analysis (2026-02-28)
# ============================================================================

class MarriageDestinyRequest(BaseModel):
    """Love Clarity 婚姻命运分析请求"""
    birth_date: str = Field(
        ...,
        description="Birth date in YYYY-MM-DD format",
        example="1992-08-15"
    )
    birth_time: str = Field(
        ...,
        description="Birth time in HH:MM format",
        example="14:30"
    )
    gender: str = Field(
        default="female",
        description="Gender: 'male' or 'female'",
        example="female"
    )
    is_lunar: bool = Field(
        default=False,
        description="True if birth date is lunar calendar"
    )
    relationship_status: Optional[str] = Field(
        default=None,
        description="Current relationship status for context",
        example="cold_war"
    )
    children_status: Optional[str] = Field(
        default=None,
        description="Children status for enhanced analysis",
        example="teenager"
    )


@app.post("/analyze-marriage-destiny")
@app.post("/api/analyze-marriage-destiny")
def analyze_marriage_destiny(
    req: MarriageDestinyRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    💕 Love & Marriage Clarity 婚姻命运深度分析

    为 Love Clarity 产品 ($39/$99) 提供规则化命理数据,包含:
    1. 日刃格检测 — 6个极端风险日柱
    2. 增强桃花分析 — 5种类型(正桃花/野桃花/桃花劫/桃花煞/滚浪桃花)
    3. 暗克分析 — 婚姻宫隐性冲克
    4. 子女星分析 — 食伤星对婚姻影响
    5. 2026流年影响 — 丙午年婚姻冲击
    6. 冷读钩子 — 3个情绪共鸣锚点
    + 完整的传统婚姻分析(配偶星、婚姻宫、100分评分、女命规则等)

    🔒 Protected endpoint - Requires API key
    """
    try:
        logger.info(f"💕 Marriage destiny analysis: {req.birth_date} {req.birth_time} ({req.gender})")

        # 1. Calculate complete BaZi with dayun
        bazi_result = BaZiCalculator.calculate_bazi_complete(
            birth_date=req.birth_date,
            birth_time=req.birth_time,
            gender=req.gender,
            is_lunar=req.is_lunar,
            num_dayun=8  # 80年大运
        )

        # 2. Traditional marriage analysis
        marriage_analyzer = MarriageAnalyzer(bazi_result)
        traditional_analysis = marriage_analyzer.analyze_marriage()

        # 3. Love Clarity enhanced analysis
        clarity_analyzer = LoveClarityAnalyzer(bazi_result)
        enhanced_analysis = clarity_analyzer.analyze_all()

        # 4. BaZi chart summary
        chart = bazi_result.get('bazi_chart', {})
        bazi_summary = {
            'four_pillars': {
                'year': {'stem': chart.get('year', {}).get('gan', ''), 'branch': chart.get('year', {}).get('zhi', '')},
                'month': {'stem': chart.get('month', {}).get('gan', ''), 'branch': chart.get('month', {}).get('zhi', '')},
                'day': {'stem': chart.get('day', {}).get('gan', ''), 'branch': chart.get('day', {}).get('zhi', '')},
                'hour': {'stem': chart.get('hour', {}).get('gan', ''), 'branch': chart.get('hour', {}).get('zhi', '')},
            },
            'day_master': bazi_result.get('day_master', {}),
            'ten_gods': bazi_result.get('ten_gods', {}),
        }

        # 5. Combine all data
        result = {
            # Enhanced (new)
            'day_blade_check': enhanced_analysis['day_blade_check'],
            'peach_blossom_analysis': enhanced_analysis['peach_blossom_analysis'],
            'hidden_clash_analysis': enhanced_analysis['hidden_clash_analysis'],
            'children_star_analysis': enhanced_analysis['children_star_analysis'],
            'annual_impact_2026': enhanced_analysis['annual_impact_2026'],
            'cold_reading_hooks': enhanced_analysis['cold_reading_hooks'],
            # Traditional
            'spouse_star_analysis': traditional_analysis['spouse_star_analysis'],
            'marriage_palace_analysis': traditional_analysis['marriage_palace_analysis'],
            'spouse_characteristics': traditional_analysis['spouse_characteristics'],
            'marriage_quality': traditional_analysis['marriage_quality'],
            'marriage_palace_score': traditional_analysis['marriage_palace_score'],
            'marriage_fortune_years': traditional_analysis['marriage_fortune_years'],
            'marriage_advice': traditional_analysis['marriage_advice'],
            'female_marriage_rules': traditional_analysis.get('female_marriage_rules', []),
            'spouse_direction': traditional_analysis.get('spouse_direction', {}),
            # Context
            'bazi_summary': bazi_summary,
            'user_context': {
                'relationship_status': req.relationship_status,
                'children_status': req.children_status,
            }
        }

        logger.info(
            f"✅ Marriage destiny complete: "
            f"palace_score={traditional_analysis['marriage_palace_score'].get('score', 'N/A')}, "
            f"day_blade={enhanced_analysis['day_blade_check']['detected']}, "
            f"peach_blossom={enhanced_analysis['peach_blossom_analysis']['has_peach_blossom']}"
        )

        return {
            "success": True,
            "data": result
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Marriage destiny analysis error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Marriage destiny analysis failed: {str(e)}")


# ==================== Qimen Dunjia (奇门遁甲) ====================

class QimenChartRequest(BaseModel):
    """Request model for Qimen Dunjia chart calculation"""
    date: str = Field(..., description="Date in YYYY-MM-DD format", example="2026-04-14")
    hour: int = Field(..., description="Hour of the day (0-23)", ge=0, le=23, example=10)
    minute: int = Field(0, description="Minute (0-59)", ge=0, le=59, example=30)
    purpose: str = Field(
        "综合",
        description="Purpose: 事业/财运/感情/健康/学业/诉讼/出行/综合",
        example="事业"
    )


@app.post("/api/qimen/chart")
@app.post("/qimen-chart")
def calculate_qimen_chart(
    req: QimenChartRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    🌀 Calculate Qimen Dunjia Chart (奇门遁甲排盘)

    🔒 Protected endpoint - Requires API key

    Time-based Rotating Plate Method (时家转盘奇门) with:
    - Complete nine-palace grid (九宫格) with earth/sky stems
    - Nine Stars (九星), Eight Doors (八门), Eight Gods (八神)
    - Void palaces (旬空), Post Horse Star (驿马)
    - Zhi Fu (值符) and Zhi Shi (值使) positions
    - Fortune analysis per palace and overall recommendations

    **Parameters:**
    - `date`: Solar calendar date (YYYY-MM-DD)
    - `hour`: Hour (0-23)
    - `minute`: Minute (0-59), default 0
    - `purpose`: 事业/财运/感情/健康/学业/诉讼/出行/综合
    """
    try:
        logger.info(f"🌀 Calculating Qimen chart for: {req.date} {req.hour}:{req.minute:02d}, purpose={req.purpose}")

        # Parse date
        try:
            parts = req.date.split("-")
            year, month, day = int(parts[0]), int(parts[1]), int(parts[2])
        except (ValueError, IndexError):
            raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")

        result = QimenCalculator.calculate(
            year=year, month=month, day=day,
            hour=req.hour, minute=req.minute,
            purpose=req.purpose
        )

        logger.info(
            f"✅ Qimen chart: {result['chart_info']['full_name']}, "
            f"值符={result['zhi_fu']['star']}→{result['zhi_fu']['palace_name']}, "
            f"overall={result['analysis']['overall_fortune']}"
        )

        return {
            "success": True,
            "data": result,
            "timestamp": datetime.now().isoformat()
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Qimen calculation error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Qimen chart calculation failed: {str(e)}")


@app.get("/api/qimen/now")
@app.get("/qimen-now")
def get_qimen_now(
    purpose: str = "综合",
    api_key: str = Depends(verify_api_key)
):
    """
    🌀 Get Qimen Dunjia chart for current moment (奇门遁甲 - 当前时刻)

    🔒 Protected endpoint - Requires API key

    **Query Parameters:**
    - `purpose`: 事业/财运/感情/健康/学业/诉讼/出行/综合

    **Returns:** Same as `/api/qimen/chart` but for the current moment
    """
    try:
        logger.info(f"🌀 Calculating Qimen chart for NOW, purpose={purpose}")

        result = QimenCalculator.calculate_now(purpose=purpose)

        logger.info(f"✅ Qimen NOW: {result['chart_info']['full_name']}, overall={result['analysis']['overall_fortune']}")

        return {
            "success": True,
            "data": result,
            "timestamp": datetime.now().isoformat()
        }

    except Exception as e:
        logger.error(f"❌ Qimen NOW error: {e}")
        import traceback
        logger.error(traceback.format_exc())
        raise HTTPException(status_code=500, detail=f"Qimen calculation failed: {str(e)}")


class LiuYaoRequest(BaseModel):
    question: str = Field("", description="占卜问题", example="我的事业发展如何?")
    purpose: str = Field("综合", description="Purpose: 财运/感情/事业/健康/学业/出行/综合", example="事业")
    coins: Optional[List[int]] = Field(
        None,
        description="手动起卦:6个整数(每爻阳面铜钱数,由下到上),每个值0-3。不传则自动起卦",
        example=[3, 2, 1, 2, 3, 1]
    )

    @field_validator("coins")
    @classmethod
    def validate_coins(cls, v: Optional[List[int]]) -> Optional[List[int]]:
        if v is None:
            return v
        if len(v) != 6:
            raise ValueError("coins must have exactly 6 elements (one per line, bottom to top)")
        if not all(0 <= c <= 3 for c in v):
            raise ValueError("each coin value must be 0–3 (count of yang-face coins)")
        return v


def _liuyao_response(coins, question, purpose) -> dict:
    result = calculate_liuyao(coins=coins, question=question, purpose=purpose)
    logger.info(
        f"✅ Liu Yao: 本卦={result['origin_hexagram']['name']}, "
        f"变卦={result['variant_hexagram']['name'] if result['variant_hexagram'] else '无'}, "
        f"动爻={result['changed_positions']}"
    )
    return {"success": True, "data": result, "timestamp": datetime.now().isoformat()}


@app.post("/api/liuyao/divine")
@app.post("/liuyao-divine")
def divine_liuyao(
    req: LiuYaoRequest,
    api_key: str = Depends(verify_api_key)
):
    """
    🔮 Liu Yao (六爻) Divination Chart — 🔒 Requires API key

    Returns full chart: 本卦/变卦, 六爻(六亲/六神/地支/五行), 世应, 动爻, 空亡, 卦身/伏神.
    """
    try:
        logger.info(f"🔮 Liu Yao: purpose={req.purpose}, manual_coins={req.coins is not None}")
        return _liuyao_response(req.coins, req.question, req.purpose)
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Liu Yao error: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Liu Yao divination failed: {str(e)}")


@app.get("/api/liuyao/now")
@app.get("/liuyao-now")
def get_liuyao_now(
    purpose: str = "综合",
    question: str = "",
    api_key: str = Depends(verify_api_key)
):
    """🔮 Liu Yao for current moment — 🔒 Requires API key"""
    try:
        logger.info(f"🔮 Liu Yao NOW: purpose={purpose}")
        return _liuyao_response(None, question, purpose)
    except Exception as e:
        logger.error(f"❌ Liu Yao NOW error: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Liu Yao calculation failed: {str(e)}")


# ==================== Fengshui (八宅法) ====================

from fengshui_calculator import FengshuiCalculator

_fengshui = FengshuiCalculator()


class FengshuiHomeRequest(BaseModel):
    facing_direction: str = Field(..., description="大门朝向,如:南/北/东/西/东南/东北/西南/西北")
    birth_year: Optional[int] = Field(None, description="主要居住者出生年份(可选)")
    gender: Optional[str] = Field(None, description="性别:male/female 或 男/女(可选)")


class FengshuiMingGuaRequest(BaseModel):
    birth_year: int = Field(..., description="出生年份(公历)")
    gender: str = Field(..., description="性别:male/female 或 男/女")


@app.post("/api/fengshui/home-analysis")
@app.post("/fengshui-home-analysis")
def fengshui_home_analysis(
    req: FengshuiHomeRequest,
    api_key: str = Depends(verify_api_key)
):
    """🏠 八宅法风水分析:宅卦+八方吉凶+房间建议+命宅匹配 — 🔒 Requires API key"""
    try:
        logger.info(f"🏠 Fengshui: facing={req.facing_direction}, birth_year={req.birth_year}")
        result = _fengshui.full_analysis(
            facing_direction=req.facing_direction,
            occupant_birth_year=req.birth_year,
            occupant_gender=req.gender,
        )
        return {"success": True, "data": result}
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Fengshui home analysis error: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Fengshui analysis failed: {str(e)}")


@app.post("/api/fengshui/ming-gua")
@app.post("/fengshui-ming-gua")
def fengshui_ming_gua(
    req: FengshuiMingGuaRequest,
    api_key: str = Depends(verify_api_key)
):
    """🔮 计算个人命卦(八宅法)— 🔒 Requires API key"""
    try:
        ming = _fengshui.calculate_ming_gua(req.birth_year, req.gender)
        personal_dirs = _fengshui.get_personal_auspicious_directions(ming['gua_num'])
        return {"success": True, "data": {"ming_gua": ming, "personal_directions": personal_dirs}}
    except Exception as e:
        logger.error(f"❌ Ming gua error: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Ming gua calculation failed: {str(e)}")


@app.get("/api/fengshui/zhai-gua/{facing}")
def fengshui_zhai_gua(
    facing: str,
    api_key: str = Depends(verify_api_key)
):
    """🏡 根据大门朝向查询宅卦+八方吉凶 — 🔒 Requires API key"""
    try:
        zhai = _fengshui.get_zhai_gua(facing)
        directions = _fengshui.get_eight_directions_fortune(zhai['gua_num'])
        rooms = _fengshui.get_room_recommendations(zhai['gua_num'])
        return {"success": True, "data": {"zhai_gua": zhai, "eight_directions": directions, "room_recommendations": rooms}}
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"❌ Zhai gua error: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Zhai gua lookup failed: {str(e)}")


if __name__ == "__main__":
    import uvicorn
    import sys

    # Check for port argument
    port = 7860  # Default for Hugging Face Spaces
    if len(sys.argv) > 1 and sys.argv[1] == "--port" and len(sys.argv) > 2:
        port = int(sys.argv[2])

    print(f"🚀 Starting server on port {port}")
    uvicorn.run(app, host="0.0.0.0", port=port)