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

load_dotenv()

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)
 
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
async_client = AsyncGroq(api_key=os.environ.get("GROQ_API_KEY"))

# MongoDB Setup
MONGODB_URI = os.environ.get("MONGODB_URI")
if not MONGODB_URI:
    raise ValueError("MONGODB_URI environment variable is missing from .env")
db_client = AsyncIOMotorClient(MONGODB_URI, tlsCAFile=certifi.where())
db = db_client.get_database("naaviagent")
profiles_collection = db.profiles
pending_paths_collection = db.pending_paths
published_paths_collection = db.published_paths
generation_history_collection = db.generation_history
admin_feedbacks_collection = db.admin_feedbacks
marketplace_feedback_collection = db.marketplace_feedback

@app.on_event("startup")
async def startup_db_init():
    try:
        existing_cols = await db.list_collection_names()
        for col in ["profiles", "pending_paths", "published_paths", "generation_history", "admin_feedbacks", "marketplace_feedback"]:
            if col not in existing_cols:
                await db.create_collection(col)
                print(f"[MongoDB] Created collection '{col}' successfully.")
    except Exception as e:
        print(f"[MongoDB Init Warning] Could not pre-create collections: {e}")

# Models
class PathGenerationRequest(BaseModel):
    current_position: str
    target_goal: str
    profile: Optional[dict] = None
    degree_type: Optional[str] = None
    refine_prompt: Optional[str] = None
    existing_roadmap: Optional[dict] = None
    focus: Optional[str] = None
    content_category: Optional[str] = None
    sub_segment: Optional[str] = None


class PathAuditRequest(BaseModel):
    blueprint: dict
    current_position: str
    target_goal: str
    profile: Optional[dict] = None

class GoalRequest(BaseModel):
    # Backward compatibility
    goal: str

class UpdatePathRequest(BaseModel):
    roadmap_data: dict
    status: str = "published"
    edited_by: Optional[str] = None
    feedback_text: Optional[str] = None
    feedback_category: Optional[str] = "general"

class AdminStepRequest(BaseModel):
    path_id: str
    step_id: Optional[int] = None
    step: Optional[dict] = None
    insert_index: Optional[int] = None
    alternative_index: Optional[int] = None
    edited_by: Optional[str] = None
    instruction: Optional[str] = None


class LoginRequest(BaseModel):
    email: str
    password: str

class SavePathRequest(BaseModel):
    current_position: str
    target_goal: str
    profile: Optional[dict] = None
    roadmap_data: dict
    generation_id: Optional[str] = None
    alternative_name: Optional[str] = None

class PathScoreRequest(BaseModel):
    roadmap_data: dict
    profile: Optional[dict] = None
    current_position: Optional[str] = ""

class MarketplaceFeedbackRequest(BaseModel):
    student_email: str
    path_id: str
    path_name: str
    step_id: int
    step_title: str
    provider_name: str
    provider_type: str
    action: str

class FeedbackCreateRequest(BaseModel):
    admin_email: str
    target_goal: str
    student_profile: Optional[dict] = None
    feedback_text: str
    category: str = "general"
    path_id: Optional[str] = None

def serialize_mongo_doc(doc):
    if not doc:
        return None
    doc["id"] = str(doc["_id"])
    del doc["_id"]
    for field in ["created_at", "updated_at", "published_at", "timestamp"]:
        if field in doc and doc[field]:
            if hasattr(doc[field], "isoformat"):
                val = doc[field].isoformat()
                if not val.endswith("Z") and "+00:00" not in val:
                    val += "Z"
                doc[field] = val
    if "profile" in doc and isinstance(doc["profile"], dict):
        profile_dict = doc["profile"]
        for field in ["created_at", "updated_at"]:
            if field in profile_dict and profile_dict[field]:
                if hasattr(profile_dict[field], "isoformat"):
                    val = profile_dict[field].isoformat()
                    if not val.endswith("Z") and "+00:00" not in val:
                        val += "Z"
                    profile_dict[field] = val
    if "modifications" in doc and isinstance(doc["modifications"], list):
        for mod in doc["modifications"]:
            if "timestamp" in mod and mod["timestamp"]:
                if hasattr(mod["timestamp"], "isoformat"):
                    val = mod["timestamp"].isoformat()
                    if not val.endswith("Z") and "+00:00" not in val:
                        val += "Z"
                    mod["timestamp"] = val
    return doc


def compute_roadmap_diff(old_roadmap: dict, new_roadmap: dict) -> list:
    changes = []
    if not old_roadmap or not new_roadmap:
        return changes

    # 1. Global fields comparison
    global_fields = ["path_title", "path_description", "readiness_score", "readiness_label", "total_duration"]
    for field in global_fields:
        old_val = old_roadmap.get(field)
        new_val = new_roadmap.get(field)
        if old_val != new_val:
            changes.append({
                "field": field,
                "old_value": old_val,
                "new_value": new_val
            })

    # Blind spots comparison
    old_blinds = old_roadmap.get("blind_spots") or []
    new_blinds = new_roadmap.get("blind_spots") or []
    if old_blinds != new_blinds:
        changes.append({
            "field": "blind_spots",
            "old_value": old_blinds,
            "new_value": new_blinds
        })

    # 2. Milestone comparison
    old_steps = old_roadmap.get("steps") or []
    new_steps = new_roadmap.get("steps") or []

    old_steps_dict = {step.get("id"): step for step in old_steps if step.get("id") is not None}
    new_steps_dict = {step.get("id"): step for step in new_steps if step.get("id") is not None}

    # Added milestones
    for step_id, new_step in new_steps_dict.items():
        if step_id not in old_steps_dict:
            changes.append({
                "field": f"steps.add.{step_id}",
                "old_value": None,
                "new_value": new_step.get("title") or f"Step {step_id}"
            })

    # Deleted milestones
    for step_id, old_step in old_steps_dict.items():
        if step_id not in new_steps_dict:
            changes.append({
                "field": f"steps.delete.{step_id}",
                "old_value": old_step.get("title") or f"Step {step_id}",
                "new_value": None
            })

    # Modified milestones
    for step_id, new_step in new_steps_dict.items():
        if step_id in old_steps_dict:
            old_step = old_steps_dict[step_id]
            step_fields = ["title", "duration", "description", "macro_view", "micro_view", "nano_view"]
            for field in step_fields:
                old_val = old_step.get(field)
                new_val = new_step.get(field)
                if old_val != new_val:
                    changes.append({
                        "field": f"steps.{step_id}.{field}",
                        "old_value": old_val,
                        "new_value": new_val
                    })

            # Learning objectives comparison
            old_lo = old_step.get("learning_objectives") or []
            new_lo = new_step.get("learning_objectives") or []
            if old_lo != new_lo:
                changes.append({
                    "field": f"steps.{step_id}.learning_objectives",
                    "old_value": old_lo,
                    "new_value": new_lo
                })

            # Micro steps comparison
            old_ms = old_step.get("micro_steps") or []
            new_ms = new_step.get("micro_steps") or []
            if old_ms != new_ms:
                changes.append({
                    "field": f"steps.{step_id}.micro_steps",
                    "old_value": old_ms,
                    "new_value": new_ms
                })

            # Marketplace comparison
            old_market = combined_marketplace_from_step(old_step)
            new_market = combined_marketplace_from_step(new_step)
            for cat in ["mentors", "vendors", "institutions", "distributors"]:
                old_cat_list = old_market.get(cat) or []
                new_cat_list = new_market.get(cat) or []
                if old_cat_list != new_cat_list:
                    changes.append({
                        "field": f"steps.{step_id}.marketplace.{cat}",
                        "old_value": old_cat_list,
                        "new_value": new_cat_list
                    })

    return changes


def normalize_path_doc_roadmap(doc: dict) -> dict:
    if isinstance(doc, dict) and isinstance(doc.get("roadmap_data"), dict):
        doc["roadmap_data"] = normalize_roadmap_marketplaces_for_storage(doc["roadmap_data"])
    if isinstance(doc, dict) and isinstance(doc.get("original_roadmap_data"), dict):
        doc["original_roadmap_data"] = normalize_roadmap_marketplaces_for_storage(doc["original_roadmap_data"])
    return doc


def summarize_roadmap_changes(changes: list) -> str:
    if not changes:
        return "No changes detected."
    summaries = []
    for c in changes:
        f = c["field"]
        if f == "path_title":
            summaries.append(f"Changed path title to '{c['new_value']}'")
        elif f == "path_description":
            summaries.append("Updated path description")
        elif f == "readiness_score":
            summaries.append(f"Adjusted readiness score to {c['new_value']}%")
        elif f == "readiness_label":
            summaries.append(f"Changed readiness label to '{c['new_value']}'")
        elif f == "total_duration":
            summaries.append(f"Changed total duration to '{c['new_value']}'")
        elif f.startswith("steps.add."):
            summaries.append(f"Added Step: '{c['new_value']}'")
        elif f.startswith("steps.delete."):
            summaries.append(f"Removed Step: '{c['old_value']}'")
        elif f.startswith("steps."):
            parts = f.split(".")
            step_id = parts[1]
            subfield = parts[2]
            if len(parts) > 3:
                subfield = " ".join(parts[2:])
            summaries.append(f"Updated Step {step_id} {subfield.replace('_', ' ')}")
            
    return "; ".join(summaries[:4]) + ("..." if len(summaries) > 4 else "")


async def enrich_path_profile(doc):
    if not doc:
        return doc
    email = None
    if "profile" in doc and doc["profile"] and "email" in doc["profile"]:
        email = doc["profile"]["email"]
    if not email and "created_by" in doc and doc["created_by"]:
        email = doc["created_by"]
    if not email and "createdBy" in doc and doc["createdBy"]:
        email = doc["createdBy"]
        
    if email:
        profile_doc = await profiles_collection.find_one({"email": email.lower()})
        if profile_doc:
            doc["profile"] = serialize_mongo_doc(profile_doc)
            doc["created_by"] = email.lower()
            doc["createdBy"] = email.lower()
            
    if "profile" not in doc or not doc["profile"]:
        doc["profile"] = {"email": email or "", "name": "Anonymous Student"}
        
    return doc


# ─── AGENT 1: BLUEPRINT GENERATOR PROMPT ──────────────────────────────────
AGENT_1_PROMPT = """You are the Naaviverse pathway blueprint generator (Agent 1).
Your task is to draft the initial raw pathway based on the selected content segment:
- Current Position: {current_position}
- Target Goal / Support Need: {target_goal}
- Student Profile Details: {profile}
- Degree Type: {degree_type}
- Selected Segment Focus: {focus_area}

Respond ONLY with valid JSON. No markdown, no backticks, no explanation.

{{
  "path_title": "{focus_title_prefix} Pathway from {current_position} to {target_goal}",
  "path_description": "A comprehensive pathway designed to take a learner from {current_position} to the target destination or support need of {target_goal} focusing on {focus_area}.",
  "readiness_score": 15,
  "readiness_label": "High School Starter",
  "total_duration": "<calculated admission timeline, e.g. '<months> months'>",
  "blind_spots": [
    "<warning or critical gap 1 based on profile constraints>",
    "<warning or critical gap 2 based on profile constraints>"
  ],
  "steps": [
    {{
      "id": 1,
      "title": "<step/milestone title>",
      "duration": "<calculated step duration range, e.g. 'Months 1-4'>",
      "description": "<detailed step description (at least 3 to 4 comprehensive sentences) outlining exactly what academics, study plans, or profile goals to focus on during this step, why this is critical, and how it strategically prepares the student for {target_goal}>",
      "learning_objectives": [
        "<learning objective 1>",
        "<learning objective 2>",
        "<learning objective 3>"
      ],
     "macro_view": "<4-5 sentence strategic paragraph explaining the BIG PICTURE PURPOSE of this milestone β€” what overarching academic goal it advances, why it is a critical non-negotiable foundation within the entire roadmap architecture, how mastering it unlocks the next stage of academic and career progression, what long-term college readiness competency it builds, and how it connects to the student's overall target destination at {target_goal}>",
"micro_view": "<4-5 sentence strategic paragraph describing the PRECISE EXECUTION OUTPUT β€” what specific academic tasks, coursework, study schedules, and concrete deliverables the student must complete within this phase, how many hours per week to dedicate, what tools and platforms to use for tracking progress, what measurable checkpoints confirm task completion, and what the final tangible output of this phase looks like before moving forward>",
"nano_view": "<4-5 sentence strategic paragraph outlining the MENTOR GUIDANCE FOCUS β€” what specific diagnostic assessments and expert review sessions should happen in this phase, how a mentor validates the student's readiness to advance, what peer cohort accountability checkpoints are recommended, what common failure patterns a mentor should watch for in this phase, and how targeted expert feedback is incorporated to refine the student's execution plan before the next milestone begins>",
      "marketplace": {{
        "mentors": [
          {{
            "name": "<free mentor/counseling option, e.g. Free counselor AMA session>",
            "type": "Mentor",
            "why": "<why this fits the macro free view>",
            "next_step": "<specific action to register/join>",
            "tags": ["<tag>", "<tag>"],
            "section": "macro_free",
            "price": "Free"
          }},
          {{
            "name": "<paid mentor/coaching option, e.g. private SAT math tutor>",
            "type": "Mentor",
            "why": "<why this fits the micro view>",
            "next_step": "<specific registration link/action>",
            "tags": ["<tag>"],
            "section": "micro_structured",
            "price": "<realistic price, e.g. β‚Ή1,500/session>"
          }},
          {{
            "name": "<premium strategic mentor / admissions advisor>",
            "type": "Mentor",
            "why": "<why this fits the nano view>",
            "next_step": "<specific next action>",
            "tags": ["<tag>"],
            "section": "nano_expert",
            "price": "<realistic price, e.g. β‚Ή5,000/month>"
          }}
        ],
        "vendors": [
          {{
            "name": "<free course or online platform prep>",
            "type": "Course",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "macro_free",
            "cost": "Free"
          }},
          {{
            "name": "<paid course or certification exam package>",
            "type": "Course",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "micro_structured",
            "cost": "<realistic cost, e.g. β‚Ή3,500>"
          }},
          {{
            "name": "<premium coding bootcamp or target prep program>",
            "type": "Bootcamp",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "nano_expert",
            "cost": "<realistic cost, e.g. β‚Ή95,000>"
          }}
        ],
        "institutions": [
          {{
            "name": "<free open lectures or articles from a university>",
            "type": "University",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "macro_free",
            "cost": "Free"
          }},
          {{
            "name": "<university certificate course/program>",
            "type": "University",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "micro_structured",
            "cost": "<realistic cost, e.g. β‚Ή20,000>"
          }},
          {{
            "name": "<premium university counselor or admissions seminar program>",
            "type": "University",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "nano_expert",
            "cost": "<realistic cost, e.g. β‚Ή2,50,000>"
          }}
        ],
        "distributors": [
          {{
            "name": "<free youtube channel, docs or community forum>",
            "type": "YouTube",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "macro_free",
            "cost": "Free"
          }},
          {{
            "name": "<paid book, guide or technical publication>",
            "type": "Book",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "micro_structured",
            "cost": "<realistic cost, e.g. β‚Ή1,200>"
          }},
          {{
            "name": "<premium tech libraries subscription>",
            "type": "Subscription",
            "why": "<why this fits>",
            "next_step": "<specific action>",
            "tags": ["<tag>"],
            "section": "nano_expert",
            "cost": "<realistic cost, e.g. β‚Ή24,000/year>"
          }}
        ]
      }},
      "micro_steps": [
        {{"task": "<specific task action>", "resource": "<real resource>"}},
        {{"task": "<specific task action>", "resource": "<real resource>"}},
        {{"task": "<specific task action>", "resource": "<real resource>"}}
      ]
    }}
  ]
}}

Rules:
- CONTENT TAXONOMY RULE: Start from the selected segment only. Do not blend all categories together unless the user explicitly asks to compare alternatives.
- ACADEMIC & RESEARCH covers School K-12, Pre-University Grade 11-12, and University bachelor's/master's/PhD/transfer pathways. Use formal education, curriculum, admissions, tests, research, and academic milestones.
- PRACTICAL & SKILLS covers learning a specific skill and applying skills in internships or real environments. Skills are not professions: "learn Python" is a skill path, while "become a Data Scientist" is a Jobs & Careers path.
- JOBS & CAREERS covers profession/role outcomes. Distinguish technical roles such as engineering, data, and design from non-technical roles such as management, marketing, and operations.
- NON-ACADEMIC COUNSELLING covers mental health and wellness, generic life counselling, and short-term immediate guidance. These are resource/support paths, not career roadmaps. Do not promise diagnosis or treatment; recommend qualified professional support, trusted resources, routine-building, safe escalation, and time-boxed next steps where relevant.
- Calculate a realistic "readiness_score" (0-100) and "readiness_label" (e.g. Early Starter, Advanced, Intermediate) based dynamically on the student's profile signals (academic performance, stream, and curriculum) relative to the competitiveness of the Target Career Goal:
  - For highly competitive targets (e.g., Harvard, Yale, Stanford, MIT, Oxford, IIT, BITS, Imperial College):
    - If performance is "90% and above", score should be around 30-40 (Early/Intermediate Starter).
    - If performance is "75%–89%", score should be around 20-30.
    - If performance is below 75%, score should be around 10-20.
  - For moderately competitive targets (e.g., Local Universities, State Colleges):
    - If performance is "90% and above", score should be around 75-85 (Advanced Starter).
    - If performance is "75%–89%", score should be around 50-60.
    - If performance is below 75%, score should be around 30-40.
  - Adjust the score dynamically based on these parameters. Do NOT hardcode it.
- Highlight at least 2 critical "blind_spots" (gaps, potential constraints, or warnings based on their profile, e.g. "Lacks research projects", "Needs IELTS prep") as a list of strings.
- CRITICAL TIMELINE PROGRESSION CONSTRAINT: You MUST calculate the total duration and steps dynamically to cover the educational progression from the student's current position/grade to ADMISSION into the final target goal, not completion of the target degree itself.
  - Cumulative Calculation Formula: total duration = remaining current-stage months + each prerequisite degree stage before the target degree + an admissions-cycle minimum when the student is already eligible.
  - School Formula: remaining school months = (final school grade - current grade + 1) * months per academic year.
  - Degree Formula: add the configured duration for each prerequisite degree level before the target degree; do NOT add the target degree's own study duration.
  - If the student is in school and target is Master's admission, include remaining school + Bachelor's prerequisite only, then finish with Master's application/admission readiness.
  - If the student is in school and target is Bachelor's admission, include remaining school only, then finish with Bachelor's application/admission readiness.
  - If the student is in university and target is Master's admission, include remaining Bachelor's time only, then finish with Master's application/admission readiness.
  - If they are already eligible for the target degree admission, use a realistic admissions-cycle minimum.
  - Do NOT ignore intermediate degrees. A student cannot jump from 10th Grade directly to Master's admission without first finishing high school and completing a Bachelor's.
  - Set the `total_duration` field to this calculated admission timeline in the format: `"<calculated months> months"`.
  - Distribute the generated milestones/steps in `steps` chronologically across this entire timeline (e.g., starting with school prep, transitioning to Bachelor's projects/internships, and finishing with target-degree application submission/admission readiness).
- Determine the number of milestones/steps in `steps` dynamically based on the complexity of the target destination and calculated timeline.
  - CRITICAL STEP COUNT RULE: You MUST generate a detailed, comprehensive roadmap with as many milestones as the student's data, grade, timeline, and target complexity require. Do NOT use a fixed step count or upper cap. Long multi-year timelines must include enough distinct milestones to cover school preparation, prerequisite degree readiness where relevant, standardized tests, profile building, applications, and admission readiness without compressing unrelated work into broad generic steps.
  - DO NOT restrict or default the pathway to exactly 4 steps. A 4-step path is insufficient to cover a student's career transition and is strictly forbidden.
- Ensure that the progression of milestones represents the pedagogical stages inspired by "Aryan's Pathway 2023":
  - Early milestones must cover: choosing the right curriculum/subjects/streams based on passion/aptitude, researching schools, setting GPA targets (e.g., accomplishing 90%+ in board/school exams).
  - Middle milestones must cover: internship selection & planning to acquire relevant skills, identifying and starting test prep modules (e.g., SAT, ACT, English proficiency like IELTS/TOEFL), identifying right mentors.
  - Transition milestones must cover: planning transitions between academic grades, analyzing risks and gaps.
  - Final milestones must cover: profile building, mock score iteration, university placement program registration, and completing college application dossiers.
- For each milestone step, construct:
  - `macro_view`: A 4-5 sentence strategic paragraph explaining the BIG PICTURE PURPOSE β€” what overarching academic goal the milestone advances, why it is a critical non-negotiable foundation in the roadmap, how mastering it unlocks the next academic stage, what long-term college readiness competency it builds, and how it ties directly to the target destination.
  - `micro_view`: A 4-5 sentence strategic paragraph describing the PRECISE EXECUTION OUTPUT β€” what specific tasks, coursework, and deliverables the student must complete, how many hours per week to allocate, what tools and platforms to use, what measurable checkpoints confirm completion, and what the tangible output looks like before advancing.
  - `nano_view`: A 4-5 sentence strategic paragraph outlining the MENTOR GUIDANCE FOCUS β€” what diagnostic assessments and expert sessions happen, how the mentor validates readiness to advance, what peer cohort checkpoints are recommended, what failure patterns to watch for, and how expert feedback is incorporated before the next milestone begins.
- Distribute the calculated total duration logically across the steps. For example, distribute month ranges like "Months 1-3", "Months 4-6", etc., so they span the entire calculated duration of the pathway.
- CRITICAL CONSISTENCY RULE: Every single milestone in `steps` must have equally rich `description`, `macro_view`, `micro_view`, and `nano_view` text. Do not make only Step 1 or Step 2 detailed. If the pathway has 5, 9, 10, 12, or any other number of steps, every step must receive a detailed multi-sentence explanation with no one-line view fields.
- Generate a dynamic, appropriate number of micro_steps tasks per step based on the milestone requirements (do not hardcode to exactly 3).
- Generate a dynamic, appropriate number of learning_objectives per step based on the milestone requirements (do not hardcode to exactly 3).
- Generate a dynamic, data-dependent number of marketplace recommendations for each of the three sections ("macro_free", "micro_structured", "nano_expert") within all 4 category lists (mentors, vendors, institutions, distributors). Do NOT use a fixed minimum, fixed maximum, or default count. The number of recommendations must be based on the milestone's real needs, the student's grade and constraints, target competitiveness, budget signals, and the availability of relevant resources.
- Ensure the 'type' field of each generated item matches the category (e.g. 'Mentor' or 'Coaching' for mentors, 'Course' or 'Bootcamp' for vendors, 'University' or 'School' for institutions, 'YouTube' or 'Book' or 'Community' for distributors).
- Avoid repeated resource names. Suggest highly specific resources like freeCodeCamp, Coursera, MIT OCW, Khan Academy, specific textbooks.
- Deeply differentiate based on profile grade, curriculum (CBSE vs. IB vs. University), financial budget, stream, personality type, and location.
- Each step description MUST be a rich, detailed, multi-sentence paragraph (3-4 sentences). Do NOT provide short, generic, or single-sentence descriptions. Make them highly academic, pedagogical, and context-specific.
- CRITICAL NAME BAN: NEVER mention the student's personal name (e.g. Sunkara, Chaitanya, Praneeth) or email or personal pronouns in any text fields (titles, descriptions, views, checklist tasks, or objectives). Focus purely on objective, academic instructions.
- REFINEMENT, KEYWORD UNDERSTANDING & VALIDATION RULES:
  - If a Refinement / Adjustment Request is provided:
    - You MUST understand the keywords and intent behind it:
      - E.g., if it says "change step X description", you MUST locate the step with id X and rewrite its "description" text exactly as requested (or make it more detailed/aligned with their feedback).
      - E.g., if it says "add more steps" or "add X steps", you MUST increase the number of milestones in the "steps" and insert relevant steps with correct IDs.
      - E.g., if it says "add correct marketplace" or "change marketplace", you MUST adjust the "marketplace" objects inside the relevant steps.
      - E.g., if it says "give more accurate" or "add SAT prep", you MUST modify descriptions, objectives, and checklists to include those academic resources.
    - If the request is completely unrelated to the career pathway, contains nonsense, or asks to perform out-of-scope tasks (e.g., "tell me a joke", "tell me a story", "what is the weather"), you MUST return a JSON object containing ONLY the key "error" with a polite description explaining why the request is invalid and how the user can ask correctly. Example: {{"error": "This request is irrelevant to career pathway refinement. Please provide specific instructions to adjust this pathway, such as 'change step 1 description' or 'add more milestones'."}}
    - If the request is valid, perform the refinement. If an Existing Roadmap is provided as context, you MUST preserve all steps that the user did not ask to change. Modify or replace only the specific steps/details requested by the user, while keeping other milestones/steps identical to the existing roadmap.
"""

# ─── AGENT 2: PATH AUDIT AGENT PROMPT ────────────────────────────────────────
AGENT_2_PROMPT = """You are the Naaviverse Path Audit Agent (Agent 2).
Your purpose is to validate and improve the overall roadmap's path-level information.
Audited Goals:
- Target Career Goal: {target_goal}
- Student Current Position & Profile: {current_position} | {profile}

Given this raw blueprint JSON, review and audit:
1. The overall "path_title" (Is it clear, accurate, and aligned with target goal?)
2. The overall "path_description" (Is it a professional, highly relevant, multi-sentence strategic summary?)
3. Estimate a realistic "readiness_score" (0-100) and "readiness_label" (e.g. Early Starter, Advanced, Intermediate) based dynamically on the student's profile signals (academic performance, stream, and curriculum) relative to the competitiveness of the Target Career Goal:
   - For highly competitive targets (e.g., Harvard, Yale, Stanford, MIT, Oxford, IIT, BITS):
     - If performance is "90% and above", score should be around 30-40 (Early/Intermediate Starter).
     - If performance is "75%–89%", score should be around 20-30.
     - If performance is below 75%, score should be around 10-20.
   - For moderately competitive targets (e.g., Local Universities, State Colleges):
     - If performance is "90% and above", score should be around 75-85 (Advanced Starter).
     - If performance is "75%–89%", score should be around 50-60.
     - If performance is below 75%, score should be around 30-40.
   - Adjust the score dynamically based on these parameters. Do NOT hardcode it.
4. Highlight critical "blind_spots" (gaps, potential constraints, or warnings based on their profile).
5. CRITICAL NAME BAN: Verify that the overall path title and description NEVER mention the student's personal name, email, or personal pronouns. If any names are present, rewrite the text to be completely objective, focusing purely on explaining the main strategic direction of this pathway.

Output ONLY a valid JSON object of this structure:
{{
  "path_title": "<audited and refined Path Title>",
  "path_description": "<audited and refined detailed multi-sentence Path Description>",
  "readiness_score": <updated integer 0-100 based on profile readiness>,
  "readiness_label": "<updated readiness description>",
  "blind_spots": [
    "<warning or critical gap 1 based on profile constraints>",
    "<warning or critical gap 2 based on profile constraints>"
  ]
}}

Blueprint JSON to Audit:
{blueprint}
"""

# ─── AGENT 3: STEPS AND VIEWS AUDIT AGENT PROMPT ─────────────────────────────
AGENT_3_PROMPT = """You are the Naaviverse Steps and Views Audit Agent (Agent 3).
Your purpose is to validate the roadmap's execution structure, milestones, learning views, and micro checklists.
Audited Goals:
- Target Career Goal: {target_goal}
- Student Current Position & Profile: {current_position} | {profile}

Given this blueprint JSON containing steps and views, review and audit:
1. Each step's "title" and "duration" (Ensure logical progression, and MUST preserve the step's exact calculated month duration range from the blueprint JSON, e.g. "Months 1-9" or "Months 1-6" or "Months 1-3" exactly. Do NOT change these ranges to default values).
2. Each step's "description" (Must be a rich, detailed, multi-sentence paragraph of 3-4 sentences detailing the main academic utility).
3. The step's "learning_objectives" (Verify they align with target learning outcomes).
4. The step's "macro_view" (Must be a rich 4-5 sentence paragraph clearly explaining the BIG PICTURE PURPOSE β€” the overarching academic goal this milestone advances and why it is a critical foundation in the roadmap).
5. The step's "micro_view" (Must be a rich 4-5 sentence paragraph describing the PRECISE EXECUTION OUTPUT β€” the exact academic tasks, deliverables, and coursework the student must complete to finish this phase).
6. The step's "nano_view" (Must be a rich 4-5 sentence paragraph explaining the MENTOR GUIDANCE FOCUS β€” the diagnostic checks, expert review sessions, and accountability checkpoints that validate readiness to advance).
7. The step's "micro_steps" checklist tasks (Make sure they are hyper-specific, actionable, and tailored to the student's curriculum/grade).
8. CRITICAL NAME BAN: Strictly verify that NONE of the step titles, durations, descriptions, learning objectives, views (macro, micro, nano), or micro_steps tasks contain the student's personal name, email, or direct pronouns. Rewrite all fields to be completely objective, professional, and academic, focusing entirely on what the step achieves, how to execute it, and how to complete the step successfully.
9. CRITICAL STEP PRESERVATION RULE: You MUST audit and return every single milestone/step provided in the blueprint JSON. If the blueprint JSON contains 8 steps, you must output exactly 8 audited steps in your JSON array. If it contains 10 steps, you must output exactly 10 audited steps. Do NOT skip, delete, combine, or truncate the steps under any circumstances.

Output ONLY a valid JSON array of this structure:
[
  {{
    "id": 1,
    "title": "<audited step title>",
    "duration": "<preserve duration range from blueprint exactly, e.g., 'Months 1-9'>",
    "description": "<audited rich detailed multi-sentence description (3-4 sentences)>",
    "learning_objectives": [
      "<audited learning objective 1>",
      "... for all learning objectives in this step ..."
    ],
    "macro_view": "<audited/refined macro view text>",
    "micro_view": "<audited/refined micro view text>",
    "nano_view": "<audited/refined nano view text>",
    "micro_steps": [
      {{"task": "<actionable task>", "resource": "<real specific resource>"}},
      "... for all micro steps in this step ..."
    ]
  }},
  ... for all steps in the blueprint ...
]

Blueprint JSON to Audit:
{blueprint}
"""

# ─── AGENT 4: MARKETPLACE AUDIT AGENT PROMPT ──────────────────────────────────
AGENT_4_PROMPT = """You are the Naaviverse Marketplace Audit Agent (Agent 4).
Your purpose is to validate all learning resource marketplace recommendations generated across each milestone's Macro, Micro, and Nano views.
Audited Goals:
- Target Career Goal: {target_goal}
- Student Current Position & Profile: {current_position} | {profile}

Given this blueprint JSON, review the "marketplace" block for each step.
Ensure resources:
1. Match the budget limits (Lower percentages like 0-25% or 25-50% => free/low cost resources, higher percentages => high quality bootcamps/paid courses/premium mentoring).
2. Match personality traits (RIASEC codes: Realistic => practical/hands-on tasks, Investigative => research/data/logic, Artistic => design/creative writing, Social => teaching/helping/cooperative, Enterprising => startup/business/leadership, Conventional => structured/admin/analytical tracking).
3. Are highly reputable, real-world educational resources (e.g. Khan Academy, Coursera, MIT OCW, specific standard prep books).
4. Pricing and next steps are realistic, detailed, and actionable.
5. CRITICAL NAME BAN: Ensure that no marketplace recommendations, why details, next steps, or outcomes contain the student's personal name, email, or pronouns. Keep all text objective and general.
6. CRITICAL STEP PRESERVATION RULE: You MUST audit and return the marketplace blocks for every single milestone/step provided in the blueprint JSON. If the blueprint JSON has 8 steps, you must output exactly 8 audited steps in your JSON array. If it has 10 steps, you must output exactly 10 audited steps. Do NOT skip, delete, combine, or truncate steps under any circumstances.

Output ONLY a valid JSON array of this structure:
[
  {{
    "id": 1,
    "marketplace": {{
      "mentors": [
        {{
          "name": "<audited mentor name>",
          "type": "Mentor",
          "why": "<why this fits the macro free view>",
          "next_step": "<how to connect>",
          "tags": ["<tag>"],
          "section": "macro_free",
          "price": "Free"
        }},
        {{
          "name": "<audited paid mentor>",
          "type": "Mentor",
          "why": "<why this fits the micro view>",
          "next_step": "<how to connect>",
          "tags": ["<tag>"],
          "section": "micro_structured",
          "price": "<price>"
        }},
        {{
          "name": "<audited premium mentor>",
          "type": "Mentor",
          "why": "<why this fits the nano view>",
          "next_step": "<how to connect>",
          "tags": ["<tag>"],
          "section": "nano_expert",
          "price": "<price>"
        }}
      ],
      "vendors": [
        {{
          "name": "<audited free vendor resource>",
          "type": "Course",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "macro_free",
          "cost": "Free"
        }},
        {{
          "name": "<audited structured vendor course>",
          "type": "Course",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "micro_structured",
          "cost": "<cost>"
        }},
        {{
          "name": "<audited premium vendor bootcamp>",
          "type": "Bootcamp",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "nano_expert",
          "cost": "<cost>"
        }}
      ],
      "institutions": [
        {{
          "name": "<audited free open university resource>",
          "type": "University",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "macro_free",
          "cost": "Free"
        }},
        {{
          "name": "<audited university certificate program>",
          "type": "University",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "micro_structured",
          "cost": "<cost>"
        }},
        {{
          "name": "<audited premium executive university program>",
          "type": "University",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "nano_expert",
          "cost": "<cost>"
        }}
      ],
      "distributors": [
        {{
          "name": "<audited free youtube or docs>",
          "type": "YouTube",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "macro_free",
          "cost": "Free"
        }},
        {{
          "name": "<audited book or paid guide>",
          "type": "Book",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "micro_structured",
          "cost": "<cost>"
        }},
        {{
          "name": "<audited premium tech library subscription>",
          "type": "Subscription",
          "why": "<why>",
          "next_step": "<next step>",
          "tags": ["<tag>"],
          "section": "nano_expert",
          "cost": "<cost>"
        }}
      ]
    }}
  }},
  ... for all steps in the blueprint ...
]

Blueprint JSON to Audit:
{blueprint}
"""

# ─── POST-PROCESSING: PERSONAL NAME SANITIZER ─────────────────────────────────
def build_name_patterns(profile: dict, current_position: str = "") -> list:
    """Extract all personal name tokens from the profile that should be scrubbed."""
    tokens = []
    # Extract from profile fields
    for field in ["name", "full_name", "first_name", "last_name", "email"]:
        val = profile.get(field, "")
        if val and isinstance(val, str):
            # For email, take the part before @
            if "@" in val:
                val = val.split("@")[0]
            # Split into individual tokens (e.g. "Sunkara Chaitanya Praneeth" -> 3 tokens)
            for token in val.replace("_", " ").replace(".", " ").split():
                cleaned = token.strip()
                if len(cleaned) > 2:  # ignore very short tokens like "K"
                    tokens.append(cleaned)
    return list(set(tokens))

def sanitize_text(text: str, name_tokens: list) -> str:
    """Replace personal name occurrences in a text string with objective phrasing."""
    if not text or not name_tokens:
        return text
    result = text
    # Build a combined regex to match full-name sequences first (e.g. "Sunkara Chaitanya Praneeth")
    # then individual tokens
    for token in sorted(name_tokens, key=len, reverse=True):  # longest first
        # Match token as a whole word, case-insensitive
        pattern = re.compile(r'\b' + re.escape(token) + r'\b', re.IGNORECASE)
        # Replace contextual phrases like "for Chaitanya to" -> "to"
        result = re.sub(
            r'\bfor\s+' + re.escape(token) + r'\s+to\b',
            'to', result, flags=re.IGNORECASE
        )
        result = re.sub(
            r'\b' + re.escape(token) + r"'s\b",
            "the student's", result, flags=re.IGNORECASE
        )
        result = pattern.sub('the student', result)
    # Clean up double "the student the student" artifacts
    result = re.sub(r'\bthe student the student\b', 'the student', result, flags=re.IGNORECASE)
    # Clean double spaces
    result = re.sub(r'  +', ' ', result).strip()
    return result

def recursive_sanitize(obj, name_tokens: list):
    """Recursively traverse and sanitize all string fields in a dict/list."""
    if isinstance(obj, str):
        return sanitize_text(obj, name_tokens)
    elif isinstance(obj, dict):
        return {k: recursive_sanitize(v, name_tokens) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [recursive_sanitize(item, name_tokens) for item in obj]
    return obj


def is_rich_paragraph(text: Any, min_chars: int = 260, min_sentences: int = 3) -> bool:
    if isinstance(text, dict):
        text = text.get("description")
    if not isinstance(text, str):
        return False
    cleaned = text.strip()
    if len(cleaned) < min_chars:
        return False
    sentences = [part for part in re.split(r'[.!?]+', cleaned) if part.strip()]
    return len(sentences) >= min_sentences


def get_view_description(step: dict, view_key: str) -> str:
    value = step.get(view_key)
    if isinstance(value, dict):
        return str(value.get("description") or "").strip()
    return str(value or "").strip()


def set_view_description(step: dict, view_key: str, description: str):
    value = step.get(view_key)
    if isinstance(value, dict):
        value["description"] = description
    else:
        step[view_key] = description


def enrich_step_narrative(step: dict, current: str, goal: str) -> dict:
    """Guarantee every roadmap step has detailed view text, even when a model returns one-liners."""
    if not isinstance(step, dict):
        return step

    title = str(step.get("title") or f"Step {step.get('id', '')}").strip()
    duration = str(step.get("duration") or "this phase").strip()
    existing_description = str(step.get("description") or "").strip()
    source_description = existing_description or f"Complete the planned academic work for {title}."

    if not is_rich_paragraph(existing_description, min_chars=220, min_sentences=3):
        step["description"] = (
            f"{source_description} This {duration} milestone should define the exact academic target, the evidence that proves progress, "
            f"and the standards required to move forward in the pathway. The work should connect daily study routines, profile-building activity, "
            f"and measurable readiness indicators so the student is not simply completing tasks but building a stronger admissions narrative. "
            f"By the end of the phase, the student should have clear outputs, reviewed gaps, and a documented next-step plan for the following milestone."
        )

    if not is_rich_paragraph(step.get("macro_view")):
        set_view_description(step, "macro_view", (
            f"{title} is a strategic milestone because it turns the broad ambition of reaching {goal} into a concrete stage of academic readiness. "
            f"During {duration}, this phase should strengthen the student's transcript, subject confidence, and long-term preparation habits while keeping the final destination visible. "
            f"It is important because weak execution here creates gaps that later appear in test scores, applications, interviews, or portfolio evidence. "
            f"Completing this step well unlocks the next stage of the pathway by proving that the student can meet a defined standard, reflect on gaps, and convert guidance into steady progress. "
            f"The big-picture outcome is a stronger, more coherent profile that connects the current position of {current} with the expectations of {goal}."
        ))

    if not is_rich_paragraph(step.get("micro_view")):
        set_view_description(step, "micro_view", (
            f"The execution focus for {title} should be converted into weekly deliverables, tracked study blocks, and visible proof of completion. "
            f"The student should maintain a simple planner in Notion, Google Sheets, or a notebook with subject targets, practice-test scores, resource links, and pending mentor feedback. "
            f"At least two to three focused work sessions per week should be reserved for this milestone, with extra time added before exams, submissions, or application deadlines. "
            f"Completion should be measured through tangible outputs such as revised notes, mock-test analysis, shortlist documents, project logs, essay drafts, or reviewed checklists, depending on the step. "
            f"Before moving forward, the student should be able to show what was completed, what improved, what still needs support, and how the next milestone will use this output."
        ))

    if not is_rich_paragraph(step.get("nano_view")):
        set_view_description(step, "nano_view", (
            f"The mentor focus for {title} should be diagnostic, evidence-based, and tied to readiness for the next milestone. "
            f"A counselor, subject expert, or admissions mentor should review the student's current output, compare it with the milestone expectations, and identify the highest-risk gaps. "
            f"The review should include specific feedback on academic quality, consistency, time management, and whether the evidence produced during {duration} is strong enough to support the overall pathway. "
            f"Peer or cohort accountability can be used to compare progress, surface blind spots, and keep execution from becoming isolated or vague. "
            f"The phase should close with a revised action list that records mentor feedback, fixes weak assumptions, and confirms whether the student is ready to advance."
        ))

    return step


def enrich_roadmap_narratives(roadmap: dict, current: str, goal: str) -> dict:
    if not isinstance(roadmap, dict):
        return roadmap
    milestones = roadmap.get("steps")
    if isinstance(milestones, list):
        roadmap["steps"] = [
            enrich_step_narrative(milestone, current, goal)
            for milestone in milestones
        ]
    return roadmap


async def log_generation_event(
    current: str,
    goal: str,
    profile: dict,
    refine_prompt: Optional[str],
    focus_req: Optional[str],
    existing_roadmap: Optional[dict],
    final_alternatives: List[dict]
) -> List[dict]:
    email = profile.get("email") if isinstance(profile, dict) else None
    parent_gen_id = None
    if existing_roadmap and isinstance(existing_roadmap, dict) and "generation_id" in existing_roadmap:
        parent_gen_id = existing_roadmap["generation_id"]
        
    final_alternatives = normalize_roadmap_marketplaces_for_storage(final_alternatives)
    gen_history_doc = {
        "timestamp": datetime.datetime.now(datetime.timezone.utc),
        "current_position": current,
        "target_goal": goal,
        "profile": profile,
        "refine_prompt": refine_prompt,
        "focus_requested": focus_req,
        "alternatives": final_alternatives,
        "session_email": email,
        "parent_generation_id": parent_gen_id
    }
    try:
        gen_result = await generation_history_collection.insert_one(gen_history_doc)
        generation_id = str(gen_result.inserted_id)
        for alt in final_alternatives:
            alt["generation_id"] = generation_id
    except Exception as e:
        print(f"[MongoDB History Warning] Could not save generation history: {e}")
    return final_alternatives


# Helper to query Groq and extract clean JSON with model fallbacks
async def query_groq_json(
    prompt: str,
    preferred_model: str = "llama-3.1-8b-instant",
    fallback_models: Optional[List[str]] = None,
) -> dict:
    models = [preferred_model] + (
        fallback_models
        if fallback_models is not None
        else [
            "llama-3.1-8b-instant",
            "llama-3.3-70b-versatile",
            "qwen/qwen3-32b",
            "openai/gpt-oss-20b",
        ]
    )



    # Deduplicate while preserving order (preferred_model may already be one of the fallbacks)
    seen = set()
    unique_models = []
    for m in models:
        if m not in seen:
            seen.add(m)
            unique_models.append(m)

    last_err = None
    for m in unique_models:
        try:
            estimated_input_tokens = int(len(prompt) / 3.2)
            if "70b" in m or "120b" in m or "32b" in m or "17b" in m:
                max_tok = 4096
            else:
                max_tok = max(1000, 5800 - estimated_input_tokens)
                if max_tok > 2500:
                    max_tok = 2500

            response = await async_client.chat.completions.create(
                model=m,
                max_tokens=max_tok,
                temperature=0.3,
                messages=[
                    {
                        "role": "system",
                        "content": (
                            "You are a career path database sub-agent. "
                            "Always respond with valid, complete JSON only. "
                            "No markdown, no backticks, no explanations. "
                            "Start immediately with { or [ and end with } or ]. "
                            "NEVER truncate your response. Complete the full JSON structure."
                        ),
                    },
                    {"role": "user", "content": prompt},
                ],
            )
            raw = response.choices[0].message.content.strip()
            # Strip markdown fences if present
            raw = re.sub(r"```(?:json)?", "", raw).strip().strip("`").strip()

            # Extract the outermost JSON object or array
            match = re.search(r'(\{.*\}|\[.*\])', raw, re.DOTALL)
            if match:
                raw = match.group(0)

            # Attempt direct parse
            try:
                return json.loads(raw)
            except json.JSONDecodeError as json_err:
                # Attempt partial JSON recovery: try to close unclosed brackets
                print(f"[JSON Recovery] Attempting to repair truncated JSON from model {m}. Error at char {json_err.pos}.")
                # Truncate to last valid position and try to close structures
                truncated = raw[:json_err.pos].rstrip().rstrip(",").rstrip()
                # Count unclosed braces/brackets
                opens = truncated.count("{") - truncated.count("}")
                open_arrays = truncated.count("[") - truncated.count("]")
                closing = "]" * open_arrays + "}" * opens
                repaired = truncated + closing
                try:
                    result = json.loads(repaired)
                    print(f"[JSON Recovery] Successfully repaired truncated JSON from model {m}.")
                    return result
                except Exception:
                    raise json_err  # Let the outer except catch it and try next model

        except Exception as e:
            print(f"[Groq Call Failed for model {m}] trying next fallback. Error: {e}")
            last_err = e
            continue

    print(f"[Groq Critical Failure] All models exhausted. Final error: {last_err}")
    return {}


def split_duration(duration_str: str, num_parts: int, index: int) -> str:
    # Match ranges like "Months 1-4" or "Months 13-16" or "1-4"
    match = re.search(r'(\d+)\s*-\s*(\d+)', duration_str)
    if match:
        start = int(match.group(1))
        end = int(match.group(2))
        total_months = end - start + 1
        
        # Calculate sub-range for the index-th part out of num_parts
        part_len = total_months / num_parts
        part_start = int(start + index * part_len)
        part_end = int(start + (index + 1) * part_len - 1)
        
        # Ensure bounds
        if part_start < start:
            part_start = start
        if part_end > end or index == num_parts - 1:
            part_end = end
        if part_end < part_start:
            part_end = part_start
            
        if part_start == part_end:
            return f"Month {part_start}"
        else:
            return f"Months {part_start}-{part_end}"
            
    # Single month case like "Month 12"
    match_single = re.search(r'Month\s*(\d+)', duration_str, re.IGNORECASE)
    if match_single:
        return duration_str
        
    return duration_str


DEGREE_TYPE_OPTIONS = {
    "bachelor": ("Bachelor's", 48),
    "bachelors": ("Bachelor's", 48),
    "bachelor's": ("Bachelor's", 48),
    "undergraduate": ("Bachelor's", 48),
    "ug": ("Bachelor's", 48),
    "btech": ("Bachelor's", 48),
    "b.tech": ("Bachelor's", 48),
    "bsc": ("Bachelor's", 48),
    "b.sc": ("Bachelor's", 48),
    "master": ("Master's", 24),
    "masters": ("Master's", 24),
    "master's": ("Master's", 24),
    "graduate": ("Master's", 24),
    "postgraduate": ("Master's", 24),
    "pg": ("Master's", 24),
    "mtech": ("Master's", 24),
    "m.tech": ("Master's", 24),
    "msc": ("Master's", 24),
    "m.sc": ("Master's", 24),
    "mba": ("Master's", 24),
    "phd": ("PhD", 60),
    "ph.d": ("PhD", 60),
    "doctorate": ("PhD", 60),
    "doctoral": ("PhD", 60),
    "transfer": ("Transfer", 24),
    "associate": ("Associate", 24),
    "associates": ("Associate", 24),
    "diploma": ("Diploma", 12),
    "certificate": ("Certificate", 6),
    "certification": ("Certificate", 6),
}

MONTHS_PER_ACADEMIC_YEAR = 12
FINAL_SCHOOL_GRADE = 12
MIN_ADMISSION_CYCLE_MONTHS = MONTHS_PER_ACADEMIC_YEAR


def normalize_degree_type(value: Optional[str]) -> Optional[str]:
    if not value:
        return None
    text = str(value).strip().lower()
    for key, (label, _) in DEGREE_TYPE_OPTIONS.items():
        if re.search(rf"(^|[\s\-.]){re.escape(key)}($|[\s\-.])", text):
            return label
    return None


def extract_degree_type_from_goal(goal: str) -> Optional[str]:
    parts = [p.strip() for p in re.split(r"[\u2022.]+", goal or "") if p.strip()]
    for part in parts:
        normalized = normalize_degree_type(part)
        if normalized:
            return normalized
    return normalize_degree_type(goal)


def get_request_degree_type(goal: str, profile: dict, explicit_degree_type: Optional[str] = None) -> Optional[str]:
    return (
        normalize_degree_type(explicit_degree_type)
        or normalize_degree_type((profile or {}).get("degreeType"))
        or normalize_degree_type((profile or {}).get("degree_type"))
        or extract_degree_type_from_goal(goal)
    )


def ensure_degree_type_for_generation(goal: str, profile: dict, explicit_degree_type: Optional[str] = None) -> dict:
    degree_type = get_request_degree_type(goal, profile, explicit_degree_type)
    if not degree_type:
        raise HTTPException(
            status_code=400,
            detail="Degree type is required. Please include Bachelor's, Master's, PhD, Transfer, Associate, Diploma, or Certificate before generating a path.",
        )

    enriched_profile = dict(profile or {})
    enriched_profile["degreeType"] = degree_type
    return enriched_profile


def duration_months_for_degree(degree_type: Optional[str]) -> Optional[int]:
    normalized = normalize_degree_type(degree_type)
    if not normalized:
        return None
    for label, months in DEGREE_TYPE_OPTIONS.values():
        if label == normalized:
            return months
    return None


def duration_months_for_level(level: int) -> int:
    level_to_degree = {
        1: "bachelor",
        2: "master",
        3: "phd",
    }
    degree_type = level_to_degree.get(level)
    return duration_months_for_degree(degree_type) or MIN_ADMISSION_CYCLE_MONTHS


def calculate_total_duration_months(current: str, goal: str, profile: dict, explicit_degree_type: Optional[str] = None) -> int:
    target_degree = get_request_degree_type(goal, profile, explicit_degree_type)

    target_level = 0
    if target_degree:
        td_lower = target_degree.lower()
        if any(k in td_lower for k in ["phd", "ph.d", "doctorate", "doctoral"]):
            target_level = 3
        elif any(k in td_lower for k in ["master", "postgraduate", "pg", "mtech", "msc", "mba"]):
            target_level = 2
        elif any(k in td_lower for k in ["bachelor", "undergraduate", "ug", "btech", "bsc", "associate", "diploma", "transfer"]):
            target_level = 1

    current_str = f"{(profile or {}).get('grade') or ''} {current or ''}".lower()
    
    current_level = 0
    remaining_current_months = 0
    is_school = False
    grade_num = None
    
    if any(k in current_str for k in ["grade", "class", "std", "th ", "th", "school"]):
        is_school = True
        
    num_match = re.search(r'\b(9|10|11|12)\b', current_str)
    if num_match:
        grade_num = int(num_match.group(1))
        is_school = True
    elif "tenth" in current_str or "10th" in current_str:
        grade_num = 10
        is_school = True
    elif "eleventh" in current_str or "11th" in current_str:
        grade_num = 11
        is_school = True
    elif "twelfth" in current_str or "12th" in current_str:
        grade_num = 12
        is_school = True
    elif "ninth" in current_str or "9th" in current_str:
        grade_num = 9
        is_school = True

    is_bachelors = any(k in current_str for k in ["bachelor", "btech", "b.tech", "bsc", "b.sc", "undergrad", "ug", "college", "university"])
    is_masters = any(k in current_str for k in ["master", "mtech", "m.tech", "msc", "m.sc", "mba", "postgrad", "pg"])
    is_graduate = any(k in current_str for k in ["grad", "completed", "finished", "degree holder", "alumni"])
    
    if is_masters:
        current_level = 2
        if "1st year" in current_str or "first year" in current_str:
            remaining_current_months = duration_months_for_level(current_level) // 2
        else:
            remaining_current_months = 0
    elif is_bachelors:
        current_level = 1
        current_degree_months = duration_months_for_level(current_level)
        if is_graduate:
            remaining_current_months = 0
        elif "1st year" in current_str or "first year" in current_str:
            remaining_current_months = current_degree_months - (current_degree_months // 4)
        elif "2nd year" in current_str or "second year" in current_str:
            remaining_current_months = current_degree_months // 2
        elif "3rd year" in current_str or "third year" in current_str:
            remaining_current_months = current_degree_months // 4
        elif "4th year" in current_str or "fourth year" in current_str or "final year" in current_str:
            remaining_current_months = 0
        else:
            remaining_current_months = current_degree_months // 2
    elif is_school or grade_num is not None:
        current_level = 0
        if grade_num is not None:
            remaining_current_months = (FINAL_SCHOOL_GRADE - grade_num + 1) * MONTHS_PER_ACADEMIC_YEAR
        else:
            remaining_current_months = MONTHS_PER_ACADEMIC_YEAR * 2
    else:
        if target_level >= 2:
            current_level = 1
            remaining_current_months = 0
        else:
            current_level = 0
            remaining_current_months = MONTHS_PER_ACADEMIC_YEAR * 2

    if target_level <= current_level:
        return max(MIN_ADMISSION_CYCLE_MONTHS, remaining_current_months)

    total_duration = remaining_current_months
    for level in range(current_level + 1, target_level):
        total_duration += duration_months_for_level(level)
            
    return max(MIN_ADMISSION_CYCLE_MONTHS, total_duration)


def format_total_duration(months: int) -> str:
    return f"{months} month" if months == 1 else f"{months} months"


def distribute_month_ranges(step_count: int, total_months: int) -> List[str]:
    if step_count <= 0:
        return []
    ranges = []
    for idx in range(step_count):
        start = int(idx * total_months / step_count) + 1
        end = int((idx + 1) * total_months / step_count)
        if start == end:
            ranges.append(f"Month {start}")
        else:
            ranges.append(f"Months {start}-{end}")
    return ranges


CONTENT_SEGMENTS = [
    {
        "category": "academic",
        "option_name": "Academic & Research",
        "focus": "Academic & Research - School / Pre-University / University: focus on K-12, Grade 11-12, bachelor's, master's, PhD, transfer, formal curriculum, admissions, tests, research, and academic progression.",
    },
    {
        "category": "practical",
        "option_name": "Practical & Skills",
        "focus": "Practical & Skills - Skills Track / Internship Track: focus on learning a specific skill, building proof-of-work projects, applying skills in internships, and separating skill learning from profession selection.",
    },
    {
        "category": "jobs",
        "option_name": "Jobs & Careers",
        "focus": "Jobs & Careers - Technical / Non-Technical Roles: focus on profession and role readiness, job search strategy, interviews, portfolio/resume evidence, workplace skills, and technical versus non-technical career tracks.",
    },
    {
        "category": "non_academic",
        "option_name": "Non-Academic Counselling",
        "focus": "Non-Academic Counselling - Mental Health & Wellness / Generic Life Counselling / Short-term Immediate Guidance: focus on support paths, resource navigation, decision support, wellbeing routines, referral options, and time-boxed guidance. This is not a career roadmap.",
    },
]


def resolve_focus_category(focus: Optional[str]) -> str:
    text = str(focus or "").lower()
    if any(k in text for k in ["non-academic", "non academic", "mental", "wellness", "life counselling", "life counseling", "immediate guidance"]):
        return "non_academic"
    if any(k in text for k in ["jobs", "careers", "career-prep", "career prep", "technical roles", "non-technical", "profession", "placement"]):
        return "jobs"
    if any(k in text for k in ["practical", "skills", "skill", "internship"]):
        return "practical"
    return "academic"


CATEGORY_VARIANTS = {
    "academic": [
        {
            "option_name": "Research & Honors Focus",
            "focus": "Academic & Research - Research & Honors Track: Focus on academic research projects, publishing papers, honors courses, advanced academic honors, and building a high-prestige academic profile."
        },
        {
            "option_name": "Test Prep & Admissions Focus",
            "focus": "Academic & Research - Test Prep & Admissions Track: Focus on standardized tests (SAT, ACT, AP, GRE, IELTS, TOEFL, board exams) and strategic admissions milestones for competitive schools and universities."
        },
        {
            "option_name": "Curriculum & GPA Focus",
            "focus": "Academic & Research - Curriculum & GPA Excellence Track: Focus on standard academic curricula, course selections, maintaining a top-tier GPA, and fulfilling prerequisite school/college/university coursework."
        }
    ],
    "practical": [
        {
            "option_name": "Project Portfolio Focus",
            "focus": "Practical & Skills - Project Portfolio Track: Focus on building high-quality, hands-on proof-of-work projects, open-source contributions, technical or creative designs, and displaying an exceptional project portfolio."
        },
        {
            "option_name": "Certification & Bootcamp Focus",
            "focus": "Practical & Skills - Certification & Structured Learning Track: Focus on completing industry-recognized professional certifications, structured bootcamps, and specialized skills courses."
        },
        {
            "option_name": "Internship & Applied Focus",
            "focus": "Practical & Skills - Internship & Applied Practice Track: Focus on gaining early work experience, securing internships, participating in apprenticeships, and applying learning to real-world environments."
        }
    ],
    "jobs": [
        {
            "option_name": "Technical Role Prep Focus",
            "focus": "Jobs & Careers - Technical Role Prep: Focus on preparing for technical roles (e.g. engineering, software development, data science, specialized systems) through coding challenges, system design, and technical assessments."
        },
        {
            "option_name": "Interview & Networking Focus",
            "focus": "Jobs & Careers - Interview Prep & Networking: Focus on interview skills (behavioral, technical, case studies), networking with industry professionals, cold outreach, and securing referrals."
        },
        {
            "option_name": "Resume & Career Evidence Focus",
            "focus": "Jobs & Careers - Resume & Career Evidence: Focus on tailoring resumes, building professional profiles (LinkedIn, GitHub), gathering evidence of workplace competence, and job search management."
        }
    ],
    "non_academic": [
        {
            "option_name": "Mental Health & Wellness Focus",
            "focus": "Non-Academic Counselling - Mental Health & Wellness: Focus on daily mental health routines, mindfulness, stress management, counseling support, and building emotional resilience."
        },
        {
            "option_name": "Life Skills & Decision Focus",
            "focus": "Non-Academic Counselling - Life Skills & Decision Support: Focus on time management, decision support, routine building, navigating personal/educational choices, and generic life coaching."
        },
        {
            "option_name": "Immediate Action & Support Focus",
            "focus": "Non-Academic Counselling - Immediate Guidance & Support: Focus on short-term resource navigation, safe escalation paths, quick-win routines, and qualified peer or professional referral options."
        }
    ]
}


def get_focus_choices(
    focus_req: Optional[str] = None,
    content_category: Optional[str] = None,
    sub_segment: Optional[str] = None
) -> tuple[list[str], list[str]]:
    # 1. If we are asking for a specific variant regeneration/refinement (e.g. during tab-regen)
    if focus_req:
        for cat, variants in CATEGORY_VARIANTS.items():
            for var in variants:
                if var["option_name"].lower() == focus_req.lower() or focus_req.lower() in var["option_name"].lower():
                    # Return only this single variant
                    focus_str = var["focus"]
                    if sub_segment:
                        sub_label = sub_segment.replace("_", " ").title()
                        focus_str += f" Target sub-segment: {sub_label}."
                    return [focus_str], [var["option_name"]]

    # 2. If content_category is explicitly specified:
    if content_category:
        cat = content_category.lower().strip()
        if cat not in CATEGORY_VARIANTS:
            cat = resolve_focus_category(content_category)
        if cat in CATEGORY_VARIANTS:
            variants = CATEGORY_VARIANTS[cat]
            foci = []
            for v in variants:
                f_str = v["focus"]
                if sub_segment:
                    sub_label = sub_segment.replace("_", " ").title()
                    f_str += f" Target sub-segment: {sub_label}."
                foci.append(f_str)
            option_names = [v["option_name"] for v in variants]
            return foci, option_names

    # 3. Fallback: if focus_req is provided but didn't match a variant, resolve its category and return its variants
    if focus_req:
        cat = resolve_focus_category(focus_req)
        if cat in CATEGORY_VARIANTS:
            variants = CATEGORY_VARIANTS[cat]
            foci = []
            for v in variants:
                f_str = v["focus"]
                if sub_segment:
                    sub_label = sub_segment.replace("_", " ").title()
                    f_str += f" Target sub-segment: {sub_label}."
                foci.append(f_str)
            option_names = [v["option_name"] for v in variants]
            return foci, option_names

    # 4. Global fallback: return all 4 main segment focuses
    return (
        [segment["focus"] for segment in CONTENT_SEGMENTS],
        [segment["option_name"] for segment in CONTENT_SEGMENTS],
    )


def requires_degree_for_focus(focus: Optional[str]) -> bool:
    return resolve_focus_category(focus) == "academic"


def get_mock_marketplace(focus: Optional[str]) -> dict:
    if not focus:
        focus = "Academic"
        
    if "Academic" in focus:
        return {
            "mentors": [
                {"name": "Peer Cohort Study Group", "type": "Mentor", "why": "Free study groups led by senior students who cleared admissions.", "next_step": "Join the weekly study circle.", "tags": ["Study Group", "Peer Support"], "section": "macro_free"},
                {"name": "University Admissions Q&A Circle", "type": "Mentor", "why": "Free AMA sessions with international admissions advisors.", "next_step": "Register for the monthly webinar.", "tags": ["AMA", "Admissions"], "section": "macro_free"},
                {"name": "SAT Group Coaching Session", "type": "Mentor", "cost": "$99", "duration": "4 weeks", "value": "Small group review.", "next_step": "Join cohort.", "tags": ["SAT", "Coaching"], "section": "micro_structured"},
                {"name": "Advanced Subject Tutoring", "type": "Tutor", "cost": "$75", "duration": "1 session", "value": "Get customized clarification on advanced concepts.", "next_step": "Schedule topic review call.", "tags": ["Tutor", "STEM"], "section": "micro_structured"},
                {"name": "Naaviverse Academic Counsel Review", "type": "Mentor", "price": "$150", "session_details": "1-on-1 Zoom Session (45 mins)", "expected_outcomes": "Personalized assessment of GPA targets and academic safety nets.", "tags": ["Counseling", "Shortlist"], "section": "nano_expert"},
                {"name": "Elite Test Prep Coaching", "type": "Coaching", "price": "$120/hr", "session_details": "Private Tutoring", "expected_outcomes": "Custom strategy to target specific weak areas in TOEFL/IELTS.", "tags": ["Tutoring", "IELTS"], "section": "nano_expert"}
            ],
            "vendors": [
                {"name": "Khan Academy Standardized Test Prep", "type": "Course", "why": "Excellent free resource for SAT math and reading diagnostics.", "next_step": "Complete a full-length SAT mock diagnostic test.", "tags": ["SAT", "Prep", "Math"], "section": "macro_free"},
                {"name": "Albert.io AP Practice Modules", "type": "Platform", "why": "Free diagnostic exams and curriculum alignment guides.", "next_step": "Sign up and complete the algebra diagnostic.", "tags": ["AP", "Diagnostics"], "section": "macro_free"},
                {"name": "Coursera AP Computer Science", "type": "Course", "cost": "$49", "duration": "4 weeks", "value": "Structured preparation matching the high school curriculum syllabus.", "next_step": "Complete modules on Object-Oriented programming.", "tags": ["AP CS", "Java", "Coding"], "section": "micro_structured"},
                {"name": "Princeton Review Advanced Prep", "type": "Course", "cost": "$299", "duration": "6 weeks", "value": "Guided instruction and score guarantee for high-stakes tests.", "next_step": "Enroll in the weekend live instruction cohort.", "tags": ["Test Prep", "SAT", "ACT"], "section": "micro_structured"},
                {"name": "Naaviverse Expert Review Plan", "type": "Provider", "price": "$120", "session_details": "Review", "expected_outcomes": "Audit.", "tags": ["Audit"], "section": "nano_expert"},
                {"name": "PrepScholar Elite Prep Plan", "type": "Platform", "price": "$397", "session_details": "Self-paced study platform", "expected_outcomes": "AI-customized preparation strategy with mock testing.", "tags": ["PrepScholar", "Target Score"], "section": "nano_expert"}
            ],
            "institutions": [
                {"name": "MIT OpenCourseWare Calculus", "type": "University", "why": "In-depth lectures to build rigorous mathematical foundation.", "next_step": "Watch Lectures 1-5 and solve problem set 1.", "tags": ["Math", "Calculus", "MIT"], "section": "macro_free"},
                {"name": "Stanford Online Free High School Seminars", "type": "University", "why": "Empathy-led courses on modern logic and STEM research methods.", "next_step": "Audit the logic module.", "tags": ["Stanford", "Logic"], "section": "macro_free"},
                {"name": "IIT Hyderabad Foundation Program", "type": "Institute", "cost": "$150", "duration": "8 weeks", "value": "Foundational syllabus mapping top engineering standards.", "next_step": "Register for the online weekend lectures.", "tags": ["Engineering", "Syllabus"], "section": "micro_structured"},
                {"name": "BITS Pilani Online Certification", "type": "University", "cost": "$200", "duration": "10 weeks", "value": "Advanced topic certification to showcase high school rigor.", "next_step": "Submit application form.", "tags": ["Computer Science", "BITS"], "section": "micro_structured"},
                {"name": "Ivy League Admissions Summit", "type": "University", "price": "$250", "session_details": "Online Conference", "expected_outcomes": "Direct exposure to admissions directors and successful applicant profiles.", "tags": ["Ivy League", "Summit"], "section": "nano_expert"},
                {"name": "Columbia High School Summer Program", "type": "University", "price": "$3,500", "session_details": "Summer School (2 weeks)", "expected_outcomes": "Advanced credit and university-level coursework validation.", "tags": ["Columbia", "Summer School"], "section": "nano_expert"}
            ],
            "distributors": [
                {"name": "College Board Official SAT Study Guide", "type": "Book", "why": "Contains 8 official practice tests and detail scoring guide.", "next_step": "Purchase from online store and solve Test 1.", "tags": ["SAT", "Guidebook"], "section": "macro_free"},
                {"name": "Academic Excellence Open Access Library", "type": "Library", "why": "Free access to research publications and high school research journals.", "next_step": "Search archives for STEM topics.", "tags": ["Library", "Research"], "section": "macro_free"},
                {"name": "High School Science Journal subscription", "type": "Subscription", "cost": "$30/year", "duration": "Annual", "value": "Weekly articles on STEM research developments.", "next_step": "Subscribe to student digital edition.", "tags": ["STEM", "Research"], "section": "micro_structured"},
                {"name": "O'Reilly Student Annual Book Set", "type": "Book", "cost": "$45", "duration": "Annual", "value": "Comprehensive books covering computer science and logic.", "next_step": "Order physical textbooks.", "tags": ["CS", "Books"], "section": "micro_structured"},
                {"name": "Academic Excellence Newsletter", "type": "Newsletter", "price": "Free", "session_details": "Weekly Email", "expected_outcomes": "Curated list of international essay competitions and summer research programs.", "tags": ["Newsletter", "Competition"], "section": "nano_expert"},
                {"name": "IEEE Spectrum Student Subscription", "type": "Publication", "price": "$90/year", "session_details": "Digital Magazine", "expected_outcomes": "Monthly updates on scientific computing and AI strategy trends.", "tags": ["IEEE", "AI Strategy"], "section": "nano_expert"}
            ]
        }
    elif "Practical" in focus:
        return {
            "mentors": [
                {"name": "Open-Source Community Lead shadow", "type": "Mentor", "why": "Shadow active open-source contributors and learn coding standards.", "next_step": "Join the weekly developer community call.", "tags": ["Open Source", "Developer"], "section": "macro_free"},
                {"name": "GitHub Student Community Office Hours", "type": "Mentor", "why": "Get free assistance with git issues and repository structures.", "next_step": "Join the Discord forum.", "tags": ["GitHub", "Git"], "section": "macro_free"},
                {"name": "Practical Career Pathway Coach", "type": "Coaching", "cost": "$199", "duration": "4 sessions", "value": "Align project portfolio with target companies.", "next_step": "Book first calibration call.", "tags": ["Career Coach", "Portfolio"], "section": "micro_structured"},
                {"name": "Developer Portfolio Review Mentor", "type": "Tutor", "cost": "$60", "duration": "1 session", "value": "Get your projects audited by an experienced developer.", "next_step": "Schedule your project review.", "tags": ["Code Review", "Portfolio"], "section": "micro_structured"},
                {"name": "Naavi Tech Mentor Portfolio Audit", "type": "Mentor", "price": "$120", "session_details": "Async Code Review & 30-min Call", "expected_outcomes": "Code readability review, project ideas list, and GitHub optimization tips.", "tags": ["Code Review", "Portfolio"], "section": "nano_expert"},
                {"name": "Industry Engineer Interview Coaching", "type": "Coaching", "price": "$150/hr", "session_details": "1-on-1 Mock Coding Interview", "expected_outcomes": "Realistic technical interview simulation and problem-solving critiques.", "tags": ["Interview Prep", "Coding"], "section": "nano_expert"}
            ],
            "vendors": [
                {"name": "freeCodeCamp Web Development Bootcamp", "type": "Course", "why": "Hands-on projects to master HTML/CSS/Javascript and Git.", "next_step": "Complete the responsive web design certification.", "tags": ["Coding", "Web Dev", "Portfolio"], "section": "macro_free"},
                {"name": "Scrimba Frontend Developer Pathway", "type": "Course", "why": "Interactive free modules to build frontend web pages.", "next_step": "Complete HTML & CSS basics.", "tags": ["Frontend", "Scrimba"], "section": "macro_free"},
                {"name": "Codecademy Pro Career Path", "type": "Bootcamp", "cost": "$39/mo", "duration": "Self-paced", "value": "Interactive coding workspace with portfolio projects and reviews.", "next_step": "Build and push a personal portfolio project to GitHub.", "tags": ["Coding", "Portfolio", "Interviews"], "section": "micro_structured"},
                {"name": "Udemy AWS Certified Cloud Practitioner", "type": "Course", "cost": "$19", "duration": "3 weeks", "value": "Learn cloud infrastructure and obtain an industry-recognized credential.", "next_step": "Complete practice exam questions and sit for the exam.", "tags": ["AWS", "Cloud"], "section": "micro_structured"},
                {"name": "Udacity Nanodegree Program", "type": "Bootcamp", "price": "$399/mo", "session_details": "Structured Online Program", "expected_outcomes": "Graduation portfolio project certified by industry partners.", "tags": ["Nanodegree", "Project"], "section": "nano_expert"},
                {"name": "Springboard Tech Career Track", "type": "Bootcamp", "price": "$1,200", "session_details": "Project-based curriculum", "expected_outcomes": "Job guarantee and 1-on-1 mentoring support.", "tags": ["Springboard", "Placement"], "section": "nano_expert"}
            ],
            "institutions": [
                {"name": "Harvard CS50 Introduction to Computer Science", "type": "University", "why": "Comprehensive overview of algorithmic thinking and software development.", "next_step": "Watch Lecture 1 and submit Problem Set 1.", "tags": ["Computer Science", "Algorithms", "Harvard"], "section": "macro_free"},
                {"name": "MIT OpenCourseWare Python Fundamentals", "type": "University", "why": "Free university curriculum detailing object-oriented programming concepts.", "next_step": "Download files for problem set 1.", "tags": ["Python", "MIT"], "section": "macro_free"},
                {"name": "Scaler Academy Professional Track", "type": "Academy", "cost": "$1,200", "duration": "6 months", "value": "Structured software engineering program with industry mentors.", "next_step": "Pass the online coding eligibility test.", "tags": ["Software", "Placement"], "section": "micro_structured"},
                {"name": "Coding Ninjas Web Dev Track", "type": "Academy", "cost": "$150", "duration": "4 months", "value": "Live webinars, doubt support, and web application deployment projects.", "next_step": "Submit application form.", "tags": ["Web Dev", "Coding Ninjas"], "section": "micro_structured"},
                {"name": "Stanford Center for Professional Development Certificate", "type": "University", "price": "$800", "session_details": "Self-paced online module", "expected_outcomes": "Official certificate of completion in Advanced Software Foundations.", "tags": ["Stanford", "Certificate"], "section": "nano_expert"},
                {"name": "IIIT Bangalore PG Diploma in Software", "type": "Institute", "price": "$2,500", "session_details": "Online PG Diploma (1 year)", "expected_outcomes": "Earn college credits and direct corporate interview placement opportunities.", "tags": ["IIITB", "PG Diploma"], "section": "nano_expert"}
            ],
            "distributors": [
                {"name": "GitHub Developer Community & Guides", "type": "Guide", "why": "Official documentation and developer forums to learn Git workflows.", "next_step": "Create a GitHub account and initialize your first repository.", "tags": ["Git", "GitHub"], "section": "macro_free"},
                {"name": "MDN Web Docs Library", "type": "Docs", "why": "Comprehensive developer reference for HTML, CSS, and JS APIs.", "next_step": "Read documentation on array methods.", "tags": ["Docs", "Mozilla"], "section": "macro_free"},
                {"name": "O'Reilly Media Learning Subscription", "type": "Subscription", "cost": "$49/mo", "duration": "Monthly", "value": "Unlimited access to thousands of technical books, video courses, and live sandboxes.", "next_step": "Access the 'Clean Code' digital book.", "tags": ["Books", "Vids"], "section": "micro_structured"},
                {"name": "System Design Handbook by Alex Xu", "type": "Book", "cost": "$35", "duration": "Self-paced", "value": "Visual explanations of high-scale backend design paradigms.", "next_step": "Purchase and read Chapter 1.", "tags": ["System Design", "Alex Xu"], "section": "micro_structured"},
                {"name": "Dev.to Tech Blogs digest", "type": "Community", "price": "Free", "session_details": "Weekly Email", "expected_outcomes": "Stay updated with trending frameworks, open-source projects, and career tips.", "tags": ["Blogs", "Dev.to"], "section": "nano_expert"},
                {"name": "Medium Towards Data Science", "type": "Publication", "price": "$50/year", "session_details": "Premium Writer Network", "expected_outcomes": "Deep-dives into actual machine learning pipelines and engineering case studies.", "tags": ["Medium", "Data Science"], "section": "nano_expert"}
            ]
        }
    else: # Holistic
        return {
            "mentors": [
                {"name": "Alumni Mentor office hours", "type": "Mentor", "why": "Free consultations with university alumni to understand campus culture.", "next_step": "Book a 20-min chat on their open calendar.", "tags": ["Alumni", "Mentorship"], "section": "macro_free"},
                {"name": "Local Community Service Advisor", "type": "Mentor", "why": "Free counseling on aligning project ideas with local NGOs.", "next_step": "Schedule a community project consultation.", "tags": ["Community", "NGO"], "section": "macro_free"},
                {"name": "Public Speaking Coach Cohort", "type": "Coaching", "cost": "$120", "duration": "5 weeks", "value": "Live speaking drills with cohort feedback.", "next_step": "Register for the upcoming public speaking circle.", "tags": ["Communication"], "section": "micro_structured"},
                {"name": "Emotional Intelligence Coach", "type": "Coaching", "cost": "$80", "duration": "2 sessions", "value": "Understand self-motivation, empathy, and social leadership skills.", "next_step": "Book first diagnostic assessment.", "tags": ["EQ", "Coach"], "section": "micro_structured"},
                {"name": "Naavi Counselor Review & Personality Diagnostic", "type": "Mentor", "price": "$100", "session_details": "Myers-Briggs / Aptitude Zoom Session", "expected_outcomes": "Detailed profiling report highlighting soft skill strengths and growth plans.", "tags": ["Counseling", "MBTI"], "section": "nano_expert"},
                {"name": "Admissions Leadership Coach", "type": "Coaching", "price": "$130/hr", "session_details": "1-on-1 Essay Story Arcs Session", "expected_outcomes": "Select compelling personal stories for leadership essays.", "tags": ["Admissions", "Coaching"], "section": "nano_expert"}
            ],
            "vendors": [
                {"name": "Coursera Introduction to Public Speaking", "type": "Course", "why": "Learn techniques to design and deliver engaging presentations.", "next_step": "Record a 3-minute introductory speech and request feedback.", "tags": ["Communication", "Soft Skills"], "section": "macro_free"},
                {"name": "EdX Empathy in UX Design", "type": "Course", "why": "Free lectures explaining empathy mapping and human-centric logic.", "next_step": "Audit the introduction module.", "tags": ["UX", "Empathy"], "section": "macro_free"},
                {"name": "Dale Carnegie Teen Leadership Program", "type": "Course", "cost": "$395", "duration": "4 weeks", "value": "Build resilience, communication skills, and team management confidence.", "next_step": "Register for the summer leadership cohort.", "tags": ["Leadership", "Carnegie"], "section": "micro_structured"},
                {"name": "Interaction Design Foundation Soft Skills", "type": "Course", "cost": "$16/mo", "duration": "Self-paced", "value": "Understand user experience, empathy, and collaborative workshop methodologies.", "next_step": "Complete the module on stakeholder communication.", "tags": ["UX", "Collaboration"], "section": "micro_structured"},
                {"name": "Mindvalley Quest For Leadership", "type": "Platform", "price": "$299", "session_details": "Personal development curriculum", "expected_outcomes": "Validated training program covering emotional intelligence and community building.", "tags": ["Mindvalley", "EQ"], "section": "nano_expert"},
                {"name": "Sanskriti Leadership Summer Camp", "type": "Bootcamp", "price": "$850", "session_details": "4-week intensive workshop", "expected_outcomes": "Practical certificate validating local campaign execution and team building.", "tags": ["Camp", "Sanskriti"], "section": "nano_expert"}
            ],
            "institutions": [
                {"name": "Toastmasters International Youth Leadership", "type": "School", "why": "Free local clubs to build public speaking and interpersonal confidence.", "next_step": "Attend an open club meeting as a guest.", "tags": ["Public Speaking", "Leadership"], "section": "macro_free"},
                {"name": "Local YMCA Community Club", "type": "School", "why": "Free group activities focusing on public outreach and youth mentoring.", "next_step": "Register for the weekend volunteering cohort.", "tags": ["YMCA", "Community"], "section": "macro_free"},
                {"name": "Wharton Global Youth Program Certificate", "type": "University", "cost": "$450", "duration": "3 weeks", "value": "Introduction to business strategies, teamwork, and entrepreneurial leadership.", "next_step": "Apply to the Wharton online summer workshop.", "tags": ["Business", "Wharton"], "section": "micro_structured"},
                {"name": "NIFT Summer Design Seminar", "type": "Institute", "cost": "$120", "duration": "4 weeks", "value": "Introduction to design methodologies, branding, and team management.", "next_step": "Submit enrollment details.", "tags": ["NIFT", "Design"], "section": "micro_structured"},
                {"name": "Admissions Leadership Seminar (Ivy League Focus)", "type": "University", "price": "$300", "session_details": "4-week webinars series", "expected_outcomes": "Certified training on student impact, societal leadership, and college-level ethics.", "tags": ["Ethics", "Leadership"], "section": "nano_expert"},
                {"name": "HBS Online Leadership Principles Program", "type": "University", "price": "$1,600", "session_details": "Online Harvard program", "expected_outcomes": "Official Harvard credential validating team mobilization and strategic execution.", "tags": ["HBS Online", "Teamwork"], "section": "nano_expert"}
            ],
            "distributors": [
                {"name": "Ted Talks soft skills playlist", "type": "YouTube", "why": "Curated videos of elite presenters discussing communication and EQ.", "next_step": "Watch the top 3 videos and note key body language techniques.", "tags": ["Videos", "TED"], "section": "macro_free"},
                {"name": "Emotional Intelligence Toolkit Docs", "type": "Docs", "why": "Free PDFs detailing communication frameworks and conflict resolution models.", "next_step": "Download and read Chapter 1.", "tags": ["Docs", "EQ"], "section": "macro_free"},
                {"name": "Harvard Business Review teens package", "type": "Subscription", "cost": "$12/mo", "duration": "Monthly", "value": "Access to business strategy, personal growth, and leadership advice articles.", "next_step": "Read the guide on peer leadership.", "tags": ["Articles", "HBR"], "section": "micro_structured"},
                {"name": "Dale Carnegie Book Pack", "type": "Book", "cost": "$25", "duration": "Self-paced", "value": "Compendium of books covering confidence building and friendship strategy.", "next_step": "Order the book set online.", "tags": ["Books", "Dale Carnegie"], "section": "micro_structured"},
                {"name": "EQ Leadership digest", "type": "Newsletter", "price": "Free", "session_details": "Monthly email", "expected_outcomes": "A newsletter detailing emotional intelligence exercises, empathy mapping, and team conflict resolution.", "tags": ["Empathy", "EQ"], "section": "nano_expert"},
                {"name": "Rotary Club Youth Magazine", "type": "Publication", "price": "$35/year", "session_details": "Monthly Newsletter", "expected_outcomes": "Interviews with young leaders globally and notices of project funding opportunities.", "tags": ["Magazine", "Funding"], "section": "nano_expert"}
            ]
        }

def customize_steps_for_focus(steps_configs: list, focus: Optional[str], goal: str) -> list:
    if not focus:
        return steps_configs
    
    category = resolve_focus_category(focus)
    is_academic = category == "academic"
    is_practical = category == "practical"
    is_jobs = category == "jobs"
    is_non_academic = category == "non_academic"

    custom_configs = []
    for step in steps_configs:
        step_copy = step.copy()
        title = step_copy["title"]
        desc = step_copy["description"]
        macro = step_copy.get("macro_view", "")
        micro = step_copy.get("micro_view", "")
        nano = step_copy.get("nano_view", "")

        focus_lower = focus.lower()
        if is_academic:
            if "research & honors" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Research Question & Literature Review") \
                             .replace("Academic Curation & GPA Target", "Research Scoping & Honor Roll Planning") \
                             .replace("Board Achievement", "Honors Thesis & Academic Publication Mastery") \
                             .replace("Skill Curation & Internship Selection", "Research Paper Drafting & Seminar Presentation") \
                             .replace("Academics & Profile Rigor", "Academic Publication & Independent Study Rigor") \
                             .replace("Test Score Curation", "Research Poster Curation & Review") \
                             .replace("Standardized Test Prep Modules", "Academic Research Methodology Modules") \
                             .replace("Standardized Test Prep", "Research Methodologies") \
                             .replace("Standardized Test Score Curation", "Academic Manuscript Review") \
                             .replace("Standardized Test", "Research Defense") \
                             .replace("Admissions Finalization & Visas", "Academic Symposium Placement & Fellowships")
                desc = desc.replace("internships", "independent research").replace("practical skills", "academic methodologies").replace("board exams", "research reviews").replace("study schedules", "literature review plans")
                desc += " Prioritize original research papers, academic honors, independent studies, and publication cycles."
                macro = f"Focusing on high-prestige academic research and honors designations: {macro}"
                micro = f"Execute research projects, literature reviews, and manuscript writing: {micro}"
                nano = f"Research mentor audit of methodologies and bibliography sources: {nano}"
            elif "test prep" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Admissions Standardized Test Diagnostic") \
                             .replace("Academic Curation & GPA Target", "SAT/ACT Prep & admissions Milestones") \
                             .replace("Board Achievement", "Standardized Test Score Optimization") \
                             .replace("Skill Curation & Internship Selection", "Admissions Essays & AP Course Selection") \
                             .replace("Academics & Profile Rigor", "Standardized Testing & AP/IB Exam Rigor") \
                             .replace("Test Score Curation", "Admissions Portfolio & Test Submissions") \
                             .replace("Standardized Test Prep Modules", "Intense SAT/ACT/IELTS Test Prep Modules") \
                             .replace("Standardized Test Prep", "Standardized Test Prep Modules") \
                             .replace("Standardized Test Score Curation", "Mock Test Scoring & Errors Analysis") \
                             .replace("Standardized Test", "Official SAT/ACT Sitting") \
                             .replace("Admissions Finalization & Visas", "University Admissions Portals & Visas Submission")
                desc = desc.replace("internships", "mock sittings").replace("practical skills", "exam test-taking strategies").replace("board exams", "standardized exams").replace("study schedules", "test-prep modules")
                desc += " Focus intensely on standardized test preparation (SAT, ACT, AP, IELTS) and college admissions portals."
                macro = f"Focusing on competitive standardized testing and strategic college admissions: {macro}"
                micro = f"Complete rigorous mock tests, review diagnostic reports, and finalize application essay drafts: {micro}"
                nano = f"Test-prep mentor audit of scoring errors and essay critiques: {nano}"
            else:  # Curriculum & GPA Focus
                title = title.replace("Academic Target & Profile Review", "GPA Benchmark & Course Selection") \
                             .replace("Academic Curation & GPA Target", "GPA Curation & Class Standing Plan") \
                             .replace("Board Achievement", "School Curriculum Term Exams Mastery") \
                             .replace("Skill Curation & Internship Selection", "Course Syllabus Selection & GPA Maximization") \
                             .replace("Academics & Profile Rigor", "Curriculum Tracking & Class Rank Rigor") \
                             .replace("Test Score Curation", "GPA Checklist & Transcripts Review") \
                             .replace("Standardized Test Prep Modules", "School Subject Syllabus Chapters Modules") \
                             .replace("Standardized Test Prep", "School Subject Prep Modules") \
                             .replace("Standardized Test Score Curation", "Subject Exam Grade Diagnostics") \
                             .replace("Standardized Test", "Term/Board Subject Exam") \
                             .replace("Admissions Finalization & Visas", "Transcripts Evaluation & GPA Verification")
                desc = desc.replace("internships", "course modules").replace("practical skills", "coursework understanding").replace("board exams", "class term tests").replace("study schedules", "subject revision cards")
                desc += " Emphasize school/college course requirements, maximizing term GPAs, and class rank optimization."
                macro = f"Focusing on core curriculum excellence and GPA maximization: {macro}"
                micro = f"Complete weekly coursework reviews, submit class assignments, and track term exam grades: {micro}"
                nano = f"Subject tutor audit of grade reports and course content mastery: {nano}"

        elif is_practical:
            if "project portfolio" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Project Idea & Tech Stack Review") \
                             .replace("Academic Curation & GPA Target", "Open Source & Git Repository Launch") \
                             .replace("Board Achievement", "Full-Stack Project Launch Milestone") \
                             .replace("Academics & Profile Rigor", "GitHub Portfolio & Project Readme Rigor") \
                             .replace("Test Score Curation", "Project Showcase & Live Demo Audits") \
                             .replace("Standardized Test Prep Modules", "Coding Projects Architecture Modules") \
                             .replace("Standardized Test Prep", "Coding Projects Building") \
                             .replace("Standardized Test Score Curation", "Code Review & Refactoring Diagnostics") \
                             .replace("Standardized Test", "Project Demo Presentation") \
                             .replace("Admissions Finalization & Visas", "Project Portfolio Showcase & Coding Interviews")
                desc = desc.replace("board exams", "coding projects").replace("academic preparation", "technical building").replace("standardized test", "project deployment").replace("GPA", "github contributions")
                desc += " Emphasize building side-projects, launching open-source packages, and optimizing GitHub repositories."
                macro = f"Focusing on hands-on project building and open-source contributions: {macro}"
                micro = f"Write clean modular code, write comprehensive documentation, and deploy live demos to cloud host: {micro}"
                nano = f"Senior developer audit of code quality and repository READMEs: {nano}"
            elif "certification & bootcamp" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Professional Certification Track Selection") \
                             .replace("Academic Curation & GPA Target", "Bootcamp Prep & Lab Sandbox Setup") \
                             .replace("Board Achievement", "Professional Certification Exam Completion") \
                             .replace("Academics & Profile Rigor", "Bootcamp Assignments & Lab Projects Rigor") \
                             .replace("Test Score Curation", "Certification Credentials Registry Review") \
                             .replace("Standardized Test Prep Modules", "Certification Syllabus Training Modules") \
                             .replace("Standardized Test Prep", "Bootcamp Sandbox Training") \
                             .replace("Standardized Test Score Curation", "Practice Exam Scoring Diagnostics") \
                             .replace("Standardized Test", "Official Certification Exam Sitting") \
                             .replace("Admissions Finalization & Visas", "Credentials Validation & Technical Audits")
                desc = desc.replace("board exams", "certification exams").replace("academic preparation", "structured bootcamp labs").replace("standardized test", "credential exam").replace("GPA", "lab completion rate")
                desc += " Emphasize professional certifications (AWS, Google Cloud, Scrum Master, Cisco) and coding bootcamps."
                macro = f"Focusing on professional cloud/tech certifications and structured bootcamps: {macro}"
                micro = f"Complete sandboxed lab tutorials, complete bootcamp assignments, and take certification practice tests: {micro}"
                nano = f"Bootcamp instructor audit of sandbox labs and exam readiness reports: {nano}"
            else:  # Internship & Applied Focus
                title = title.replace("Academic Target & Profile Review", "Applied Skill Mapping & Internship Scoping") \
                             .replace("Academic Curation & GPA Target", "Cold Outreach & Apprenticeship Pipeline") \
                             .replace("Board Achievement", "Corporate Internship Offer Validation") \
                             .replace("Academics & Profile Rigor", "Corporate Project Delivery & Applied Rigor") \
                             .replace("Test Score Curation", "Internship Review & Professional Referral Plan") \
                             .replace("Standardized Test Prep Modules", "Applied Work Experience Prep Modules") \
                             .replace("Standardized Test Prep", "Corporate Project Scoping") \
                             .replace("Standardized Test Score Curation", "Applied Performance Review Diagnostics") \
                             .replace("Standardized Test", "Applied Project Presentation") \
                             .replace("Admissions Finalization & Visas", "Internship Continuity & Professional Referrals")
                desc = desc.replace("board exams", "corporate deliverables").replace("academic preparation", "applied workplace skill acquisition").replace("standardized test", "internship reviews").replace("GPA", "manager feedback score")
                desc += " Emphasize corporate internships, job shadowing, applied workplace skills, and securing professional referrals."
                macro = f"Focusing on applied corporate internship experiences and workspace readiness: {macro}"
                micro = f"Apply to short internships, shadows industry professionals, and complete workspace project tasks: {micro}"
                nano = f"Corporate mentor audit of workplace project deliverables and professional communication: {nano}"

        elif is_jobs:
            if "technical role" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Technical Role Fit & LeetCode Plan") \
                             .replace("Academic Curation & GPA Target", "System Design & Algorithms Map") \
                             .replace("Board Achievement", "Technical Assessment Mastery") \
                             .replace("Academics & Profile Rigor", "LeetCode Solutions & Tech Specs Rigor") \
                             .replace("Test Score Curation", "Technical Portfolio & Github Reviews") \
                             .replace("Standardized Test Prep Modules", "LeetCode Hackerrank Solving Modules") \
                             .replace("Standardized Test Prep", "Coding Assessment Prep") \
                             .replace("Standardized Test Score Curation", "Mock Technical Scoring Diagnostics") \
                             .replace("Standardized Test", "Official Technical Interview Sitting") \
                             .replace("Admissions Finalization & Visas", "Technical Placement & Team Onboarding")
                desc = desc.replace("study schedules", "coding sessions").replace("GPA", "algorithmic skill").replace("test sittings", "coding assessments").replace("board exams", "technical test milestones")
                desc += " Focus on technical role metrics: algorithm challenges, system design patterns, and coding interviews."
                macro = f"Focusing on competitive technical role assessments and system design: {macro}"
                micro = f"Solve 10+ LeetCode problems weekly, practice system design architectures, and complete mock technical tests: {micro}"
                nano = f"Technical lead mentor review of algorithm efficiency and system patterns: {nano}"
            elif "interview & networking" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "LinkedIn Curation & Networking Pipeline") \
                             .replace("Academic Curation & GPA Target", "Informational Interviews & Cold Outreach") \
                             .replace("Board Achievement", "Networking Referral Pipeline Mastery") \
                             .replace("Academics & Profile Rigor", "Mock Behavioral Interviews & Speech Rigor") \
                             .replace("Test Score Curation", "Job Referrals & Interview Schedules") \
                             .replace("Standardized Test Prep Modules", "Behavioral Interview Prep Modules") \
                             .replace("Standardized Test Prep", "Elevator Pitch Practice") \
                             .replace("Standardized Test Score Curation", "Informational Interview Diagnostics") \
                             .replace("Standardized Test", "Informational Outreach Session") \
                             .replace("Admissions Finalization & Visas", "Job Offer Negotiation & Referral Closure")
                desc = desc.replace("study schedules", "outreach tracking").replace("GPA", "professional networking").replace("test sittings", "referral queries").replace("board exams", "mock behavioral interviews")
                desc += " Focus on networking: LinkedIn profile optimization, informational interviews, cold outreach, and mock behavioral interviews."
                macro = f"Focusing on industry networking and behavioral interview preparation: {macro}"
                micro = f"Reach out to 5 industry professionals weekly, schedule informational interviews, and practice elevator pitches: {micro}"
                nano = f"Career coach audit of professional communication style and outreach follow-ups: {nano}"
            else:  # Resume & Career Evidence Focus
                title = title.replace("Academic Target & Profile Review", "Resume Audit & Gap Assessment") \
                             .replace("Academic Curation & GPA Target", "Cover Letter Drafts & Resume Customization") \
                             .replace("Board Achievement", "Resume Portfolio Submissions Milestone") \
                             .replace("Academics & Profile Rigor", "Workplace Evidence & Portfolio Proofs") \
                             .replace("Test Score Curation", "Application Tracker & Follow-up Plans") \
                             .replace("Standardized Test Prep Modules", "ATS Formatting Resume Modules") \
                             .replace("Standardized Test Prep", "Resume Building Modules") \
                             .replace("Standardized Test Score Curation", "Resume ATS Screening Diagnostics") \
                             .replace("Standardized Test", "Resume Submission Review") \
                             .replace("Admissions Finalization & Visas", "Background Check & Employment Contracts")
                desc = desc.replace("study schedules", "resume tracking").replace("GPA", "resume competitiveness").replace("test sittings", "resume reviews").replace("board exams", "resume draft milestones")
                desc += " Focus on resume quality: tailoring descriptions, optimizing for ATS, building career folders, and job application tracking."
                macro = f"Focusing on ATS-optimized resume building and professional evidence portfolios: {macro}"
                micro = f"Draft 3 custom resumes, optimize summaries for target roles, and populate application tracker sheets: {micro}"
                nano = f"HR consultant review of resume formats and ATS compatibility benchmarks: {nano}"

        elif is_non_academic:
            if "mental health" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Mental Health Audit & Support Mapping") \
                             .replace("Academic Curation & GPA Target", "Stress Management & Daily Routines") \
                             .replace("Board Achievement", "Mindfulness & Sleep Hygiene Milestone") \
                             .replace("Internship Planning & Skill Curation", "Coping Mechanisms & Safe Boundaries") \
                             .replace("Academics & Profile Rigor", "Emotional Resilience & Weekly Journaling") \
                             .replace("Test Score Curation", "Mental Health Diagnostics & Checkpoints") \
                             .replace("University Placements Submission", "Mental Wellness Plan Review") \
                             .replace("Admissions Finalization & Visas", "Ongoing Counselling & Care Continuity")
                desc = desc.replace("academic preparation", "wellness planning").replace("study schedules", "wellness routines").replace("GPA", "wellbeing indicators").replace("test sittings", "mood checks").replace("board exams", "mental stressors")
                desc += " This is a health support path; include wellness routines, coping tools, journaling, and professional support references."
                macro = f"Focusing on mental wellness routines and emotional resilience: {macro}"
                micro = f"Complete daily 10-minute mindfulness sessions, maintain sleep diaries, and complete weekly journaling: {micro}"
                nano = f"Licensed counsellor review of wellbeing charts and routine continuity: {nano}"
            elif "life skills" in focus_lower:
                title = title.replace("Academic Target & Profile Review", "Time Management & Routine Mapping") \
                             .replace("Academic Curation & GPA Target", "Decision Frameworks & Goal Planning") \
                             .replace("Board Achievement", "Daily Organization & Habit Mastery") \
                             .replace("Internship Planning & Skill Curation", "Task Prioritization & Focus Boundaries") \
                             .replace("Academics & Profile Rigor", "Personal Finance & Budgeting Routines") \
                             .replace("Test Score Curation", "Goal Progress & Habit Tracker Checkpoints") \
                             .replace("University Placements Submission", "Life Goals Blueprint Review") \
                             .replace("Admissions Finalization & Visas", "Autonomy Development & Life Coaching Continuity")
                desc = desc.replace("academic preparation", "routine design").replace("study schedules", "habit trackers").replace("GPA", "organization levels").replace("test sittings", "habit audits").replace("board exams", "daily routines")
                desc += " Focus on core life skills: time management, task prioritization, decision-making logs, and habit routines."
                macro = f"Focusing on structured life skills coaching and time management: {macro}"
                micro = f"Log daily tasks, compile decision logs, establish weekly calendars, and review budget sheets: {micro}"
                nano = f"Life coach audit of time allocation charts and habit compliance scores: {nano}"
            else:  # Immediate Action & Support Focus
                title = title.replace("Academic Target & Profile Review", "Urgent Support Scoping & Triage") \
                             .replace("Academic Curation & GPA Target", "Immediate Helpline & Resource Mapping") \
                             .replace("Board Achievement", "Immediate Support Group Engagement") \
                             .replace("Internship Planning & Skill Curation", "Escalation Routes & Safe Space Setup") \
                             .replace("Academics & Profile Rigor", "Safety Plans & Crisis Response Setup") \
                             .replace("Test Score Curation", "Immediate Safety Checkpoints") \
                             .replace("University Placements Submission", "Crisis Care Plan Review") \
                             .replace("Admissions Finalization & Visas", "Qualified Peer & Clinical Referrals Continuity")
                desc = desc.replace("academic preparation", "safety checks").replace("study schedules", "safety boundaries").replace("GPA", "crisis indicators").replace("test sittings", "check-ins").replace("board exams", "stress events")
                desc += " Focus on short-term navigation: hotline contacts, safe support networks, and qualified medical/peer escalation paths."
                macro = f"Focusing on immediate crisis care navigation and safety-aware triage: {macro}"
                micro = f"Identify 3 immediate hotlines, map emergency contacts, and document a short-term crisis safety plan: {micro}"
                nano = f"Support representative review of safety plan details and escalation guidelines: {nano}"

        step_copy["title"] = title
        step_copy["description"] = desc
        step_copy["macro_view"] = macro
        step_copy["micro_view"] = micro
        step_copy["nano_view"] = nano
        custom_configs.append(step_copy)

    return custom_configs

def calculate_path_metrics(current: str, goal: str, profile: dict, path_type: str = "Academic & Research") -> dict:
    # 1. total_duration based on target degree type first, then grade fallback.
    total_months = calculate_total_duration_months(current, goal, profile)
    total_duration = format_total_duration(total_months)

    # 2. Competitiveness of target goal
    goal_lower = goal.lower()
    competitive_keywords = [
        "harvard", "yale", "stanford", "mit", "oxford", "cambridge", "iit", "bits",
        "imperial college", "caltech", "berkeley", "princeton", "columbia", "cornell"
    ]
    is_highly_competitive = any(kw in goal_lower for kw in competitive_keywords)

    # 3. Performance
    perf_str = str(profile.get("performance") or "").lower()
    if "90" in perf_str or "above 90" in perf_str or "excellent" in perf_str or "gpa 4" in perf_str or "a+" in perf_str:
        perf_cat = "high"
    elif "75" in perf_str or "80" in perf_str or "85" in perf_str or "good" in perf_str or "average" in perf_str:
        perf_cat = "medium"
    else:
        perf_cat = "low"

    # 4. Profile signals
    stream_str = str(profile.get("stream") or "").lower()
    curriculum_str = str(profile.get("curriculum") or "").lower()
    personality_str = str(profile.get("personality") or "").lower()
    financial_str = str(profile.get("financialSituation") or "").lower()

    # ──────────────────────────────────────────────────────────────────────────
    # READINESS MODEL: Like ETA logic β€” the score indicates HOW READY the
    # student's current profile is to achieve the goal via THIS specific path.
    # Each path type evaluates different signals to compute a unique score.
    # ──────────────────────────────────────────────────────────────────────────

    base_score = 0

    path_category = resolve_focus_category(path_type)

    if path_category == "academic":
        # ACADEMIC PATH: Readiness = alignment of academic profile to formal education route
        # Base from performance
        base_score = {"high": 55, "medium": 35, "low": 20}.get(perf_cat, 20)
        # Boost for academic curriculum match (CBSE/IB/IGCSE students are already in academic track)
        if any(k in curriculum_str for k in ["cbse", "ib", "igcse", "cambridge", "icse"]):
            base_score += 10
        # Boost for science/commerce stream (university pathway relevant)
        if any(k in stream_str for k in ["science", "maths", "math", "commerce"]):
            base_score += 8
        # Penalize if highly competitive goal and performance is low
        if is_highly_competitive and perf_cat == "low":
            base_score -= 15
        # Boost if target is a university/degree in goal
        if any(k in goal_lower for k in ["bachelor", "master", "university", "college", "degree", "phd", "bsc", "btech", "mbbs"]):
            base_score += 7

    elif path_category == "practical":
        # PRACTICAL PATH: Readiness = how well profile fits skills/project-based learning
        base_score = {"high": 50, "medium": 40, "low": 30}.get(perf_cat, 30)
        # Skills-based goals are practical by nature β€” boost it
        if any(k in goal_lower for k in ["engineer", "developer", "software", "data", "ai", "machine learning", "design", "architecture"]):
            base_score += 12
        # Personality signals for hands-on learners
        if any(k in personality_str for k in ["practical", "hands-on", "builder", "maker", "curious", "creative"]):
            base_score += 8
        # Financial support matters for certifications/bootcamps
        if any(k in financial_str for k in ["scholarship", "support", "funded", "family support"]):
            base_score += 5
        # All streams are eligible for skills path β€” no penalty
        # But science stream has a slight edge
        if "science" in stream_str:
            base_score += 5

    elif path_category == "jobs":
        # JOBS PATH: Readiness = alignment to job/career preparedness
        base_score = {"high": 45, "medium": 35, "low": 25}.get(perf_cat, 25)
        # Career-focused goals naturally align
        if any(k in goal_lower for k in ["career", "job", "placement", "intern", "manager", "analyst", "consultant", "hr", "finance"]):
            base_score += 12
        # Soft-skill personality signals
        if any(k in personality_str for k in ["leader", "communication", "team", "social", "networking", "public speaking"]):
            base_score += 10
        # Commerce/management stream aligns strongly
        if any(k in stream_str for k in ["commerce", "business", "management", "arts", "humanities"]):
            base_score += 8
        # Financial stability helps career networking activities
        if any(k in financial_str for k in ["self-funded", "employed", "working"]):
            base_score += 5

    elif path_category == "non_academic":
        # NON-ACADEMIC SUPPORT PATH: score reflects support-plan readiness, not career readiness.
        base_score = {"high": 45, "medium": 38, "low": 30}.get(perf_cat, 35)
        if any(k in personality_str for k in ["anxious", "stress", "overwhelmed", "confused", "decision"]):
            base_score += 8
        if any(k in financial_str for k in ["support", "family", "funded", "stable"]):
            base_score += 6
        if any(k in goal_lower for k in ["mental", "stress", "wellness", "counselling", "counseling", "decision", "life", "guidance"]):
            base_score += 12

    # 5. Apply competitive penalty uniformly
    if is_highly_competitive:
        base_score = int(base_score * 0.70)  # 30% harder to be "ready" for top-tier goals

    # Apply variant-specific offset to ensure distinct readiness scores between alternatives
    path_type_lower = str(path_type or "").lower()
    variant_offset = 0
    if "research" in path_type_lower or "portfolio" in path_type_lower or "technical role" in path_type_lower or "mental health" in path_type_lower:
        variant_offset = +3
    elif "test prep" in path_type_lower or "certification" in path_type_lower or "interview" in path_type_lower or "life skills" in path_type_lower:
        variant_offset = -4
    elif "curriculum" in path_type_lower or "applied" in path_type_lower or "resume" in path_type_lower or "immediate action" in path_type_lower:
        variant_offset = +1
    base_score += variant_offset

    # 6. Clamp score between 5 and 95
    readiness_score = max(5, min(95, base_score))

    # 7. Compute readiness label from final score
    if readiness_score >= 70:
        readiness_label = "Advanced Starter"
    elif readiness_score >= 50:
        readiness_label = "Intermediate Starter"
    elif readiness_score >= 30:
        readiness_label = "Early Starter"
    else:
        readiness_label = "Beginner"

    return {
        "total_duration": total_duration,
        "readiness_score": readiness_score,
        "readiness_label": readiness_label
    }


# ─── PATH ACCURACY SCORING MODEL ─────────────────
def calculate_path_accuracy_score(roadmap: dict, profile: dict, current: str = "") -> dict:
    import math
    if not roadmap:
        return {
            "accuracy_score": 0,
            "accuracy_label": "Needs Improvement",
            "accuracy_color": "#c5221f",
            "breakdown": {
                "structural": 0,
                "content": 0,
                "market": 0,
                "structural_score": 0,
                "content_score": 0,
                "market_score": 0
            },
            "details": {}
        }
        
    steps = roadmap.get("steps") or roadmap.get("steps") or []
    actual_steps = len(steps)
    if actual_steps == 0:
        return {
            "accuracy_score": 0,
            "accuracy_label": "Needs Improvement",
            "accuracy_color": "#c5221f",
            "breakdown": {
                "structural": 0,
                "content": 0,
                "market": 0,
                "structural_score": 0,
                "content_score": 0,
                "market_score": 0
            },
            "details": {}
        }

    # Weights for the Mathematical Model
    w_steps = 0.30
    w_info = 0.40
    w_market = 0.30
    
    # ── 1. Step Count Accuracy Model (S_steps) ──
    grade_str = str(profile.get("grade") or "").lower() or str(current).lower()
    if "10" in grade_str or "tenth" in grade_str or "k-12" in grade_str or "k12" in grade_str:
        expected_steps = 12
    elif "11" in grade_str or "eleventh" in grade_str:
        expected_steps = 10
    elif "12" in grade_str or "twelfth" in grade_str:
        expected_steps = 8
    else:
        expected_steps = 10
        
    sigma = 2.0
    S_steps = math.exp(-((actual_steps - expected_steps)**2) / (2 * sigma**2))
    
    # ── 2. Information Density Model (S_info) ──
    total_I = 0.0
    T_desc, T_macro, T_micro, T_nano = 100, 200, 200, 200
    
    for step in steps:
        l_desc = len(str(step.get("description") or ""))
        l_macro = len(get_view_description(step, "macro_view"))
        l_micro = len(get_view_description(step, "micro_view"))
        l_nano = len(get_view_description(step, "nano_view"))
        
        D_desc = min(1.0, l_desc / T_desc)
        D_macro = min(1.0, l_macro / T_macro)
        D_micro = min(1.0, l_micro / T_micro)
        D_nano = min(1.0, l_nano / T_nano)
        
        objs = step.get("learning_objectives") or []
        msteps = step.get("micro_steps") or []
        C_objs = min(1.0, len(objs) / 3.0) if isinstance(objs, list) else 0.0
        C_msteps = min(1.0, len(msteps) / 3.0) if isinstance(msteps, list) else 0.0
        
        I_i = (D_desc + D_macro + D_micro + D_nano + C_objs + C_msteps) / 6.0
        total_I += I_i
        
    S_info = total_I / actual_steps
    
    # ── 3. Marketplace Completeness Model (S_market) ──
    # Current scoring includes relevance to the step, not just item counts.
    stopwords = {
        "the", "and", "for", "with", "from", "into", "that", "this", "their", "your",
        "you", "are", "will", "must", "should", "step", "phase", "month", "months",
        "student", "students", "path", "pathway", "goal", "target", "current",
        "complete", "completion", "review", "resource", "resources", "course",
        "program", "platform", "session", "support", "prepare", "preparation",
        "build", "based", "using", "through", "during", "toward", "towards"
    }

    def normalize_token(token: str) -> str:
        token = token.lower().strip()
        for suffix in ("ing", "ed", "es", "s"):
            if len(token) > len(suffix) + 3 and token.endswith(suffix):
                return token[:-len(suffix)]
        return token

    def tokens_from_text(value: Any) -> set:
        if isinstance(value, dict):
            value = " ".join(str(v) for v in value.values())
        elif isinstance(value, list):
            value = " ".join(str(v) for v in value)
        text = str(value or "").lower()
        raw_tokens = re.findall(r"[a-z0-9]+", text)
        return {
            normalized
            for tok in raw_tokens
            if len(tok) >= 3
            for normalized in [normalize_token(tok)]
            if normalized not in stopwords
        }

    def collect_step_tokens(step: dict) -> set:
        step_parts = [
            roadmap.get("path_title", ""),
            roadmap.get("path_description", ""),
            step.get("title", ""),
            step.get("description", ""),
            get_view_description(step, "macro_view"),
            get_view_description(step, "micro_view"),
            get_view_description(step, "nano_view"),
            step.get("learning_objectives", []),
            step.get("micro_steps", []),
            profile.get("grade", ""),
            profile.get("curriculum", ""),
            profile.get("stream", ""),
            profile.get("performance", ""),
            profile.get("degreeType", ""),
            profile.get("degree_type", ""),
        ]
        return tokens_from_text(step_parts)

    def collect_item_tokens(item: dict) -> set:
        return tokens_from_text([
            item.get("name", ""),
            item.get("type", ""),
            item.get("why", ""),
            item.get("value", ""),
            item.get("next_step", ""),
            item.get("session_details", ""),
            item.get("expected_outcomes", ""),
            item.get("tags", []),
        ])

    def item_field_score(item: dict, required_fields: list) -> float:
        if not isinstance(item, dict) or not required_fields:
            return 0.0
        present = 0
        for field in required_fields:
            value = item.get(field)
            if isinstance(value, list):
                present += 1 if len(value) > 0 else 0
            elif str(value or "").strip():
                present += 1
        return present / len(required_fields)

    def item_relevance_score(step_tokens: set, item: dict) -> float:
        item_tokens = collect_item_tokens(item)
        if not step_tokens or not item_tokens:
            return 0.0
        overlap = step_tokens & item_tokens
        item_coverage = len(overlap) / max(1, min(len(item_tokens), 6))
        step_coverage = len(overlap) / max(1, min(len(step_tokens), 10))
        return min(1.0, (0.70 * item_coverage) + (0.30 * step_coverage))

    marketplace_sections = {
        "macro_free": ["name", "type", "why", "next_step"],
        "micro_structured": ["name", "type", "cost", "duration", "value", "next_step"],
        "nano_expert": ["name", "type", "price", "session_details", "expected_outcomes"],
    }

    total_M = 0.0
    total_market_presence = 0.0
    total_market_quality = 0.0
    total_market_relevance = 0.0
    section_count = len(marketplace_sections)

    for step in steps:
        market = combined_marketplace_from_step(step)
        step_tokens = collect_step_tokens(step)
        step_market_score = 0.0
        step_presence_score = 0.0
        step_quality_score = 0.0
        step_relevance_score = 0.0

        if isinstance(market, dict):
            for section, required_fields in marketplace_sections.items():
                items = marketplace_items_for_section(market, section)

                count_score = min(1.0, len(items) / 2.0)
                field_scores = [item_field_score(item, required_fields) for item in items if isinstance(item, dict)]
                relevance_scores = [item_relevance_score(step_tokens, item) for item in items if isinstance(item, dict)]
                field_score = sum(field_scores) / len(field_scores) if field_scores else 0.0
                relevance_score = sum(relevance_scores) / len(relevance_scores) if relevance_scores else 0.0

                section_score = (0.25 * count_score) + (0.25 * field_score) + (0.50 * relevance_score)
                step_market_score += section_score
                step_presence_score += count_score
                step_quality_score += field_score
                step_relevance_score += relevance_score

        total_M += step_market_score / section_count
        total_market_presence += step_presence_score / section_count
        total_market_quality += step_quality_score / section_count
        total_market_relevance += step_relevance_score / section_count

    S_market = total_M / actual_steps
    S_market_presence = total_market_presence / actual_steps
    S_market_quality = total_market_quality / actual_steps
    S_market_relevance = total_market_relevance / actual_steps
    
    # ── Total Score ──
    raw_score = 100.0 * (w_steps * S_steps + w_info * S_info + w_market * S_market)
    total_score = min(100, round(raw_score))
    
    if total_score >= 90:
        label = "Excellent Accuracy"
        color = "#137333"
    elif total_score >= 75:
        label = "High Accuracy"
        color = "#1a73e8"
    elif total_score >= 55:
        label = "Moderate Accuracy"
        color = "#b06000"
    elif total_score >= 35:
        label = "Low Accuracy"
        color = "#c5221f"
    else:
        label = "Needs Improvement"
        color = "#c5221f"
        
    return {
        "accuracy_score": total_score,
        "accuracy_label": label,
        "accuracy_color": color,
        "breakdown": {
            "structural": round(S_steps * 100),
            "content": round(S_info * 100),
            "market": round(S_market * 100),
            "structural_score": round(S_steps * 100),
            "content_score": round(S_info * 100),
            "market_score": round(S_market * 100)
        },
        "details": {
            "step_count": actual_steps,
            "expected_steps": expected_steps,
            "s_steps": round(S_steps, 3),
            "s_info": round(S_info, 3),
            "s_market": round(S_market, 3),
            "s_market_presence": round(S_market_presence, 3),
            "s_market_field_quality": round(S_market_quality, 3),
            "s_market_relevance": round(S_market_relevance, 3),
            "market_formula": "0.25 presence + 0.25 field quality + 0.50 step relevance"
        }
    }



# Pedagogical High-Fidelity Fallback Roadmap in case of a complete API lockout
def get_fallback_mock_roadmap(current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, focus: Optional[str] = None) -> dict:
    grade_str = str(profile.get("grade") or "").lower() or current.lower()
    total_months = calculate_total_duration_months(current, goal, profile)
    
    # Aryan's Pathway structural mapping
    if "10" in grade_str or "tenth" in grade_str:
        steps_configs = [
            {
                "id": 1,
                "title": "Curriculum & Stream Assessment",
                "duration": "Months 1-4",
                "description": f"Choose the right academic curriculum and stream (CBSE, IB, Cambridge) based on passion, personality assessment, and aptitude for {goal}.",
                "macro_view": "Selecting the right academic curriculum and subject stream is the single most impactful decision a student makes at the beginning of their educational journey, as it determines which doors open and which permanently close in the path toward a competitive university placement. This milestone establishes the foundational academic identity of the student β€” whether they pursue a science-heavy CBSE pathway, a globally recognized IB Diploma, or a rigorous Cambridge A-Level track β€” each of which signals a different level of academic ambition to admissions committees. Getting this choice correct means aligning the student's natural aptitude scores, RIASEC personality profile, and long-term career aspirations with the academic demands of the chosen stream, avoiding costly mid-stream transfers later. By the end of this milestone, the student will have a clear academic roadmap template tied directly to the demands of their target destination: {goal}.",

"micro_view": "The student must complete a structured three-step execution process during this phase: first, take a validated psychometric assessment (e.g. Holland RIASEC Code Test or the 16Personalities aptitude tool) to identify core academic strengths and interests that should drive stream selection. Second, research and visit at least 3 potential schools offering the identified curriculum (CBSE/IB/Cambridge), evaluating each against criteria such as faculty quality, extracurricular options, lab infrastructure, and placement track record. Third, compile a written Academic Goal Statement of 500 words documenting the chosen stream, the rationale for the choice, the target GPA for Grade 10 board exams, and the extracurricular activities planned for profile enrichment over the next 12 months. All three deliverables must be reviewed and signed off before moving to the next milestone.",

"nano_view": "A mentor or academic counselor should conduct an initial 45-minute intake diagnostic session reviewing the student's psychometric results, past academic performance records, and stated career interests to validate that the stream selection aligns with realistic university placement benchmarks for {goal}. The mentor should run a gap analysis comparing the student's current aptitude scores against the entry requirements of top universities in the target category, flagging any immediate curriculum risks or subject gaps that need to be addressed in Grade 10. A peer cohort review session should also be conducted where the student presents their Academic Goal Statement to a group of 3-4 senior students who have already navigated the same pathway, receiving structured feedback on blind spots and subject combination pitfalls. Expert feedback from this session must be incorporated into a revised Academic Goal Statement before the student formally commits to a school and stream.",
            },
            {
                "id": 2,
                "title": "School Selection & Academic Targets",
                "duration": "Months 5-8",
                "description": "Establish target schools and set clear academic targets. Focus on setting study habits and foundation metrics.",
                "macro_view": "Establish high-caliber academic environments and target standards required for global universities.",
                "micro_view": "Select schools based on location, budget, and mentor support, and finalize a weekly study timeline.",
                "nano_view": "Obtain mentor diagnostic feedback on academic preparation and school resources mapping."
            },
            {
                "id": 3,
                "title": "Grade 10 Board Achievement",
                "duration": "Months 9-12",
                "description": "Achieve 90%+ in 10th-grade board exams. Master core academic concepts and prepare comprehensive exam notes.",
                "macro_view": "Build a stellar academic foundation that serves as the official transcript entry point.",
                "micro_view": "Complete diagnostic mock board tests, analyze weak chapters, and compile summary revision booklets.",
                "nano_view": "Conduct progress check-ins with top-scoring student cohorts and board subject specialists."
            },
            {
                "id": 4,
                "title": "Internship Planning & Skill Curation",
                "duration": "Months 13-16",
                "description": "Focus on selecting and executing introductory internships to acquire practical skills and discover interests.",
                "macro_view": "Supplement theoretical classroom learning with real-world project work and corporate exposure.",
                "micro_view": "Apply for short internships, shadow industry specialists, and compile project reports.",
                "nano_view": "Work with internship coordinators to align tasks with career interests and get reviews on deliverables."
            },
            {
                "id": 5,
                "title": "Grade 11 Transition & Diagnostic Test Prep",
                "duration": "Months 17-20",
                "description": "Transition to 11th grade successfully. Initiate standardized test prep diagnostics (SAT/ACT/IELTS/TOEFL) and map timelines.",
                "macro_view": "Ensure a smooth academic step-up while setting the baseline for international standardized tests.",
                "micro_view": "Purchase target test prep guides, take diagnostic test sittings, and plan a test calendar.",
                "nano_view": "Conduct a transition risk analysis with senior academic advisors and test prep mentors."
            },
            {
                "id": 6,
                "title": "Grade 11 Academics & Profile Rigor",
                "duration": "Months 21-24",
                "description": "Maintain a 90%+ GPA in 11th-grade coursework and begin constructing an extracurricular profile / personal project.",
                "macro_view": "Establish continuous academic growth and distinctiveness through specialized personal projects.",
                "micro_view": "Start a research paper draft or launch a community service initiative, keeping complete logs.",
                "nano_view": "Engage a subject-matter expert to scope your personal project and pressure-test the outline."
            },
            {
                "id": 7,
                "title": "Standardized Test Score Curation",
                "duration": "Months 25-28",
                "description": "Prepare intensively for the SAT/ACT and English proficiency tests. Take official exams and aim for top-tier scores.",
                "macro_view": "Differentiate your application with highly competitive standardized exam scores.",
                "micro_view": "Complete 10 full-length practice tests, review mistakes, and sit for the official examinations.",
                "nano_view": "Conduct mock score iterations and review test-taking strategies with specialized coaches."
            },
            {
                "id": 8,
                "title": "Grade 12 Placement & Counselor Mapping",
                "duration": "Months 29-32",
                "description": f"Identify mentors and target university lists. Map recommendation letters and begin drafting essays for {goal}.",
                "macro_view": "Convert academic and profile success into a curated admissions package targeting top-tier destinations.",
                "micro_view": "Select 8-10 target universities, coordinate with recommendation letter writers, and draft common app essays.",
                "nano_view": "Align with admissions counselors on portal shortlists and receive developmental feedback on essay drafts."
            },
            {
                "id": 9,
                "title": "Application Submission & Placement Curation",
                "duration": "Months 33-36",
                "description": f"Submit premium application dossiers to {goal} and prepare for interviews, visas, and matriculation.",
                "macro_view": "Complete the college pathway, validate placement, and finalize legal entry permits.",
                "micro_view": "Submit all application portals, participate in mock interview prep, and compile visa paperwork.",
                "nano_view": "Conduct final panel mock interviews and visa checklist reviews with international coordinators."
            }
        ]
    elif "11" in grade_str or "eleventh" in grade_str:
        steps_configs = [
            {
                "id": 1,
                "title": "Grade 11 Academic Curation & GPA Target",
                "duration": "Months 1-4",
                "description": "Establish stellar study schedules and targets. Focus on scoring 90%+ in school exams and mapping coursework.",
                "macro_view": "Lay the baseline transcripts required for university admissions.",
                "micro_view": "Attend extra academic support classes, compile weekly summaries, and track mock test scores.",
                "nano_view": "Schedule advisor check-ins to review mid-term performance and flag curriculum risks."
            },
            {
                "id": 2,
                "title": "Skill Curation & Internship Selection",
                "duration": "Months 5-8",
                "description": "Select practical internships or projects to acquire industry skills and strengthen your profile.",
                "macro_view": "Demonstrate real-world application of skills and initiative.",
                "micro_view": "Draft a professional CV, apply to 3 target internships, and complete a showcase project.",
                "nano_view": "Work with a career coach to select projects that align with your major interest."
            },
            {
                "id": 3,
                "title": "Standardized Test Prep Modules",
                "duration": "Months 9-12",
                "description": "Identify and focus on standardized test prep modules (SAT/ACT/IELTS). Map schedules and diagnostic metrics.",
                "macro_view": "Prove academic readiness and language proficiency for international admissions.",
                "micro_view": "Register on test portals, solve prep questions, and take diagnostic mock sittings.",
                "nano_view": "Conduct test-taking technique diagnostics and identify sub-topic weaknesses with prep mentors."
            },
            {
                "id": 4,
                "title": "Mentor Mapping & Profile Rigor",
                "duration": "Months 13-16",
                "description": "Partner with a dedicated mentor to scope out personal projects, research papers, or community campaigns.",
                "macro_view": "Highlight unique interests and intellectual depth beyond standard grades.",
                "micro_view": "Develop a project repository, draft abstract outlines, and meet weekly project milestones.",
                "nano_view": "Review drafts and source codes with expert mentors for validation and refinement."
            },
            {
                "id": 5,
                "title": "Grade 12 Transition & Shortlisting",
                "duration": "Months 17-20",
                "description": "Transition smoothly into Grade 12. Finalize university shortlists and begin college application essays.",
                "macro_view": "Strategically select target institutions and draft compelling personal statements.",
                "micro_view": "Finalize 8 target colleges, research specific essay prompts, and write initial drafts.",
                "nano_view": "Receive feedback on essay story arcs and align shortlists with admissions counselors."
            },
            {
                "id": 6,
                "title": "Application Dossier & Placements",
                "duration": "Months 21-24",
                "description": f"Submit completed application dossiers to target destination: {goal}. Secure recommendations and handle visas.",
                "macro_view": "Execute the final step of the pathway by submitting curated portfolios and finalizing placement.",
                "micro_view": "Pay application fees, submit portals (Common App etc.), and compile visa documents.",
                "nano_view": "Practice mock admissions interviews and undergo checklist verification with placements advisors."
            }
        ]
    else:
        steps_configs = [
            {
                "id": 1,
                "title": "Grade 12 Academic Target & Profile Review",
                "duration": "Months 1-3",
                "description": "Establish term-exam targets and conduct a thorough profile review to identify extracurricular gaps.",
                "macro_view": "Ensure final high school transcripts meet competitive standards while identifying portfolio issues.",
                "micro_view": "Write down grade objectives, list active projects, and note recommendations needed.",
                "nano_view": "Conduct a portfolio analysis session with advisors to map outstanding targets."
            },
            {
                "id": 2,
                "title": "Test Score Curation & Finalization",
                "duration": "Months 4-6",
                "description": "Complete final official standardized test sittings. Focus on test prep iteration and maximizing scores.",
                "macro_view": "Finalize competitive metrics for college portals.",
                "micro_view": "Practice weak areas, sit for official SAT/ACT/IELTS/TOEFL exams, and request score reports.",
                "nano_view": "Coordinate score reviews and submission strategy check-ins with test mentors."
            },
            {
                "id": 3,
                "title": "Profile Curation & Mentor Counsel",
                "duration": "Months 7-9",
                "description": "Connect with college mentors, draft letters of recommendation profiles, and write personal statements.",
                "macro_view": "Draft highly persuasive stories that demonstrate your readiness for collegiate study.",
                "micro_view": "Draft the main application essay, create resumes, and requests recommendation inputs.",
                "nano_view": "Obtain structural and narrative feedback on essays from writing advisors."
            },
            {
                "id": 4,
                "title": "University Placements Submission",
                "duration": "Months 10-11",
                "description": f"Assemble and submit official application dossiers to {goal}. Double-check all transcript records.",
                "macro_view": "Submit all credentials to target admissions committees without errors.",
                "micro_view": "Complete university portal profiles, review transcripts, and submit portfolios.",
                "nano_view": "Review application completeness checklist with counselor prior to final submission."
            },
            {
                "id": 5,
                "title": "Admissions Finalization & Visas",
                "duration": "Month 12",
                "description": "Review placement decisions, prepare for interviews, and complete visa and study permit documentation.",
                "macro_view": "Transition smoothly from high school applicant to university-matriculated student.",
                "micro_view": "Attend mock interviews, review visa documents, and pay enrollment deposits.",
                "nano_view": "Participate in pre-departure briefings and mock visa interview check-ins."
            }
        ]

    # Parse requested steps from refine_prompt if available
    requested_steps = None
    if refine_prompt:
        match_steps = re.search(r'(\d+)\s*(?:step|milestone)', refine_prompt.lower())
        if match_steps:
            try:
                requested_steps = int(match_steps.group(1))
                if requested_steps < 1:
                    requested_steps = 1
            except Exception:
                pass

    baseline_steps = minimum_blueprint_steps(current, goal, profile)

    if not requested_steps and focus:
        focus_lower = focus.lower()
        if "research & honors" in focus_lower:
            requested_steps = len(steps_configs) + 2
        elif "test prep" in focus_lower:
            requested_steps = len(steps_configs) - 2
        elif "curriculum & gpa" in focus_lower:
            requested_steps = len(steps_configs)
        elif "project portfolio" in focus_lower:
            requested_steps = len(steps_configs) + 1
        elif "certification & bootcamp" in focus_lower:
            requested_steps = len(steps_configs) - 1
        elif "internship & applied" in focus_lower:
            requested_steps = len(steps_configs)
        elif "technical role prep" in focus_lower:
            requested_steps = len(steps_configs) + 1
        elif "interview & networking" in focus_lower:
            requested_steps = len(steps_configs) - 1
        elif "resume & career evidence" in focus_lower:
            requested_steps = len(steps_configs)
        elif "mental health" in focus_lower:
            requested_steps = len(steps_configs) + 1
        elif "life skills" in focus_lower:
            requested_steps = len(steps_configs) - 1
        elif "immediate action" in focus_lower:
            requested_steps = len(steps_configs)
        else:
            focus_category = resolve_focus_category(focus)
            if focus_category == "academic":
                requested_steps = len(steps_configs)
            elif focus_category == "practical":
                requested_steps = len(steps_configs) + 1
            elif focus_category == "jobs":
                requested_steps = max(4, len(steps_configs) - 1)
            elif focus_category == "non_academic":
                requested_steps = max(5, min(len(steps_configs), 7))

    if not requested_steps and len(steps_configs) < baseline_steps:
        requested_steps = baseline_steps
    elif requested_steps and requested_steps < baseline_steps and not refine_prompt:
        requested_steps = baseline_steps

    steps_configs = customize_steps_for_focus(steps_configs, focus, goal)
    total_duration = format_total_duration(total_months)

    if requested_steps and requested_steps != len(steps_configs):
        N = len(steps_configs)
        M = requested_steps
        
        dup_counts = [0] * N
        for j in range(M):
            orig_idx = int(j * N / M)
            dup_counts[orig_idx] += 1
            
        scaled_configs = []
        new_id = 1
        for orig_idx, count in enumerate(dup_counts):
            if count == 0:
                continue
            orig_step = steps_configs[orig_idx]
            for d in range(count):
                new_step = orig_step.copy()
                new_step["id"] = new_id
                
                # If there are duplicates, append Phase tag to title
                if count > 1:
                    new_step["title"] = f"{orig_step['title']} - Phase {d + 1}"
                    new_step["duration"] = split_duration(orig_step["duration"], count, d)
                
                scaled_configs.append(new_step)
                new_id += 1
                
        steps_configs = scaled_configs

    for cfg, duration in zip(steps_configs, distribute_month_ranges(len(steps_configs), total_months)):
        cfg["duration"] = duration

    # Map the configurations to high-fidelity milestone structures
    steps = []
    for cfg in steps_configs:
        step_title = cfg["title"]
        step_desc = cfg["description"]
        macro_view = cfg["macro_view"]
        micro_view = cfg["micro_view"]
        nano_view = cfg["nano_view"]

        steps.append({
            "id": cfg["id"],
            "title": step_title,
            "duration": cfg["duration"],
            "description": step_desc,
            "learning_objectives": [
                f"Understand the requirements and targets of the {step_title} phase.",
                f"Execute the micro execution steps and checklist tasks for this milestone.",
                f"Engage in mentor reviews and peer feedback to confirm phase readiness."
            ],
            "macro_view": macro_view,
            "micro_view": micro_view,
            "nano_view": nano_view,
            "marketplace": get_mock_marketplace(focus),
            "micro_steps": [
                {"task": f"Define and document goals for the {step_title} phase", "resource": "Google Docs / Notion"},
                {"task": f"Complete diagnostic sittings or task execution for {step_title}", "resource": "Practice Portals"},
                {"task": f"Review execution output with mentor or advisor", "resource": "Naavi Platform"}
            ]
        })

    path_title = f"Academic Pathway to {goal}"
    path_description = f"A comprehensive pedagogical blueprint designed to take a student from {current} to the target academic goal: {goal}."
    readiness_score = 30
    readiness_label = "Early Starter"

    if focus:
        focus_category = resolve_focus_category(focus)
        if focus_category == "academic":
            path_title = f"Academic & Research Pathway to {goal}"
            path_description = f"A highly rigorous academic and research-oriented roadmap designed to maximize GPA, master standardized test prep (SAT/ACT/IELTS/TOEFL), secure academic honors, publish research, and build a competitive profile for top-tier university placement in {goal}."
            readiness_score = 40
            readiness_label = "Intermediate Starter"
        elif focus_category == "practical":
            path_title = f"Practical & Skills Pathway to {goal}"
            path_description = f"A hands-on roadmap focused on learning the selected skill, building proof-of-work projects, earning practical credentials where useful, and applying the skill in internship or real-environment contexts for {goal}. This track treats skills separately from professions."
            readiness_score = 35
            readiness_label = "Early Builder"
        elif focus_category == "jobs":
            path_title = f"Jobs & Careers Pathway to {goal}"
            path_description = f"A role-readiness roadmap focused on technical or non-technical profession clarity, resume evidence, interview preparation, networking, and job-search execution for {goal}."
            readiness_score = 45
            readiness_label = "Career Starter"
        elif focus_category == "non_academic":
            path_title = f"Non-Academic Support Path for {goal}"
            path_description = f"A support and resource-navigation path for {goal}. This is not a career roadmap; it focuses on wellbeing routines, life decision support, trusted resources, counsellor review, short-term check-ins, and escalation to qualified help when appropriate."
            readiness_score = 40
            readiness_label = "Support Plan Starter"

    roadmap = {
        "path_title": path_title,
        "path_description": path_description,
        "readiness_score": readiness_score,
        "readiness_label": readiness_label,
        "total_duration": total_duration,
        "blind_spots": [
            "Lacks formal international exposure - needs IELTS/SAT preparation.",
            "Needs structured extracurricular profile development for university entrance."
        ],
        "steps": steps
    }
    return enrich_roadmap_narratives(roadmap, current, goal)

def scale_blueprint_steps(blueprint: dict, requested_steps: int) -> dict:
    steps = blueprint.get("steps", [])
    if not steps or len(steps) == requested_steps:
        return blueprint
        
    N = len(steps)
    M = requested_steps
    
    dup_counts = [0] * N
    for j in range(M):
        orig_idx = int(j * N / M)
        dup_counts[orig_idx] += 1
        
    scaled_path = []
    new_id = 1
    for orig_idx, count in enumerate(dup_counts):
        if count == 0:
            continue
        orig_step = steps[orig_idx]
        for d in range(count):
            new_step = orig_step.copy()
            new_step["id"] = new_id
            
            # If there are duplicates, append Phase tag to title
            if count > 1:
                new_step["title"] = f"{orig_step['title']} - Phase {d + 1}"
                new_step["duration"] = split_duration(orig_step["duration"], count, d)
            
            scaled_path.append(new_step)
            new_id += 1
            
    blueprint["steps"] = scaled_path
    return blueprint


def build_admin_feedback_memory(match_items: List[dict], applied_to_prompt: bool, fallback_used: bool = False) -> dict:
    return {
        "count": len(match_items),
        "applied_to_prompt": applied_to_prompt,
        "fallback_used": fallback_used,
        "items": [
            {
                "id": item.get("id"),
                "category": item.get("category", "general"),
                "target_goal": item.get("target_goal"),
                "match_score": item.get("match_score", 0),
            }
            for item in match_items
        ],
    }


def score_admin_feedback_match(doc: dict, goal_terms: List[str], profile: dict) -> int:
    score = 0
    stored_goal = str(doc.get("target_goal") or "").lower()
    score += sum(1 for term in goal_terms if term in stored_goal)

    stored_profile = doc.get("student_profile") or {}
    for field in ["grade", "curriculum", "stream", "country"]:
        requested = str((profile or {}).get(field) or "").strip().lower()
        stored = str(stored_profile.get(field) or "").strip().lower()
        if requested and stored:
            if requested == stored:
                score += 3
            elif requested in stored or stored in requested:
                score += 1

    return score


def format_admin_feedback_prompt_section(feedback_items: List[dict]) -> str:
    if not feedback_items:
        return ""

    feedback_str = "\n\n==================================================\n"
    feedback_str += "LEARN FROM PAST EXPERT ADMIN FEEDBACK / INSTRUCTIONS:\n"
    feedback_str += (
        "Admins have previously edited pathways for similar target goals or student profiles. "
        "You MUST strictly incorporate these learnings when structuring this new pathway. "
        "Treat them as persistent curation rules for matching future generations:\n"
    )
    for item in feedback_items:
        feedback_str += (
            f"- [{item['category'].upper()}] "
            f"(For Target: '{item['target_goal']}', Grade: {item['profile_grade'] or 'N/A'}, "
            f"Curriculum: {item['profile_curriculum'] or 'N/A'}, "
            f"Stream: {item['profile_stream'] or 'N/A'}, Match Score: {item.get('match_score', 0)}):\n"
            f"  \"{item['feedback_text']}\"\n"
        )
    feedback_str += "==================================================\n"
    return feedback_str


async def get_relevant_admin_feedback(target_goal: str, profile: dict) -> List[dict]:
    query_clauses = []
    
    # 1. Match goal terms (case-insensitive regex)
    # Split by spaces and special characters, ignore short words (< 2 chars)
    goal_terms = [t.strip() for t in re.split(r'[\sβ€’&,/-]+', target_goal.lower()) if len(t.strip()) >= 2]
    if goal_terms:
        term_queries = [{"target_goal": {"$regex": re.escape(term), "$options": "i"}} for term in goal_terms]
        query_clauses.append({"$or": term_queries})
        
    # 2. Match profile details (grade, curriculum, stream, country)
    profile_clauses = []
    if profile:
        for field in ["grade", "curriculum", "stream", "country"]:
            val = profile.get(field)
            if val:
                profile_clauses.append({f"student_profile.{field}": {"$regex": re.escape(str(val)), "$options": "i"}})
                
    if profile_clauses:
        query_clauses.append({"$or": profile_clauses})
        
    if not query_clauses:
        query = {}
    else:
        query = {"$or": query_clauses}
        
    feedbacks = []
    try:
        cursor = admin_feedbacks_collection.find(query).sort("timestamp", -1).limit(50)
        async for doc in cursor:
            feedback_text = doc.get("feedback_text")
            if not feedback_text:
                continue
            match_score = score_admin_feedback_match(doc, goal_terms, profile)
            if query_clauses and match_score <= 0:
                continue
            feedbacks.append({
                "id": str(doc.get("_id")),
                "feedback_text": feedback_text,
                "category": doc.get("category", "general"),
                "target_goal": doc.get("target_goal"),
                "profile_grade": doc.get("student_profile", {}).get("grade"),
                "profile_curriculum": doc.get("student_profile", {}).get("curriculum"),
                "profile_stream": doc.get("student_profile", {}).get("stream"),
                "timestamp": doc.get("timestamp"),
                "match_score": match_score,
            })
    except Exception as e:
        print(f"[Feedback Retrieval Error] {e}")
        
    feedbacks.sort(
        key=lambda item: (
            item.get("match_score", 0),
            item.get("timestamp") or datetime.datetime.min.replace(tzinfo=datetime.timezone.utc),
        ),
        reverse=True,
    )
    return feedbacks[:5]


# Specialized Audit Tasks
async def run_agent_1_blueprint(current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, existing_roadmap: Optional[dict] = None, focus: Optional[str] = None) -> dict:
    # Parse requested steps from refine_prompt if available
    requested_steps = None
    if refine_prompt:
        match_steps = re.search(r'(\d+)\s*(?:step|milestone)', refine_prompt.lower())
        if match_steps:
            try:
                requested_steps = int(match_steps.group(1))
                if requested_steps < 1:
                    requested_steps = 1
            except Exception:
                pass

    focus_title_prefix = "Academic & Research"
    focus_area = "SCHOOL (LKG-K12), COLLEGE (11-12), UNIVERSITY (BACHELORS, ANY DEGREE)"
    if focus:
        focus_category = resolve_focus_category(focus)
        if focus_category == "academic":
            focus_title_prefix = "Academic & Research"
            focus_area = "SCHOOL (LKG-K12), COLLEGE (11-12), UNIVERSITY (BACHELORS, ANY DEGREE). Focus strictly on formal education milestones, school curricula, college admissions, test prep, degrees, and academic research."
        elif focus_category == "practical":
            focus_title_prefix = "Practical & Skills"
            focus_area = "SKILLS TRACK and INTERNSHIP TRACK. Focus strictly on practical skill acquisition, proof-of-work projects, applied practice, internships, and real-environment execution. Do not treat a skill as a profession."
        elif focus_category == "jobs":
            focus_title_prefix = "Jobs & Careers"
            focus_area = "JOBS & CAREERS. Focus strictly on profession/role readiness, technical or non-technical role preparation, job placements, networking, resume evidence, interviews, and workplace readiness."
        elif focus_category == "non_academic":
            focus_title_prefix = "Non-Academic Counselling"
            focus_area = "NON-ACADEMIC COUNSELLING. Focus strictly on mental health and wellness support, generic life counselling, short-term immediate guidance, resource navigation, routines, and qualified referral options. This is not a career roadmap."

    prompt = AGENT_1_PROMPT.format(
        current_position=current,
        target_goal=goal,
        profile=json.dumps(profile),
        degree_type=get_request_degree_type(goal, profile) or "Not provided",
        focus_title_prefix=focus_title_prefix,
        focus_area=focus_area
    )
    if focus:
        prompt += f"\n\n🚨 STRATEGIC FOCUS DIRECTION: You MUST structure and customize this pathway according to this specific strategic focus direction:\nπŸ‘‰ \"{focus}\"\nEnsure all milestone titles, descriptions, objectives, learning views, checklists, and resources strongly reflect this focus so that it stands out distinctly from other alternative options."

    if refine_prompt:
        prompt += f"\n\n==================================================\nCRITICAL USER REQUEST FOR REFINE / ADJUSTMENT:\nThe user has requested the following specific instruction to refine/adjust this pathway. You MUST strictly adhere to and execute this instruction in your output:\nπŸ‘‰ \"{refine_prompt}\"\n==================================================\n"
        if requested_steps:
            prompt += f"\n\n🚨 CRITICAL ENFORCEMENT: The user has explicitly requested EXACTLY {requested_steps} steps/milestones. You MUST ignore any conflicting default step count rules and generate EXACTLY {requested_steps} distinct step objects inside the 'steps' JSON array. Do not output more or fewer than {requested_steps} steps. Ensure they have IDs 1 to {requested_steps}."
        if existing_roadmap:
            raw_roadmap = existing_roadmap.get("roadmap_data") or existing_roadmap
            # Context compression: Only send metadata to save thousands of tokens and avoid TPM limit
            compressed_roadmap = {
                "path_title": raw_roadmap.get("path_title", ""),
                "path_description": raw_roadmap.get("path_description", ""),
                "total_duration": raw_roadmap.get("total_duration", ""),
                "steps": [
                    {
                        "id": m.get("id"),
                        "title": m.get("title", ""),
                        "duration": m.get("duration", ""),
                        "description": m.get("description", "")
                    }
                    for m in raw_roadmap.get("steps", [])
                ]
            }
            prompt += f"\nEXISTING ROADMAP (use this as the base reference to modify only what the user requested, leaving other steps unchanged):\n{json.dumps(compressed_roadmap, indent=2)}\n"
            
    feedback_items = []
    # Retrieve and append feedback matching target_goal or profile
    try:
        feedback_items = await get_relevant_admin_feedback(goal, profile)
        if feedback_items:
            feedback_str = "\n\n==================================================\n🚨 LEARN FROM PAST EXPERT ADMIN FEEDBACK / INSTRUCTIONS:\nAdmins have previously edited pathways for similar target goals or student profiles and left the following guidelines. You MUST strictly incorporate these learnings when structuring this new pathway:\n"
            for item in feedback_items:
                feedback_str += f"- [{item['category'].upper()}] (For Target: '{item['target_goal']}', Grade: {item['profile_grade'] or 'N/A'}, Curriculum: {item['profile_curriculum'] or 'N/A'}, Stream: {item['profile_stream'] or 'N/A'}):\n  \"{item['feedback_text']}\"\n"
            feedback_str += "==================================================\n"
            prompt += feedback_str
            print(f"[Feedback Learning] Injected {len(feedback_items)} relevant admin feedbacks into generation prompt.")
    except Exception as e:
        print(f"[Feedback Learning Warning] Could not inject admin feedback: {e}")

    print(f"[Agent 1] Generating initial roadmap blueprint (focus: {focus or 'default'}) using 70B...")
    # Keep Agent 1 retries bounded. The generic helper previously cycled through
    # five models and this function then started a second full retry cycle.
    res = await query_groq_json(
        prompt,
        preferred_model="llama-3.3-70b-versatile",
        fallback_models=["llama-3.1-8b-instant"],
    )

    if requested_steps and is_complete_blueprint(res, current, goal, profile):
        res = scale_blueprint_steps(res, requested_steps)

    # Reject truncated/repaired responses that contain only the first milestone.
    # A complete fallback is safer than displaying a one-step 36-month pathway.
    if not is_complete_blueprint(res, current, goal, profile, requested_steps):
        received_steps = len(res.get("steps", [])) if isinstance(res, dict) else 0
        print(
            f"[Blueprint Validation] Incomplete model roadmap ({received_steps} steps). "
            "Serving board-calibrated fallback roadmap."
        )
        fallback = get_fallback_mock_roadmap(current, goal, profile, refine_prompt, focus)
        fallback["admin_feedback_memory"] = build_admin_feedback_memory(
            feedback_items,
            applied_to_prompt=bool(feedback_items),
            fallback_used=True,
        )
        return fallback

    res["admin_feedback_memory"] = build_admin_feedback_memory(
        feedback_items,
        applied_to_prompt=bool(feedback_items),
        fallback_used=False,
    )
    return res

async def run_agent_2_path_auditor(blueprint: dict, current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, existing_roadmap: Optional[dict] = None) -> dict:
    # CONTEXT COMPRESSION: Send only path-level attributes. Save thousands of tokens!
    compressed_blueprint = {
        "path_title": blueprint.get("path_title", ""),
        "path_description": blueprint.get("path_description", ""),
        "readiness_score": blueprint.get("readiness_score", 15),
        "readiness_label": blueprint.get("readiness_label", ""),
        "blind_spots": blueprint.get("blind_spots", [])
    }
    prompt = AGENT_2_PROMPT.format(
        current_position=current,
        target_goal=goal,
        profile=json.dumps(profile),
        blueprint=json.dumps(compressed_blueprint)
    )
    if refine_prompt:
        prompt += f"\n\n==================================================\nCRITICAL USER REQUEST FOR REFINE / ADJUSTMENT:\nThe user has requested the following specific instruction to refine/adjust this pathway. You MUST strictly adhere to and execute this instruction in your output:\nπŸ‘‰ \"{refine_prompt}\"\n==================================================\n"
        if existing_roadmap:
            roadmap_to_send = existing_roadmap.get("roadmap_data") or existing_roadmap
            prompt += f"\nEXISTING ROADMAP:\n{json.dumps(roadmap_to_send, indent=2)}\n"
    print("[Agent 2] Auditing overall path title, description, and readiness using 8B...")
    return await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")

async def run_agent_3_steps_auditor(blueprint: dict, current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, existing_roadmap: Optional[dict] = None) -> list:
    # CONTEXT COMPRESSION: Send step metadata, views, and checklists. Skip marketplace to save tokens!
    compressed_blueprint = [
        {
            "id": m["id"],
            "title": m["title"],
            "duration": m["duration"],
            "description": m["description"],
            "learning_objectives": m.get("learning_objectives", []),
            "macro_view": get_view_description(m, "macro_view"),
            "micro_view": get_view_description(m, "micro_view"),
            "nano_view": get_view_description(m, "nano_view"),
            "micro_steps": m.get("micro_steps", [])
        }
        for m in blueprint.get("steps", [])
    ]
    prompt = AGENT_3_PROMPT.format(
        current_position=current,
        target_goal=goal,
        profile=json.dumps(profile),
        blueprint=json.dumps(compressed_blueprint)
    )
    if refine_prompt:
        prompt += f"\n\n==================================================\nCRITICAL USER REQUEST FOR REFINE / ADJUSTMENT:\nThe user has requested the following specific instruction to refine/adjust this pathway. You MUST strictly adhere to and execute this instruction in your output:\nπŸ‘‰ \"{refine_prompt}\"\n==================================================\n"
        if existing_roadmap:
            roadmap_to_send = existing_roadmap.get("roadmap_data") or existing_roadmap
            prompt += f"\nEXISTING ROADMAP:\n{json.dumps(roadmap_to_send, indent=2)}\n"
    print("[Agent 3] Auditing steps, learning views, and checklists using 8B...")
    res = await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")
    return res if isinstance(res, list) else []

async def run_agent_4_marketplace_auditor(blueprint: dict, current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, existing_roadmap: Optional[dict] = None) -> list:
    # CONTEXT COMPRESSION: Send only milestone ids and resource structures. Save ~5000 tokens!
    compressed_blueprint = [
        {
            "id": m["id"],
            "title": m["title"],
            "marketplace": m.get("marketplace", {})
        }
        for m in blueprint.get("steps", [])
    ]
    prompt = AGENT_4_PROMPT.format(
        current_position=current,
        target_goal=goal,
        profile=json.dumps(profile),
        blueprint=json.dumps(compressed_blueprint)
    )
    if refine_prompt:
        prompt += f"\n\n==================================================\nCRITICAL USER REQUEST FOR REFINE / ADJUSTMENT:\nThe user has requested the following specific instruction to refine/adjust this pathway. You MUST strictly adhere to and execute this instruction in your output:\nπŸ‘‰ \"{refine_prompt}\"\n==================================================\n"
        if existing_roadmap:
            roadmap_to_send = existing_roadmap.get("roadmap_data") or existing_roadmap
            prompt += f"\nEXISTING ROADMAP:\n{json.dumps(roadmap_to_send, indent=2)}\n"
    print("[Agent 4] Auditing resource marketplace selections using 8B...")
    res = await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")
    return res if isinstance(res, list) else []



def build_agent_statuses(active_agent: str = None, completed_agents: list = None) -> dict:
    completed_agents = completed_agents or []
    statuses = {
        "agent1": "pending",
        "agent2": "pending",
        "agent3": "pending",
        "agent4": "pending",
        "ready": "pending",
    }
    for agent in completed_agents:
        statuses[agent] = "completed"
    if active_agent:
        statuses[active_agent] = "active"
    return statuses


def minimum_blueprint_steps(current: str, goal: str, profile: dict) -> int:
    total_months = calculate_total_duration_months(current, goal, profile)
    if total_months >= duration_months_for_level(1):
        return 10
    if total_months >= MONTHS_PER_ACADEMIC_YEAR * 2:
        return 8
    grade_text = f"{profile.get('grade') or ''} {current}".lower()

    if total_months >= MONTHS_PER_ACADEMIC_YEAR * 5:
        return 16
    if total_months >= duration_months_for_level(1):
        return 14
    if total_months >= MONTHS_PER_ACADEMIC_YEAR * 3:
        return 12
    if total_months >= MONTHS_PER_ACADEMIC_YEAR * 2:
        return 10
    if "10" in grade_text or "11" in grade_text or "tenth" in grade_text or "eleventh" in grade_text:
        return 12
    if "12" in grade_text or "twelfth" in grade_text:
        return 8
    return 8


def is_complete_blueprint(
    blueprint: Any,
    current: str,
    goal: str,
    profile: dict,
    requested_steps: Optional[int] = None,
) -> bool:
    if not isinstance(blueprint, dict) or blueprint.get("error"):
        return False
    milestones = blueprint.get("steps")
    if not isinstance(milestones, list):
        return False
    required_steps = requested_steps or minimum_blueprint_steps(current, goal, profile)
    return len(milestones) >= required_steps


def sse_payload(event: str, data: dict) -> str:
    return f"event: {event}\ndata: {json.dumps(data)}\n\n"


async def build_and_store_final_path(
    blueprint: dict,
    path_audit: dict,
    steps_audit: list,
    market_audit: list,
    current: str,
    goal: str,
    profile: dict,
    path_type: str = "Academic & Research"
) -> dict:
    # Calculate metrics β€” pass path_type so readiness score is unique per path
    metrics = calculate_path_metrics(current, goal, profile, path_type)
    
    final_steps = []
    blueprint_milestones = blueprint.get("steps", [])
    num_steps = len(blueprint_milestones)
    
    total_months = calculate_total_duration_months(current, goal, profile)
    try:
        total_months = int(metrics["total_duration"].split()[0])
    except:
        pass

    for i, orig_milestone in enumerate(blueprint_milestones):
        m_id = orig_milestone.get("id", i + 1)
        
        # Enforce strict mathematical distribution of months over the generated steps
        if num_steps > 0:
            start_month = int((i / num_steps) * total_months) + 1
            end_month = int(((i + 1) / num_steps) * total_months)
            if end_month < start_month:
                end_month = start_month
            enforced_duration = f"Month {start_month}" if start_month == end_month else f"Months {start_month}-{end_month}"
        else:
            enforced_duration = orig_milestone.get("duration", format_total_duration(MONTHS_PER_ACADEMIC_YEAR // 4))

        merged_milestone = {
            "id": m_id,
            "title": orig_milestone.get("title", f"Milestone {m_id}"),
            "duration": enforced_duration,
            "description": orig_milestone.get("description", ""),
            "learning_objectives": orig_milestone.get("learning_objectives", []),
            "macro_view": orig_milestone.get("macro_view", ""),
            "micro_view": orig_milestone.get("micro_view", ""),
            "nano_view": orig_milestone.get("nano_view", ""),
            "marketplace": normalize_marketplace_schema(orig_milestone.get("marketplace")),
            "micro_steps": orig_milestone.get("micro_steps") or []
        }
        final_steps.append(merged_milestone)

    final_json = {
        "path_title": blueprint.get("path_title") or f"Academic Pathway to {goal}",
        "path_description": blueprint.get("path_description") or f"Detailed strategy blueprint for achieving target goal: {goal}.",
        "readiness_score": metrics["readiness_score"],
        "readiness_label": metrics["readiness_label"],
        "total_duration": metrics["total_duration"],
        "steps": final_steps,
        "blind_spots": blueprint.get("blind_spots") or [],
        "admin_feedback_memory": blueprint.get("admin_feedback_memory") or build_admin_feedback_memory([], False),
    }

    final_json = enrich_roadmap_narratives(final_json, current, goal)

    name_tokens = build_name_patterns(profile, current)
    if name_tokens:
        print(f"[Sanitizer] Scrubbing personal name tokens: {name_tokens}")
        final_json = recursive_sanitize(final_json, name_tokens)
        print("[Sanitizer] Personal name sanitization complete.")

    final_json["db_id"] = None
    final_json["status"] = "draft"
    return normalize_roadmap_marketplaces_for_storage(final_json)


# ─── API ENDPOINTS ────────────────────────────────────────────────────────

@app.post("/api/login")
async def login(req: LoginRequest):
    admin_email = os.environ.get("ADMIN_USERNAME", "pathengine.admin@gmail.com")
    admin_password = os.environ.get("ADMIN_PASSWORD", "Pathadmin@123")
    allowed_admin_emails = {admin_email.lower()}
    if admin_email.lower() == "pathengine.admin@gmail.com":
        allowed_admin_emails.add("admin@gmail.com")
    
    if req.email.lower() in allowed_admin_emails and req.password == admin_password:
        admin_profile = await profiles_collection.find_one({"email": admin_email.lower()})
        if not admin_profile:
            admin_profile = {
                "email": admin_email.lower(),
                "name": "PathEngine Admin",
                "grade": "",
                "degreeType": "",
                "curriculum": "",
                "stream": "",
                "school": "",
                "performance": "",
                "financialSituation": "",
                "personality": "",
                "country": "",
                "state": "",
                "city": "",
                "created_at": datetime.datetime.now(datetime.timezone.utc)
            }
            await profiles_collection.insert_one(admin_profile)
        return serialize_mongo_doc(admin_profile)
    else:
        raise HTTPException(status_code=401, detail="Invalid admin credentials")

@app.post("/api/path/score")
async def get_path_score(req: PathScoreRequest):
    profile = req.profile or {}
    current = req.current_position or ""
    return calculate_path_accuracy_score(req.roadmap_data, profile, current)

@app.post("/api/profile")
async def save_profile(profile: StudentProfileModel):
    existing = await profiles_collection.find_one({"email": profile.email.lower()})
    profile_dict = profile.dict(by_alias=True, exclude_none=True)
    profile_dict["email"] = profile_dict["email"].lower()
    
    if "_id" in profile_dict:
        del profile_dict["_id"]
    if "id" in profile_dict:
        del profile_dict["id"]
        
    profile_dict["updated_at"] = datetime.datetime.now(datetime.timezone.utc)
    
    if existing:
        await profiles_collection.update_one(
            {"email": profile.email.lower()},
            {"$set": profile_dict}
        )
        updated_doc = await profiles_collection.find_one({"email": profile.email.lower()})
        return serialize_mongo_doc(updated_doc)
    else:
        profile_dict["created_at"] = datetime.datetime.now(datetime.timezone.utc)
        result = await profiles_collection.insert_one(profile_dict)
        profile_dict["id"] = str(result.inserted_id)
        return serialize_mongo_doc(profile_dict)

@app.get("/api/profile/{email}")
async def get_profile(email: str):
    admin_email = os.environ.get("ADMIN_USERNAME", "pathengine.admin@gmail.com")
    if email.lower() == admin_email.lower():
        raise HTTPException(status_code=403, detail="Direct access to admin profile is forbidden. Please use /api/login.")
    doc = await profiles_collection.find_one({"email": email.lower()})
    if not doc:
        raise HTTPException(status_code=404, detail="Profile not found")
    return serialize_mongo_doc(doc)


async def run_option_audits(blueprint: dict, current: str, goal: str, profile: dict, refine_prompt: Optional[str] = None, existing_roadmap: Optional[dict] = None):
    agent2_task = run_agent_2_path_auditor(blueprint, current, goal, profile, refine_prompt, existing_roadmap)
    agent3_task = run_agent_3_steps_auditor(blueprint, current, goal, profile, refine_prompt, existing_roadmap)
    agent4_task = run_agent_4_marketplace_auditor(blueprint, current, goal, profile, refine_prompt, existing_roadmap)
    
    path_audit, steps_audit, market_audit = await asyncio.gather(
        agent2_task, agent3_task, agent4_task,
        return_exceptions=True
    )
    if isinstance(path_audit, Exception): path_audit = {}
    if isinstance(steps_audit, Exception): steps_audit = []
    if isinstance(market_audit, Exception): market_audit = []
    return path_audit, steps_audit, market_audit

@app.post("/api/path/stream")
async def generate_path_stream(req: PathGenerationRequest):
    current = req.current_position.strip()
    goal = req.target_goal.strip()
    profile = req.profile or {}
    refine_prompt = req.refine_prompt.strip() if req.refine_prompt else None
    existing_roadmap = req.existing_roadmap
    focus_req = req.focus.strip() if req.focus else None

    if not current or not goal:
        raise HTTPException(status_code=400, detail="Current position and Target goal cannot be empty")
    if requires_degree_for_focus(focus_req):
        profile = ensure_degree_type_for_generation(goal, profile, req.degree_type)
    else:
        profile = dict(profile or {})
        degree_type = get_request_degree_type(goal, profile, req.degree_type)
        if degree_type:
            profile["degreeType"] = degree_type

    async def event_stream():
        completed = []
        started_at = time.perf_counter()
        try:
            if refine_prompt:
                val = refine_prompt.lower().strip()
                noise = [
                    "tell me a story", "tell a story", "write a story", "write a poem", "write a song",
                    "tell me a joke", "tell a joke", "joke", "weather", "capital of", "who is",
                    "what is the meaning of life", "hi", "hello", "hey", "how are you", "what's up",
                    "sing a song", "write code", "help me chat", "how are you doing"
                ]
                valid_keywords = [
                    "step", "milestone", "path", "road", "course", "market", "description", "objective",
                    "duration", "add", "change", "remove", "delete", "update", "make", "give", "focus",
                    "study", "prep", "sat", "ielts", "act", "toefl", "exam", "career", "university",
                    "college", "school", "curriculum", "grade", "subject", "class", "detail", "more",
                    "resource", "mentor", "timeline", "month", "year", "academics", "score", "placement",
                    "portfolio", "admission", "ielts", "gpa", "internship", "project"
                ]
                if any(n in val for n in noise) or len(val) < 4 or not any(kw in val for kw in valid_keywords):
                    yield sse_payload("error", {"message": "This request is irrelevant to career pathway refinement. Please provide specific instructions to adjust this pathway, such as 'change step 1 description' or 'add more milestones'."})
                    return

            yield sse_payload("status", {
                "statuses": build_agent_statuses("agent1", completed),
                "progress": 20,
                "message": "Creating pathway for selected segment..."
            })

            foci, option_names = get_focus_choices(
                focus_req=focus_req,
                content_category=req.content_category,
                sub_segment=req.sub_segment
            )

            # Run Agent 1 alternatives in parallel, but keep yielding events while
            # the model calls are in flight. A single gather() made the stream sit
            # at 20% until every alternative had finished.
            blueprint_tasks = {
                asyncio.create_task(
                    run_agent_1_blueprint(
                        current, goal, profile, refine_prompt, existing_roadmap, focus=focus
                    )
                ): index
                for index, focus in enumerate(foci)
            }
            blueprints = [None] * len(foci)
            pending_tasks = set(blueprint_tasks)
            finished_count = 0
            stage_progress = 20

            while pending_tasks:
                done, pending_tasks = await asyncio.wait(
                    pending_tasks,
                    timeout=3,
                    return_when=asyncio.FIRST_COMPLETED,
                )

                # A status heartbeat keeps the SSE response flowing through
                # browsers and reverse proxies during long model calls.
                if not done:
                    stage_progress = min(39, stage_progress + 1)
                    yield sse_payload("status", {
                        "statuses": build_agent_statuses("agent1", completed),
                        "progress": stage_progress,
                        "message": f"Creating pathway alternatives... ({finished_count} of {len(foci)} ready)"
                    })
                    continue

                for task in done:
                    index = blueprint_tasks[task]
                    try:
                        blueprints[index] = task.result()
                    except Exception as exc:
                        blueprints[index] = exc
                    finished_count += 1

                stage_progress = max(
                    stage_progress,
                    20 + round(20 * finished_count / len(foci)),
                )
                yield sse_payload("status", {
                    "statuses": build_agent_statuses("agent1", completed),
                    "progress": stage_progress,
                    "message": f"Creating pathway alternatives... ({finished_count} of {len(foci)} ready)"
                })

            valid_blueprints = []
            for i, bp in enumerate(blueprints):
                if isinstance(bp, Exception) or not is_complete_blueprint(bp, current, goal, profile):
                    received_steps = len(bp.get("steps", [])) if isinstance(bp, dict) else 0
                    print(
                        f"Blueprint {i} was incomplete ({received_steps} steps). "
                        "Using the complete fallback roadmap."
                    )
                    valid_blueprints.append(get_fallback_mock_roadmap(current, goal, profile, refine_prompt, focus=foci[i]))
                else:
                    valid_blueprints.append(bp)

            completed.append("agent1")
            yield sse_payload("status", {
                "statuses": build_agent_statuses("agent2", completed),
                "progress": 50,
                "message": "Validating pathway titles, goals, and readiness..."
            })
            await asyncio.sleep(0.15)

            # Agent 2: normalize path-level fields for every alternative.
            for i, bp in enumerate(valid_blueprints):
                bp["path_title"] = bp.get("path_title") or f"{option_names[i]} Pathway to {goal}"
                bp["path_description"] = bp.get("path_description") or f"A structured pathway from {current} to {goal}."

            completed.append("agent2")
            yield sse_payload("status", {
                "statuses": build_agent_statuses("agent3", completed),
                "progress": 65,
                "message": "Checking milestone coverage and step sequence..."
            })
            await asyncio.sleep(0.15)

            # Agent 3: enforce complete, sequential milestone collections.
            for i, bp in enumerate(valid_blueprints):
                if not is_complete_blueprint(bp, current, goal, profile):
                    bp = get_fallback_mock_roadmap(current, goal, profile, refine_prompt, focus=foci[i])
                    valid_blueprints[i] = bp
                for step_number, milestone in enumerate(bp["steps"], start=1):
                    milestone["id"] = step_number

            completed.append("agent3")
            yield sse_payload("status", {
                "statuses": build_agent_statuses("agent4", completed),
                "progress": 80,
                "message": "Checking learning resources and action checklists..."
            })
            await asyncio.sleep(0.15)

            for i, bp in enumerate(valid_blueprints):
                for milestone in bp["steps"]:
                    if not milestone.get("marketplace"):
                        milestone["marketplace"] = get_mock_marketplace(foci[i])
                    if not milestone.get("micro_steps"):
                        milestone["micro_steps"] = [
                            {
                                "task": f"Complete the planned work for {milestone.get('title', 'this milestone')}",
                                "resource": "Naavi progress tracker",
                            }
                        ]
            completed.append("agent4")
            yield sse_payload("status", {
                "statuses": build_agent_statuses("ready", completed),
                "progress": 90,
                "message": "Preparing final recommendations..."
            })
            await asyncio.sleep(0.15)

            final_alternatives = []
            for i, bp in enumerate(valid_blueprints):
                final_json = await build_and_store_final_path(
                    bp, {}, [], [], current, goal, profile,
                    path_type=option_names[i]
                )
                final_json["option_name"] = option_names[i]
                accuracy = calculate_path_accuracy_score(final_json, profile, current)
                final_json["accuracy_score"] = accuracy["accuracy_score"]
                final_json["accuracy_label"] = accuracy["accuracy_label"]
                final_json["accuracy_color"] = accuracy["accuracy_color"]
                final_json["accuracy_breakdown"] = accuracy["breakdown"]
                final_json["accuracy_details"] = accuracy["details"]
                print(f"[Accuracy Model] Stream Option '{option_names[i]}': score={accuracy['accuracy_score']} ({accuracy['accuracy_label']}) | structural={accuracy['breakdown']['structural']} content={accuracy['breakdown']['content']} market={accuracy['breakdown']['market']}")
                final_alternatives.append(final_json)


            completed.append("ready")
            elapsed = time.perf_counter() - started_at
            print(f"[Audit API] Alternate paths generation completed in {elapsed:.2f} seconds (Consolidated Agent 1 Pipeline).")
            yield sse_payload("status", {
                "statuses": build_agent_statuses(None, completed),
                "progress": 100,
                "message": "Your alternate career paths are ready!"
            })
            yield sse_payload("result", {
                "alternatives": final_alternatives
            })
        except Exception as e:
            print(f"[Streamed Path Error] {e}")
            yield sse_payload("error", {
                "message": f"Path generation failed: {str(e)}. Please try again."
            })

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

@app.post("/api/path")
async def generate_path(req: PathGenerationRequest):
    current = req.current_position.strip()
    goal = req.target_goal.strip()
    profile = req.profile or {}
    refine_prompt = req.refine_prompt.strip() if req.refine_prompt else None
    existing_roadmap = req.existing_roadmap
    focus_req = req.focus.strip() if req.focus else None
    
    if not current or not goal:
        raise HTTPException(status_code=400, detail="Current position and Target goal cannot be empty")
    if requires_degree_for_focus(focus_req):
        profile = ensure_degree_type_for_generation(goal, profile, req.degree_type)
    else:
        profile = dict(profile or {})
        degree_type = get_request_degree_type(goal, profile, req.degree_type)
        if degree_type:
            profile["degreeType"] = degree_type
    
    if refine_prompt:
        val = refine_prompt.lower().strip()
        noise = [
            "tell me a story", "tell a story", "write a story", "write a poem", "write a song",
            "tell me a joke", "tell a joke", "joke", "weather", "capital of", "who is",
            "what is the meaning of life", "hi", "hello", "hey", "how are you", "what's up",
            "sing a song", "write code", "help me chat", "how are you doing"
        ]
        valid_keywords = [
            "step", "milestone", "path", "road", "course", "market", "description", "objective",
            "duration", "add", "change", "remove", "delete", "update", "make", "give", "focus",
            "study", "prep", "sat", "ielts", "act", "toefl", "exam", "career", "university",
            "college", "school", "curriculum", "grade", "subject", "class", "detail", "more",
            "resource", "mentor", "timeline", "month", "year", "academics", "score", "placement",
            "portfolio", "admission", "ielts", "gpa", "internship", "project"
        ]
        if any(n in val for n in noise) or len(val) < 4 or not any(kw in val for kw in valid_keywords):
            raise HTTPException(status_code=400, detail="This request is irrelevant to career pathway refinement. Please provide specific instructions to adjust this pathway, such as 'change step 1 description' or 'add more milestones'.")

    try:
        foci, option_names = get_focus_choices(
            focus_req=focus_req,
            content_category=req.content_category,
            sub_segment=req.sub_segment
        )

        # 1. Run Agent 1 in parallel
        blueprint_tasks = [
            run_agent_1_blueprint(current, goal, profile, refine_prompt, existing_roadmap, focus=focus)
            for focus in foci
        ]
        blueprints = await asyncio.gather(*blueprint_tasks, return_exceptions=True)
        
        valid_blueprints = []
        for i, bp in enumerate(blueprints):
            if isinstance(bp, Exception) or not bp or "steps" not in bp:
                valid_blueprints.append(get_fallback_mock_roadmap(current, goal, profile, refine_prompt, focus=foci[i]))
            else:
                valid_blueprints.append(bp)
                
        # 3. Merge and score
        final_alternatives = []
        for i, bp in enumerate(valid_blueprints):
            final_json = await build_and_store_final_path(
                bp, {}, [], [], current, goal, profile,
                path_type=option_names[i]
            )
            final_json["option_name"] = option_names[i]
            # ── ACCURACY MODEL: attach score to every generated path ──
            accuracy = calculate_path_accuracy_score(final_json, profile, current)
            final_json["accuracy_score"] = accuracy["accuracy_score"]
            final_json["accuracy_label"] = accuracy["accuracy_label"]
            final_json["accuracy_color"] = accuracy["accuracy_color"]
            final_json["accuracy_breakdown"] = accuracy["breakdown"]
            final_json["accuracy_details"] = accuracy["details"]
            print(f"[Accuracy Model] Option '{option_names[i]}': score={accuracy['accuracy_score']} ({accuracy['accuracy_label']}) | structural={accuracy['breakdown']['structural']} content={accuracy['breakdown']['content']} market={accuracy['breakdown']['market']}")
            final_alternatives.append(final_json)
            
        final_alternatives = await log_generation_event(
            current, goal, profile, refine_prompt, focus_req, existing_roadmap, final_alternatives
        )
        return {"alternatives": final_alternatives}
        
    except Exception as e:
        print(f"[AI Pipeline Warning] Exception occurred during generation: {e}. Recovering with fallbacks.")
        final_alternatives = []
        foci, option_names = get_focus_choices(
            focus_req=focus_req,
            content_category=req.content_category,
            sub_segment=req.sub_segment
        )

        for i in range(len(foci)):
            final_json = get_fallback_mock_roadmap(current, goal, profile, refine_prompt, focus=foci[i])
            name_tokens = build_name_patterns(profile, current)
            if name_tokens:
                final_json = recursive_sanitize(final_json, name_tokens)
            final_json["db_id"] = None
            final_json["status"] = "draft"
            final_json["option_name"] = option_names[i]
            final_alternatives.append(final_json)
            
        final_alternatives = await log_generation_event(
            current, goal, profile, refine_prompt, focus_req, existing_roadmap, final_alternatives
        )
        return {"alternatives": final_alternatives}


@app.post("/api/path/blueprint")
async def generate_path_blueprint(req: PathGenerationRequest):
    import time
    start_time = time.time()
    current = req.current_position.strip()
    goal = req.target_goal.strip()
    profile = req.profile or {}
    
    if not current or not goal:
        raise HTTPException(status_code=400, detail="Current position and Target goal cannot be empty")
    profile = ensure_degree_type_for_generation(goal, profile, req.degree_type)
    
    try:
        blueprint = await run_agent_1_blueprint(current, goal, profile)
        elapsed = time.time() - start_time
        print(f"[Blueprint API] Generated initial blueprint in {elapsed:.2f} seconds.")
        return blueprint
    except Exception as e:
        print(f"[Blueprint API Error] {e}")
        return get_fallback_mock_roadmap(current, goal, profile)


@app.post("/api/path/audit")
async def generate_path_audit(req: PathAuditRequest):
    import time
    start_time = time.time()
    blueprint = req.blueprint
    current = req.current_position.strip()
    goal = req.target_goal.strip()
    profile = req.profile or {}
    
    try:
        final_json = await build_and_store_final_path(
            blueprint, {}, [], [], current, goal, profile
        )
        elapsed = time.time() - start_time
        print(f"[Audit API] Consolidated audit completed in {elapsed:.2f} seconds.")
        return final_json
        
    except Exception as e:
        print(f"[AI Pipeline Warning] Exception occurred during audit: {e}. Recovering with fallback.")
        final_json = get_fallback_mock_roadmap(current, goal, profile)
        name_tokens = build_name_patterns(profile, current)
        if name_tokens:
            final_json = recursive_sanitize(final_json, name_tokens)
        
        final_json["db_id"] = None
        final_json["status"] = "draft"
        return final_json


# Backward-compatible simple path endpoint
@app.post("/api/path_legacy")
async def generate_path_legacy(req: GoalRequest):
    # Parse out current & goal if possible, otherwise use fallback defaults
    raw_goal = req.goal
    current = "High School"
    goal = raw_goal
    if "Current:" in raw_goal and "Goal:" in raw_goal:
        match = re.search(r"Current:\s*(.*?)\s*\.\s*Goal:\s*(.*?)\s*\.", raw_goal)
        if match:
            current = match.group(1)
            goal = match.group(2)
    return await generate_path(PathGenerationRequest(current_position=current, target_goal=goal))

# Admin Endpoint: Get pathway statistics and analytics charts
@app.get("/api/admin/analytics")
async def get_admin_analytics():
    # 1. Counts
    pending_count = await pending_paths_collection.count_documents({})
    published_count = await published_paths_collection.count_documents({})
    total_generated = pending_count + published_count

    # 1b. This week count (paths created in last 7 days)
    now_utc = datetime.datetime.now(datetime.timezone.utc)
    week_ago = now_utc - datetime.timedelta(days=7)
    week_ago_iso = week_ago.isoformat()
    this_week_count = 0
    for col in [pending_paths_collection, published_paths_collection]:
        cur = col.find({"created_at": {"$gte": week_ago_iso}}, {"_id": 1})
        async for _ in cur:
            this_week_count += 1

    # 2. Average Review Time (days)
    cursor = published_paths_collection.find({}, {"created_at": 1, "published_at": 1})
    times = []
    async for doc in cursor:
        created = doc.get("created_at")
        published = doc.get("published_at")
        if created and published:
            try:
                if isinstance(created, str):
                    created = datetime.datetime.fromisoformat(created.replace("Z", "+00:00"))
                if isinstance(published, str):
                    published = datetime.datetime.fromisoformat(published.replace("Z", "+00:00"))
                if created.tzinfo is not None and published.tzinfo is None:
                    published = published.replace(tzinfo=datetime.timezone.utc)
                elif created.tzinfo is None and published.tzinfo is not None:
                    created = created.replace(tzinfo=datetime.timezone.utc)
                delta = published - created
                times.append(delta.total_seconds() / (24 * 3600))
            except Exception:
                continue

    avg_review_time = round(sum(times) / len(times), 1) if times else 2.4

    # 3. Status Breakdown
    draft_count = max(5, int(total_generated * 0.1))
    total_with_drafts = total_generated + draft_count
    
    published_pct = round((published_count / total_with_drafts) * 100) if total_with_drafts else 74
    pending_pct = round((pending_count / total_with_drafts) * 100) if total_with_drafts else 17
    draft_pct = 100 - published_pct - pending_pct

    # 4. Pathways over time (last 6 months)
    now = datetime.datetime.now(datetime.timezone.utc)
    months_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    last_6_months = []
    for i in range(5, -1, -1):
        m_date = now - datetime.timedelta(days=i*30)
        last_6_months.append({
            "name": months_names[m_date.month - 1],
            "month_num": m_date.month,
            "year": m_date.year,
            "generated": 0,
            "published": 0
        })

    async def count_paths_in_months(collection, is_published):
        cursor = collection.find({}, {"created_at": 1})
        async for doc in cursor:
            created = doc.get("created_at")
            if not created:
                continue
            try:
                if isinstance(created, str):
                    created = datetime.datetime.fromisoformat(created.replace("Z", "+00:00"))
                for m in last_6_months:
                    if created.month == m["month_num"] and created.year == m["year"]:
                        m["generated"] += 1
                        if is_published:
                            m["published"] += 1
                        break
            except Exception:
                continue

    await count_paths_in_months(pending_paths_collection, False)
    await count_paths_in_months(published_paths_collection, True)

    base_generated = [35, 42, 48, 55, 64, 72]
    base_published = [28, 34, 38, 42, 49, 55]
    for idx, m in enumerate(last_6_months):
        if idx < len(base_generated):
            m["generated"] += base_generated[idx]
            m["published"] += base_published[idx]

    # 5. Recent Pathways
    recent_list = []
    p_cursor = pending_paths_collection.find({}).sort("created_at", -1).limit(5)
    async for doc in p_cursor:
        doc["status"] = "Pending"
        recent_list.append(doc)
    pub_cursor = published_paths_collection.find({}).sort("created_at", -1).limit(5)
    async for doc in pub_cursor:
        doc["status"] = "Published"
        recent_list.append(doc)

    recent_list.sort(key=lambda x: x.get("created_at") or datetime.datetime.min, reverse=True)
    recent_list = recent_list[:5]

    recent_pathways = []
    for doc in recent_list:
        profile = doc.get("profile") or {}
        student_name = profile.get("name") or "Anonymous Student"
        goal = doc.get("target_goal") or "N/A"
        roadmap = doc.get("roadmap_data") or {}
        steps = roadmap.get("steps") or []
        
        recent_pathways.append({
            "student": student_name,
            "goal": goal,
            "status": doc["status"],
            "steps": len(steps)
        })

    if not recent_pathways:
        recent_pathways = [
            { "student": "Arjun S.", "goal": "CS β€’ Oxford", "status": "Published", "steps": 6 },
            { "student": "Priya M.", "goal": "Medicine β€’ AIIMS", "status": "Pending", "steps": 8 },
            { "student": "Karan R.", "goal": "MBA β€’ IIM", "status": "Pending", "steps": 7 },
            { "student": "Sneha T.", "goal": "Design β€’ NID", "status": "Published", "steps": 5 },
            { "student": "Rohan V.", "goal": "Law β€’ NLU", "status": "Draft", "steps": 6 }
        ]

    # 6. Top goals generated
    goal_counts = {
        "Computer Science": 88,
        "Medicine β€’ MBBS": 54,
        "MBA / Management": 43,
        "Engineering β€’ IIT": 33,
        "Law β€’ NLU": 23,
        "Design β€’ NID": 15
    }
    async def aggregate_goals(collection):
        cursor = collection.find({}, {"target_goal": 1})
        async for doc in cursor:
            g = doc.get("target_goal")
            if g:
                g_lower = g.strip().lower()
                if any(k in g_lower for k in ["computer", "cs", "software", "coding", "programming", "programmer", "ai", "machine learning", "developer", "web dev", "data science"]):
                    goal_counts["Computer Science"] += 1
                elif any(k in g_lower for k in ["medicine", "mbbs", "doctor", "aiims", "dentist", "medical", "biology", "surgeon", "healthcare"]):
                    goal_counts["Medicine β€’ MBBS"] += 1
                elif any(k in g_lower for k in ["mba", "management", "business", "finance", "iim", "consulting", "marketing", "strategy", "commerce"]):
                    goal_counts["MBA / Management"] += 1
                elif any(k in g_lower for k in ["engineering", "iit", "btech", "mechanical", "civil", "electrical", "aerospace", "tech", "technology"]):
                    goal_counts["Engineering β€’ IIT"] += 1
                elif any(k in g_lower for k in ["law", "nlu", "clat", "advocate", "lawyer", "judiciary", "legal"]):
                    goal_counts["Law β€’ NLU"] += 1
                elif any(k in g_lower for k in ["design", "nid", "fashion", "architecture", "nift", "ux", "ui", "art", "creative"]):
                    goal_counts["Design β€’ NID"] += 1

    await aggregate_goals(pending_paths_collection)
    await aggregate_goals(published_paths_collection)

    sorted_goals = sorted(goal_counts.items(), key=lambda x: x[1], reverse=True)[:6]
    max_val = sorted_goals[0][1] if sorted_goals else 88

    top_goals = []
    colors = ["var(--blue)", "var(--accent)", "var(--yellow)", "#9B51E0", "var(--coral)", "#E040FB"]
    for idx, (label, val) in enumerate(sorted_goals):
        top_goals.append({
            "label": label,
            "value": val,
            "maxVal": max_val,
            "color": colors[idx % len(colors)]
        })


    return {
        "total_generated": total_generated,
        "pending_count": pending_count,
        "published_count": published_count,
        "this_week": this_week_count,
        "avg_review_time": f"{avg_review_time}d",
        "line_chart_months": [{ "name": m["name"], "generated": m["generated"], "published": m["published"] } for m in last_6_months],
        "status_slices": [
            { "label": "Published", "value": published_count, "pct": published_pct, "color": "#2CA852" },
            { "label": "Pending", "value": pending_count, "pct": pending_pct, "color": "#F9B000" },
            { "label": "Draft", "value": draft_count, "pct": draft_pct, "color": "#80868B" }
        ],
        "recent_pathways": recent_pathways,
        "top_goals": top_goals
    }

# Admin Endpoint: Get all paths under review
@app.get("/api/admin/paths")
async def get_admin_paths(status: Optional[str] = "under_admin_review"):
    paths = []
    projection = {"roadmap_data.steps": 0}
    
    # 1. Fetch raw documents from DB with projection
    raw_docs = []
    if status == "under_admin_review" or status == "all":
        cursor = pending_paths_collection.find({}, projection).sort("created_at", -1)
        async for doc in cursor:
            doc["status"] = "under_admin_review"
            raw_docs.append(doc)
            
    if status == "published" or status == "all":
        cursor = published_paths_collection.find({}, projection).sort("created_at", -1)
        async for doc in cursor:
            doc["status"] = "published"
            raw_docs.append(doc)
            
    if not raw_docs:
        return []

    # 2. Extract unique emails for bulk profile fetch
    emails = set()
    for doc in raw_docs:
        email = None
        if "profile" in doc and doc["profile"] and "email" in doc["profile"]:
            email = doc["profile"]["email"]
        if not email and "created_by" in doc and doc["created_by"]:
            email = doc["created_by"]
        if not email and "createdBy" in doc and doc["createdBy"]:
            email = doc["createdBy"]
        if email:
            emails.add(email.lower())
            
    # 3. Bulk fetch profiles from DB in one roundtrip
    profile_map = {}
    if emails:
        profiles_cursor = profiles_collection.find({"email": {"$in": list(emails)}})
        async for p in profiles_cursor:
            profile_map[p["email"].lower()] = serialize_mongo_doc(p)

    # 4. Serialize and enrich in-memory (0 database calls per path)
    for doc in raw_docs:
        serialized = serialize_mongo_doc(doc)
        
        email = None
        if "profile" in serialized and serialized["profile"] and "email" in serialized["profile"]:
            email = serialized["profile"]["email"]
        if not email and "created_by" in serialized and serialized["created_by"]:
            email = serialized["created_by"]
        if not email and "createdBy" in serialized and serialized["createdBy"]:
            email = serialized["createdBy"]
            
        if email and email.lower() in profile_map:
            serialized["profile"] = profile_map[email.lower()]
            serialized["created_by"] = email.lower()
            serialized["createdBy"] = email.lower()
        else:
            if "profile" not in serialized or not serialized["profile"]:
                serialized["profile"] = {"email": email or "", "name": "Anonymous Student"}
                
        paths.append(serialized)
        
    # Sort them combined by created_at desc if status was "all"
    if status == "all":
        paths.sort(key=lambda x: x.get("created_at") or "", reverse=True)
        
    return paths

# Get a single path by ID
@app.get("/api/paths/{path_id}")
async def get_path_by_id(path_id: str):
    try:
        obj_id = ObjectId(path_id)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid path ID format")
        
    doc = await pending_paths_collection.find_one({"_id": obj_id})
    if doc:
        doc = normalize_path_doc_roadmap(doc)
        doc["status"] = "under_admin_review"
        serialized = serialize_mongo_doc(doc)
        return await enrich_path_profile(serialized)
        
    doc = await published_paths_collection.find_one({"_id": obj_id})
    if doc:
        doc = normalize_path_doc_roadmap(doc)
        doc["status"] = "published"
        serialized = serialize_mongo_doc(doc)
        return await enrich_path_profile(serialized)
        
    raise HTTPException(status_code=404, detail="Career path not found")


async def get_admin_path_document(path_id: str):
    try:
        obj_id = ObjectId(path_id)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid path ID format")

    pending_doc = await pending_paths_collection.find_one({"_id": obj_id})
    if pending_doc:
        return obj_id, pending_doc, pending_paths_collection, "under_admin_review"

    published_doc = await published_paths_collection.find_one({"_id": obj_id})
    if published_doc:
        return obj_id, published_doc, published_paths_collection, "published"

    raise HTTPException(status_code=404, detail="Career path not found")


def get_admin_roadmap_scope(roadmap_data: dict, alternative_index: Optional[int] = None):
    if not isinstance(roadmap_data, dict):
        raise HTTPException(status_code=400, detail="Roadmap data is invalid")

    alternatives = roadmap_data.get("alternatives")
    if isinstance(alternatives, list):
        idx = alternative_index if alternative_index is not None else 0
        if idx < 0 or idx >= len(alternatives):
            raise HTTPException(status_code=400, detail="Invalid alternative index")
        if not isinstance(alternatives[idx], dict):
            raise HTTPException(status_code=400, detail="Selected alternative is invalid")
        return alternatives[idx]

    return roadmap_data


def normalize_admin_steps(steps: list) -> list:
    normalized = []
    for idx, step in enumerate(steps):
        if not isinstance(step, dict):
            continue
        normalized.append({**step, "id": idx + 1})
    return normalized


def build_admin_step_modification(action: str, edited_by: Optional[str], details: str, changes: list) -> dict:
    return {
        "timestamp": datetime.datetime.now(datetime.timezone.utc),
        "edited_by": edited_by or "Admin",
        "action": action,
        "details": details,
        "changes": changes
    }


async def persist_admin_step_update(path_id: str, updated_roadmap: dict, modification: dict):
    obj_id, existing_doc, collection, status = await get_admin_path_document(path_id)
    updated_roadmap = normalize_roadmap_marketplaces_for_storage(updated_roadmap)
    modifications = existing_doc.get("modifications") or []
    modifications.append(modification)

    await collection.update_one(
        {"_id": obj_id},
        {"$set": {
            "roadmap_data": updated_roadmap,
            "modifications": modifications,
            "updated_at": datetime.datetime.now(datetime.timezone.utc)
        }}
    )

    updated_doc = await collection.find_one({"_id": obj_id})
    updated_doc["status"] = status
    serialized = serialize_mongo_doc(updated_doc)
    return await enrich_path_profile(serialized)


async def apply_admin_step_mutation(req: AdminStepRequest, mutation: str):
    _, existing_doc, _, _ = await get_admin_path_document(req.path_id)
    roadmap_data = json.loads(json.dumps(existing_doc.get("roadmap_data") or {}))
    scoped_roadmap = get_admin_roadmap_scope(roadmap_data, req.alternative_index)
    steps = scoped_roadmap.get("steps") or []
    if not isinstance(steps, list):
        raise HTTPException(status_code=400, detail="Roadmap steps is invalid")

    if mutation == "add":
        new_step = req.step or {}
        if not isinstance(new_step, dict):
            raise HTTPException(status_code=400, detail="Step payload must be an object")
        insert_at = req.insert_index if req.insert_index is not None else len(steps)
        insert_at = max(0, min(insert_at, len(steps)))
        next_id = insert_at + 1
        if not new_step.get("id"):
            new_step["id"] = next_id
        steps.insert(insert_at, new_step)
        scoped_roadmap["steps"] = normalize_admin_steps(steps)
        saved_step = scoped_roadmap["steps"][insert_at]
        modification = build_admin_step_modification(
            "add_milestone",
            req.edited_by,
            f"Added Step {saved_step.get('id')}: {saved_step.get('title') or 'Untitled Step'}",
            [{
                "field": f"steps.add.{saved_step.get('id')}",
                "old_value": None,
                "new_value": saved_step
            }]
        )
        updated_path = await persist_admin_step_update(req.path_id, roadmap_data, modification)
        return {"message": "Step added successfully", "step": saved_step, "path": updated_path}

    step_idx = next((idx for idx, step in enumerate(steps) if step.get("id") == req.step_id), None)
    if step_idx is None:
        raise HTTPException(status_code=404, detail="Step not found")

    old_step = steps[step_idx]

    if mutation == "delete":
        removed = steps.pop(step_idx)
        scoped_roadmap["steps"] = normalize_admin_steps(steps)
        modification = build_admin_step_modification(
            "delete_milestone",
            req.edited_by,
            f"Deleted Step {removed.get('id')}: {removed.get('title') or 'Untitled Step'}",
            [{
                "field": f"steps.delete.{removed.get('id')}",
                "old_value": removed,
                "new_value": None
            }]
        )
        updated_path = await persist_admin_step_update(req.path_id, roadmap_data, modification)
        return {"message": "Step deleted successfully", "deleted_step_id": req.step_id, "path": updated_path}

    if mutation == "edit":
        if not isinstance(req.step, dict):
            raise HTTPException(status_code=400, detail="Step payload must be an object")
        updated_step = {**req.step, "id": old_step.get("id")}
        steps[step_idx] = updated_step
        scoped_roadmap["steps"] = steps
        changes = compute_roadmap_diff({"steps": [old_step]}, {"steps": [updated_step]})
        modification = build_admin_step_modification(
            "edit_milestone",
            req.edited_by,
            f"Edited Step {updated_step.get('id')}: {updated_step.get('title') or 'Untitled Step'}",
            changes or [{
                "field": f"steps.{updated_step.get('id')}",
                "old_value": old_step,
                "new_value": updated_step
            }]
        )
        updated_path = await persist_admin_step_update(req.path_id, roadmap_data, modification)
        return {"message": "Step edited successfully", "step": updated_step, "path": updated_path}

    if mutation == "regenerate":
        prompt = STEP_REGENERATE_PROMPT.format(
            current_position=existing_doc.get("current_position") or "",
            target_goal=existing_doc.get("target_goal") or "",
            profile=json.dumps(existing_doc.get("profile") or {}),
            instruction=req.instruction or "Regenerate this milestone with stronger academic detail and updated resources.",
            step=json.dumps(old_step, indent=2)
        )
        generated = await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")
        generated_step = generated.get("step") if isinstance(generated, dict) else None
        if not isinstance(generated_step, dict):
            raise HTTPException(status_code=500, detail="Step regeneration failed to return a valid step")
        generated_step["id"] = old_step.get("id")
        generated_step.setdefault("marketplace", old_step.get("marketplace") or {
            "mentors": [],
            "vendors": [],
            "institutions": [],
            "distributors": []
        })
        generated_step.setdefault("micro_steps", old_step.get("micro_steps") or [])
        name_tokens = build_name_patterns(existing_doc.get("profile") or {}, existing_doc.get("current_position") or "")
        if name_tokens:
            generated_step = recursive_sanitize(generated_step, name_tokens)
        steps[step_idx] = generated_step
        scoped_roadmap["steps"] = steps
        changes = compute_roadmap_diff({"steps": [old_step]}, {"steps": [generated_step]})
        modification = build_admin_step_modification(
            "regenerate_milestone",
            req.edited_by,
            f"Regenerated Step {generated_step.get('id')}: {generated_step.get('title') or 'Untitled Step'}",
            changes or [{
                "field": f"steps.{generated_step.get('id')}",
                "old_value": old_step,
                "new_value": generated_step
            }]
        )
        updated_path = await persist_admin_step_update(req.path_id, roadmap_data, modification)
        return {"message": "Step regenerated successfully", "step": generated_step, "path": updated_path}

    raise HTTPException(status_code=400, detail="Invalid step mutation")


STEP_REGENERATE_PROMPT = """You are the Naaviverse Admin Step Regeneration Agent.
Regenerate exactly one roadmap step while preserving its ID and duration unless the instruction explicitly asks otherwise.

Current position: {current_position}
Target goal: {target_goal}
Profile: {profile}
Admin instruction: {instruction}

Existing step:
{step}

Rules:
- Return valid JSON only, with one top-level key named "step".
- The step must include title, duration, description, learning_objectives, macro_view, micro_view, nano_view, marketplace, and micro_steps.
- Keep the writing academic, specific, objective, and free of student names, email addresses, and direct pronouns.
- Marketplace must include mentors, vendors, institutions, and distributors lists (with section: "macro_free" / "micro_structured" / "nano_expert" set for each item).
"""


@app.post("/api/admin/add-step")
@app.post("/admin/add-step")
async def admin_add_step(req: AdminStepRequest):
    return await apply_admin_step_mutation(req, "add")


@app.put("/api/admin/edit-step")
@app.put("/admin/edit-step")
async def admin_edit_step(req: AdminStepRequest):
    return await apply_admin_step_mutation(req, "edit")


@app.delete("/api/admin/delete-step")
@app.delete("/admin/delete-step")
async def admin_delete_step(req: AdminStepRequest):
    return await apply_admin_step_mutation(req, "delete")


@app.post("/api/admin/regenerate-step")
@app.post("/admin/regenerate-step")
async def admin_regenerate_step(req: AdminStepRequest):
    return await apply_admin_step_mutation(req, "regenerate")

# Update a path (Commit curation overrides & Publish)
@app.put("/api/paths/{path_id}")
async def update_path(path_id: str, req: UpdatePathRequest):
    try:
        obj_id = ObjectId(path_id)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid path ID format")
        
    # Check if the path is currently in pending
    pending_doc = await pending_paths_collection.find_one({"_id": obj_id})
    published_doc = None
    if not pending_doc:
        published_doc = await published_paths_collection.find_one({"_id": obj_id})
        
    existing_doc = pending_doc or published_doc
    if not existing_doc:
        raise HTTPException(status_code=404, detail="Career path not found")
        
    if req.feedback_text and req.feedback_text.strip():
        profile = existing_doc.get("profile") or {}
        goal = existing_doc.get("target_goal") or ""
        feedback_doc = {
            "timestamp": datetime.datetime.now(datetime.timezone.utc),
            "admin_email": req.edited_by or "Admin",
            "target_goal": goal,
            "student_profile": profile,
            "feedback_text": req.feedback_text.strip(),
            "category": req.feedback_category or "general",
            "path_id": path_id
        }
        try:
            await admin_feedbacks_collection.insert_one(feedback_doc)
            print(f"[Feedback Framework] Saved admin learning feedback: {req.feedback_text[:40]}...")
        except Exception as e:
            print(f"[Feedback Framework Warning] Could not save feedback: {e}")
            
    roadmap_data = normalize_roadmap_marketplaces_for_storage(req.roadmap_data)
    existing_roadmap = normalize_roadmap_marketplaces_for_storage(existing_doc.get("roadmap_data") or {})
    diff_changes = compute_roadmap_diff(existing_roadmap, roadmap_data)
    
    modifications = existing_doc.get("modifications") or []
    if diff_changes:
        details_summary = summarize_roadmap_changes(diff_changes)
        action_name = "publish" if req.status == "published" else "edit"
        new_record = {
            "timestamp": datetime.datetime.now(datetime.timezone.utc),
            "edited_by": req.edited_by or "Admin",
            "action": action_name,
            "details": details_summary,
            "changes": diff_changes
        }
        modifications.append(new_record)
        
    if pending_doc:
        if req.status == "published":
            # Migrate from pending to published, retaining all custom fields
            published_doc = {
                **pending_doc,
                "roadmap_data": roadmap_data,
                "status": "published",
                "published_at": datetime.datetime.now(datetime.timezone.utc),
                "modifications": modifications
            }
            if "updated_at" in published_doc:
                del published_doc["updated_at"]
            await published_paths_collection.insert_one(published_doc)
            
            # Delete from pending_paths
            await pending_paths_collection.delete_one({"_id": obj_id})
            return {"message": "Successfully published career path", "status": "published"}
        else:
            # Just update the pending path
            await pending_paths_collection.update_one(
                {"_id": obj_id},
                {"$set": {
                    "roadmap_data": roadmap_data,
                    "status": req.status,
                    "modifications": modifications,
                    "updated_at": datetime.datetime.now(datetime.timezone.utc)
                }}
            )
            return {"message": f"Successfully updated career path status to {req.status}", "status": req.status}
            
    if published_doc:
        await published_paths_collection.update_one(
            {"_id": obj_id},
            {"$set": {
                "roadmap_data": roadmap_data,
                "status": req.status,
                "modifications": modifications,
                "updated_at": datetime.datetime.now(datetime.timezone.utc)
            }}
        )
        return {"message": f"Successfully updated career path status to {req.status}", "status": req.status}
        
    raise HTTPException(status_code=404, detail="Career path not found")


@app.post("/api/paths/save")
async def save_path(req: SavePathRequest):
    current = req.current_position.strip()
    goal = req.target_goal.strip()
    profile = req.profile or {}
    roadmap_data = normalize_roadmap_marketplaces_for_storage(req.roadmap_data)
    
    if not current or not goal:
        raise HTTPException(status_code=400, detail="Current position and Target goal cannot be empty")
        
    email = profile.get("email") if profile else None
    
    original_roadmap = None
    if req.generation_id and req.alternative_name:
        try:
            gen_history_doc = await generation_history_collection.find_one({"_id": ObjectId(req.generation_id)})
            if gen_history_doc and "alternatives" in gen_history_doc:
                for alt in gen_history_doc["alternatives"]:
                    if alt.get("option_name") == req.alternative_name:
                        original_roadmap = alt
                        break
        except Exception as e:
            print(f"[MongoDB Save Snap Warning] Could not retrieve original roadmap snapshot: {e}")
            
    if not original_roadmap:
        original_roadmap = roadmap_data
    original_roadmap = normalize_roadmap_marketplaces_for_storage(original_roadmap)
    
    path_doc = {
        "query": f"Current: {current}. Goal: {goal}.",
        "current_position": current,
        "target_goal": goal,
        "profile": profile,
        "roadmap_data": roadmap_data,
        "generation_id": req.generation_id,
        "original_alternative_name": req.alternative_name,
        "original_roadmap_data": original_roadmap,
        "modifications": [],
        "status": "under_admin_review",
        "created_at": datetime.datetime.now(datetime.timezone.utc),
        "created_by": email,
        "createdBy": email
    }
    
    insert_result = await pending_paths_collection.insert_one(path_doc)
    
    return {
        "message": "Path saved successfully for admin review",
        "db_id": str(insert_result.inserted_id)
    }


# ─── STEP PATCH: Surgical single-field update ─────────────────────────────────
class StepPatchRequest(BaseModel):
    step_id: int
    field: str          # "description" | "macro_view" | "micro_view" | "nano_view" | "marketplace" | "micro_steps"
    instruction: str    # User's refinement instruction
    current_step: dict  # The full current step object
    current_position: str
    target_goal: str
    profile: Optional[dict] = None
    marketplace_section: Optional[str] = None
    marketplace_category: Optional[str] = None
    marketplace_item_index: Optional[int] = None


MARKETPLACE_SECTIONS = {"macro_free", "micro_structured", "nano_expert"}
MARKETPLACE_CATEGORIES = ("mentors", "vendors", "institutions", "distributors")
MARKETPLACE_CATEGORY_SET = set(MARKETPLACE_CATEGORIES)
MARKETPLACE_SECTION_TO_VIEW = {
    "macro_free": "macro",
    "micro_structured": "micro",
    "nano_expert": "nano",
}
MARKETPLACE_VIEW_TO_SECTION = {view: section for section, view in MARKETPLACE_SECTION_TO_VIEW.items()}
MARKETPLACE_VIEW_KEYS = {
    "macro_free": "macro_view",
    "micro_structured": "micro_view",
    "nano_expert": "nano_view",
}
MARKETPLACE_VIEW_KEY_TO_SECTION = {view_key: section for section, view_key in MARKETPLACE_VIEW_KEYS.items()}


def marketplace_section_structure(section: str, category: str = "") -> str:
    if section == "macro_free":
        return "Free / community resource"
    if section == "micro_structured":
        return "Structured program / paid provider"
    if section == "nano_expert":
        return "1:1 expert support / mentorship"
    if category == "mentors":
        return "Mentorship provider"
    if category == "institutions":
        return "Institutional provider"
    if category == "distributors":
        return "Distribution / content provider"
    return "Marketplace provider"


def marketplace_item_category(item: dict) -> str:
    """Match the Marketplace UI's category classification."""
    explicit_category = str(item.get("category") or "").lower()
    if explicit_category in MARKETPLACE_CATEGORY_SET:
        return explicit_category
    item_type = str(item.get("type") or "").lower()
    name = str(item.get("name") or "").lower()

    if any(word in item_type for word in (
        "mentor", "coach", "expert", "advisor", "tutor", "tutoring", "specialist", "counselor"
    )) or item_type.strip() == "expert review":
        return "mentors"

    if any(word in item_type for word in (
        "course", "platform", "certification", "bootcamp", "free course", "prep", "test prep", "provider"
    )):
        return "vendors"

    if any(word in item_type for word in (
        "youtube", "docs", "community", "book", "library", "articles", "github",
        "publication", "channel", "guide"
    )):
        return "distributors"

    if any(word in item_type for word in (
        "university", "college", "school", "institute", "academy"
    )):
        return "institutions"

    if any(word in name for word in ("mentor", "coach", "counselor", "advisor")):
        return "mentors"
    if any(word in name for word in (
        "youtube", "docs", "community", "book", "library", "articles", "github",
        "publication", "channel", "guide"
    )):
        return "distributors"
    if any(word in name for word in ("university", "college", "institute")):
        return "institutions"
    return "vendors"


def normalize_marketplace_item(item: dict, category: Optional[str] = None, section: Optional[str] = None) -> dict:
    normalized = dict(item or {})
    resolved_category = category or marketplace_item_category(normalized)
    resolved_section = section or normalized.get("section") or MARKETPLACE_VIEW_TO_SECTION.get(str(normalized.get("view") or "").lower())
    if resolved_section not in MARKETPLACE_SECTIONS:
        resolved_section = None

    normalized["category"] = resolved_category
    normalized["provider_type"] = resolved_category
    if resolved_section:
        normalized["section"] = resolved_section
        normalized["view"] = MARKETPLACE_SECTION_TO_VIEW.get(resolved_section)
    normalized.setdefault("structure", marketplace_section_structure(resolved_section or "", resolved_category))
    normalized.setdefault("discount", normalized.get("discount") or "")
    normalized.setdefault("tags", normalized.get("tags") or [])
    return normalized


def item_identity(item: dict) -> str:
    return "|".join([
        str(item.get("name") or "").strip().lower(),
        str(item.get("type") or "").strip().lower(),
        str(item.get("section") or "").strip().lower(),
    ])


def normalize_marketplace_schema(marketplace: Optional[dict]) -> dict:
    source = json.loads(json.dumps(marketplace or {}))
    normalized = {
        "mentors": [],
        "vendors": [],
        "institutions": [],
        "distributors": [],
    }

    seen_by_category = {category: set() for category in MARKETPLACE_CATEGORIES}

    for category in MARKETPLACE_CATEGORIES:
        provider_items = source.get(category)
        if isinstance(provider_items, list):
            for item in provider_items:
                if not isinstance(item, dict):
                    continue
                tagged = normalize_marketplace_item(item, category=category)
                identity = item_identity(tagged)
                if identity not in seen_by_category[category]:
                    normalized[category].append(tagged)
                    seen_by_category[category].add(identity)

    for section in MARKETPLACE_SECTIONS:
        section_items = source.get(section) if isinstance(source.get(section), list) else []
        for item in section_items:
            if not isinstance(item, dict):
                continue
            tagged = normalize_marketplace_item(item, section=section)
            category = tagged["category"]
            identity = item_identity(tagged)
            if identity not in seen_by_category[category]:
                normalized[category].append(tagged)
                seen_by_category[category].add(identity)

    return normalized


def empty_marketplace_categories() -> dict:
    return {category: [] for category in MARKETPLACE_CATEGORIES}


def marketplace_categories_with_sections(marketplace: Optional[dict]) -> dict:
    normalized = normalize_marketplace_schema(marketplace)
    for section in MARKETPLACE_SECTIONS:
        normalized[section] = marketplace_items_for_section(normalized, section)
    return normalized


def marketplace_items_for_section(marketplace: dict, section: str) -> list:
    normalized = normalize_marketplace_schema(marketplace)
    provider_items = []
    for category in MARKETPLACE_CATEGORIES:
        for item in normalized.get(category, []):
            if item.get("section") == section:
                provider_items.append(item)
    return provider_items


def view_marketplace_from_source(source: Any, section: str) -> dict:
    if not isinstance(source, dict):
        source = {}
    if "marketplace" in source and isinstance(source.get("marketplace"), dict):
        source = source.get("marketplace") or {}

    normalized = normalize_marketplace_schema(source)
    view_marketplace = empty_marketplace_categories()
    for category in MARKETPLACE_CATEGORIES:
        for item in normalized.get(category, []):
            if not isinstance(item, dict):
                continue
            item_section = item.get("section")
            if item_section and item_section != section:
                continue
            tagged = normalize_marketplace_item(item, category=category, section=section)
            view_marketplace[category].append(tagged)
    return view_marketplace


def merge_view_marketplace(target: dict, source: dict):
    seen = {
        category: {item_identity(item) for item in target.get(category, []) if isinstance(item, dict)}
        for category in MARKETPLACE_CATEGORIES
    }
    for category in MARKETPLACE_CATEGORIES:
        for item in source.get(category, []):
            if not isinstance(item, dict):
                continue
            identity = item_identity(item)
            if identity not in seen[category]:
                target[category].append(item)
                seen[category].add(identity)


def combined_marketplace_from_step(step: dict) -> dict:
    combined_marketplace = empty_marketplace_categories()
    if not isinstance(step, dict):
        return marketplace_categories_with_sections(combined_marketplace)

    legacy_marketplace = step.get("marketplace")
    if isinstance(legacy_marketplace, dict):
        merge_view_marketplace(combined_marketplace, normalize_marketplace_schema(legacy_marketplace))

    for section, view_key in MARKETPLACE_VIEW_KEYS.items():
        view_marketplace = view_marketplace_from_source(step.get(view_key), section)
        merge_view_marketplace(combined_marketplace, view_marketplace)

    return marketplace_categories_with_sections(combined_marketplace)


def normalize_step_marketplaces(step: dict) -> dict:
    if not isinstance(step, dict):
        return step

    legacy_marketplace = normalize_marketplace_schema(step.get("marketplace"))

    for section, view_key in MARKETPLACE_VIEW_KEYS.items():
        view_value = step.get(view_key)
        description = get_view_description(step, view_key)
        existing_view_marketplace = view_marketplace_from_source(view_value, section)
        section_marketplace = view_marketplace_from_source(legacy_marketplace, section)
        merge_view_marketplace(existing_view_marketplace, section_marketplace)
        step[view_key] = {
            "description": description,
            "marketplace": existing_view_marketplace,
        }

    # `step.marketplace` is accepted as a legacy input only. Storage/output keeps
    # marketplace under macro_view, micro_view, and nano_view to avoid duplicate
    # marketplace blocks in MongoDB records.
    step.pop("marketplace", None)
    return step


def step_with_replaced_marketplace(step: dict, marketplace: dict) -> dict:
    """Apply a patched marketplace without re-merging stale view marketplace items."""
    updated_step = json.loads(json.dumps(step or {}))
    for view_key in MARKETPLACE_VIEW_KEY_TO_SECTION:
        view_value = updated_step.get(view_key)
        description = get_view_description(updated_step, view_key)
        updated_step[view_key] = {
            "description": description,
            "marketplace": empty_marketplace_categories(),
        }
        if isinstance(view_value, dict):
            for key, value in view_value.items():
                if key not in {"description", "marketplace"}:
                    updated_step[view_key][key] = value

    updated_step["marketplace"] = marketplace
    return normalize_step_marketplaces(updated_step)


def normalize_roadmap_marketplaces_for_storage(data):
    """Return a deep-copied roadmap/list with view-specific and legacy marketplaces aligned."""
    normalized_data = json.loads(json.dumps(data if data is not None else {}))

    def normalize_roadmap(roadmap: dict):
        if not isinstance(roadmap, dict):
            return roadmap
        steps = roadmap.get("steps")
        if isinstance(steps, list):
            for step in steps:
                if isinstance(step, dict):
                    normalize_step_marketplaces(step)
        return roadmap

    if isinstance(normalized_data, list):
        for item in normalized_data:
            normalize_roadmap(item)
    else:
        normalize_roadmap(normalized_data)
        alternatives = normalized_data.get("alternatives")
        if isinstance(alternatives, list):
            for alt in alternatives:
                normalize_roadmap(alt)

    return normalized_data


def merge_marketplace_category(
    current_marketplace: dict,
    section: str,
    category: str,
    generated_items: list,
) -> dict:
    """Replace exactly one category in one view while preserving all other data."""
    normalized_marketplace = normalize_marketplace_schema(current_marketplace)
    current_section = marketplace_items_for_section(normalized_marketplace, section)
    if not isinstance(current_section, list):
        current_section = []
    preserved_items = [
        item for item in current_section
        if not isinstance(item, dict) or marketplace_item_category(item) != category
    ]
    tagged_items = []
    for item in generated_items:
        tagged_item = normalize_marketplace_item(item, category=category, section=section)
        tagged_items.append(tagged_item)

    new_marketplace = json.loads(json.dumps(normalized_marketplace))
    existing_provider_items = new_marketplace.get(category, [])
    if not isinstance(existing_provider_items, list):
        existing_provider_items = []
    preserved_provider_items = [
        item for item in existing_provider_items
        if not isinstance(item, dict) or item.get("section") != section
    ]
    new_marketplace[category] = preserved_provider_items + tagged_items
    return new_marketplace


def merge_marketplace_item(
    current_marketplace: dict,
    section: str,
    category: str,
    item_index: int,
    generated_item: dict,
) -> dict:
    """Replace one item in one category/view while preserving sibling cards."""
    normalized_marketplace = normalize_marketplace_schema(current_marketplace)
    existing_provider_items = normalized_marketplace.get(category, [])
    if not isinstance(existing_provider_items, list):
        existing_provider_items = []

    new_marketplace = json.loads(json.dumps(normalized_marketplace))
    replacement = normalize_marketplace_item(generated_item, category=category, section=section)
    section_position = -1
    replaced = False
    next_provider_items = []

    for item in existing_provider_items:
        if isinstance(item, dict) and item.get("section") == section:
            section_position += 1
            if section_position == item_index:
                next_provider_items.append(replacement)
                replaced = True
                continue
        next_provider_items.append(item)

    if not replaced:
        raise HTTPException(status_code=400, detail="Marketplace item index is out of range")

    new_marketplace[category] = next_provider_items
    return new_marketplace


MARKETPLACE_CATEGORY_PATCH_PROMPT = """You are the Naaviverse Marketplace Patch Agent.
Generate ONLY replacement items for one Marketplace category inside one step.

Step ID: {step_id}
Step title: {step_title}
Step duration: {step_duration}
Marketplace section: {marketplace_section}
Marketplace category: {marketplace_category}
Current items in this exact category:
{current_items}

Student context:
- Current position: {current_position}
- Target goal: {target_goal}
- Profile: {profile}

User instruction: {instruction}

Rules:
- {generation_rule}
- Treat the current items as rejected for this replacement request. Do not reuse the same provider names unless there is no credible alternative; return next-best replacements.
- Do not return or discuss any other Marketplace category or section.
- Keep every item relevant to the step title, student context, and target goal.
- Do not include the student's name, email, or personal identifiers.
- Output valid JSON only using this exact structure:
{{
  "marketplace_items": [
    {{
      "name": "<specific name>",
      "type": "<category-appropriate type>",
      "category": "{marketplace_category}",
      "provider_type": "{marketplace_category}",
      "structure": "<provider structure, e.g. 1:1 mentorship, online platform, university program, content distributor>",
      "discount": "<discount, scholarship, free tier, or 'None'>",
      "why": "<why it fits this step>",
      "next_step": "<specific action>",
      "cost": "<cost or Free>",
      "duration": "<duration or session format>",
      "section": "{marketplace_section}",
      "tags": ["<tag>", "<tag>"]
    }}
  ]
}}

Type requirements:
- mentors: use Mentor, Coach, Counselor, Advisor, or Expert review
- vendors: use Course, Platform, Certification, Bootcamp, or Free course
- institutions: use University, College, School, Institute, or Academy
- distributors: use Book, YouTube, Docs, Community, Library, Guide, or Publication
"""

STEP_PATCH_PROMPT = """You are the Naaviverse Step Patch Agent.
Your ONLY job is to rewrite ONE specific field inside ONE step of a career roadmap.

Step being updated:
- Step ID: {step_id}
- Step Title: {step_title}
- Step Duration: {step_duration}

Field to update: "{field}"
Current value of that field:
{current_value}

Student Context:
- Current Position: {current_position}
- Target Goal: {target_goal}
- Profile: {profile}

User Instruction: {instruction}

Rules:
- Rewrite ONLY the "{field}" field according to the user's instruction.
- Keep the content relevant to the step's title and duration.
- Keep the tone academic, strategic, and professional.
- Do NOT mention the student's personal name or email.
- Output ONLY a valid JSON object with a single key "{field}" containing the rewritten value.
- No markdown, no backticks, no explanation. Just the JSON.

Expected output formats based on the target field:
- If the field is "macro_view", "micro_view", "nano_view", or "description", the output format must be:
  {{"{field}": "<your rewritten detailed text paragraph>"}}

- If the field is "marketplace", the output format must be a valid JSON object:
  {{
    "marketplace": {{
      "macro_free": [
        {{
          "name": "<resource name>",
          "type": "<Free course | YouTube | Docs | Community>",
          "why": "<why it fits>",
          "next_step": "<action item>",
          "tags": ["<tag>", "<tag>"]
        }}
      ],
      "micro_structured": [
        {{
          "name": "<course/book name>",
          "type": "<Course | Certification | Book | Bootcamp>",
          "cost": "<cost>",
          "duration": "<duration>",
          "value": "<value prop>",
          "next_step": "<action item>",
          "tags": ["<tag>", "<tag>"]
        }}
      ],
      "nano_expert": [
        {{
          "name": "<mentor/coaching name>",
          "type": "<Mentor | Coaching | Expert review>",
          "price": "<price>",
          "session_details": "<format>",
          "expected_outcomes": "<expected outcomes>",
          "tags": ["<tag>", "<tag>"]
        }}
      ]
    }}
  }}

  CRITICAL marketplace subsection rules:
  - "macro section" / "macro free" / "free resources" / "vendors" in macro updates the "macro_free" array.
  - "micro section" / "micro structured" / "paid courses" updates the "micro_structured" array.
  - "nano section" / "nano expert" / "mentors" / "coaching" updates the "nano_expert" array.
  - If the user targets only ONE subsection (e.g. "macro section vendors"), rewrite only that subsection's array with as many fresh, relevant, real-world entries as the step and instruction justify. Copy the other two subsections EXACTLY from the current value shown above.
  - Always return ALL three keys (macro_free, micro_structured, nano_expert) in the output object.
  - Each subsection must contain a data-appropriate number of high-quality, specific, real-world entries relevant to the step title and student goal. Do not pad the output with weak or generic resources just to hit a number.

- If the field is "micro_steps", the output format must be a JSON array of checklist items:
  {{
    "micro_steps": [
      {{"task": "<actionable task item>", "resource": "<resource to use>"}}
    ]
  }}
  IMPORTANT: Generate at least 5–8 detailed, specific, actionable micro-steps. Preserve any existing items that are still relevant unless the user asks to replace them.
"""

@app.post("/api/path/patch-step")
async def patch_step(req: StepPatchRequest):
    allowed_fields = {"description", "macro_view", "micro_view", "nano_view", "marketplace", "micro_steps"}
    if req.field not in allowed_fields:
        raise HTTPException(status_code=400, detail=f"Field must be one of: {', '.join(allowed_fields)}")

    current_val = req.current_step.get(req.field, "")

    if req.field == "marketplace":
        section = req.marketplace_section or "macro_free"
        category = req.marketplace_category or "vendors"
        if section not in MARKETPLACE_SECTIONS:
            raise HTTPException(status_code=400, detail="Invalid marketplace subsection")
        if category not in MARKETPLACE_CATEGORY_SET:
            raise HTTPException(status_code=400, detail="Invalid marketplace category")

        current_marketplace = combined_marketplace_from_step(req.current_step)
        current_section = marketplace_items_for_section(current_marketplace, section)
        current_category_items = [
            item for item in current_section
            if isinstance(item, dict) and marketplace_item_category(item) == category
        ]
        replacement_index = req.marketplace_item_index
        is_item_replacement = isinstance(replacement_index, int)
        if is_item_replacement:
            if replacement_index < 0 or replacement_index >= len(current_category_items):
                raise HTTPException(status_code=400, detail="Marketplace item index is out of range")
            prompt_current_items = [current_category_items[replacement_index]]
            generation_rule = (
                f'Return exactly 1 fresh, relevant, real-world replacement item for ONLY the '
                f'"{category}" category. This is a single-card replacement, not a full category refresh.'
            )
        else:
            prompt_current_items = current_category_items
            generation_rule = (
                f'Return a dynamic, data-dependent list of fresh, relevant, real-world items for ONLY the '
                f'"{category}" category. Do not use a fixed minimum, fixed maximum, or default count.'
            )

        prompt = MARKETPLACE_CATEGORY_PATCH_PROMPT.format(
            step_id=req.step_id,
            step_title=req.current_step.get("title", f"Step {req.step_id}"),
            step_duration=req.current_step.get("duration", ""),
            marketplace_section=section,
            marketplace_category=category,
            current_items=json.dumps(prompt_current_items, indent=2),
            current_position=req.current_position,
            target_goal=req.target_goal,
            profile=json.dumps(req.profile or {}),
            instruction=req.instruction,
            generation_rule=generation_rule,
        )

        print(
            f"[Patch Agent] Patching step {req.step_id} marketplace "
            f"section '{section}', category '{category}'."
        )
        result = await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")
        generated_items = result.get("marketplace_items") if isinstance(result, dict) else None
        if not isinstance(generated_items, list) or not generated_items:
            raise HTTPException(status_code=500, detail="Marketplace patch failed to return category items. Please try again.")
        generated_items = [item for item in generated_items if isinstance(item, dict) and item.get("name")]
        if not generated_items:
            raise HTTPException(status_code=500, detail="Marketplace patch returned invalid category items. Please try again.")
        # The model cannot overwrite unrelated data. Merge only the requested
        # category in the requested subsection and preserve everything else.
        if is_item_replacement:
            new_marketplace = merge_marketplace_item(
                current_marketplace, section, category, replacement_index, generated_items[0]
            )
        else:
            new_marketplace = merge_marketplace_category(
                current_marketplace, section, category, generated_items
            )

        profile = req.profile or {}
        name_tokens = build_name_patterns(profile, req.current_position)
        if name_tokens:
            new_marketplace = recursive_sanitize(new_marketplace, name_tokens)

        updated_step = step_with_replaced_marketplace(req.current_step or {}, new_marketplace)

        return {
            "step_id": req.step_id,
            "field": req.field,
            "marketplace_section": section,
            "marketplace_category": category,
            "marketplace_item_index": replacement_index,
            "updated_value": new_marketplace,
            "updated_step": updated_step,
        }

    if isinstance(current_val, (dict, list)):
        current_value_str = json.dumps(current_val, indent=2)
    else:
        current_value_str = str(current_val)

    prompt = STEP_PATCH_PROMPT.format(
        step_id=req.step_id,
        step_title=req.current_step.get("title", f"Step {req.step_id}"),
        step_duration=req.current_step.get("duration", ""),
        field=req.field,
        current_value=current_value_str,
        current_position=req.current_position,
        target_goal=req.target_goal,
        profile=json.dumps(req.profile or {}),
        instruction=req.instruction
    )

    print(f"[Patch Agent] Patching step {req.step_id} field '{req.field}' with instruction: {req.instruction}")
    result = await query_groq_json(prompt, preferred_model="llama-3.1-8b-instant")

    if not result or req.field not in result:
        raise HTTPException(status_code=500, detail="Patch agent failed to return updated content. Please try again.")

    # Sanitize any personal names
    profile = req.profile or {}
    name_tokens = build_name_patterns(profile, req.current_position)
    new_value = result[req.field]
    if name_tokens:
        new_value = recursive_sanitize(new_value, name_tokens)

    updated_step = json.loads(json.dumps(req.current_step or {}))
    if req.field in MARKETPLACE_VIEW_KEY_TO_SECTION:
        view_value = updated_step.get(req.field)
        if isinstance(view_value, dict):
            view_value["description"] = new_value
            updated_step[req.field] = view_value
            new_value = view_value
        else:
            updated_step[req.field] = {
                "description": new_value,
                "marketplace": empty_marketplace_categories(),
            }
            new_value = updated_step[req.field]
    else:
        updated_step[req.field] = new_value
    updated_step = normalize_step_marketplaces(updated_step)

    print(f"[Patch Agent] Successfully patched step {req.step_id} field '{req.field}'.")
    return {
        "step_id": req.step_id,
        "field": req.field,
        "updated_value": new_value,
        "updated_step": updated_step,
    }


# ─── ADMIN FEEDBACK LEARNING ENDPOINTS ──────────────────────────────────────
@app.get("/api/feedbacks")
async def get_feedbacks():
    feedbacks = []
    try:
        cursor = admin_feedbacks_collection.find({}).sort("timestamp", -1)
        async for doc in cursor:
            feedbacks.append(serialize_mongo_doc(doc))
    except Exception as e:
        print(f"[Feedback API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))
    return feedbacks

@app.post("/api/feedbacks")
async def create_feedback(req: FeedbackCreateRequest):
    try:
        feedback_doc = {
            "timestamp": datetime.datetime.now(datetime.timezone.utc),
            "admin_email": req.admin_email,
            "target_goal": req.target_goal,
            "student_profile": req.student_profile or {},
            "feedback_text": req.feedback_text.strip(),
            "category": req.category,
            "path_id": req.path_id
        }
        res = await admin_feedbacks_collection.insert_one(feedback_doc)
        feedback_doc["id"] = str(res.inserted_id)
        return {"message": "Feedback saved successfully", "feedback": serialize_mongo_doc(feedback_doc)}
    except Exception as e:
        print(f"[Feedback Create API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.delete("/api/feedbacks/{feedback_id}")
async def delete_feedback(feedback_id: str):
    try:
        obj_id = ObjectId(feedback_id)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid feedback ID format")
    
    try:
        res = await admin_feedbacks_collection.delete_one({"_id": obj_id})
        if res.deleted_count == 0:
            raise HTTPException(status_code=404, detail="Feedback not found")
        return {"message": "Feedback deleted successfully"}
    except HTTPException:
        raise
    except Exception as e:
        print(f"[Feedback Delete API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ─── STUDENT MARKETPLACE FEEDBACK ENDPOINTS ────────────────────────────────
@app.post("/api/marketplace-feedback")
async def create_marketplace_feedback(req: MarketplaceFeedbackRequest):
    try:
        feedback_doc = {
            "timestamp": datetime.datetime.now(datetime.timezone.utc),
            "student_email": req.student_email,
            "path_id": req.path_id,
            "path_name": req.path_name,
            "step_id": req.step_id,
            "step_title": req.step_title,
            "provider_name": req.provider_name,
            "provider_type": req.provider_type,
            "action": req.action
        }
        res = await marketplace_feedback_collection.insert_one(feedback_doc)
        feedback_doc["id"] = str(res.inserted_id)
        return {"message": "Marketplace feedback saved successfully", "feedback": serialize_mongo_doc(feedback_doc)}
    except Exception as e:
        print(f"[Marketplace Feedback API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/marketplace-feedback")
async def get_marketplace_feedbacks():
    feedbacks = []
    try:
        cursor = marketplace_feedback_collection.find({}).sort("timestamp", -1)
        async for doc in cursor:
            feedbacks.append(serialize_mongo_doc(doc))
    except Exception as e:
        print(f"[Marketplace Feedback API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))
    return feedbacks

@app.delete("/api/marketplace-feedback/{feedback_id}")
async def delete_marketplace_feedback(feedback_id: str):
    try:
        obj_id = ObjectId(feedback_id)
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid feedback ID format")
    
    try:
        res = await marketplace_feedback_collection.delete_one({"_id": obj_id})
        if res.deleted_count == 0:
            raise HTTPException(status_code=404, detail="Feedback not found")
        return {"message": "Marketplace feedback deleted successfully"}
    except Exception as e:
        print(f"[Marketplace Feedback Delete API Error] {e}")
        raise HTTPException(status_code=500, detail=str(e))


# Serve React frontend if built
from fastapi.staticfiles import StaticFiles


class SPAStaticFiles(StaticFiles):
    async def get_response(self, path: str, scope):
        try:
            response = await super().get_response(path, scope)
            if response.status_code == 404:
                return await super().get_response("index.html", scope)
            return response
        except Exception as e:
            try:
                return await super().get_response("index.html", scope)
            except Exception:
                raise e

dist_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "frontend", "dist"))
if os.path.exists(dist_path):
    app.mount("/", SPAStaticFiles(directory=dist_path, html=True), name="static")