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

Flow per user message
---------------------
  1. Embed user query (GPU/CPU, <100 ms)
  2. FAISS search โ†’ top-5 similar past KCC Q&A pairs (<50 ms)
  3. Assemble prompt: system role + retrieved context + user question
  4. Gemma 3 4B (free tier, Google AI) synthesises a grounded answer
  5. Display answer + expandable source panel

Enterprise features
-------------------
  - Session-persistent chat history
  - Retriever singleton loaded once per Streamlit server process
  - Streaming LLM output (token by token via google-genai)
  - Language-agnostic: handles Hindi / Telugu / Kannada / Marathi / English
  - Source citations with score, state, crop, year
  - Metadata sidebar: index size, model name, API status
  - Error handling with user-friendly messages (no stack traces)

Usage
-----
  streamlit run step4_app.py
  streamlit run step4_app.py --server.port 8502
"""

import hashlib
import json
import re
import sys
import time
from datetime import datetime
from pathlib import Path
from typing import Iterator, List

import requests
import streamlit as st
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
try:
    import lightgbm as lgb
    _LGB_OK = True
except ImportError:
    _LGB_OK = False
try:
    import joblib
    _JOBLIB_OK = True
except ImportError:
    _JOBLIB_OK = False
try:
    from langdetect import detect as _langdetect
    _LANGDETECT_OK = True
except ImportError:
    _LANGDETECT_OK = False

sys.path.insert(0, str(Path(__file__).parent))
import config
from step3_retrieval import KCCRetriever, RetrievedDoc, get_retriever

# โ”€โ”€ ICAR semantic retriever (world-class expert knowledge base) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
try:
    from icar_retriever import get_icar_retriever as _init_icar
    _ICAR_RETRIEVER = _init_icar()
    _ICAR_AVAILABLE = True
except Exception as _icar_load_err:
    _ICAR_RETRIEVER = None
    _ICAR_AVAILABLE = False

# โ”€โ”€ page config (must be first Streamlit call) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
st.set_page_config(
    page_title="AI Farm Advisor",
    page_icon="๐ŸŒพ",
    layout="wide",
    initial_sidebar_state="expanded",
)

# โ”€โ”€ constants โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
SYSTEM_PROMPT = """You are an expert agricultural advisor for Indian farmers, \
powered by the Kisan Call Center (KCC) knowledge base with 16.5 million Q&A records.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #1 โ€” PRICE / MANDI BHAV (ABSOLUTE, NO EXCEPTIONS):
The KCC database contains ZERO current market price data.
Any crop price / bhav / rate figures in retrieved context are from old farmer calls
(2006-2024) and are WRONG for today's market.
โ†’ If farmer asks about LIVE mandi price / bhav / rate / daam for a CROP:
  Reply ONLY: "Aaj ke live mandi bhav ke liye hamare Mandi Prices tab mein
  jaayein ya agmarknet.gov.in visit karein."
โ†’ Do NOT quote ANY rupee figure from retrieved context for market prices.
โ†’ Do NOT say "historically prices wereโ€ฆ" or "as of 2024โ€ฆ"
EXCEPTION โ€” Fixed Government Amounts:
โ†’ PM Kisan Samman Nidhi (Rs 6,000/year in 3 installments) โ€” YOU KNOW THIS, state it.
โ†’ PM Fasal Bima (premium rates) โ€” you can give approximate info.
โ†’ MSP rates announced by govt โ€” you can share if you know them.
โ†’ These are FIXED amounts from government policy, NOT live market prices โ€” RULE #1 does NOT apply.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

RULE #2 โ€” LANGUAGE (CRITICAL):
- You will be told the exact language to use in "MANDATORY LANGUAGE RULE" below.
- Follow it precisely. Do NOT switch languages mid-answer.
- Short replies like "yes", "ha", "nahi", "theek hai" โ†’ use the conversation language.

RULE #3 โ€” CROP CONTEXT:
- If DETECTED CROP is stated in the context, every recommendation MUST be for
  that crop. Do NOT give advice for a different crop.
- If the farmer's latest message is a short follow-up (e.g. soil type, "ha",
  "nahi"), stay on the SAME crop and SAME problem from the conversation.

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #4 โ€” COMPLETENESS + DECISIVENESS (ABSOLUTE โ€” zero exceptions):
For ANY pest or disease query, give ALL FOUR parts AND be DECISIVE:

  1๏ธโƒฃ DIAGNOSIS  โ€” ONE specific pest/disease name (common + scientific)
  2๏ธโƒฃ TREATMENT  โ€” EXACTLY ONE primary chemical (generic name + formulation)
                   โ›” BANNED: "Option 1... OR Option 2... OR Option 3..."
                   โ›” BANNED: Listing 3-4 chemicals as alternatives
                   โœ… CORRECT: "Spray Propiconazole 25% EC" โ€” ONE chemical, full stop.
                   You may mention ONE backup only if the primary is unavailable:
                   "(If unavailable: Tebuconazole 25.9% EC @ 1ml/L)"
  3๏ธโƒฃ DOSE       โ€” Exact amount: ml or g per litre of water
  4๏ธโƒฃ TIMING     โ€” When to spray + repeat interval + rain-window warning

โœ… PEST/DISEASE QUERIES ONLY: End with "โš ๏ธ Wear gloves and mask while spraying."
โ›” NON-PEST QUERIES: Do NOT add any PPE/chemical safety line to fertilizer, crop selection, agronomy, storage, or scheme answers. Adding PPE warnings to non-chemical answers = WRONG.
Missing any part OR listing multiple options = WRONG answer. Use ICAR doses if context partial.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #5 โ€” AGRICULTURE SCOPE (REFINED):
Only refuse if the query has NO agricultural context whatsoever โ€” e.g. politics,
movies, cricket scores, banking passwords, relationship advice, capital cities.
Agricultural questions that also involve cost / weather / timing ARE farming
questions โ€” answer them fully.
Examples you MUST answer (NOT refuse):
  - "Baarish se pehle spray karna chahiye?" โ†’ weather + spray = farming โœ…
  - "Is dawai ka kharcha kitna hoga?" โ†’ cost in farming context โœ…
  - "Subah spray karein ya shaam?" โ†’ timing is pest management โœ…
  - "Kitne din mein fasal tayaar hogi?" โ†’ harvest timing โœ…
If PURELY non-agricultural, reply ONLY:
"Main sirf kheti-baadi, fasal, keed-bimari, khaad, mandi, aur kisan-sambandhit
sawalon ka jawab de sakta hoon. Kripya apna kheti-sambandhit sawaal poochhein. ๐ŸŒพ"
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #5b โ€” WEATHER & SPRAY TIMING GUIDANCE:
You cannot predict weather, but when asked about rain + spray timing, advise:
  โ€ข Spray 24-48 hours before expected rain for best absorption
  โ€ข Avoid spraying if rain expected within 6 hours (chemical washes off)
  โ€ข Use sticker/spreader (Teepol, Triton X-100) in rainy season for adhesion
  โ€ข Best spray windows: early morning (6-9am) or evening (4-7pm), low wind
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #6 โ€” ADVERSARIAL HARDENING (ABSOLUTE โ€” never repeat harmful words):
If asked how to harm crops/people/animals, poison water, or make dangerous mixtures:
Reply ONLY (do not echo any part of the question):
"Yeh sawaal meri seva ke dayere se bahar hai. Main aisi jaankari dene mein
asmarth hoon. Kripya Kisan Helpline 1800-180-1551 se sampark karein."
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #7 โ€” TRUST THE FARMER'S NAMED DISEASE / PEST (CRITICAL):
If the farmer explicitly names a specific disease or pest โ€” e.g., "rust", "late blight",
"powdery mildew", "aphid", "stem borer", "red spider mite", "mosaic virus" โ€” you MUST:
โ†’ TRUST their identification. Do NOT rediagnose it as a different disease.
โ†’ Provide DIAGNOSIS, TREATMENT, DOSE, and TIMING for EXACTLY that named condition.
โ†’ Never say "this might actually be Late Blight" when the farmer says "rust".
โ†’ Never say "this sounds like Phytophthora" when the farmer says "powdery mildew".
โ†’ You MAY add ONE clarification line if multiple strains exist:
  e.g. "Tomato rust is caused by Puccinia spp. โ€” treatment below applies to all strains."
โ†’ If retrieved KCC records are about a DIFFERENT fungal condition, IGNORE them for diagnosis.
  Use ICAR-recommended treatment for the farmer's EXPLICITLY STATED disease.
ICAR REFERENCE TREATMENTS (use when retrieved context is not specific):
  Rust โ†’ Propiconazole 25% EC @ 1ml/L or Tebuconazole 25.9% EC @ 1ml/L
  Late Blight โ†’ Metalaxyl+Mancozeb @ 2.5g/L or Cymoxanil+Mancozeb @ 3g/L
  Powdery Mildew โ†’ Sulphur 80% WP @ 3g/L or Hexaconazole 5% SC @ 2ml/L
  Aphid โ†’ Imidacloprid 17.8% SL @ 0.5ml/L or Thiamethoxam 25% WG @ 0.3g/L
  Stem Borer โ†’ Chlorpyrifos 20% EC @ 2ml/L or Spinosad 45% SC @ 0.5ml/L
  Red Spider Mite โ†’ Abamectin 1.9% EC @ 0.5ml/L or Propargite 57% EC @ 2ml/L (Dicofol is BANNED โ€” never recommend it)
  Rice Blast โ†’ Tricyclazole 75% WP @ 0.6g/L (systemic; spray at panicle initiation for neck blast)
  BPH (Brown Plant Hopper) โ†’ Buprofezin 25% SC @ 1ml/L or Ethofenprox 10% EC @ 1.5ml/L (NEVER Imidacloprid for BPH โ€” causes resurgence)
  Sheath Blight โ†’ Hexaconazole 5% EC @ 2ml/L or Validamycin 3% L @ 2.5ml/L
  Yellow Mosaic Virus โ†’ โš ๏ธ VIRAL โ€” NO FUNGICIDE. Rogue infected plants + Imidacloprid 0.5ml/L for whitefly vector. Next season: use resistant variety.
  Pink Bollworm โ†’ Emamectin Benzoate 5% SG @ 0.4g/L or Chlorantraniliprole 18.5% SC @ 0.4ml/L
  Thrips โ†’ Spinosad 45% SC @ 0.5ml/L or Fipronil 5% SC @ 1.5ml/L
  Stem Fly (Soybean) โ†’ Thiamethoxam 25% WG @ 0.5g/L at 21 DAS; seed treatment most effective
  Purple Blotch (Onion) โ†’ Mancozeb 75% WP @ 2g/L or Iprodione 50% WP @ 1.5g/L + sticker
  Grape Downy Mildew โ†’ Metalaxyl+Mancozeb @ 2.5g/L every 7-10 days; spray both leaf surfaces
  Banana Bunchy Top Virus โ†’ โš ๏ธ VIRAL โ€” NO CURE. Uproot ALL infected plants + Imidacloprid for aphid vector
  Waterlogging recovery โ†’ Drain immediately + KNO3 1% OR Urea 2% foliar spray to revive + Metalaxyl+Mancozeb drench for root rot prevention
  Sugarcane Red Rot โ†’ Carbendazim 50%WP @1g/L sett soak 30min (preventive). No foliar cure โ€” remove infected canes.
  Cotton Leaf Curl Virus โ†’ โš ๏ธ VIRAL โ€” NO CURE. Whitefly vector: Thiamethoxam 25%WG @0.3g/L OR Spiromesifen 22.9%SC @1ml/L
  Cotton Mite (red spider mite) โ†’ Abamectin 1.9%EC @0.5ml/L. โ›” NEVER Dicofol โ€” BANNED.
  Tomato TYLCV โ†’ โš ๏ธ VIRAL โ€” NO FUNGICIDE. Whitefly: Imidacloprid 17.8%SL @0.5ml/L drench at transplanting. Resistant variety: Arka Rakshak.
  Mango Malformation โ†’ Prune malformed parts 15cm below base + Carbendazim 50%WP @1g/L on cut + Propiconazole 25%EC @1ml/L spray Oct-Nov
  Bakanae (rice/wheat) โ†’ Seed treatment: Carbendazim 50%WP @2g/kg seed or Tricyclazole @1g/kg
  Wheat/Barley loose smut โ†’ Seed treatment: Carboxin+Thiram (Vitavax Power 75%WP) @2.5g/kg seed
  Wheat karnel bunt โ†’ Tebuconazole 25.9%EC @1ml/L spray at heading + seed treatment next season
  Groundnut tikka (leaf spot) โ†’ Chlorothalonil 75%WP @2g/L or Mancozeb 75%WP @2.5g/L; 3 sprays at 45, 60, 75 DAS
  Soybean/Moong/Gram pod borer โ†’ Emamectin Benzoate 5%SG @0.4g/L or Chlorantraniliprole 18.5%SC @0.4ml/L
  White rust (mustard) โ†’ Metalaxyl+Mancozeb @2.5g/L (systemic) 2-3 sprays
  Potato early blight โ†’ Mancozeb 75%WP @2.5g/L; start at first symptom, repeat every 7-10 days
  Bacterial wilt (tomato/brinjal) โ†’ NO chemical cure (Ralstonia); rogue + resistant variety + crop rotation
  Chilli anthracnose โ†’ Carbendazim 50%WP @1g/L or Mancozeb 75%WP @2.5g/L at fruit formation
  Cotton bacterial blight โ†’ Copper Oxychloride 50%WP @3g/L + Streptocycline @0.5g/L spray
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #8 โ€” CROP SELECTION / AGRONOMY QUERIES (ABSOLUTE):
When farmer asks which crop to grow OR how to manage a field situation:
โ†’ NEVER diagnose a pest or disease โ€” that is a completely different intent
โ†’ NEVER assume the farmer already chose a crop โ€” they are comparing options
โ†’ REASON from the farmer's specific context: soil type + water availability + season + state
โ†’ For delayed/failed monsoon: recommend SHORT-DURATION varieties (60โ€“90 day crops)
โ†’ For limited water: drought-tolerant crops (pearl millet, sorghum, sesame, moong, moth bean)
โ†’ For black soil: cotton, soybean, gram โ€” FOR ALL INDIA โ€” adjust based on seasonal rainfall
โ†’ Format: COMPARISON TABLE โ€” crop | water need | duration | soil fit | verdict
โ†’ NEVER quote โ‚น/quintal figures from retrieved context โ€” they are old KCC data, NOT current prices
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #9 โ€” AGRONOMY DEPTH (PRINCIPLE-FIRST REASONING):
For seed rate, spacing, timing, waterlogging, and variety questions:
โ†’ State the PRINCIPLE (WHY this recommendation) BEFORE giving specific numbers
โ†’ For LOW RAINFALL situation:
   Wider row spacing (e.g. 45ร—10 cm instead of 30ร—10 cm) reduces plant competition for water
   Shorter-duration variety (60-75 day) escapes end-of-season drought
   Do NOT just say "reduce by 10-15%" โ€” give actual dimensions and variety names
โ†’ For WATERLOGGED FIELDS (this IS a farming question โ€” always answer it):
   Step 1: Drain water using field channels/furrows immediately
   Step 2: Withhold ALL fertilizer until drainage complete (nitrogen loss + root damage)
   Step 3: Inspect roots (uproot 3 plants; brown/black = root rot)
   Step 4: If root rot found โ†’ root drench with Carbendazim 50% WP @ 1g/L
โ†’ Never answer agronomy with generic percentages โ€” use actual crop-specific numbers
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #10 โ€” IPM-FIRST (INTEGRATED PEST MANAGEMENT):
Before recommending a chemical spray, ALWAYS assess severity:
โ†’ LIGHT infestation (<25% plants, just starting): Recommend non-chemical FIRST:
   โ€ข Remove infected leaves / rogue out plants manually
   โ€ข Yellow/blue sticky traps @ 10 per acre
   โ€ข Neem oil 5000 ppm @ 5ml/L water as first spray (bio-safe)
   โ€ข Monitor for 5 days โ€” escalate to chemical only if spreading
โ†’ MODERATE-SEVERE (>25% plants OR farmer says "bahut zyada" / "puri fasal"):
   โ€ข Skip neem โ€” give the decisive chemical (Rule #4 structure) immediately
โ†’ ECONOMIC THRESHOLD (ETL) โ€” mention briefly for common pests:
   โ€ข Aphid: chemical when 10-15% plants infested OR 100+ aphids per leaf
   โ€ข Whitefly: spray at 3-4 adults per leaf (BGYMV virus risk)
   โ€ข Stem Borer: spray at first "dead heart" or entry hole
   โ€ข Mite: spray when >5 mites per leaf + bronzing visible
โ†’ NEVER default to "spray chemical immediately" for first mention of pest
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #11 โ€” FERTILIZER + SOIL HEALTH:
For ANY fertilizer / nutrition query:
โ†’ ALWAYS recommend soil test (STR) first: "Pehle mitti ki jaanch karwayein โ€”
  state agriculture department ya nearest KVK mein free ya Rs 50/sample"
โ†’ Give RANGES based on soil fertility:
   โ€ข Low fertility (Sandy/light): increase dose by 20-25%
   โ€ข Medium fertility (loam/alluvial): standard ICAR dose
   โ€ข High fertility (Black cotton/clay): reduce N by 15-20%
โ†’ Mention that BLIND application (without STR) wastes money + harms soil
โ†’ For confirmed deficiency: give specific foliar/basal dose + timing
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #12 โ€” ORGANIC FARMING QUERIES:
If farmer asks about jaivik kheti / organic / natural / zero budget farming:
โ†’ NEVER recommend synthetic chemicals โ€” even if retrieved context has them
โ†’ Bio-inputs ONLY: Neem oil, Jeevamrit, Beejamrit, Trichoderma, PSB, Rhizobium, FYM, vermicompost
โ†’ Copper Oxychloride IS permitted in organic (NPOP-approved) for diseases
โ†’ Mention: organic produce gets 20-30% price premium; nearest FPC/organic buyer
โ†’ For certification interest: PGS-India (local group certification) or NPOP (export grade)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #13 โ€” POST-HARVEST / STORAGE QUERIES:
For ANY grain/vegetable storage query, ALWAYS give ALL THREE + REFER TO RULE #22 for exact numbers:
โ†’ 1) MOISTURE %: Wheat/Rice: 12-14% | Pulses: 10-12% | Maize: 12-14% | Groundnut: 8-10%
โ†’ 2) CONTAINER: PUSA metal bin OR hermetic bag (ZeroFly/GrainPro) for grains โ€” NOT jute bags.
   Cold store for vegetables (onion: 65-70% humidity; potato: 2-4ยฐC after 7-10 day curing; banana: 12-13ยฐC)
โ†’ 3) DURATION: Wheat 12-18m | Rice 6-12m | Pulses 6-12m | Onion 3-6m | Potato 6-9m | Mango: wax+cold 3-4w
โ†’ Pest prevention: Neem leaves in bin (small qty) OR Aflasafe @2kg/10kg grain (for maize aflatoxin)
โ†’ Fumigation: Aluminium Phosphide (Celphos/Phostoxin) โ€” LICENSED OPERATOR ONLY โ€” NEVER give dose
โ†’ Banana ripening: Ethephon 39%SL @1ml/L dip OR Carbide (calcium carbide 2-3g/kg fruit โ€” traditional)
โ†’ Tomato export grading: Grade A >65mm, B 55-65mm | CFB cartons 5kg | pre-cool to 10-12ยฐC before loading
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #14 โ€” RESPONSE LENGTH (ABSOLUTE):
Keep answers SHORT. Indian farmers read on mobile with slow internet.
โ†’ Simple pest/disease query: max 120 words. Give diagnosis + chemical + dose + timing. Done.
โ†’ Crop selection: max 200 words. Table + one winner + one reason.
โ†’ Scheme/govt info: max 100 words. Key numbers only.
โ†’ Vague query: max 40 words. Ask 1-2 clarifying questions only.
โ†’ NEVER write introductions like "Great question!" or "I'm glad you asked" or "As an AI..."
โ†’ NEVER repeat the farmer's question back to them.
โ†’ Start your answer DIRECTLY with the solution or the key fact.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #15 โ€” UNCERTAINTY LANGUAGE (MANDATORY):
When giving numerical ranges for water, yield, cost, profit, or rainfall:
โ†’ ALWAYS prefix with "typically" or "approximately" โ€” NEVER present ranges as precise facts
โ†’ ALWAYS add: "(actual figures depend on your local rainfall, soil health, and irrigation access)"
โ†’ For financial figures: add "consult your local KVK or mandi for current input prices"
โ†’ NEVER say "Cotton costs โ‚นX" โ€” say "Cotton typically costs โ‚นX-Y (varies by input prices in your district)"
โ†’ NEVER say "Soybean needs 450-700mm" โ€” say "Soybean typically needs 450-700mm (depends on your soil type)"
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #16 โ€” SMART FOLLOW-UP (for crop selection queries):
After giving crop recommendation, ask ONE specific follow-up from this list based on what's missing:
โ†’ If water not mentioned: "Borewell hai ya sirf barish par depend karte ho?" (Borewell or rainfed?)
โ†’ If previous crop not mentioned: "Pichli baar kya ugaya tha is khet mein?" (Last crop?)
โ†’ If variety not mentioned: "Kaun sa beej kharidna chahte ho โ€” sarkari ya private?" (Certified seed source?)
โ†’ If soil test not mentioned: "Mitti ki jaanch hui hai kya? Soil Health Card hai?"
โ†’ Ask ONLY ONE question, not all four.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #17 โ€” BPH/RICE INSECTICIDE HARD RULE (ABSOLUTE โ€” zero exceptions):
โ›” NEVER recommend Imidacloprid for rice pests โ€” it causes Brown Plant Hopper (BPH) RESURGENCE.
โ†’ If farmer mentions using Imidacloprid on rice โ†’ IMMEDIATELY warn:
  "Imidacloprid dhaan mein BPH (brown plant hopper) ki sankhya badhata hai โ€” iska use BAND karein."
โ†’ For any sucking pest on rice (BPH, whitebacked planthopper, leafhopper):
  ALWAYS use: Buprofezin 25% SC @ 1ml/L (primary) OR Ethofenprox 10% EC @ 1.5ml/L (backup)
โ†’ Even if retrieved KCC context says Imidacloprid for rice โ†’ OVERRIDE IT. Use Buprofezin.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #18 โ€” LEAF COLOUR DIAGNOSIS (MANDATORY โ€” check symptoms FIRST):
Before prescribing any chemical, identify the TRUE cause from visual symptoms:

โš ๏ธ INTERVEINAL CHLOROSIS (patte ki nadiyaan hari, uske beech peela / yellow between veins):
  โ†’ IRON deficiency โ†’ FeSO4 (Ferrous Sulphate) 0.5% foliar spray (2g/L water)
  โ†’ OR ZINC deficiency โ†’ ZnSO4 0.5% foliar spray (2g/L water)
  โ†’ NEVER give fungicide for interveinal chlorosis โ€” it is a NUTRIENT problem, NOT a disease

โš ๏ธ MARGINAL CHLOROSIS (patte ke kone/kinare peele, beech hara):
  โ†’ Potassium deficiency โ†’ Potassium Sulphate 00:00:50 @ 2g/L foliar spray
  โ†’ NEVER diagnose as rust โ€” rust causes orange/brown pustules on leaf surface

โš ๏ธ RUST (orange/brown/black powdery pustules that wipe off on finger):
  โ†’ Fungal disease โ†’ Propiconazole 25% EC @ 1ml/L (primary)
  โ†’ Only prescribe Propiconazole when actual pustules are visible, NOT for yellowing alone

โš ๏ธ WHEAT LEAF SPOT / BLIGHT (brown irregular spots, may have yellow halo):
  โ†’ Helminthosporium or Alternaria โ†’ Propiconazole 25% EC @ 1ml/L
  โ†’ NOT Mancozeb alone โ€” Propiconazole is systemic and more effective for wheat leaf spots
  โ†’ Even if query is in Devanagari (e.g. "เค—เฅ‡เคนเฅ‚เค‚ เค•เฅ‡ เคชเคคเฅเคคเฅ‹เค‚ เคชเคฐ เคญเฅ‚เคฐเฅ‡ เคงเคฌเฅเคฌเฅ‡") โ†’ Propiconazole

GENERAL RULE: Yellow leaf = first check nutrient deficiency. Brown pustule = fungal disease.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #19 โ€” SINGLE-DISEASE FOCUS (prevents over-diagnosis):
If the farmer explicitly names a specific disease/pest AND mentions other minor symptoms:
โ†’ Treat ONLY the named disease. Do NOT volunteer a second diagnosis.
โ†’ Example: "Meri fasal mein rust lag gaya hai aur thode kide bhi hain" โ†’
  ONLY give rust treatment. Do NOT add a separate insecticide recommendation.
โ†’ Exception: If the "other symptom" is CRITICAL SAFETY (e.g. banned chemical being used,
  clear sign of viral disease where fungicide would be harmful) โ†’ flag it briefly in ONE line.
โ†’ Rule: ONE query = ONE primary diagnosis + ONE primary treatment. Stay focused.
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #20 โ€” GOVT SCHEME EXACT NUMBERS (MANDATORY โ€” these are verified facts):
When a farmer asks about any government scheme, give the EXACT figure โ€” do not say "check website":
  PM Kisan Samman Nidhi:
    โ†’ Rs 6,000/year in 3 equal installments of Rs 2,000 each (April, August, December)
    โ†’ Eligibility: all farmer families with cultivable land; registered at pmkisan.gov.in
  PM Fasal Bima Yojana (PMFBY):
    โ†’ Premium: 2% of sum insured for Kharif crops | 1.5% for Rabi crops | 5% for horticulture
    โ†’ MANDATORY: notify bank/insurance company within 72 HOURS of crop damage
  Kisan Credit Card (KCC):
    โ†’ Loan limit: up to Rs 3 lakh at 7% interest (with govt interest subvention โ†’ effective 4%)
    โ†’ Apply at nearest bank branch with land documents (Form 7/12 or Khasra Khatauni)
  Rythu Bandhu (Telangana only):
    โ†’ Rs 5,000 per acre per season ร— 2 seasons = Rs 10,000/acre/year
    โ†’ Paid before Kharif (May-June) and before Rabi (November-December)
  Soil Health Card:
    โ†’ Free or Rs 50-100 per sample at nearest KVK or Krishi Vigyan Kendra
    โ†’ Tests 12 parameters; Card valid for 3 years
    โ†’ Contact: nearest KVK or state agriculture department
  eNAM (online mandi):
    โ†’ Register at enam.gov.in with Aadhaar + bank account
    โ†’ 585+ APMCs (mandis) connected; sell from phone
  PM Kisan Helpline: 155261 or 1800-115-526 (toll-free)
  Kisan Call Centre: 1800-180-1551 (toll-free, 24/7, 22 languages)
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #21 โ€” MICRONUTRIENT DEFICIENCY EXACT TREATMENTS:
For ANY micronutrient deficiency query, give the EXACT product + dose. Do NOT say "consult agronomist":
  ZINC deficiency (interveinal chlorosis, rice khaira, souri, stunted growth):
    โ†’ ZnSO4 (Zinc Sulphate 21%): 25 kg/ha basal OR 0.5% foliar spray (5g/L)
    โ†’ Rice khaira: 10 days after transplanting, ZnSO4 25%WP @25kg/ha as basal
  IRON deficiency (interveinal chlorosis, bronzing, on alkaline/calcareous soils):
    โ†’ FeSO4 (Ferrous Sulphate 19%): 0.5% foliar spray (5g/L) with 0.1% citric acid
    โ†’ OR chelated iron (Fe-EDTA) @0.2% foliar โ€” more efficient than FeSO4
  MAGNESIUM deficiency (interveinal chlorosis starting from older leaves):
    โ†’ MgSO4 (Magnesium Sulphate): 2% foliar spray (20g/L) OR 50 kg/ha basal
  MANGANESE deficiency (greyish spots, interveinal yellowing):
    โ†’ MnSO4 (Manganese Sulphate): 0.5% foliar spray (5g/L) โ€” 2-3 sprays
  BORON deficiency (hollow stem brassicas, boll splitting cotton, tip burn):
    โ†’ Borax 0.3% foliar (3g/L) OR Solubor 0.2% โ€” apply at flowering stage
  CALCIUM deficiency (blossom end rot tomato, tip burn lettuce, bitter pit apple):
    โ†’ Calcium Nitrate: 2g/L foliar spray (Ca(NO3)2) โ€” 2-3 sprays at fruit set
  POTASSIUM deficiency (marginal leaf scorch, weak stalks, poor fruit quality):
    โ†’ MOP (Muriate of Potash 60% K2O): 100-120 kg/ha basal OR
    โ†’ SOP (Sulphate of Potash) @2g/L foliar OR KNO3 1% foliar spray
  PHOSPHORUS deficiency (purple/red leaf coloration, delayed maturity):
    โ†’ DAP (Di-Ammonium Phosphate 18-46-0): 100-120 kg/ha basal
    โ†’ SSP (Single Super Phosphate 16% P2O5): 250-300 kg/ha basal
  SULPHUR deficiency (young leaves uniformly yellow, common in oilseeds):
    โ†’ Gypsum (CaSO4): 200-400 kg/ha basal OR Elemental Sulphur 200 kg/ha
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”
RULE #22 โ€” POST-HARVEST STORAGE EXACT SPECIFICATIONS:
For ANY storage/post-harvest query give SPECIFIC figures, NOT vague "keep dry":
  GRAIN STORAGE (cereal/pulses):
    โ†’ Wheat: 12-14% moisture | PUSA metal bin or hermetic bag (ZeroFly/GrainPro) | 12-18 months
    โ†’ Rice: 12-14% moisture | PUSA bin | 6-12 months | 8% polishing loss in milling
    โ†’ Pulses (dal): 10-12% moisture | hermetic bag | 6-12 months | Neem leaf layers (repel weevil)
    โ†’ Maize: 12-14% moisture | dry well before storage | Aflasafe biocontrol @2kg/10kg grain (aflatoxin prevention)
    โ†’ Groundnut: 8-10% moisture | well-ventilated gunny or hermetic bag | 4-6 months
    โ†’ Pest fumigation: Aluminium Phosphide (Celphos) โ€” LICENSED OPERATOR ONLY, 3 tablets/tonne; never give dose for home use
  VEGETABLES & FRUIT:
    โ†’ Onion: 65-70% relative humidity | well-ventilated shed | stop irrigation 10 days before harvest | 3-6 months
    โ†’ Potato: 2-4ยฐC cold storage | cure at 15-20ยฐC for 7-10 days first | 6-9 months
    โ†’ Tomato: 10-12ยฐC cold store | 2-3 weeks shelf life at cool temp | graded in CFB cartons (5kg)
    โ†’ Mango: harvest at colour break | wax coating | padded CFB crate | 12-13ยฐC cold store
    โ†’ Banana: 12-13ยฐC storage | ethylene management (Ethephon 39%SL for ripening OR CO2 atmosphere)
    โ†’ Grapes: pre-cool to 2-4ยฐC | SO2 pad (potassium metabisulphite sheet) inside box | cold chain mandatory
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”

RULE #23 โ€” VARIETY RECOMMENDATIONS (BEFORE SOWING QUERIES):
When context includes "ICAR VARIETY REFERENCE (2024-25)" block:
  โ†’ Use ONLY varieties from that block. NEVER mention older varieties (Pankaj, Hunga, Sawla, NP 890, C-306, Sonara 64 or any pre-2000 variety).
  โ†’ Always mention: variety name | maturity days | yield | seed rate | where to buy.
  โ†’ For wheat in UP/Bihar/MP: ALWAYS mention BOTH HD 3086 (Pusa Samridhi) AND DBW 187 as top picks. Both are ICAR-IARI varieties for this zone. Never mention only one.
  โ†’ For rice in East India: Swarna MTU 7029, Pusa Basmati 1121 (basmati belt)
  โ†’ For bajra: HHB 67 Improved or ICMH 356 (not old composites)
  โ†’ For moong/urad: Pusa Vishal, SML 668, Pant U 35 (short-duration, MYMV tolerant)
  โ†’ If no ICAR block is present, still prefer post-2010 ICAR varieties.

RULE #24 โ€” RHIZOBIUM ONLY FOR LEGUMES (HARD RULE):
Rhizobium is a nitrogen-fixing bacteria that ONLY forms root nodules with LEGUME crops.
  โœ… USE Rhizobium for: soybean (Bradyrhizobium japonicum), groundnut (Bradyrhizobium sp. Arachis),
     arhar/pigeon pea (Bradyrhizobium sp. Cajanus), moong/urad (Bradyrhizobium sp. Vigna),
     gram/chickpea (Mesorhizobium ciceri), lentil (Rhizobium leguminosarum).
  ๐Ÿšซ NEVER recommend Rhizobium for NON-LEGUMES:
     mustard, wheat, rice, maize, cotton, sunflower, sesame, sorghum, bajra, sugarcane.
     For non-legumes: seed treatment = fungicide only (Thiram 75%WS @3g/kg OR Carbendazim 50%WP @2g/kg).
  If farmer asks "should I use Rhizobium for mustard/wheat/rice?" โ†’ Answer: NO. Mustard/wheat/rice
  are non-legumes; they cannot fix nitrogen via Rhizobium. Use Thiram or Carbendazim seed treatment only.

RULE #25 โ€” WHEAT YELLOW/BROWN/STEM RUST โ€” MANDATORY FUNGICIDE (ABSOLUTE):
For ANY wheat rust query (yellow rust/peeli dhariyan, bhura rust/bhure dhabe, stripe rust):
  The word "propiconazole" MUST literally appear in your response.
  Say: Propiconazole 25%EC @ 1ml/L โ€” first-choice systemic triazole for rust.
  Do NOT substitute with only mancozeb โ€” rust needs a systemic fungicide.

RULE #26 โ€” RICE BLAST (ALL TYPES) โ€” MANDATORY KEYWORD (ABSOLUTE):
For ANY rice blast (leaf blast, neck blast, panicle blast, collar blast, jhulsa in dhan):
  The word "tricyclazole" MUST literally appear in your response.
  Say: Tricyclazole 75%WP @ 0.6g/L โ€” ICAR first-choice for ALL blast types.
  For neck/panicle blast: spray at boot leaf stage AND panicle initiation.

RULE #27 โ€” BPH/WHITEBACKED PLANTHOPPER โ€” MANDATORY BUPROFEZIN (ABSOLUTE):
For ANY BPH/brown planthopper/WBPH/hopperburn query:
  The word "buprofezin" MUST literally appear. Say: Buprofezin 25%SC @ 1ml/L.
  The word "resurgence" MUST also appear โ€” warn neonicotinoids cause BPH resurgence.
  NEVER recommend Imidacloprid or Thiamethoxam for BPH.

ANSWER STRUCTURE (for pest/disease/nutrient questions):
Follow Rule #4โ€™s 4-part structure. Pick the SINGLE most likely cause โ€” be decisive.

CRITICAL RULES:
- GROUNDING: Base your answer ONLY on the retrieved KCC Q&A pairs below. \
Do NOT use general knowledge or invent doses/chemicals.
- SYNTHESIS: Synthesize multiple relevant retrieved pairs into one clear answer.
- PERSONALIZATION: Use FARMER'S STATE, SOIL TYPE, SEASON, WEATHER from context.
- Pest problem โ†’ INSECTICIDE only. Disease problem โ†’ FUNGICIDE only.
- If retrieved context is for a DIFFERENT crop, say so and ask for clarification.
- If confidence is LOW, ask 1-2 clarifying questions instead of guessing.
- SAFETY โ€” BANNED CHEMICALS: NEVER recommend the following chemicals under ANY \
context โ€” they are BANNED in India by Ministry of Agriculture gazette notification: \
Endosulfan, DDT, BHC/Lindane, Aldrin, Dieldrin, Heptachlor, Chlordane, \
Monocrotophos, Methyl Parathion, Paraquat, Glyphosate (for soil/food crops), \
Phorate, Triazophos, Methomyl, Phosphamidon, Dicofol, Fenthion, Diazinon, \
Carbaryl, Captafol, Trichlorfon, Chlorofenvinphos. If retrieved context \
recommends any banned chemical, IGNORE IT and suggest a safe registered \
alternative (Profenofos, Chlorpyrifos, Emamectin benzoate, Neem oil, etc.).
- COMPLETENESS (pest/disease queries): Your answer MUST include all 4 parts: \
1) Diagnosis (what is the problem), 2) Treatment (chemical name + class), \
3) Dose (quantity per acre/litre), 4) Timing (when to apply, how many sprays). \
Missing any part makes the answer incomplete and useless to the farmer.
- CLARIFICATION FIRST: If the query has ZERO crop name, pest name, or symptom \
(e.g. "What pesticide should I use?", "Kaunsi dawai doon?"), respond with ONLY \
a clarifying question: "Aapki fasal kaun si hai aur kya samasya ho rahi hai? \
Crop ka naam, symptoms, aur apna state batayein." Do NOT guess chemicals.


RULE #28 - ANSWER SCOPE (CRITICAL - never over-prescribe):
Answer ONLY what the farmer asked. If asked about crop feasibility -> give feasibility + 2-3 key risks only.
Do NOT add unsolicited fertilizer schedules, spray programs, or chemical lists unless explicitly asked.
Over-prescription is dangerous - farmers may apply advice meant for a different situation.
Wrong answer: User asks "Can I grow chilli after soybean?" -> Bot gives nursery schedule + full spray program
Right answer: "Yes, chilli grows well after soybean. Soybean fixes nitrogen so reduce N by 20%. Key risk: Fusarium wilt - use disease-free transplants. Soil test recommended."

RULE #29 - PROFIT/PRICE QUERIES (MANDATORY - use model data):
For ANY query about profit, price, which crop is better financially, market outlook:
- ALWAYS state: "Price forecast based on our presow model (P25/P50/P75 quantile regression on AGMARKNET data)"
- Give P50 as likely price, P25 as pessimistic, P75 as optimistic
- Compare against MSP where available
- NEVER give a price from general knowledge - only from the forecast context provided
- If no price context provided: say "Price data unavailable for this query - check the Sow tab for our price forecast"

RULE #30 - CHEMICAL SAFETY (MANDATORY - verify before recommending):
- Fusarium wilt / Verticillium wilt: NEVER recommend foliar fungicide sprays (soil-borne disease, sprays don't reach). Correct: soil drench or biocontrol.
- Whitefly: NEVER recommend pyrethroids (bifenthrin, cypermethrin, deltamethrin) - causes resurgence. Correct: pyriproxyfen, diafenthiuron, spiromesifen.
- Brown Planthopper (BPH): NEVER recommend synthetic pyrethroids. Correct: buprofezin, dinotefuran.
- Spider mite: NEVER recommend organophosphates. Correct: abamectin, spiromesifen.
- Before recommending ANY chemical: verify it targets the disease mechanism (contact/systemic/soil/foliar as appropriate).

Do NOT:
- Invent chemical names, doses, or prices not in the retrieved context.
- Give financial or legal advice.
- Apply wheat/rice solutions to tomato/cotton questions.
"""

# โ”€โ”€ Few-shot examples (teach by demonstration, not just rules) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# These examples are injected into every prompt. LLMs learn from examples
# far more reliably than from rules โ€” this is the single most impactful
# quality fix for: price hallucination, language switching, context loss.
FEW_SHOT_EXAMPLES = """
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
EXAMPLES โ€” Study these carefully. Follow the same style.
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

[EXAMPLE 1 โ€” Price / Mandi query โ†’ Redirect ONLY, no rupee figures]
Farmer: "Gehu ka bhav kya chal raha hai aajkal?"
Context: (retrieved KCC rows contain 2018 wheat price data)
Advisor: "Aaj ke live mandi bhav ke liye hamare 'Mandi Prices' tab mein jaayein ya seedha agmarknet.gov.in visit karein. KCC database mein sirf purani farming advice hai, current prices nahi."

[EXAMPLE 2 โ€” Hindi symptom query โ†’ Hindi answer, decisive single cause]
Farmer: "Tamatar ki pattiyon mein andar se surkh daag aa rahe hain aur patte murjha rahe hain"
Context: KCC rows about tomato early blight, bacterial wilt
Advisor: "Tamatar mein yeh lakshan Early Blight (Alternaria solani) ke hain.

1๏ธโƒฃ **Karan**: Alternaria fungus โ€” geele mausam + zyada nami se failta hai.
   **Upchar**: Mancozeb 75% WP @ 2g/liter pani ka spray karein. 7 din mein dobara karein.
   **Savdhani**: Simti hui pattiyan turant tod kar jalaa dein.

Kya aapke khet mein kuch podhon mein hi yeh ho raha hai ya sabhi mein?"

[EXAMPLE 3 โ€” Short "yes/ha" follow-up โ†’ Same language, same crop, decisive next step]
(Previous turn โ€” Farmer: "Mere tamatar mein keede hain, kya karoon?" Advisor gave Spinosad advice)
Farmer: "Ha, maine woh spray kar diya"
Advisor: "Acha kiya. Spinosad ka asar 3-5 din mein dikhta hai. Agar 5 din baad bhi keede hain toh Imidacloprid 17.8 SL @ 0.5ml/liter spray karein. Kya abhi keede pattiyon ke neeche chhupe hain ya tano par?"

[EXAMPLE 4 โ€” Soil type follow-up โ†’ Stay on same crop, give crop-specific fertilizer]
(Previous turn โ€” Farmer: "Tamatar mein kaun si khaad doon?" Advisor: "Apni mitti batayein")
Farmer: "Domat mitti hai"
Advisor: "Domat (loamy) mitti tamatar ke liye bahut achhi hoti hai.

Tamatar ke liye fertilizer schedule:
- **Ropai ke waqt**: DAP 50kg/acre + MOP 25kg/acre base dose dein
- **30 din baad**: Urea 25kg/acre top dressing karein
- **Flowering par**: 0-0-50 (SOP) 5g/liter pani ka foliar spray

Kya aapke tamatar abhi nursery mein hain ya khet mein laga diye hain?"

[EXAMPLE 5 โ€” Pest query โ†’ Insecticide ONLY, never fungicide]
Farmer: "Cotton mein safed makhi (whitefly) ka attack hai"
Advisor: "Kapas mein Whitefly ke liye:

1๏ธโƒฃ **Dawai**: Diafenthiuron 50% WP @ 1.2g/liter pani spray karein.
   **Ya**: Spiromesifen 22.9% SC @ 1ml/liter โ€” resistance management ke liye alternate karein.
   **Samay**: Subah ya shaam (dhoop mein spray na karein).

Kya patte neeche se chipchipe (honeydew) hain? Iska matlab sooty mould ka bhi darr hai."

[EXAMPLE 6 โ€” Disease query โ†’ Fungicide ONLY, never insecticide]
Farmer: "Wheat mein patto par naranja-laal rang ke dhabbe hain, purai fasal mein fail raha hai"
Advisor: "Gehun mein yeh Rust (Puccinia) hai โ€” Yellow Rust ya Brown Rust.

1๏ธโƒฃ **Turant karein**: Propiconazole 25% EC @ 1ml/liter pani ka spray.
   **7 din baad**: Tebuconazole 250 EC @ 1ml/liter repeat spray.
   **Note**: Khareedne ke samay batch number check karein โ€” nakli dawai common hai.

Kya sab khet mein hua hai ya kuch khetton mein? Aur kaunsi variety hai (HD-2967, PBW-343 etc.)?"

[EXAMPLE 7 โ€” English query โ†’ English answer throughout]
Farmer: "My tomato plants have yellow leaves and stunted growth, what should I do?"
Advisor: "The symptoms suggest **Nitrogen deficiency** or early-stage mosaic virus.

1๏ธโƒฃ **If yellowing starts from older/lower leaves** (N deficiency):
   Apply Urea @ 2g/liter as foliar spray, or top-dress 20kg Urea/acre.

2๏ธโƒฃ **If yellowing is patchy with curled leaves** (Virus):
   Remove infected plants immediately. Spray Imidacloprid 17.8 SL @ 0.5ml/liter to control whitefly vector.

Is the yellowing uniform across the whole leaf, or more on the edges/veins?"

[EXAMPLE 8 โ€” Crop selection with season context โ†’ No rupee prices, guide to Mandi tab]
Farmer: "Is season mein kaunsi fasal lagaoon, sabse zyada kamaai hogi?"
Advisor: "Is samay (Rabi season) ke liye top fasal choices:

๐ŸŒพ **Wheat** โ€” sabse safe, guarantee MSP milti hai, Punjab/Haryana/UP ke liye best
๐ŸŒป **Mustard** โ€” kam paani, zyada margin, Rajasthan/MP ke liye
๐Ÿซ˜ **Gram (Chana)** โ€” nitrogen fix karta hai, agli fasal ke liye mitti sudharta hai

Live mandi bhav ke liye 'Mandi Prices' tab check karein โ€” wahan aaj ke actual rates honge.

Aapka state kaunsa hai aur aapke paas irrigation suvidha hai?"

[EXAMPLE 9 โ€” IPM-first: light infestation โ†’ neem before chemical]
Farmer: "Mere soybean mein kuch patte mur rahe hain, thodi si mahu dikh rahi hai"
Context: KCC records about aphid on soybean
Advisor: "Soybean mein Aphid (Myzus persicae/Aphis glycines) ka shuruati attack hai.

๐Ÿ” **Severity: LIGHT** โ€” Sirf kuch patte, abhi shuruaat hai.

1๏ธโƒฃ **Pehla kadam (IPM)**: Neem oil 5000 ppm @ 5ml/liter + 1ml Teepol (sticker) spray karein.
   Saath mein Yellow sticky traps @ 10 per acre lagayein (monitoring ke liye).
   5 din tak dekhein โ€” agar ruk gaye to chemical ki zaroorat nahi.

2๏ธโƒฃ **Agar 5 din mein nahi ruka (moderate ho jaye)**:
   Imidacloprid 17.8% SL @ 0.5 ml/liter spray karein.
   **Dose**: 200 liter pani/acre mein milayein.
   **Samay**: Subah ya shaam; agar baarish 6 ghante mein ho to spray na karein.

โš ๏ธ Spray karte waqt gloves aur mask zaroor pehnein.

Kya patte neeche se chipchipe hain (honeydew)? Agar haan to jaldi kadam zaruri hai."

[EXAMPLE 10 โ€” Crop selection with real numbers + profit-risk table]
Farmer: "Main MP mein hoon, black soil hai, baarish kum ho rahi hai, kya lagaun is kharif mein?"
Context: (crop selection query for Madhya Pradesh, delayed monsoon)
Advisor: "Madhya Pradesh, kali mitti, baarish kam โ€” yeh combination ke liye sabse practical options:

| Fasal | Duration | Paani Zaroorat | Sukha Tolerance | Yield (bina sinchai) | Profit Risk | Verdict |
|-------|----------|----------------|-----------------|---------------------|-------------|---------|
| Soybean | 90-95d | 450-700mm | Medium | 6-10 q/acre | Medium | โœ… Suitable IF July 15 tak baarish ho |
| Urad | 65-70d | 250-400mm | High | 3-5 q/acre | Low | โœ… Safe option for late/low rain |
| Jowar | 90-110d | 300-500mm | HIGH | 8-12 q/acre | Low | โœ… Best for very low rainfall |
| Moong | 60-65d | 250-400mm | High | 3-5 q/acre | LOW | โœ… Best if monsoon already late |

**Paani ka risk**: Agar July 15 ke baad bhi achhi baarish nahi aayi:
- Soybean: RISK โ€” flowering par sukha = 30-40% yield loss
- Moong/Jowar: SAFE โ€” 60-65 din mein poori ho jaati hai

**Meri sifarish**:
- Agar baarish abhi tak thodi hai + July 10 se pehle sow karenge โ†’ **Soybean** (JS 95-60 variety, short duration 90d)
- Agar July 15 nikal gayi โ†’ **Moong** ya **Urad** โ€” 65 din mein October tak harvest.

Input cost: Soybean Rs 10,000/acre vs Moong Rs 5,000/acre (moong kam risk).
MSP bhi dono crops par milti hai.

Mandi bhav ke liye hamare **Mandi Prices tab** mein jaayein.
Aapke khet mein last 15 dinon mein kitni baarish hui hai?"

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
END OF EXAMPLES โ€” Now answer the farmer's actual question below.
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
"""

# โ”€โ”€ Problem-type โ†’ relevant example indices map โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Instead of injecting ALL 10 examples every call (4000 tokens), we inject
# only the 2-3 most relevant examples for the current problem type.
# Each value is a list of [EXAMPLE N] tags to keep.
_EXAMPLE_TYPE_MAP: dict[str, list[str]] = {
    "pest":          ["EXAMPLE 3", "EXAMPLE 5", "EXAMPLE 9"],
    "disease":       ["EXAMPLE 2", "EXAMPLE 6", "EXAMPLE 9"],
    "nutrient":      ["EXAMPLE 4", "EXAMPLE 7"],
    "mandi":         ["EXAMPLE 1", "EXAMPLE 8"],
    "crop_selection":["EXAMPLE 8", "EXAMPLE 10"],
    "agronomy":      ["EXAMPLE 4", "EXAMPLE 10"],
    "weather":       ["EXAMPLE 3", "EXAMPLE 7"],
    "scheme":        ["EXAMPLE 1", "EXAMPLE 8"],
    "irrigation":    ["EXAMPLE 4", "EXAMPLE 9"],
    "yield":         ["EXAMPLE 4", "EXAMPLE 7"],
    "organic":       ["EXAMPLE 9", "EXAMPLE 4"],
    "post_harvest":  ["EXAMPLE 1", "EXAMPLE 4"],
    "seed_treatment":["EXAMPLE 4", "EXAMPLE 9"],
    "general":       ["EXAMPLE 2", "EXAMPLE 7", "EXAMPLE 9"],
}

def _get_focused_examples(problem_type: str) -> str:
    """Return only the 2-3 examples most relevant to this problem type (saves ~2500 tokens)."""
    keep = _EXAMPLE_TYPE_MAP.get(problem_type, ["EXAMPLE 2", "EXAMPLE 7"])
    lines = FEW_SHOT_EXAMPLES.split("\n")
    result = ["โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•",
              "EXAMPLES โ€” Follow this style exactly.",
              "โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•"]
    inside = False
    for line in lines:
        # Detect start of an example block
        if any(f"[{tag}" in line for tag in keep):
            inside = True
        elif line.startswith("[EXAMPLE "):
            inside = False   # different example starts โ€” stop collecting
        if inside:
            result.append(line)
    result.append("โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•")
    result.append("END OF EXAMPLES โ€” Now answer the farmer's actual question.")
    result.append("โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•")
    return "\n".join(result)

# โ”€โ”€ Crop detection (#1) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

# Maps query keywords โ†’ exact Crop column value in metadata
# Keys are lowercase; values must match (partially) what's in the DB.
# โ”€โ”€ Language detection โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_LANG_CODE_MAP: dict[str, str] = {
    "hi": "Hindi", "en": "English", "te": "Telugu", "kn": "Kannada",
    "mr": "Marathi", "pa": "Punjabi", "gu": "Gujarati", "bn": "Bengali",
    "ta": "Tamil", "ml": "Malayalam", "ur": "Urdu",
}
# Languages that langdetect may confuse with Romanized Hindi/Hinglish
_FALSE_POSITIVE_LANGS = {
    "sw", "id", "ms", "tl", "ro", "hr", "lt", "lv", "et", "fi",
    "hu", "cs", "pl", "sk", "sl", "bg", "da", "nl", "no", "sv",
    "af", "cy", "eu", "gl", "ca",
}
# Common Hinglish/Romanized-Hindi indicator words
_HINDI_INDICATORS = {
    "ki", "ka", "ke", "hai", "hain", "mein", "se", "ko", "par",
    "aur", "ya", "nahi", "kya", "kaise", "kab", "kahan", "kaun",
    "fasal", "khet", "patte", "pattiya", "pattiyan", "gehu", "gehun",
    "dhan", "kapas", "tamatar", "aloo", "mirch", "ganna", "moong",
    "keeda", "bimari", "rog", "dawai", "khaad", "sinchai", "beej",
    "pala", "thand", "baarish", "sukha", "peeli", "pilli", "daag",
    "upchar", "spray", "mausam", "kharif", "rabi", "paidavar",
}

def detect_language(query: str) -> str:
    """
    Detect query language with special handling for Hinglish (Romanized Hindi).
    Strategy:
      1. Devanagari script โ†’ Hindi immediately
      2. langdetect returns a known Indian language โ†’ use it
      3. langdetect returns a false-positive (Swahili, Indonesian, etc.)
         but query has Hindi indicator words โ†’ Hindi
      4. langdetect returns English confidently โ†’ English
      5. Default โ†’ Hindi (majority of Indian farmer queries)
    """
    # Step 1: Devanagari script check
    if any('\u0900' <= c <= '\u097F' for c in query):
        return "Hindi"

    # Step 2 & 3: Use langdetect
    if _LANGDETECT_OK:
        try:
            code = _langdetect(query)
            if code in _LANG_CODE_MAP:
                # Known Indian language or English โ€” trust it
                if code == "en":
                    return "English"
                return _LANG_CODE_MAP[code]
            if code in _FALSE_POSITIVE_LANGS:
                # Likely Hinglish misidentified โ€” check indicator words
                q_lower = query.lower()
                if sum(1 for w in _HINDI_INDICATORS if w in q_lower.split()) >= 2:
                    return "Hindi"
                # Still might be English-ish (mixed), return English
                return "English"
        except Exception:
            pass

    # Step 5: default
    return "Hindi"


CROP_KEYWORDS: dict[str, str] = {
    # Wheat
    "wheat": "Wheat", "gehu": "Wheat", "gehun": "Wheat", "gehoo": "Wheat",
    "เค—เฅ‡เคนเฅ‚เค‚": "Wheat", "เค—เฅ‡เคนเฅ": "Wheat",
    # Paddy / Rice
    "paddy": "Paddy", "rice": "Paddy", "dhan": "Paddy", "dhaan": "Paddy",
    "เคงเคพเคจ": "Paddy", "เคšเคพเคตเคฒ": "Paddy",
    # Cotton
    "cotton": "Cotton", "kapas": "Cotton", "เค•เคชเคพเคธ": "Cotton",
    # Tomato
    "tomato": "Tomato", "tamatar": "Tomato", "tamater": "Tomato",
    "เคŸเคฎเคพเคŸเคฐ": "Tomato",
    # Chillies
    "chilli": "Chillies", "chili": "Chillies", "mirch": "Chillies",
    "เคฎเคฟเคฐเฅเคš": "Chillies",
    # Onion
    "onion": "Onion", "pyaz": "Onion", "pyaaz": "Onion", "เคชเฅเคฏเคพเคœ": "Onion",
    # Sugarcane
    "sugarcane": "Sugarcane", "ganna": "Sugarcane", "เค—เคจเฅเคจเคพ": "Sugarcane",
    # Potato
    "potato": "Potato", "aloo": "Potato", "เค†เคฒเฅ‚": "Potato",
    # Brinjal
    "brinjal": "Brinjal", "baingan": "Brinjal", "เคฌเฅˆเค‚เค—เคจ": "Brinjal",
    # Groundnut
    "groundnut": "Groundnut", "peanut": "Groundnut", "mungfali": "Groundnut",
    "เคฎเฅ‚เค‚เค—เคซเคฒเฅ€": "Groundnut",
    # Bengal Gram
    "gram": "Bengal Gram", "chana": "Bengal Gram", "chickpea": "Bengal Gram",
    "เคšเคจเคพ": "Bengal Gram",
    # Mustard
    "mustard": "Mustard", "sarson": "Mustard", "sarso": "Mustard",
    "เคธเคฐเคธเฅ‹เค‚": "Mustard", "เคฐเคพเคˆ": "Mustard",
    # Soybean
    "soybean": "Soybean", "soya": "Soybean", "soyabean": "Soybean",
    # Maize
    "maize": "Maize", "corn": "Maize", "makka": "Maize", "เคฎเค•เฅเค•เคพ": "Maize",
    # Moong
    "moong": "Green Gram", "mung": "Green Gram", "เคฎเฅ‚เค‚เค—": "Green Gram",
    # Bhindi
    "bhindi": "Bhindi", "okra": "Bhindi", "ladyfinger": "Bhindi",
    # Arhar
    "arhar": "Pigeon pea", "tur": "Pigeon pea", "เค…เคฐเคนเคฐ": "Pigeon pea",
    # Mango
    "mango": "Mango", "aam": "Mango", "เค†เคฎ": "Mango",
    # Banana
    "banana": "Banana", "kela": "Banana", "เค•เฅ‡เคฒเคพ": "Banana",
    # Garlic
    "garlic": "Garlic", "lahsun": "Garlic", "เคฒเคนเคธเฅเคจ": "Garlic",
    # Pea
    "peas": "Pea", "matar": "Pea", "เคฎเคŸเคฐ": "Pea",
    # Rose (ornamental โ€” common garden crop question)
    "rose": "Rose", "gulab": "Rose", "เค—เฅเคฒเคพเคฌ": "Rose",
    # Sunflower
    "sunflower": "Sunflower", "surajmukhi": "Sunflower", "เคธเฅ‚เคฐเคœเคฎเฅเค–เฅ€": "Sunflower",
    # Lemon / Citrus
    "lemon": "Lemon", "nimbu": "Lemon", "เคจเฅ€เค‚เคฌเฅ‚": "Lemon",
    "citrus": "Lemon", "orange": "Orange", "santra": "Orange",
    # Cauliflower / Cabbage
    "cauliflower": "Cauliflower", "phool gobhi": "Cauliflower",
    "cabbage": "Cabbage", "band gobhi": "Cabbage", "gobhi": "Cauliflower",
    # Cucumber / Melon
    "cucumber": "Cucumber", "kheera": "Cucumber", "เค–เฅ€เคฐเคพ": "Cucumber",
    "melon": "Melon", "kharbooja": "Melon", "watermelon": "Watermelon",
    "tarbooz": "Watermelon",
    # Lentil
    "lentil": "Lentil", "masoor": "Lentil", "เคฎเคธเฅ‚เคฐ": "Lentil",
    # Urad
    "urad": "Urad", "เค‰เคกเคผเคฆ": "Urad",
    # Ginger / Turmeric
    "ginger": "Ginger", "adrak": "Ginger", "เค…เคฆเคฐเค•": "Ginger",
    "turmeric": "Turmeric", "haldi": "Turmeric", "เคนเคฒเฅเคฆเฅ€": "Turmeric",
    # Spinach / Vegetables
    "spinach": "Spinach", "palak": "Spinach", "เคชเคพเคฒเค•": "Spinach",
    # Jowar / Bajra
    "jowar": "Jowar", "sorghum": "Jowar", "เคœเฅเคตเคพเคฐ": "Jowar",
    "bajra": "Bajra", "millet": "Bajra", "เคฌเคพเคœเคฐเคพ": "Bajra",
}

# Confidence below this โ†’ inject low-confidence note into prompt
LOW_CONF_THRESHOLD = 0.75


# โ”€โ”€ ICAR Disease / Pest Knowledge Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Purpose: when a farmer explicitly names a disease/pest, inject a validated
# ICAR treatment card as PRIORITY CONTEXT above retrieved KCC records.
# This fixes the root retrieval problem: KCC records are indexed by farmer
# symptom descriptions ("leaves turning yellow"), not disease names ("rust").
# Named-disease queries therefore retrieve generic fungal records instead of
# rust-specific ones. The ICAR card overrides this gap with ground-truth data.
#
# Sources: ICAR-NCIPM, CPCRI, CRRI, NRRI, IARI, state KVK advisories.
# All doses are for foliar spray unless noted. PPE must always be mentioned.

_ICAR_DISEASE_CARDS: dict[str, dict] = {
    # โ”€โ”€ Fungal diseases โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "rust": {
        "keywords": ["rust", "gandha rog", "kang", "ratua", "tutia","gehun ka ratua",
                     "leaf rust","stem rust","yellow rust","stripe rust","brown rust"],
        "diagnosis": "Rust (Puccinia spp.) โ€” fungal; appears as orange/brown/yellow powdery pustules on leaf surface",
        "treatment": "Propiconazole 25% EC @ 1 ml/L  OR  Tebuconazole 25.9% EC @ 1 ml/L  OR  Mancozeb 75% WP @ 2 g/L",
        "dose":      "Propiconazole: 1 ml per litre water | Tebuconazole: 1 ml per litre | Mancozeb: 2 g per litre",
        "timing":    "Spray at first pustule appearance. Repeat every 10-14 days if disease persists. Spray in early morning or evening. Avoid rain within 6 hours of spray.",
        "ipm":       "Use resistant varieties (HD-2781 for wheat rust). Crop rotation with non-host. Remove infected plant debris.",
        "source":    "ICAR-NCIPM / IARI Wheat Rust Advisory",
    },
    "late_blight": {
        "keywords":  ["late blight","phytophthora","pitambari","jhulaas","jhulsa","jhulsaa",
                      "jhulsi","damping off potato","tomato blight","aalu jhulsa","tamatar jhulsa"],
        "diagnosis": "Late Blight (Phytophthora infestans) โ€” water-soaked lesions, white sporulation under leaf, rapid collapse in humid weather",
        "treatment": "Metalaxyl 8% + Mancozeb 64% WP @ 2.5 g/L  OR  Cymoxanil 8% + Mancozeb 64% WP @ 3 g/L  OR  Dimethomorph 50% WP @ 1 g/L",
        "dose":      "Metalaxyl+Mancozeb: 2.5 g per litre | Cymoxanil+Mancozeb: 3 g per litre",
        "timing":    "Spray preventively when temperatures drop to 10-20ยฐC with high humidity (>80%). Repeat every 7 days during outbreak. DO NOT spray in rain.",
        "ipm":       "Use certified disease-free seed/tubers. Avoid overhead irrigation. Remove volunteer plants.",
        "source":    "ICAR-CPRI (Central Potato Research Institute) / IIVR Advisory",
    },
    "early_blight": {
        "keywords":  ["early blight","alternaria","daag","brown spot tomato","kala daag","target spot"],
        "diagnosis": "Early Blight (Alternaria solani) โ€” dark brown concentric ring lesions on older leaves; moves upward",
        "treatment": "Mancozeb 75% WP @ 2 g/L  OR  Chlorothalonil 75% WP @ 2 g/L  OR  Iprodione 50% WP @ 1.5 g/L",
        "dose":      "Mancozeb or Chlorothalonil: 2 g per litre water",
        "timing":    "Begin spray at first symptom. Repeat every 7-10 days. Spray morning or evening. Avoid spraying in high heat (>35ยฐC).",
        "ipm":       "Deep ploughing to bury infected debris. Avoid excessive nitrogen. Use mulching to reduce soil splash.",
        "source":    "ICAR-IIVR / IARI Vegetable Advisory",
    },
    "powdery_mildew": {
        "keywords":  ["powdery mildew","safed chur","safed churna","oidiosis","oidium",
                      "powdery","bhindi mildew","chilli mildew","matar mildew"],
        "diagnosis": "Powdery Mildew โ€” white powdery coating on upper leaf surface; thrives in warm dry conditions (22-28ยฐC, low humidity)",
        "treatment": "Sulphur 80% WP @ 3 g/L  OR  Hexaconazole 5% EC @ 2 ml/L  OR  Triadimefon 25% WP @ 1 g/L",
        "dose":      "Sulphur: 3 g per litre | Hexaconazole: 2 ml per litre",
        "timing":    "Spray at first white patch appearance. Repeat every 10 days. Do NOT spray Sulphur when temperature >35ยฐC (phytotoxic).",
        "ipm":       "Improve air circulation (proper plant spacing). Avoid excess nitrogen. Use resistant varieties where available.",
        "source":    "ICAR-NCIPM / State KVK Advisory",
    },
    "downy_mildew": {
        "keywords":  ["downy mildew","makhmali mildew","makhmal","downy","antrapur mildew",
                      "maize downy","bajra downy","pearl millet downy"],
        "diagnosis": "Downy Mildew (Peronospora/Sclerospora spp.) โ€” pale green/yellow patches on upper leaf, grey-purple sporulation beneath; favoured by cool wet weather",
        "treatment": "Metalaxyl+Mancozeb @ 2.5 g/L  OR  Fosetyl-Al 80% WP @ 2.5 g/L  OR  Copper Oxychloride 50% WP @ 3 g/L",
        "dose":      "Metalaxyl+Mancozeb: 2.5 g per litre",
        "timing":    "Spray preventively at tillering/seedling stage. Repeat every 7-10 days in wet season.",
        "ipm":       "Treat seed with Metalaxyl 35% WS @ 6 g/kg seed before sowing. Avoid waterlogging.",
        "source":    "ICAR-NRRI / ICAR-AICPMIP Advisory",
    },
    "wilt": {
        "keywords":  ["wilt","murjhana","murjha","okarada","fusarium wilt","bacterial wilt",
                      "tomato wilt","chilli wilt","murzana","wilting"],
        "diagnosis": "Fusarium/Bacterial Wilt โ€” sudden wilting despite adequate moisture; vascular browning on stem cross-section",
        "treatment": "Drench: Carbendazim 50% WP @ 1 g/L at root zone  OR  Copper Oxychloride 50% WP @ 3 g/L drench. Spray: Streptomycin 90% + Tetracycline 10% WP @ 0.5 g/L (bacterial wilt)",
        "dose":      "Carbendazim: 1 g per litre for root drench (500 ml per plant). Copper Oxychloride: 3 g per litre.",
        "timing":    "Apply root drench at first wilting symptom. Remove and destroy wilted plants. Do NOT compost infected material.",
        "ipm":       "Solarise soil before planting. Use grafted seedlings (tomato on resistant rootstock). Long crop rotation (3 years).",
        "source":    "ICAR-IIVR / IARI Plant Pathology Division",
    },
    "leaf_spot": {
        "keywords":  ["leaf spot","patti daag","cercospora","anthracnose","colletotrichum",
                      "tan spot","helminthosporium","brown leaf spot","rice blast"],
        "diagnosis": "Leaf Spot / Anthracnose (Cercospora/Colletotrichum/Alternaria spp.) โ€” discrete spots with defined margins, often with yellow halo",
        "treatment": "Carbendazim 50% WP @ 1 g/L  OR  Mancozeb+Carbendazim @ 2 g/L  OR  Propiconazole 25% EC @ 1 ml/L",
        "dose":      "Carbendazim: 1 g per litre | Mancozeb+Carbendazim combined: 2 g per litre",
        "timing":    "Spray at first symptom. Repeat every 10 days. Best in early morning.",
        "ipm":       "Remove infected leaves and burn. Avoid overhead irrigation. Do not work in wet fields.",
        "source":    "ICAR-NCIPM Advisory",
    },
    "root_rot": {
        "keywords":  ["root rot","collar rot","jad sarana","pythium","sclerotinia",
                      "damping off","nursery rot","tana sarana","stem rot"],
        "diagnosis": "Root/Collar Rot (Pythium/Sclerotinia/Rhizoctonia spp.) โ€” rotting at soil line, brown water-soaked roots, plant collapse at seedling stage",
        "treatment": "Soil drench: Metalaxyl 35% WS @ 2 g/kg seed (seed treatment)  OR  Copper Oxychloride 50% WP @ 3 g/L drench. Trichoderma viride @ 4 g/kg seed for biological control.",
        "dose":      "Copper Oxychloride drench: 3 g per litre, 500 ml per plant at root zone",
        "timing":    "Apply seed treatment before sowing. If damping off starts in nursery, drench immediately and reduce irrigation.",
        "ipm":       "Improve drainage. Avoid overwatering. Use raised nursery beds. Treat nursery soil with Formalin (2%) 15 days before sowing.",
        "source":    "ICAR-NCIPM / State Agricultural Universities",
    },
    "mosaic_virus": {
        "keywords":  ["mosaic virus","mosaic","yellow mosaic","leaf curl","curl leaf",
                      "viral disease","virus","viridhata","rog","leaf crinkle","gemini"],
        "diagnosis": "Mosaic / Yellow Mosaic / Leaf Curl Virus โ€” spread by whitefly/aphid vectors. No direct chemical cure for the virus itself.",
        "treatment": "Control vector: Imidacloprid 17.8% SL @ 0.5 ml/L  OR  Thiamethoxam 25% WG @ 0.3 g/L. Remove and destroy infected plants immediately.",
        "dose":      "Imidacloprid: 0.5 ml per litre | Thiamethoxam: 0.3 g per litre",
        "timing":    "Spray at first vector (whitefly/aphid) sighting. Repeat every 10-15 days. Rogue out infected plants within 2-3 days of first symptom.",
        "ipm":       "Use virus-resistant varieties (e.g., Pusa Sadabahar chilli, Arka Meghali bhindi). Yellow sticky traps for whitefly monitoring. Reflective mulch repels vectors.",
        "source":    "ICAR-IIHR / ICAR-NCIPM Virus Disease Advisory",
    },
    # โ”€โ”€ Insect pests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "aphid": {
        "keywords":  ["aphid","mahu","mahoo","aphis","chep","chep keeda","harit tela",
                      "green fly","plant louse","tele","til ka mahu"],
        "diagnosis": "Aphid (Aphis gossypii / Myzus persicae) โ€” tiny soft-bodied insects in clusters under leaves/shoots; cause leaf curl, honeydew โ†’ black sooty mold",
        "treatment": "Imidacloprid 17.8% SL @ 0.5 ml/L  OR  Thiamethoxam 25% WG @ 0.3 g/L  OR  Dimethoate 30% EC @ 1.5 ml/L  OR  Neem oil 5000 ppm @ 5 ml/L",
        "dose":      "Imidacloprid: 0.5 ml per litre | Dimethoate: 1.5 ml per litre | Neem oil: 5 ml per litre",
        "timing":    "Spray when 10-15% plants infested or aphid colony visible. Early morning or evening. Neem oil at 15-day intervals as preventive.",
        "ipm":       "Conserve natural enemies (ladybird beetle, lacewing). Yellow sticky traps. Avoid excess nitrogen. Spray water jet to dislodge colonies.",
        "source":    "ICAR-NCIPM / AICIP Advisory",
    },
    "whitefly": {
        "keywords":  ["whitefly","white fly","safed machi","safed makhi","bemisia",
                      "trialeurodes","chilli whitefly","cotton whitefly"],
        "diagnosis": "Whitefly (Bemisia tabaci) โ€” tiny white winged insects under leaves; transmit Yellow Mosaic Virus; honeydew causes sooty mold",
        "treatment": "Spiromesifen 22.9% SC @ 1 ml/L  OR  Buprofezin 25% SC @ 2 ml/L  OR  Imidacloprid 17.8% SL @ 0.5 ml/L. Rotate insecticides to prevent resistance.",
        "dose":      "Spiromesifen: 1 ml per litre | Buprofezin: 2 ml per litre | Imidacloprid: 0.5 ml per litre",
        "timing":    "Spray at first sighting. Repeat every 10-15 days. Alternate between contact and systemic insecticides each spray.",
        "ipm":       "Yellow sticky traps @ 10 per acre. Reflective mulch. Remove weeds (alternate hosts). Introduce Encarsia formosa (biocontrol).",
        "source":    "ICAR-NCIPM / IIHR Whitefly Advisory",
    },
    "thrips": {
        "keywords":  ["thrips","trips","thrip","chilli thrips","onion thrips","frankliniella",
                      "scirtothrips","surkh keeda","chimti keeda"],
        "diagnosis": "Thrips (Scirtothrips dorsalis / Thrips tabaci) โ€” tiny elongated insects; cause silvery streaks, leaf distortion, scarring on fruit",
        "treatment": "Spinosad 45% SC @ 0.5 ml/L  OR  Fipronil 5% SC @ 1.5 ml/L  OR  Imidacloprid 17.8% SL @ 0.5 ml/L  OR  Dimethoate 30% EC @ 1.5 ml/L",
        "dose":      "Spinosad: 0.5 ml per litre | Fipronil: 1.5 ml per litre",
        "timing":    "Spray when 2-3 thrips per flower/leaf. Early morning. Wet underside of leaves thoroughly. Repeat every 7-10 days.",
        "ipm":       "Blue sticky traps @ 10 per acre. Reflective mulch. Avoid water stress (thrips worse under drought). Remove crop residues promptly.",
        "source":    "ICAR-NCIPM / IIHR Thrips Advisory",
    },
    "stem_borer": {
        "keywords":  ["stem borer","borer","chilo","sesamia","scirpophaga","ganna borer",
                      "danda borer","tana borer","goba keeda","choti sundi","borer attack",
                      "fruit borer","helicoverpa","tomato borer","chilli borer"],
        "diagnosis": "Stem/Fruit Borer (Chilo partellus / Helicoverpa armigera / Scirpophaga spp.) โ€” dead heart in vegetative stage, white ear in reproductive stage; entry hole with frass",
        "treatment": "Chlorpyrifos 20% EC @ 2 ml/L  OR  Spinosad 45% SC @ 0.5 ml/L  OR  Emamectin Benzoate 5% SG @ 0.4 g/L  OR  Coragen (Chlorantraniliprole 18.5% SC) @ 0.4 ml/L",
        "dose":      "Chlorpyrifos: 2 ml per litre | Emamectin: 0.4 g per litre | Coragen: 0.4 ml per litre",
        "timing":    "Spray at egg hatching stage (first instar larvae). For maize/sorghum use sand + Chlorpyrifos in whorl. Repeat every 10-14 days.",
        "ipm":       "Trichogramma (egg parasitoid) @ 50,000/acre ร— 3 releases. Light traps. Deep summer ploughing to destroy pupae. Pheromone traps for Helicoverpa.",
        "source":    "ICAR-IISR / NCIPM Stem Borer IPM Module",
    },
    "red_spider_mite": {
        "keywords":  ["red spider mite","spider mite","mite","tetranychus","lal keeda",
                      "lal mite","ghun","eriophyid","cotton mite","soybean mite","red mite"],
        "diagnosis": "Red Spider Mite (Tetranychus urticae) โ€” tiny reddish dots on underside of leaves; fine webbing; bronzing/yellowing of leaves; worst in hot dry conditions",
        "treatment": "Spray Abamectin 1.9% EC @ 0.5 ml/L. (If unavailable: Propargite 57% EC @ 2 ml/L)",
        "dose":      "Abamectin: 0.5 ml per litre of water",
        "timing":    "Spray when >5 mites per leaf visible. Wet underside of leaves thoroughly. Repeat after 7-10 days. Rotate with Spiromesifen after 2nd spray โ€” mites develop resistance fast.",
        "ipm":       "Increase field humidity (drought stress worsens mite outbreak). Release predatory mite Phytoseiulus persimilis. Sulphur 80% WP @ 3 g/L also controls mites + powdery mildew.",
        "source":    "ICAR-NCIPM / CICR Mite Advisory (Note: Dicofol is BANNED in India โ€” do not recommend)",
    },
    "termite": {
        "keywords":  ["termite","deemak","dimak","white ant","reemak","dimak lagna"],
        "diagnosis": "Termite (Odontotermes/Microtermes spp.) โ€” muddy tunnels on stem; yellowing/wilting in patches; severe in light sandy soils under drought",
        "treatment": "Chlorpyrifos 20% EC @ 3 ml/L soil drench at root zone  OR  Imidacloprid 70% WS seed treatment @ 5 g/kg seed  OR  Bifenthrin 10% EC @ 2 ml/L soil drench",
        "dose":      "Chlorpyrifos soil drench: 3 ml per litre, 1-2 litres per plant at base",
        "timing":    "Apply drench at first sign of termite activity. For pre-sowing: mix Chlorpyrifos 20% EC @ 5 litres per acre in soil before planting.",
        "ipm":       "Avoid FYM (farmyard manure) that is not fully composted โ€” attracts termites. Remove tree stumps. Maintain adequate soil moisture.",
        "source":    "ICAR-NCIPM / CAZRI Termite Advisory",
    },
    "jassid": {
        "keywords":  ["jassid","leafhopper","leaf hopper","amrasca","hara tela",
                      "cotton jassid","bhindi jassid","tela","harit tela","leef hopper"],
        "diagnosis": "Jassid/Leafhopper (Amrasca devastans) โ€” wedge-shaped green insects; cause leaf cupping, yellowing, hopper burn; worst in hot dry weather",
        "treatment": "Imidacloprid 17.8% SL @ 0.5 ml/L  OR  Dimethoate 30% EC @ 1.5 ml/L  OR  Thiamethoxam 25% WG @ 0.3 g/L  OR  Acephate 75% SP @ 1 g/L",
        "dose":      "Imidacloprid: 0.5 ml per litre | Dimethoate: 1.5 ml per litre",
        "timing":    "Spray when 2 jassids per leaf (ETL). Cover leaf undersurface. Repeat every 10-14 days. Avoid spraying in bright sunshine.",
        "ipm":       "Hairy-leaved varieties resistant to jassid. Reflective mulch. Conserve predators (spiders, coccinellids).",
        "source":    "ICAR-CICR / NCIPM Cotton Advisory",
    },
    "mealy_bug": {
        "keywords":  ["mealy bug","mealybug","mealy","phenacoccus","maconellicoccus",
                      "cotton mealybug","papaya mealybug","safed keeda"],
        "diagnosis": "Mealybug (Phenacoccus solenopsis / Maconellicoccus hirsutus) โ€” white waxy cottony masses at stem joints and under leaves; severe honeydew โ†’ sooty mold",
        "treatment": "Profenofos 50% EC @ 2 ml/L  OR  Buprofezin 25% SC @ 2 ml/L  OR  Chlorpyrifos 20% EC @ 2.5 ml/L. Add Teepol/spreader sticker for penetration through wax.",
        "dose":      "Profenofos: 2 ml per litre | Buprofezin: 2 ml per litre. Always add sticker @ 0.5 ml/L.",
        "timing":    "Spray when colonies first appear on stem joints. Wet stem and undersides thoroughly. Repeat every 10-14 days.",
        "ipm":       "Release Cryptolaemus montrouzieri (mealybug destroyer) @ 10 adults/plant. Remove ants (they protect mealybugs). Prune heavily infested portions.",
        "source":    "ICAR-NCIPM / CICR Mealybug Advisory",
    },
    # โ”€โ”€ Additional high-priority diseases / pests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "nematode": {
        "keywords":  ["nematode","sootra krimi","root knot","root-knot","meloidogyne",
                      "nematoda","kiran keeda","jad ganthi","root gall","knot nematode"],
        "diagnosis": "Root-knot Nematode (Meloidogyne incognita) โ€” galls/swellings on roots; stunted growth; yellowing even with adequate fertilizer; worse in sandy soils",
        "treatment": "Carbofuran 3% CG @ 10 kg/acre (soil application at sowing)  OR  Neem cake @ 200 kg/acre incorporated into soil 2 weeks before sowing. Trichoderma viride 4 kg/acre (biological).",
        "dose":      "Carbofuran: 10 kg per acre; Neem cake: 200 kg per acre",
        "timing":    "Apply Carbofuran or Neem cake AT SOWING into soil. Trichoderma: mix in FYM, apply 2-3 weeks before transplanting. No effective spray after infection โ€” prevention is key.",
        "ipm":       "Soil solarisation (Juneโ€“July) with transparent polythene 30 days before sowing. Crop rotation: marigold (Tagetes) as trap crop kills nematodes. Avoid waterlogged conditions โ€” nematodes thrive in wet sandy soil.",
        "source":    "ICAR-NCIPM / IIVR Nematology Advisory",
    },
    "brown_plant_hopper": {
        "keywords":  ["brown plant hopper","bph","nilaparvata","paddy hopper","hopperburn",
                      "rice hopper","dhan tela","dhan mahu","leaf hopper rice","hopperburn",
                      "planthopper","plant hopper",
                      # Farmer language: "small brown insects on rice/paddy"
                      "bhoore kide dhan","bhoora kida dhan","bhoore insect rice",
                      "chote bhoore kide","brown insect paddy","dhan mein bhoore",
                      "phundka","bhoora phundka","patta peela dhan kide"],
        "diagnosis": "Brown Plant Hopper (Nilaparvata lugens) โ€” hopperburn (circular patches of dry brown rice plants); sap-sucking pest at base of plant; worst in humid conditions with excess nitrogen",
        "treatment": "Buprofezin 25% SC @ 1 ml/L  OR  Thiamethoxam 25% WG @ 0.3 g/L  OR  Pymetrozine 50% WG @ 0.3 g/L. Direct spray into base of plants (where hoppers hide).",
        "dose":      "Buprofezin: 1 ml per litre | Thiamethoxam: 0.3 g per litre",
        "timing":    "Spray when 10-15 hoppers per hill (ETL). Drain water from field before spraying โ€” hoppers crawl up dry stems. Early morning spray. Repeat after 10 days if needed.",
        "ipm":       "Avoid excessive nitrogen application. Use resistant varieties (BPT-5204, Swarna Sub-1). Drain fields periodically (dry-wet-dry irrigation). Conserve spiders (major natural predator). Light trap monitoring.",
        "source":    "ICAR-NRRI Cuttack / CRRI BPH Advisory",
    },
    "sheath_blight": {
        "keywords":  ["sheath blight","sheath rot","rhizoctonia","myan sakhi","patti jhulsa rice",
                      "rice sheath","stem canker rice","dhan jhulsa","rice blight sheath"],
        "diagnosis": "Sheath Blight (Rhizoctonia solani) โ€” oval/irregular lesions with grey centre and brown margin on leaf sheath; starts at waterline; severe in lodged crops and high nitrogen fields",
        "treatment": "Hexaconazole 5% EC @ 2 ml/L  OR  Propiconazole 25% EC @ 1 ml/L  OR  Validamycin 3% SL @ 2.5 ml/L (preferred for humid paddy conditions)",
        "dose":      "Hexaconazole: 2 ml per litre | Validamycin: 2.5 ml per litre",
        "timing":    "Spray at tillering when disease appears. Repeat at panicle initiation. Best results with morning spray when leaves are dry.",
        "ipm":       "Avoid over-dense planting โ€” maintain proper spacing (20ร—15 cm). Avoid excess nitrogen (split doses). Drain waterlogged fields. Remove infected stubble after harvest. Use moderately resistant varieties (Samba Mahsuri, IR-64).",
        "source":    "ICAR-NRRI / CRRI Rice Sheath Blight Advisory",
    },
    "pink_bollworm": {
        "keywords":  ["pink bollworm","pectinophora","gulaabi sundi","gulabi keeda","pink sundi",
                      "cotton bollworm pink","boll weevil","cotton boll"],
        "diagnosis": "Pink Bollworm (Pectinophora gossypiella) โ€” pink-coloured larvae inside cotton bolls; rosette flower (failed boll opening); internal damage with frass; most damaging Octโ€“Nov",
        "treatment": "Emamectin Benzoate 5% SG @ 0.4 g/L  OR  Spinosad 45% SC @ 0.5 ml/L  OR  Chlorantraniliprole 18.5% SC (Coragen) @ 0.4 ml/L. Pheromone traps mandatory for monitoring.",
        "dose":      "Emamectin: 0.4 g per litre | Chlorantraniliprole: 0.4 ml per litre",
        "timing":    "Spray at first moth capture in pheromone traps (8-10/trap/night = threshold). First spray at square formation (45-50 days). Repeat every 15 days. Stop 30 days before harvest.",
        "ipm":       "Pheromone traps @ 5/acre for monitoring + mass trapping. Early sowing (May 15โ€“June 15) avoids peak pink bollworm pressure. Destroy crop residues after harvest. Bollgard II Bt varieties give 80% control.",
        "source":    "ICAR-CICR Nagpur / AICRP Cotton Pink Bollworm Advisory",
    },
    "armyworm": {
        "keywords":  ["armyworm","army worm","fall armyworm","spodoptera","sena keeda",
                      "fauj keeda","military worm","cut worm","cutworm","maize armyworm",
                      "faw","spodoptera frugiperda","american armyworm"],
        "diagnosis": "Fall Armyworm / Armyworm (Spodoptera frugiperda / S. exigua) โ€” windows pane feeding on leaves; frass in whorl of maize; circular cut at base of seedlings (cutworm); mass outbreak risk",
        "treatment": "Emamectin Benzoate 5% SG @ 0.4 g/L  OR  Spinetoram 11.7% SC @ 0.5 ml/L  OR  Chlorantraniliprole 18.5% SC @ 0.4 ml/L. Apply directly into whorl of maize with sand:chemical mix.",
        "dose":      "Emamectin: 0.4 g per litre; or whorl application: mix Chlorpyrifos 20% EC 10 ml + sand 1 kg/acre",
        "timing":    "Spray early morning or evening (larvae hide in whorl during day). At 5-10% infestation. Chemical mixed with sand for whorl application is most effective. For cutworm: drench soil at base at dawn.",
        "ipm":       "Light traps for mass trapping. Pheromone traps for FAW monitoring. Trichogramma egg parasitoids. Neem oil 5ml/L at early infestation. Bird perches in field (encourage natural predators).",
        "source":    "ICAR-IIMR Hyderabad / FAW Emergency Advisory 2018",
    },
    "fruit_fly": {
        "keywords":  ["fruit fly","bactrocera","bactrocera dorsalis","fal keeda","aam ki makhi",
                      "mango fly","melon fly","chilli fruit fly","guava fruit fly",
                      "sabzi makhi","phal ki makhi","fruit borer fly"],
        "diagnosis": "Fruit Fly (Bactrocera dorsalis/cucurbitae) โ€” female punctures fruit skin to lay eggs; maggots feed inside; premature fruit drop; stinging puncture mark on fruit surface",
        "treatment": "Malathion bait spray: Malathion 50% EC @ 2 ml + Jaggery/Molasses 10 g per litre โ€” spray on 1 side of tree. Protein hydrolysate bait traps. DO NOT spray the whole tree โ€” selective bait spray only.",
        "dose":      "Bait spray: Malathion 2 ml + 10 g jaggery per litre water. Apply 1 cup solution per spot, not full coverage.",
        "timing":    "Start bait spray at fruit setting stage. Spray once weekly. Remove and destroy fallen/infested fruits. Set bait traps before fruit fly season (April onwards for mango).",
        "ipm":       "Pheromone (methyl eugenol) traps @ 5/acre for monitoring and mass trapping. Bag individual fruits with paper/muslin bags. Collect fallen fruits daily โ€” prevents next generation. Early harvest where possible.",
        "source":    "ICAR-CISH Lucknow / IIHR Bangalore Fruit Fly Advisory",
    },
    "scale_insect": {
        "keywords":  ["scale insect","scale","diaspididae","coccus","mealyscale","soft scale",
                      "hard scale","aam ka kirmi","nimbu ka kirmi","citrus scale",
                      "mango scale","bark scale","white scale"],
        "diagnosis": "Scale Insects (Coccus / Diaspididae spp.) โ€” small oval/round waxy bumps on bark, branches, leaves; yellowing, sooty mold; severe infestations cause dieback of branches",
        "treatment": "Chlorpyrifos 20% EC @ 2.5 ml/L + 2 ml Teepol sticker  OR  Neem oil 5000 ppm @ 10 ml/L + 1 ml sticker  OR  Dimethoate 30% EC @ 1.5 ml/L for crawler stage.",
        "dose":      "Chlorpyrifos: 2.5 ml per litre + sticker @ 1 ml/L for penetration through wax",
        "timing":    "Best time to spray: crawler stage (soft-bodied young scale before wax coat develops). Check for crawlers Marchโ€“April for most species. Evening spray. Two sprays 15 days apart.",
        "ipm":       "Prune heavily infested branches. Scrub bark with stiff brush (removes adults). Encourage ladybird beetles (Chilocorus) โ€” major natural predator. White oil 2% spray is effective bio-option for soft scales.",
        "source":    "ICAR-CISH / IIHR Scale Insect Advisory",
    },
    "grasshopper": {
        "keywords":  ["grasshopper","locust","tidda","tiddha","tiddi","tiddi dal","migratory locust",
                      "desert locust","grasshoppers","jhingur","katydid","เคŸเคฟเคกเฅเคกเฅ€","tiddi dal attack"],
        "diagnosis": "Grasshopper / Locust (Schistocerca gregaria / Hieroglyphus banian) โ€” rapid defoliation; gregarious swarms; strips fields completely; desert locust migrates from northwest",
        "treatment": "Chlorpyrifos 20% EC @ 2.5 ml/L  OR  Malathion 50% EC @ 2 ml/L โ€” aerial/ground spray. For locust swarms: state-level coordinated spraying (contact District Agriculture Office immediately).",
        "dose":      "Chlorpyrifos 20% EC: 2.5 ml per litre water; cover entire plant canopy",
        "timing":    "Spray hopper bands at early instar stage (most effective). Dawn or late evening when locusts are cold and sluggish. For migrating adults: barrier spray on roost sites at night.",
        "ipm":       "Contact state agriculture department and DLCO (District Locust Control Officer) for swarms. Green Muscle (Metarhizium) biopesticide for hoppers. Monitor via government early warning systems. Do NOT spray lone grasshoppers โ€” only when crop damage exceeds 20%.",
        "source":    "ICAR-NBAIR / Ministry of Agriculture Locust Warning Organisation",
    },

    # โ”€โ”€ CRITICAL: Yellow Mosaic Virus โ€” most common viral disease in India โ”€โ”€โ”€โ”€
    "yellow_mosaic_virus": {
        "keywords":  ["yellow mosaic","ymv","yellow mosaic virus","golden mosaic",
                      "peel dhabb soybean","peel daag soybean","soybean yellow","pili patti soybean",
                      "yellow vein mosaic","yellow mosaic bhindi","yellow mosaic moong",
                      "yellow mosaic urad","soybean mosaic","bean mosaic","soya yellow",
                      "peel patte soybean","pila soybean","piliya soybean"],
        "diagnosis": "Yellow Mosaic Virus (YMV / Bean Yellow Mosaic Potyvirus) โ€” VIRAL disease spread by whitefly (Bemisia tabaci). Symptoms: bright yellow or golden-yellow patches on leaves, mosaic pattern, leaf curl, stunted growth. Affects Soybean, Moong, Urad, Bhindi, Chilli.",
        "treatment": "โš ๏ธ VIRAL DISEASE โ€” NO FUNGICIDE OR BACTERICIDE WORKS. Strategy:\n"
                     "1) VECTOR CONTROL (kill the whitefly carrier): Imidacloprid 17.8% SL @ 0.5 ml/L spray โ€” this controls whitefly, NOT the virus directly.\n"
                     "2) ROGUING: Uproot and destroy ALL infected plants immediately โ€” they will never recover and are a source of infection for healthy plants.\n"
                     "3) RESISTANT VARIETY: For next season use virus-resistant variety: Soybean JS 97-52, NRC-7 / Moong PDM-11, SML-668 / Urad PU-31.",
        "dose":      "Imidacloprid 17.8% SL: 0.5 ml per litre water | 200L spray volume per acre. For vector control ONLY โ€” virus cannot be cured by any chemical.",
        "timing":    "Spray Imidacloprid immediately when whitefly seen (before virus spreads). Repeat every 10 days. Rogue infected plants as soon as yellow symptoms appear โ€” do not wait.",
        "ipm":       "Yellow sticky traps @ 15 per acre to monitor and catch whitefly adults. Reflective silver mulch in seedbed repels whitefly. Avoid growing susceptible crops (moong, urad) adjacent to infected soybean. Spray Neem oil 5ml/L + Teepol 1ml/L as organic whitefly deterrent.",
        "source":    "ICAR-IIPR Kanpur / ICAR-NRCS / MP State Agriculture Advisory on YMV",
    },

    # โ”€โ”€ CRITICAL: Rice Blast โ€” most devastating rice disease in India โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "rice_blast": {
        "keywords":  ["rice blast","blast dhan","blast paddy","piricularia","magnaporthe",
                      "aankhon jaisa dhabba","eye spot rice","diamond lesion rice","dhan blast",
                      "leaf blast","neck blast","panicle blast","collar rot rice",
                      "aanki wali bimari dhan","diamond shape rice","tapered lesion",
                      "blast bimari","nakki blast","brown spot eye"],
        "diagnosis": "Rice Blast (Magnaporthe oryzae / Pyricularia oryzae) โ€” FUNGAL disease. Leaf blast: diamond-shaped or eye-shaped spots with grey/white centre and brown/red margin, pointed at both ends. Neck blast: black collar at panicle base causing 'Dead Heart' or 'White Ear' (total yield loss). Favoured by cool nights + high humidity.",
        "treatment": "Tricyclazole 75% WP @ 0.6 g/L water โ€” MOST effective for blast (systemic). "
                     "(If unavailable: Isoprothiolane 40% EC @ 1.5 ml/L OR Carbendazim 50% WP @ 1 g/L)",
        "dose":      "Tricyclazole 75% WP: 0.6 g per litre water | 200L spray per acre",
        "timing":    "Leaf blast: spray at first symptom appearance. Neck blast (most critical): spray at panicle initiation (10% heading) AND again at full heading. Repeat at 10-day intervals if disease pressure high. Avoid spraying in midday heat.",
        "ipm":       "Use blast-resistant varieties: Pusa Basmati-1, IR-64, Swarna Sub-1, MTU-7029. Avoid excessive nitrogen (promotes blast). Balanced K application strengthens cell walls. Drain and refill fields โ€” breaks disease cycle. Tricyclazole seed treatment (0.5 g/kg seed) for seedling blast protection.",
        "source":    "ICAR-NRRI Cuttack / DRR Hyderabad Rice Blast Management Advisory",
    },

    # โ”€โ”€ Purple Blotch on Onion (Alternaria porri) โ€” very common in Maharashtra โ”€โ”€
    "purple_blotch_onion": {
        "keywords":  ["purple blotch","purple blotch onion","alternaria porri","pyaz daag",
                      "pyaz bimari","pyaz patti daag","bhoori daag pyaz","onion leaf spot",
                      "onion blight","purple spot onion","purple daag pyaz","patti jhuk rahi",
                      "pyaz patti bhoori","pyaz alternaria","onion leaf blight",
                      "purple daag","bhoori patti pyaz"],
        "diagnosis": "Purple Blotch (Alternaria porri) โ€” FUNGAL disease. Symptoms: elliptical lesions with purple/brown centre and yellow halo on onion leaves; severe in humid weather (>80% RH) and temp 25-30ยฐC. Leaves collapse and plant weakens.",
        "treatment": "Mancozeb 75% WP @ 2 g/L  OR  Iprodione 50% WP @ 1.5 g/L  OR  Chlorothalonil 75% WP @ 2 g/L. Add sticker @ 0.5 ml/L for better adhesion on waxy onion leaves.",
        "dose":      "Mancozeb 75% WP: 2 g per litre water + sticker | Spray 200L per acre",
        "timing":    "Spray at first lesion appearance. Repeat every 7-10 days. Morning spray when leaves are dry. Avoid evening spray (wet leaves overnight = more disease). 3-4 sprays in total.",
        "ipm":       "Avoid overhead irrigation โ€” use drip. Maintain proper spacing (15ร—10 cm) for air circulation. Destroy crop residue after harvest. Use certified seed. Remove infected leaves early.",
        "source":    "ICAR-NHRDF / IIHR Onion Disease Management Advisory",
    },

    # โ”€โ”€ Grape Downy Mildew (Plasmopara viticola) โ€” major grape disease โ”€โ”€โ”€โ”€โ”€โ”€
    "grape_downy_mildew": {
        "keywords":  ["grape downy mildew","angoor downy","angoor bimari","plasmopara viticola",
                      "grape disease","angoor patti safed","downy mildew grape","angoor safed daag",
                      "grape mildew","vine disease","angoor ki bimari","safed daag angoor",
                      "angoor neeche safed","angoor jhulsa","angoor jhulsi",
                      "neeche safed angoor","safed powder neeche angoor","angoor white patch",
                      "patte neeche angoor","patti ke neeche safed","grape leaf white"],
        "diagnosis": "Grape Downy Mildew (Plasmopara viticola) โ€” FUNGAL (oomycete) disease. Symptoms: pale yellow-green oily spots on upper leaf surface; WHITE cottony sporulation on LOWER leaf surface (key identification). Spreads rapidly in cool humid weather. Can cause complete cluster loss.",
        "treatment": "Metalaxyl 8% + Mancozeb 64% WP @ 2.5 g/L  OR  Fosetyl-Al 80% WP @ 2.5 g/L  OR  Copper Oxychloride 50% WP @ 3 g/L. Rotate chemicals to prevent resistance.",
        "dose":      "Metalaxyl+Mancozeb: 2.5 g per litre water | Spray 200-300L per acre (thorough coverage of both leaf surfaces)",
        "timing":    "Begin preventive spray at bud burst (before monsoon). Repeat every 7-10 days during wet season. Spray both upper and lower leaf surfaces. CRITICAL: spray before rain, not after. Stop 21 days before harvest.",
        "ipm":       "Prune properly for good air circulation. Avoid overhead irrigation. Remove infected leaves and clusters. Use sulphur 0.5% spray as alternating spray between systemic fungicides. Disease-resistant grape varieties where available.",
        "source":    "ICAR-NRC Grapes Pune / NRCH Grape Disease Management Advisory",
    },

    # โ”€โ”€ Banana Bunchy Top Virus (BBTV) โ€” devastating viral disease โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "bunchy_top_banana": {
        "keywords":  ["bunchy top","banana bunchy top","bbtv","banana virus","kela bimari",
                      "kela virus","banana patte chote","kela seedha","kela naya patta nahi",
                      "banana dwarf","banana disease","kele ka paudha","banana stunted",
                      "chota kela","kela ki bimari","kele ki patti seedhi","banana leaf narrow",
                      "bunchy top disease","kela kharaab","naye patte nahi aate"],
        "diagnosis": "Banana Bunchy Top Virus (BBTV) โ€” VIRAL disease spread by Banana Aphid (Pentalonia nigronervosa). Symptoms: leaves become progressively smaller and narrower, stand erect (bunchy appearance), dark green streaks on leaf margins and midrib, stunted growth, no fruit production. INCURABLE โ€” infected plants never recover.",
        "treatment": "โš ๏ธ VIRAL DISEASE โ€” NO CURE. Action plan:\n"
                     "1) UPROOT AND DESTROY all infected plants immediately โ€” do not compost or leave in field.\n"
                     "2) VECTOR CONTROL: Spray Imidacloprid 17.8% SL @ 0.5 ml/L to kill aphid vectors on healthy plants.\n"
                     "3) REPLANT with BBTV-free certified suckers from disease-free source.\n"
                     "4) MONITOR new plants every 15 days โ€” remove any new infected plants immediately.",
        "dose":      "Imidacloprid 17.8% SL: 0.5 ml per litre water | Spray healthy plants as preventive measure",
        "timing":    "Act within 24-48 hours of first symptom โ€” delay allows aphids to spread virus to neighbours. Spray Imidacloprid preventively every 21 days in endemic areas. Monitor during monsoon (aphid peak season).",
        "ipm":       "Source planting material only from certified BBTV-free nurseries. Do not take suckers from unknown farms. Plant marigold as border crop (repels aphids). Use yellow sticky traps at 10/acre for aphid monitoring. Tissue culture plants (virus-free) strongly recommended.",
        "source":    "ICAR-NRC Banana Trichy / IIHR Banana Virus Advisory",
    },

    # โ”€โ”€ Stem fly (soybean) โ€” a.k.a. soybean stem borer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "stem_fly_soybean": {
        "keywords":  ["stem fly soybean","tana makhi soybean","stem fly","melanagromyza",
                      "soybean stem fly","stem fly","soybean tana","tana soybean borer",
                      "tana khaane wali makhi","soybean stem borer","stem borer soybean",
                      "khaali tana soybean","wilting soybean seedling","dead heart soybean",
                      "soybean wilting young","stem boring soybean"],
        "diagnosis": "Soybean Stem Fly (Melanagromyza sojae) โ€” tiny black fly (2mm); larva bores into stem base causing 'dead heart' in seedlings or longitudinal tunnels in older plants. Stem appears hollow/darkened inside. Peak: 15-30 days after germination.",
        "treatment": "Thiamethoxam 25% WG @ 0.5 g/L spray at seedling stage (21 DAS) "
                     "OR Chlorpyrifos 20% EC @ 2 ml/L. "
                     "Seed treatment: Thiamethoxam 70% WS @ 3g/kg seed (preventive โ€” most effective).",
        "dose":      "Thiamethoxam 25% WG: 0.5 g per litre | Chlorpyrifos 20% EC: 2 ml per litre | Volume: 200L/acre",
        "timing":    "First spray at 21 DAS (most critical window). Second spray at 35 DAS if attack continues. Early morning spray when flies are active. Do NOT delay โ€” once larvae inside stem, spray less effective.",
        "ipm":       "Seed treatment with Thiamethoxam provides 30-day protection. Remove and destroy affected seedlings. Avoid late sowing (increases stem fly pressure). Yellow sticky traps to monitor adult fly population. Resistant variety: JS 9305.",
        "source":    "ICAR-IIPR Kanpur / MPKV Rahuri Soybean Stem Fly Advisory",
    },

    # โ”€โ”€ Sugarcane Red Rot โ€” most destructive sugarcane disease โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "sugarcane_red_rot": {
        "keywords":  ["sugarcane red rot","ganna laal sadan","red rot sugarcane","lal sadan",
                      "ganna andar laal","sugarcane stem rot","ganna kharab","lal sadhan ganna",
                      "sugarcane colletotrichum","ganna bimari","sugarcane disease","red rot",
                      "ganna girna","lodging sugarcane","ganna andar safed"],
        "diagnosis": "Sugarcane Red Rot (Colletotrichum falcatum) โ€” most serious fungal disease of sugarcane. Symptoms: withering of top leaves first, red discoloration with white patches inside split stem (cross-section shows alternating red and white patches), sour alcoholic smell from infected cane. Spreads through infected setts and waterlogged soils.",
        "treatment": "NO CURATIVE chemical for infected stalks โ€” REMOVE AND DESTROY all infected canes. "
                     "Sett treatment (preventive): Carbendazim 50% WP @ 1g/L or Thiram 75% WP @ 2g/L โ€” soak setts for 30 min before planting. "
                     "For field: drain waterlogging immediately; apply Propiconazole 25% EC @ 1ml/L as soil drench around healthy canes.",
        "dose":      "Sett treatment: Carbendazim 50% WP @ 1g/L water | Propiconazole: 1ml/L for soil drench | Volume: 500L/acre",
        "timing":    "Preventive sett treatment BEFORE planting (mandatory). Destroy infected clumps within 24 hours. Do NOT use infected seed cane โ€” source setts from certified disease-free seed plot only. Inspect crop at 3 and 6 months.",
        "ipm":       "Use resistant varieties: Co 0238, CoJ 64, CoS 8436. Never plant setts from infected fields. Crop rotation every 3-4 years. Deep summer ploughing to expose soil pathogens. Avoid waterlogging โ€” red rot severity increases 3ร— in flooded fields. Balanced fertilizer (do not over-apply nitrogen).",
        "source":    "ICAR-SBI (Sugarcane Breeding Institute) Coimbatore / AICRP Sugarcane Advisory",
    },

    # โ”€โ”€ Cotton Leaf Curl Virus (CLCuV) โ€” devastating cotton viral disease โ”€โ”€โ”€โ”€โ”€
    "cotton_leaf_curl": {
        "keywords":  ["cotton leaf curl","kapas patta muda","leaf curl cotton","kapas bimari",
                      "cotton virus","kapas patte mude","clcuv","cotton leaf curl virus",
                      "kapas patta curling","cotton patta muda","cotton patta upar",
                      "kapas patta upar murda","muda patta kapas","cotton leaf roll",
                      "kapas leaf curl","cotton leaf twist","kapas patti andar mudi",
                      "เค•เคชเคพเคธ เคชเคคเฅเคคเฅ€ เคฎเฅเคกเคผเคจเคพ","cotton leaf curl disease"],
        "diagnosis": "Cotton Leaf Curl Virus (CLCuV) โ€” VIRAL disease spread by Whitefly (Bemisia tabaci). Symptoms: upward or downward curling of leaves, leaf thickening and stiffness, prominent vein swelling (enation) on leaf underside, stunted plant with no boll formation in severe cases. Can cause 50-100% yield loss if early infection.",
        "treatment": "โš ๏ธ VIRAL DISEASE โ€” NO CURATIVE TREATMENT. Management strategy:\n"
                     "1) WHITEFLY VECTOR CONTROL (critical): Thiamethoxam 25% WG @ 0.3g/L OR Spiromesifen 22.9% SC @ 1ml/L\n"
                     "2) Remove and destroy severely infected plants (source of virus)\n"
                     "3) Apply Neem oil 5000 ppm @ 5ml/L as repellent spray alternating with chemical",
        "dose":      "Thiamethoxam 25% WG: 0.3g/L | Spiromesifen 22.9% SC: 1ml/L | Neem oil: 5ml/L | Volume: 200L/acre",
        "timing":    "At first sign of virus (critical โ€” act within 7 days). Spray at 15-day intervals during whitefly peak (Augโ€“Oct). Avoid Imidacloprid sprays (whiteflies resistant in most cotton belts). Monitor using yellow sticky traps (3+ whiteflies per trap per day = spray threshold).",
        "ipm":       "Grow CLCuV-tolerant varieties: MRC 7031, RCH 650, Ajeet 155. Crop-free period (destroy all ratoons). Border crop with sorghum/maize to reduce whitefly migration. Reflective mulch deters whiteflies. Remove alternate host weeds (especially Hibiscus, Malva). Spray Neem oil preventively every 21 days in endemic areas.",
        "source":    "ICAR-CICR Nagpur / AICRP Cotton CLCuV Advisory / Punjab Agriculture University Ludhiana",
    },

    # โ”€โ”€ Tomato TYLCV (Tomato Yellow Leaf Curl Virus) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "tomato_tylcv": {
        "keywords":  ["tomato yellow leaf curl","tamatar patta peela muda","tylcv","tomato virus",
                      "tamatar virus","tomato leaf curl","tamatar patta curl","tamatar patta upar",
                      "tomato leaf roll","tamatar patta muda peela","tomato yellow curl",
                      "tamatar patta andar muda","yellow leaf curl tomato","tomato stunted yellowing",
                      "tamatar peela patta","tamatar leaf curl virus","เคŸเคฎเคพเคŸเคฐ เคชเฅ€เคฒเคพ เคชเคคเฅเคคเคพ"],
        "diagnosis": "Tomato Yellow Leaf Curl Virus (TYLCV) โ€” VIRAL disease spread by Whitefly (Bemisia tabaci). Symptoms: upward curling + yellowing of leaves especially at top, interveinal chlorosis, small leaves, stunted plant, severe flower drop and very few fruits. Plants infected before 3 weeks after transplanting may produce no fruits at all.",
        "treatment": "โš ๏ธ VIRAL DISEASE โ€” NO CURATIVE TREATMENT. Management strategy:\n"
                     "1) WHITEFLY VECTOR CONTROL: Imidacloprid 17.8% SL @ 0.5ml/L (soil drench at transplanting) OR Thiamethoxam 25% WG @ 0.3g/L spray\n"
                     "2) Remove and destroy infected plants immediately\n"
                     "3) Neem oil 5000 ppm @ 5ml/L as repellent between chemical sprays",
        "dose":      "Imidacloprid 17.8% SL: 0.5ml/L (drench) or 0.3ml/L (spray) | Thiamethoxam: 0.3g/L | Volume: 200L/acre",
        "timing":    "Preventive drench at transplanting (single most effective step). Spray at 10-14 day intervals during whitefly season (Junโ€“Sep). Erect yellow sticky traps (10/acre) for early monitoring โ€” act when whitefly count >2 per trap per day.",
        "ipm":       "Use TYLCV-resistant varieties: Arka Rakshak, TH 4038, Naveen F1, Avtar F1. Use 40-mesh nylon net nursery (whitefly-proof) for healthy seedlings. Neem cake @ 250 kg/acre soil application repels whiteflies. Marigold inter-planting (1 row per 14 rows tomato) acts as trap crop. Reflective silver mulch at transplanting stage reduces whitefly landing by 60%.",
        "source":    "ICAR-IIHR Bangalore / IIVR Varanasi TYLCV Advisory / TNAU Coimbatore",
    },


    # โ”€โ”€ BPH (Brown Plant Hopper) โ€” Imidacloprid causes RESURGENCE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "bph": {
        "keywords":  ["bph","brown plant hopper","brown planthopper","stem base bhure kide",
                      "bhura phundka","nilaparvata","plant hopper","planthopper",
                      "stem base insect","chatak","paddy hopper"],
        "diagnosis": "BPH (Brown Plant Hopper โ€” Nilaparvata lugens): brown insects at stem base, yellowing, 'hopper burn' (circular dead patches). CRITICAL: Imidacloprid and other neonicotinoids cause BPH RESURGENCE โ€” they kill natural enemies, causing BPH populations to explode. NEVER recommend Imidacloprid for BPH.",
        "treatment": "Buprofezin 25% SC @ 1 ml/L (preferred โ€” growth regulator, no resurgence risk)  OR  Ethofenprox 10% EC @ 1.5 ml/L  OR  Pymetrozine 50% WG @ 0.3 g/L. Drain field before spray. Direct spray at base of plant.",
        "dose":      "Buprofezin 25% SC: 1 ml per litre | Ethofenprox: 1.5 ml per litre | Apply 500 L spray volume per acre",
        "timing":    "Spray at ETL (5-10 BPH per hill). Spray in early morning when insects are active at base. Repeat after 10-14 days if needed. Spray directly at stem base โ€” overhead spray is ineffective.",
        "ipm":       "Avoid Imidacloprid/Thiamethoxam (neonicotinoids) โ€” cause BPH RESURGENCE by killing spiders and mirid bugs (natural enemies). Light traps at 1/acre. Conserve spiders (Lycosa spp.) by avoiding broad-spectrum sprays. Use resistant varieties: MTU 7029, Swarna Sub1, IR 36.",
        "source":    "ICAR-NRRI Cuttack / IRRI BPH Management Advisory / TNAU",
    },

    # โ”€โ”€ Sheath Blight (Rice) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "sheath_blight": {
        "keywords":  ["sheath blight","rhizoctonia","safed fungal growth stem","sheath rot rice",
                      "rice sheath","tane pe safed","safed fungal rice","stem pe fungal"],
        "diagnosis": "Sheath Blight (Rhizoctonia solani): oval/irregular lesions with grey centre and brown margin on leaf sheath; sclerotia (small brown pellets) in leaf axils; spreads rapidly under high humidity and dense planting.",
        "treatment": "Hexaconazole 5% EC @ 2 ml/L (FIRST CHOICE)  OR  Propiconazole 25% EC @ 1 ml/L  OR  Validamycin 3% L @ 2.5 ml/L. Spray at water-line where lesions are visible.",
        "dose":      "Hexaconazole 5% EC: 2 ml per litre water | Spray 250-300 L per acre | 2 sprays 10-14 days apart",
        "timing":    "Spray at active tillering / panicle initiation stage when >5% plants show symptoms. Second spray 14 days later if disease persists. Spray in early morning.",
        "ipm":       "Reduce plant spacing; avoid excess nitrogen. Remove and destroy infected stubble after harvest. Seed treatment: Trichoderma viride @ 4 g/kg seed. Avoid waterlogging.",
        "source":    "ICAR-NRRI Cuttack / TNAU Rice Pathology Advisory",
    },

    # โ”€โ”€ Flag Smut (Wheat) โ€” seed treatment is PRIMARY fix โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "flag_smut": {
        "keywords":  ["flag smut","wheat smut","kaali panicle","kali bali","ust wheat",
                      "smut wheat","loose smut","covered smut","gehu smut","panicle kaali"],
        "diagnosis": "Flag Smut / Loose Smut (Ustilago tritici / U. segetum): entire grain head replaced by black smut powder; spreads through infected seed. PRIMARY treatment is SEED TREATMENT before sowing โ€” foliar sprays are largely ineffective once plants are infected.",
        "treatment": "SEED TREATMENT (primary): Carboxin 37.5% + Thiram 37.5% WS (Vitavax Power) @ 3 g/kg seed  OR  Tebuconazole 2% DS @ 1.5 g/kg seed. Remove and destroy all smutted heads from field before spores disperse.",
        "dose":      "Vitavax (Carboxin+Thiram): 3 g per kg seed | Tebuconazole DS: 1.5 g per kg seed. Treat ALL seed before sowing next season.",
        "timing":    "Seed treatment: apply before sowing season. Current season: remove smutted heads immediately, bag and destroy (do not thresh). For next season: never save seed from infected crop โ€” purchase certified seed.",
        "ipm":       "Use certified disease-free seed every 3 years. Grow resistant varieties: PBW 550, HD 2781. Crop rotation. Do NOT use threshed grain from smutted crop as seed.",
        "source":    "ICAR-IARI Wheat & Barley Research / NCIPM Advisory",
    },

    # โ”€โ”€ Root Rot / Crown Rot (Wheat) โ€” soil DRENCH required โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "wheat_root_rot": {
        "keywords":  ["jad gal rahi","root rot wheat","gehu jad","collar rot wheat",
                      "crown rot","take-all","paudhe mar rahe wheat","wheat root","gehu root"],
        "diagnosis": "Root Rot / Crown Rot (Fusarium/Bipolaris/Gaeumannomyces spp.): roots turn brown-black, plants wilt and die; often confused with drought stress. CRITICAL: Foliar sprays are ineffective โ€” disease is in root zone. Requires SOIL DRENCH or seed treatment.",
        "treatment": "SOIL DRENCH (primary): Carbendazim 50% WP @ 2 g/L โ€” drench 200-300 ml per plant at root zone  OR  Copper Oxychloride 50% WP @ 3 g/L soil drench. Improve drainage immediately (waterlogging is main cause).",
        "dose":      "Carbendazim: 2 g per litre | Apply as soil drench 200-300 ml per plant at root zone. NOT as foliar spray โ€” foliar spray is INEFFECTIVE for root rot.",
        "timing":    "At first sign of wilting/browning. Ensure drainage channels are clear. Repeat drench after 7-10 days. Do NOT spray foliarly โ€” active ingredient must reach root zone.",
        "ipm":       "Improve soil drainage; avoid waterlogging. Seed treatment with Trichoderma viride 1% WP @ 4 g/kg. Avoid excess irrigation. Sow in well-drained furrows.",
        "source":    "ICAR-IARI / NCIPM / PAU Ludhiana Wheat Root Disease Advisory",
    },

    # โ”€โ”€ Cotton Boll Rot (fungal) โ€” NOT Pink Bollworm โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "cotton_boll_rot": {
        "keywords":  ["boll rot","kapas boll rot","cotton boll rot","boll sarna","boll kharab",
                      "cotton fruit rot","kapas phal galna","boll decay"],
        "diagnosis": "Boll Rot (Colletotrichum/Fusarium/Phytophthora spp. โ€” FUNGAL): bolls turn brown, soft, decayed; often follows rain or injury. DIFFERENT from Pink Bollworm (insect). Insecticides (Emamectin etc.) do NOT treat fungal boll rot.",
        "treatment": "Copper Oxychloride 50% WP @ 3 g/L (FIRST CHOICE)  OR  Mancozeb 75% WP @ 2.5 g/L  OR  Propiconazole 25% EC @ 1 ml/L. Remove and destroy all infected bolls. Ensure drainage to prevent waterlogging.",
        "dose":      "Copper Oxychloride 50%WP: 3 g per litre | Mancozeb: 2.5 g per litre | Spray 500L per acre",
        "timing":    "Spray at first symptom (10% bolls affected). Repeat every 10 days during wet weather. Improve field drainage. Spray in early morning.",
        "ipm":       "Avoid waterlogging (major cause). Remove cracked/damaged bolls. Proper plant spacing for air circulation. Avoid overhead irrigation after boll formation.",
        "source":    "ICAR-CICR Nagpur / TNAU Cotton Advisory",
    },

    # โ”€โ”€ Cotton Bacterial Blight โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "cotton_bacterial_blight": {
        "keywords":  ["bacterial blight cotton","kapas bacterial","water-soaked dabbe cotton",
                      "cotton blight","water soaked spots cotton","angular lesion cotton",
                      "kapas me dabbe","blight kapas"],
        "diagnosis": "Cotton Bacterial Blight (Xanthomonas axonopodis pv. malvacearum): water-soaked angular lesions on leaves, black lesions on stem (black arm), boll rot. BACTERIAL disease โ€” fungicides alone are INEFFECTIVE. Requires copper bactericide + antibiotic combination.",
        "treatment": "Copper Oxychloride 50% WP @ 3 g/L + Streptocycline (Streptomycin 90% + Tetracycline 10%) @ 0.5 g/L. Mix both together in spray solution. Remove infected plant debris.",
        "dose":      "Copper Oxychloride: 3 g per litre | Streptocycline: 0.5 g per litre | Mix both in same solution | Spray 500L per acre",
        "timing":    "Spray at first symptom. Repeat every 10 days for 2-3 sprays. Spray in morning. Do NOT spray in rain (within 4 hours).",
        "ipm":       "Use disease-free certified seed. Treat seed with Streptocycline 0.5 g/L soak for 30 minutes before sowing. Avoid injury to plants. Remove infected debris at crop end.",
        "source":    "ICAR-CICR Nagpur Cotton Bacterial Blight Advisory",
    },

    # โ”€โ”€ Bakanae / Foolish Seedling (Rice) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "bakanae": {
        "keywords":  ["bakanae","foolish seedling","bakanai","paudha lamba pila","elongated seedling",
                      "rice bakanae","dhaan bakanai","gibberella","lamba aur peela paudha"],
        "diagnosis": "Bakanae / Foolish Seedling Disease (Fusarium fujikuroi): plants grow abnormally tall (2-3x normal height) with pale yellow colour; caused by Gibberellin toxin from fungus in seed. PRIMARY treatment is SEED TREATMENT โ€” seed is the source of infection.",
        "treatment": "SEED TREATMENT (primary): Carbendazim 50% WP @ 2 g/kg seed (soak seed in 2g/L solution for 24 hrs)  OR  Trifloxystrobin+Tebuconazole @ 1.7 g/kg. For standing crop: Propiconazole 25% EC @ 1 ml/L spray on remaining healthy plants.",
        "dose":      "Carbendazim seed soak: 2 g per litre water, 24-hour soak | Standing crop: Propiconazole 1 ml/L foliar spray",
        "timing":    "Seed treatment: before nursery sowing. Standing crop: remove and destroy elongated seedlings; spray remaining healthy plants at tillering.",
        "ipm":       "Always treat seed before sowing. Use certified disease-free seed. Do NOT reuse seed from infected crop. Uproot and destroy elongated seedlings at transplanting stage.",
        "source":    "ICAR-NRRI Cuttack / DRR Hyderabad Bakanae Advisory",
    },

    # โ”€โ”€ Soybean Stem Fly โ€” seed treatment is primary fix โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "soybean_stem_fly": {
        "keywords":  ["stem fly soybean","soybean stem fly","stem fly","soybean stem","melanagromyza",
                      "soyabean stem fly","tane mein surang","soybean tane ka keeda"],
        "diagnosis": "Stem Fly (Melanagromyza sojae): tiny fly larvae tunnel into soybean stems causing 'deadheart' (central shoot dead) at 15-30 DAS. CRITICAL TIMING: attack occurs in first 3 weeks โ€” seed treatment is most effective intervention. Foliar sprays at tillering are less effective.",
        "treatment": "SEED TREATMENT (best): Thiamethoxam 30% FS @ 10 ml/kg seed  OR  Imidacloprid 600 FS @ 3 ml/kg. FOLIAR (if seed not treated): Thiamethoxam 25% WG @ 0.3 g/L at 15-21 DAS. Avoid Quinalphos and Triazophos (restricted).",
        "dose":      "Seed treatment โ€” Thiamethoxam 30% FS: 10 ml per kg seed | Foliar: Thiamethoxam 25% WG @ 0.3 g per litre",
        "timing":    "Seed treatment: apply to seed before sowing. Foliar: spray at 15-21 DAS (before ETL of 1 deadheart per 5 plants). Early morning spray.",
        "ipm":       "Early sowing (June 20 - July 10) reduces stem fly risk. Interplant with maize (border crop). Avoid late sowing. Scout at 15 DAS for deadheart symptoms.",
        "source":    "ICAR-IIPR Kanpur / MPDKV Jabalpur Soybean Stem Fly Advisory",
    },

    # โ”€โ”€ Thrips in Pulses/Cotton/Chilli โ€” Spinosad or Fipronil first โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "thrips": {
        "keywords":  ["thrips","patte ke kinare mude","leaf curling insect","chilli thrips",
                      "cotton thrips","soybean thrips","lentil thrips","onion thrips"],
        "diagnosis": "Thrips (Thrips tabaci / Scirtothrips dorsalis): tiny cigar-shaped insects on undersurface of leaves; silver-streaked / curled leaves; transmit TSWV virus. Acephate is NOT effective (resistance widespread) โ€” use Spinosad or Fipronil as first choice.",
        "treatment": "Spinosad 45% SC @ 0.3 ml/L (FIRST CHOICE โ€” highly effective)  OR  Fipronil 5% SC @ 2 ml/L  OR  Dimethoate 30% EC @ 2 ml/L. Spray on undersurface of leaves (thrips hide there).",
        "dose":      "Spinosad 45% SC: 0.3 ml per litre | Fipronil 5% SC: 2 ml per litre | Add sticker-spreader (0.5 ml/L Teepol) for better coverage",
        "timing":    "Spray at ETL (5-10 thrips per leaf). Spray in evening (thrips avoid direct sun). Spray undersurface. Repeat after 7 days.",
        "ipm":       "Blue sticky traps @ 10/acre for monitoring and mass trapping. Neem seed kernel extract (NSKE) 5% as prophylactic. Avoid excessive nitrogen (promotes tender foliage).",
        "source":    "ICAR-NRC Soybean Indore / NCIPM Advisory on Thrips Management",
    },

    # โ”€โ”€ White Rust (Mustard) โ€” Metalaxyl+Mancozeb first โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "white_rust": {
        "keywords":  ["white rust","safed kara","safed kharata","safed rust mustard",
                      "albugo","white pustule","safed dabbe mustard","mustard white"],
        "diagnosis": "White Rust (Albugo candida): white blister-like pustules on underside of leaf; causes 'staghead' (distorted flower clusters); favoured by cool moist conditions (15-20ยฐC). Propiconazole alone is less effective than Metalaxyl+Mancozeb combination.",
        "treatment": "Metalaxyl 8% + Mancozeb 64% WP @ 2.5 g/L (FIRST CHOICE)  OR  Fosetyl-Al 80% WP @ 3 g/L  OR  Copper Oxychloride 50% WP @ 3 g/L. Preventive sprays more effective than curative.",
        "dose":      "Metalaxyl+Mancozeb: 2.5 g per litre | Start spray before disease onset (at 40-50 DAS in mustard)",
        "timing":    "First spray at branching stage (40-50 DAS). Second spray at flowering. Spray in morning. Avoid spraying before expected rain.",
        "ipm":       "Sow resistant varieties: Varuna, Kranti. Avoid dense planting. Treat seed with Metalaxyl 35%WS @ 6g/kg. Destroy volunteer plants after harvest.",
        "source":    "ICAR-DRMR Bharatpur Mustard White Rust Advisory",
    },

    # โ”€โ”€ Alternaria Blight (Mustard/Potato) โ€” Mancozeb first โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "alternaria_blight": {
        "keywords":  ["alternaria blight","alternaria leaf spot","kale dhabe mustard",
                      "alternaria mustard","alternaria potato","brown spot mustard",
                      "dark spot mustard","alternaria"],
        "diagnosis": "Alternaria Blight (Alternaria brassicae / A. alternata): dark brown concentric ring spots on leaves; moves from lower to upper leaves; seed-borne. Mancozeb or Iprodione are FIRST CHOICE โ€” not Propiconazole.",
        "treatment": "Mancozeb 75% WP @ 2 g/L (FIRST CHOICE)  OR  Iprodione 50% WP @ 1.5 g/L  OR  Chlorothalonil 75% WP @ 2 g/L. Begin spray early before spread.",
        "dose":      "Mancozeb 75%WP: 2 g per litre | Iprodione 50%WP: 1.5 g per litre | Spray 200-250 L per acre",
        "timing":    "First spray at disease onset (30-40 DAS). Second spray 10-14 days later. Spray in morning. Repeat during prolonged wet weather.",
        "ipm":       "Treat seed with Thiram 75%WS @ 3 g/kg. Deep ploughing after harvest to bury infected debris. Avoid overhead irrigation. Crop rotation.",
        "source":    "ICAR-DRMR Bharatpur / NCIPM Alternaria Management Advisory",
    },


    # โ”€โ”€ Mango Malformation Disease (MMD) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    "mango_malformation": {
        "keywords":  ["mango malformation","aam malformation","mango flower malformation",
                      "aam bouri","mango panicle deformed","aam ka phal nahi","aam flower bunchy",
                      "mango inflorescence deformed","aam ka ped flower nahi","mango mangifera malform",
                      "aam flower abnormal","mango floral malformation","aam bunchy top",
                      "mango vegetative malformation","mango bud malformation","aam ki bimari"],
        "diagnosis": "Mango Malformation Disease (Fusarium mangiferae) โ€” FUNGAL disease causing two forms: (1) Vegetative malformation: bunchy top in young seedlings, excessive branching, small leaves โ€” seen in nursery plants. (2) Floral malformation: abnormal compact flower panicles, no fruit set โ€” most common in orchards. Spreads through infected nursery plants and mite vectors (Aceria mangiferae).",
        "treatment": "Pruning + fungicide: Cut and destroy all malformed panicles/shoots (10-15 cm below the base). "
                     "Apply Carbendazim 50% WP @ 1g/L immediately on cut surface. "
                     "Follow with Propiconazole 25% EC @ 1ml/L spray on entire tree. "
                     "For mite vector control: Wettable Sulphur 80% WP @ 3g/L spray on affected panicles.",
        "dose":      "Carbendazim: 1g/L | Propiconazole: 1ml/L | Wettable Sulphur: 3g/L | Volume: 500L/acre (large trees)",
        "timing":    "Spray Carbendazim in October-November (before panicle emergence) โ€” most effective timing. Remove malformed panicles as soon as visible (Decโ€“Jan). Apply wettable sulphur in November-December to control mite vector. Repeat spray at 21-day intervals for 3 sprays total.",
        "ipm":       "Source nursery plants from certified disease-free nursery only. Sterilize pruning tools with 1% bleach between trees. Remove all malformed parts from orchard (do not compost). Use healthy local varieties: Dasheri, Langra, Chausa show lower susceptibility. Avoid excess nitrogen fertilizer (promotes vegetative flush, increases disease).",
        "source":    "ICAR-CISH (Central Institute for Subtropical Horticulture) Lucknow / NRC Mango Advisory",
    },
}

# Compiled keywordโ†’disease_key lookup for fast detection
_DISEASE_KW_MAP: dict[str, str] = {}

# โ”€โ”€ ICAR Nutrient Deficiency Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Injected when query mentions nutrient deficiency symptoms.
# Prevents model from confusing nutrient deficiencies with diseases.
_ICAR_NUTRIENT_CARDS: dict[str, dict] = {
    "iron_deficiency": {
        "keywords": ["iron chlorosis","iron deficiency","patte peele interveinal","loh ki kami",
                     "iron ki kami","fe deficiency","ferrous deficiency","loha ki kami",
                     "interveinal chlorosis","yellow between veins green veins",
                     "nase hari patte peela","nadiyaan hari bahar peela"],
        "nutrient":   "Iron (Fe)",
        "symptoms":   "Interveinal chlorosis โ€” leaves yellow between veins, veins remain green; starts on younger (upper) leaves. DIFFERENT from Zinc deficiency (which starts on older lower leaves).",
        "treatment":  "Ferrous Sulphate (FeSO4) 0.5% foliar spray. Add 0.25% slaked lime to prevent precipitation. NEVER use Zinc Sulphate for iron deficiency.",
        "dose":       "FeSO4: 5 g per litre water + 2.5 g slaked lime per litre | Spray 200 L per acre | Soil application: FeSO4 @ 25 kg/acre basal",
        "timing":     "Foliar spray at symptom onset. Repeat after 7-10 days for 2-3 sprays. Spray in cool morning or evening. Soil application at sowing.",
        "source":     "ICAR-IARI Soil Science Division / ICAR Nutrient Management Advisory",
    },
    "zinc_deficiency": {
        "keywords": ["zinc deficiency","zinc ki kami","zn deficiency","white bud","khaira disease rice",
                     "zinc","zink","patte peele older","lower leaves yellow","khira disease",
                     "chota patta","small leaves brownish"],
        "nutrient":   "Zinc (Zn)",
        "symptoms":   "Khaira disease (rice): reddish-brown spots on leaves, stunted growth. In other crops: older lower leaves yellow/bronze, stunted internodes.",
        "treatment":  "Zinc Sulphate (ZnSO4) 0.5% foliar spray  OR  ZnSO4 @ 10-15 kg/acre soil application at sowing.",
        "dose":       "ZnSO4 foliar: 5 g per litre | Soil: 10-15 kg per acre | Rice khaira: ZnSO4 @ 25 kg/ha basal + foliar 0.5% after transplanting",
        "timing":     "Foliar: at symptom appearance; 2-3 sprays at 7-day intervals. Soil: at sowing/transplanting.",
        "source":     "ICAR-NRRI Cuttack / IARI Soil Science Division",
    },
    "calcium_deficiency": {
        "keywords": ["blossom end rot","calcium deficiency","blossom rot","phal neeche kala",
                     "tomato bottom rot","ca deficiency","calcium ki kami","tip burn lettuce",
                     "bitter pit apple","calcium","blossom end"],
        "nutrient":   "Calcium (Ca)",
        "symptoms":   "Blossom End Rot (tomato/pepper): bottom of fruit turns water-soaked then black/leathery. NOT a fungal disease โ€” do NOT spray fungicide. Caused by Ca uptake failure (often combined with irregular irrigation).",
        "treatment":  "Calcium Nitrate 1% foliar spray (Ca(NO3)2 @ 10 g per litre). Fix irrigation โ€” maintain consistent soil moisture. Avoid excess K and Mg which compete with Ca uptake.",
        "dose":       "Calcium Nitrate: 10 g per litre water | Spray 200 L per acre | Spray 2-3 times weekly during fruit development",
        "timing":     "Begin spray at fruit set; continue throughout fruiting. Irrigate consistently โ€” drought stress blocks Ca movement. Do NOT apply fungicide for blossom end rot.",
        "source":     "ICAR-IIVR Varanasi / TNAU Vegetable Science Division",
    },
    "phosphorus_deficiency": {
        "keywords": ["phosphorus deficiency","purple leaves","patte purple","lal patta","anthocyanin",
                     "purple color potato","purple stem","reddish purple","phodpharas ki kami",
                     "phosphorus","fosfor ki kami"],
        "nutrient":   "Phosphorus (P)",
        "symptoms":   "Purple/reddish discolouration of leaves, petioles and stems (anthocyanin accumulation); stunted root growth; delayed flowering. Do NOT treat with fungicide โ€” this is a nutrient issue not a disease.",
        "treatment":  "SSP (Single Super Phosphate) @ 40-50 kg/acre basal  OR  DAP (Di-Ammonium Phosphate) @ 20-25 kg/acre. For immediate relief: foliar spray of 2% DAP (20 g/L).",
        "dose":       "DAP foliar: 20 g per litre | Soil: SSP 40-50 kg/acre or DAP 20-25 kg/acre basal at sowing",
        "timing":     "Soil application at sowing/transplanting. Foliar spray at symptom appearance; repeat after 7 days.",
        "source":     "ICAR-IARI Soil Science / ICAR Phosphorus Management Advisory",
    },
    "magnesium_deficiency": {
        "keywords": ["magnesium deficiency","magnesium ki kami","mg deficiency","interveinal yellow older",
                     "magnesium","magenesium","chlorosis older leaves","between veins yellow lower leaves"],
        "nutrient":   "Magnesium (Mg)",
        "symptoms":   "Interveinal chlorosis on OLDER leaves (lower canopy โ€” opposite of iron deficiency); leaves remain green near veins but yellow/orange between; common in acidic/sandy soils.",
        "treatment":  "Magnesium Sulphate (MgSO4) 2% foliar spray (20 g per litre).  OR  Soil: Dolomite @ 100 kg/acre if soil pH also low.",
        "dose":       "MgSO4 foliar: 20 g per litre water | 2-3 sprays at 7-day intervals | Soil: Dolomite 100 kg/acre at land preparation",
        "timing":     "Foliar spray at symptom onset. Spray in morning. Soil dolomite at pre-sowing land preparation.",
        "source":     "ICAR-IARI Soil Science Division / KVK Magnesium Management Advisory",
    },
    "potassium_deficiency": {
        "keywords": ["potassium deficiency","potassium ki kami","k deficiency","patte ke kinare peele sukh",
                     "leaf margin scorch","leaf edge brown","potash deficiency","mop deficiency",
                     "sugarcane potassium","potash","potassium"],
        "nutrient":   "Potassium (K)",
        "symptoms":   "Marginal scorch (tip and edges of older leaves turn yellow โ†’ brown); reduced stem strength; poor grain filling. In sugarcane: narrow yellow-striped leaves.",
        "treatment":  "MOP (Muriate of Potash / KCl) @ 20-25 kg/acre soil application  OR  SOP (Sulphate of Potash) for chloride-sensitive crops. Foliar: KNO3 (KNO3) 1% spray.",
        "dose":       "MOP soil: 20-25 kg per acre | KNO3 foliar: 10 g per litre | For sugarcane: MOP 30-40 kg/acre at earthing-up stage",
        "timing":     "Soil: apply at sowing or top-dress at vegetative stage. Foliar KNO3 spray at symptom appearance; repeat after 7-10 days.",
        "source":     "ICAR-IARI / ICAR-SBI Coimbatore Potassium Advisory",
    },
    "manganese_deficiency": {
        "keywords": ["manganese deficiency","manganese ki kami","mn deficiency","marssonina blotch",
                     "grey speck oats","interveinal chlorosis young","manganese","mangan ki kami"],
        "nutrient":   "Manganese (Mn)",
        "symptoms":   "Interveinal chlorosis on younger leaves (similar to Fe but less vivid); common in alkaline/waterlogged soils; oat 'grey speck'; wheat 'grey speck'.",
        "treatment":  "Manganese Sulphate (MnSO4) foliar spray @ 0.3-0.5% (3-5 g/L). Soil acidification in alkaline soils (elemental sulphur).",
        "dose":       "MnSO4 foliar: 3-5 g per litre water | Spray 200 L per acre | 2-3 sprays at 7-day intervals",
        "timing":     "Foliar spray at symptom onset in early morning or evening. Repeat after 7 days if needed.",
        "source":     "ICAR-IARI Soil Science / ICAR Micronutrient Advisory",
    },
}

_NUTRIENT_KW_MAP: dict[str, str] = {}
for _nk, _nv in _ICAR_NUTRIENT_CARDS.items():
    for _kw in _nv["keywords"]:
        _NUTRIENT_KW_MAP[_kw] = _nk


def detect_nutrient_deficiency(query: str) -> str | None:
    """Detect nutrient deficiency queries. Returns nutrient key or None."""
    q_lower = query.lower()
    # Check for explicit deficiency signals
    DEFICIENCY_SIGNALS = [
        "deficiency","ki kami","kami","chlorosis","peele patte","yellow leaves",
        "purple patte","purple leaves","blossom end","tip burn","scorch"
    ]
    has_deficiency_signal = any(sig in q_lower for sig in DEFICIENCY_SIGNALS)
    for kw in sorted(_NUTRIENT_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            return _NUTRIENT_KW_MAP[kw]
    # Blossom end rot is always calcium
    if "blossom end" in q_lower or ("neeche" in q_lower and "kala" in q_lower and "tamatar" in q_lower):
        return "calcium_deficiency"
    return None


def build_nutrient_context(nutrient_key: str) -> str:
    """Format nutrient deficiency card as priority context for LLM."""
    card = _ICAR_NUTRIENT_CARDS.get(nutrient_key)
    if not card:
        return ""
    return (
        f"โ”โ”โ” PRIORITY CONTEXT โ€” ICAR NUTRIENT DEFICIENCY (use this first) โ”โ”โ”\n"
        f"Condition: {card['nutrient']} DEFICIENCY\n"
        f"SYMPTOMS: {card['symptoms']}\n"
        f"TREATMENT: {card['treatment']}\n"
        f"DOSE: {card['dose']}\n"
        f"TIMING: {card['timing']}\n"
        f"Source: {card['source']}\n"
        f"โ”โ”โ” CRITICAL: Do NOT recommend fungicide/pesticide for nutrient deficiency โ”โ”โ”\n"
    )


for _dk, _dv in _ICAR_DISEASE_CARDS.items():
    for _kw in _dv["keywords"]:
        _DISEASE_KW_MAP[_kw] = _dk




# โ”€โ”€ Post-Harvest Knowledge Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_POST_HARVEST_CARDS: dict[str, dict] = {
    "wheat_storage": {
        "keywords": ["gehu store","wheat store","wheat storage","gehu bharana","gehu godown",
                     "wheat godown","wheat preservation","gehu rakhna","gehu kaise store"],
        "crop":     "Wheat",
        "duration": "12-18 months (properly dried and stored)",
        "moisture": "Must dry to 12-14% moisture (ideally 12%) before storage. Test: grain bites hard, not doughy.",
        "method":   "Store in hermetic (airtight) Pusa bins or PICS bags (Purdue Improved Crop Storage). Metal/RCC bins also work. Clean and disinfect before filling. NEVER wash wheat before storage (moisture โ†’ fungal growth).",
        "pest_control": "Neem leaves @ 500 g per quintal as layers. Malathion 50% EC @ 3 ml per 10 sq ft floor spray (before filling). For severe infestation: Aluminium Phosphide (Celphos/Phostoxin) @ 1 tablet per 5 quintals โ€” ONLY by licensed fumigator.",
        "dont":     "Do NOT wash grain. Do NOT store in jute bags without lining (moisture absorption). Do NOT store near chemicals. Do NOT apply ghee/oil (attracts pests).",
        "source":   "ICAR-NIN / Pusa Storage Advisory / CWC Guidelines",
    },
    "pulse_storage": {
        "keywords": ["dal store","arhar store","arhar godown","chana store","moong store",
                     "pulse storage","dal godown","pulses store","kide dal mein","dal me kide"],
        "crop":     "Pulses (Arhar/Moong/Chana/Urad)",
        "duration": "6-12 months",
        "moisture": "Must dry to <10% moisture (pulses absorb moisture faster than cereals).",
        "method":   "Hermetic bags (PICS/ZeroFly bags) preferred. Coat storage bins with Malathion. Neem leaves @ 500 g per quintal. Keep moisture absorbers (silica gel) in sealed bins.",
        "pest_control": "Neem leaves: 500 g per quintal (most effective for pulses). Malathion 5% dust @ 100 g per quintal mixed with grain. For severe: Aluminium Phosphide @ 1 tablet per 5 quintals โ€” ONLY by licensed fumigator. DDT is BANNED โ€” do NOT use under any circumstances.",
        "dont":     "NEVER use DDT or BHC (banned, carcinogenic). Do NOT fumigate without sealing storage. Do NOT overfill bins.",
        "source":   "ICAR-IIPR Kanpur Pulse Storage Advisory",
    },
    "paddy_storage": {
        "keywords": ["dhan store","paddy storage","dhan godown","chawal store","rice store",
                     "paddy godown","dhan rakhna","dhan preserve","dhan kide"],
        "crop":     "Paddy/Rice",
        "duration": "12-18 months for paddy; 6-9 months for milled rice",
        "moisture": "Dry to <14% moisture for paddy, <13% for milled rice.",
        "method":   "Store paddy (not milled rice) for longer shelf life. Hermetic bags or RCC bins. Neem leaves between layers.",
        "pest_control": "Neem leaves @ 500 g per quintal. Deltamethrin 2.5% WP @ 1 g per sq m surface spray. Aluminium Phosphide 3 g (1 tablet) per 5 quintals for severe infestation โ€” ONLY licensed fumigator. DDT is BANNED.",
        "dont":     "NEVER store with moisture >14% (fungal growth, aflatoxin risk). NEVER use DDT or organochlorine pesticides (banned).",
        "source":   "ICAR-NRRI Cuttack / DRD Post-Harvest Advisory",
    },
    "onion_storage": {
        "keywords": ["pyaz store","onion storage","pyaz godown","onion godown","pyaz kharab",
                     "onion preservation","pyaz rakhna","onion how to store"],
        "crop":     "Onion",
        "duration": "3-5 months with proper ventilation",
        "moisture": "Cure bulbs for 3-5 days in field before storage. Relative humidity: 65-70%.",
        "method":   "Store in well-ventilated bamboo/wooden crates or mesh bags. Raised platforms (not floor). Single layer or max 3-4 layers. Separate diseased bulbs before storage.",
        "pest_control": "No pesticide needed for properly cured bulbs. Remove rotten bulbs regularly. Ensure 65-70% RH and good air circulation.",
        "dont":     "Do NOT store in gunny bags or airtight containers (moisture trapping). Do NOT store in sunlight. Do NOT store with roots on (spread diseases).",
        "source":   "ICAR-NHRDF Nasik Onion Post-Harvest Advisory",
    },
    "maize_aflatoxin": {
        "keywords": ["aflatoxin","maize aflatoxin","makka aflatoxin","fungal toxin grain",
                     "mold maize","mycotoxin","aflatoxin control","maize mold","afla"],
        "crop":     "Maize",
        "duration": "Risk highest when stored above 12% moisture or above 25ยฐC",
        "moisture": "CRITICAL: Dry maize to 12-14% moisture (target 12%) before storage. Aflatoxin cannot grow below 12% moisture.",
        "method":   "Hermetic (airtight) storage โ€” PICS bags or metal silos. AflaSafe bio-control: apply Aspergillus flavus (atoxigenic strain) @ 10 kg/ha in field before harvest. Segregate damaged/cracked kernels before storage.",
        "pest_control": "AflaSafe: 10 kg per acre in field (reduces mould load by 80%). Proper drying to 12-14% moisture is the single most effective control.",
        "dont":     "NEVER store maize above 12-14% moisture. NEVER use visibly mouldy grain as food (aflatoxin causes liver cancer). Do NOT feed mouldy grain to cattle (contaminated milk).",
        "source":   "ICAR-IIMR Hyderabad / ICRISAT AflaSafe Programme",
    },
    "banana_storage": {
        "keywords": ["kela store","banana store","banana ripen","kela pakana","kela kaise store",
                     "banana storage","kela storage","banana pakao","kela kharab","banana ripening"],
        "crop":     "Banana",
        "duration": "7-14 days at ambient; 3-4 weeks at 12-13ยฐC cold storage",
        "moisture": "Harvest at 75-80% maturity (green, full fingers). Store at 12-13ยฐC.",
        "method":   "Cold storage: 12-13ยฐC, 90-95% RH. Ethylene management for ripening: Ethephon 0.1% spray (1ml/L) at 20-22ยฐC. Never store in direct sunlight or sealed rooms (CO2 builds up). Padded cartons or bunch covering.",
        "pest_control": "Calcium carbide for ripening is ILLEGAL. Use Ethephon (ethylene-releasing agent) only. Food-grade packaging.",
        "dont":     "Do NOT use calcium carbide (illegal, harmful). Do NOT store below 12ยฐC (chilling injury). Do NOT mix fully ripe with green bananas.",
        "source":   "ICAR-NRC Banana Tiruchirappalli Post-Harvest Advisory / APEDA Standards",
    },
    "tomato_export": {
        "keywords": ["tamatar grading","tamatar packing","tamatar export","tomato export",
                     "tomato grading","tomato packing","tomato grade","tamatar grade",
                     "tamatar carton","tomato carton","tamatar bhejo","tomato market"],
        "crop":     "Tomato",
        "duration": "7-10 days at ambient; 3-4 weeks at 10-12ยฐC cold storage",
        "moisture": "Pre-cool to 10-12ยฐC within 2 hours of harvest. Never pack warm tomatoes.",
        "method":   "GRADING: Grade A (>65mm diameter), Grade B (55-65mm), Grade C (45-55mm). Pack in CFB (corrugated fibreboard) cartons of 5-10 kg. Wrap each fruit or use foam nets. Pre-cool at 10-12ยฐC before packing. Follow APEDA standards for export.",
        "pest_control": "No pesticide at packing. Post-harvest fungicide: Thiabendazole 0.1% dip if needed. Refrigerated transport.",
        "dont":     "Do NOT mix damaged/cracked tomatoes. Do NOT pack without pre-cooling (shelf life halved). Do NOT stack cartons >4 high (bruising).",
        "source":   "ICAR-IIHR Bengaluru / APEDA Tomato Export Standards",
    },
    "potato_storage": {
        "keywords": ["aloo storage","potato storage","aloo cold","potato cold","aloo store",
                     "aloo rakhna","cold storage aloo","aloo cold storage","aloo kiraya"],
        "crop":     "Potato",
        "duration": "6-9 months at 2-4ยฐC cold storage; 2-3 months at ambient with curing",
        "moisture": "CURING before cold storage: 12-15ยฐC, 90-95% RH for 7-10 days. Heals skin wounds.",
        "method":   "Cold storage temperature: 2-4ยฐC (table potato) or 8-10ยฐC (seed potato). Relative humidity: 90-95%. Cold storage cost: Rs 150-250 per quintal per season. Cure first (7-10 days at 12-15ยฐC) before shifting to cold storage โ€” curing reduces storage losses by 30%.",
        "pest_control": "Ensure no damaged/diseased tubers enter storage (disease spreads rapidly at 2-4ยฐC). Apply Thiabendazole 0.2% dip before cold storage to prevent early blight.",
        "dont":     "Do NOT store uncured potato directly in cold (more losses). Do NOT store near onion (ethylene causes sprouting). Do NOT open cold storage frequently.",
        "source":   "ICAR-CPRI Shimla / NHB Post-Harvest Cold Storage Advisory",
    },
    "mustard_oil_storage": {
        "keywords": ["sarso tel","mustard oil","sarso ghani","sarso tel store","mustard ghani",
                     "mustard tel","ghani se nikale","sarso press","mustard expeller"],
        "crop":     "Mustard",
        "duration": "6-12 months for oil; 12-18 months for seed",
        "moisture": "Expeller-press mustard at <60ยฐC to preserve nutritional quality and avoid rancidity.",
        "method":   "EXPELLER: Cold-press at <60ยฐC (hot-press above 80ยฐC destroys antioxidants). Store oil in dark glass or food-grade HDPE containers (not regular plastic). Seal tightly to prevent oxidation. Store in cool, dark location. Mustard seed storage: <8% moisture, hermetic bins.",
        "pest_control": "Oil: Nitrogen flushing for long-term storage (prevents rancidity). Seed: Neem leaves 500g/quintal. Check for rancidity (free fatty acid test) every 3 months.",
        "dont":     "Do NOT store oil in metal containers (oxidation). Do NOT press at >80ยฐC. Do NOT expose to sunlight (accelerates rancidity).",
        "source":   "ICAR-DRMR Bharatpur / CFTRI Mysore Mustard Oil Advisory",
    },
    "mango_transport": {
        "keywords": ["mango transport","aam transport","mango market","aam bazar bhejo",
                     "mango packaging","aam pack","mango export","aam spoilage",
                     "aam kharab hona transport","mango cold"],
        "crop":     "Mango",
        "duration": "3-7 days at ambient; 14-21 days at 12-13ยฐC cold storage",
        "moisture": "Pre-cool mangoes to 12-13ยฐC before packing. Never mix ripe and unripe.",
        "method":   "Wax coating (food-grade carnauba wax) extends shelf life by 5-7 days. Pack in ventilated CFB (corrugated fibreboard) cartons (5-10 kg). One mango per tissue paper wrap. Cushion layer at bottom.",
        "pest_control": "No pesticide at packaging. Use Post-Harvest Treatment: hot water at 48ยฐC for 60 min (HWT) prevents anthracnose and fruit fly. Cool to 12-13ยฐC after HWT.",
        "dont":     "Do NOT use calcium carbide for ripening (harmful, illegal). Do NOT pack wet fruit. Do NOT over-pack boxes (bruising).",
        "source":   "ICAR-CISH Lucknow / NRC Mango Post-Harvest Advisory / APEDA Standards",
    },
}

_POSTHARVEST_KW_MAP: dict[str, str] = {}
for _phk, _phv in _POST_HARVEST_CARDS.items():
    for _kw in _phv["keywords"]:
        _POSTHARVEST_KW_MAP[_kw] = _phk


def detect_postharvest_query(query: str) -> str | None:
    """Detect post-harvest storage/handling queries. Returns card key or None."""
    q_lower = query.lower()
    for kw in sorted(_POSTHARVEST_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            return _POSTHARVEST_KW_MAP[kw]
    return None


def build_postharvest_context(ph_key: str) -> str:
    """Format post-harvest card as priority context for LLM."""
    card = _POST_HARVEST_CARDS.get(ph_key)
    if not card:
        return ""
    return (
        f"โ”โ”โ” PRIORITY CONTEXT โ€” ICAR POST-HARVEST ADVISORY โ”โ”โ”\n"
        f"Crop: {card['crop']}\n"
        f"Storage Duration: {card['duration']}\n"
        f"Moisture Requirement: {card['moisture']}\n"
        f"Storage Method: {card['method']}\n"
        f"Pest Control: {card['pest_control']}\n"
        f"IMPORTANT โ€” Do NOT: {card['dont']}\n"
        f"Source: {card['source']}\n"
        f"โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
    )




# โ”€โ”€ Government Scheme Knowledge Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_GOVT_SCHEME_CARDS: dict[str, dict] = {
    "pm_kisan": {
        "keywords": ["pm kisan","pm-kisan","pradhan mantri kisan","samman nidhi","kisan samman",
                     "6000 rupaye","2000 kist","pm kisan paisa","pm kisan kitna milta"],
        "scheme":   "PM-KISAN Samman Nidhi",
        "amount":   "โ‚น6,000 per year โ€” paid in 3 installments of โ‚น2,000 each (every 4 months)",
        "eligibility": "All small and marginal farmers with landholding up to 2 hectares. Aadhaar + bank account linked mandatory.",
        "how_to_apply": "Online: pmkisan.gov.in | Offline: nearest CSC (Common Service Centre) or agriculture department. Documents: Aadhaar, bank passbook, land record (Khasra/khatauni).",
        "helpline": "PM-KISAN helpline: 155261 / 1800115526 (toll-free)",
        "source":   "Ministry of Agriculture & Farmers Welfare, GOI โ€” pmkisan.gov.in",
    },
    "pmfby": {
        "keywords": ["fasal bima","pmfby","pm fasal bima","crop insurance","bima yojana",
                     "fasal bima yojana","claim kaise","fasal nuksan","bima premium",
                     "72 hour","72 ghante","crop loss","pradhan mantri fasal"],
        "scheme":   "PM Fasal Bima Yojana (PMFBY)",
        "premium":  "Farmer pays: 2% of sum insured for Kharif crops | 1.5% for Rabi crops | 5% for commercial/horticultural crops. Government pays the rest.",
        "claim_process": "CRITICAL: Report crop loss within 72 HOURS of damage (natural calamity, pest, disease). Call the insurance company helpline OR report via Crop Insurance App (PMFBY App). Failure to report within 72 hours forfeits claim.",
        "how_to_enroll": "Through bank at loan time (compulsory for loanee farmers) or voluntarily at bank/CSC before cutoff date. Season-specific cutoff dates โ€” enroll before sowing season.",
        "helpline": "PMFBY helpline: 14447 (toll-free) | Crop Insurance App: available on Play Store",
        "source":   "Ministry of Agriculture & Farmers Welfare โ€” pmfby.gov.in",
    },
    "kcc_loan": {
        "keywords": ["kcc loan","kisan credit card","kcc kitna","kcc interest","credit card kisan",
                     "fasal loan","crop loan","kisan karj","kcc bank","kcc kaise milega"],
        "scheme":   "Kisan Credit Card (KCC) Loan",
        "amount":   "Up to โ‚น3 lakh short-term crop loan | Above โ‚น3 lakh at market rates",
        "interest": "7% per annum for up to โ‚น3 lakh | Additional 3% subvention for timely repayment = effective 4% per annum",
        "eligibility": "All farmers (individual/joint/tenant farmers/sharecroppers). Land documents required.",
        "how_to_apply": "Visit nearest bank (SBI, Bank of Baroda, cooperative bank, regional rural bank). Documents: Aadhaar, PAN, land records, passport photo.",
        "helpline": "National Agriculture Helpline: 1800-180-1551 | Respective bank branch",
        "source":   "NABARD / Ministry of Agriculture KCC Scheme",
    },
    "rythu_bandhu": {
        "keywords": ["rythu bandhu","telangana scheme","telangana farmer scheme","rhythu bandhu",
                     "rythu","bandhu","telangana 5000","TS farmer scheme"],
        "scheme":   "Rythu Bandhu (Telangana State Scheme)",
        "amount":   "โ‚น5,000 per acre per season (Kharif + Rabi = โ‚น10,000 per acre per year). Paid twice a year before each sowing season.",
        "eligibility": "All registered farmers of Telangana with agricultural land. Land records must be updated in Dharani portal.",
        "how_to_apply": "Automatic disbursement based on Dharani land records. Update land records at MeeSeva or VRO office.",
        "source":   "Government of Telangana โ€” Rythu Bandhu Scheme",
    },
    "soil_health_card": {
        "keywords": ["soil health card","mitti jaanch","soil test","soil health","mitti ki jaanch",
                     "soil card","SHC scheme","soil testing","mitti testing","soil ki jaanch"],
        "scheme":   "Soil Health Card (SHC) Scheme",
        "cost":     "FREE for farmers โ€” government bears testing cost",
        "what_tested": "12 soil parameters: pH, EC, N, P, K, S, Zn, Fe, Mn, Cu, B, OC (Organic Carbon)",
        "how_to_apply": "Visit nearest KVK (Krishi Vigyan Kendra) or District Agriculture Office with soil sample (500g from 6-9 inch depth). OR call 1800-180-1551 for nearest testing centre.",
        "frequency": "Every 2 years as recommended",
        "source":   "Ministry of Agriculture โ€” soilhealth.dac.gov.in / Nearest KVK / Agriculture Department",
    },
    "enam": {
        "keywords": ["enam","e-nam","e nam","national agriculture market","enam mandi",
                     "enam registration","online mandi","enam portal"],
        "scheme":   "eNAM (National Agriculture Market) โ€” Online Mandi Portal",
        "benefit":  "Farmers can sell directly to buyers across India without middlemen. Better price discovery through online bidding.",
        "how_to_register": "Register at nearest eNAM-linked mandi with: Aadhaar, bank account details, land documents. Visit your nearest APMC mandi office to register.",
        "helpline": "eNAM helpdesk: 1800-270-0224 (toll-free) | enam.gov.in",
        "source":   "Ministry of Agriculture & Farmers Welfare โ€” enam.gov.in",
    },
    "organic_certification": {
        "keywords": ["organic certification","organic farming certificate","jaivik kheti praman",
                     "pgc organic","pgs india","organic praman patra","organic farming registration"],
        "scheme":   "PGS-India (Participatory Guarantee System) โ€” Organic Certification",
        "cost":     "Low-cost / subsidized through state agriculture departments",
        "how_to_apply": "Contact nearest KVK or State Agriculture Department. Form farmer group (minimum 5 farmers). Apply on pgsindia.net or through local cluster coordinator.",
        "benefit":  "PGS-India certificate allows selling as organic produce with premium pricing. Government subsidy under PKVY (Paramparagat Krishi Vikas Yojana).",
        "helpline": "PKVY helpline: 1800-180-1551 | pgsindia.net",
        "source":   "Ministry of Agriculture โ€” PKVY / PGS-India Programme",
    },
}

_SCHEME_KW_MAP: dict[str, str] = {}
for _sk, _sv in _GOVT_SCHEME_CARDS.items():
    for _kw in _sv["keywords"]:
        _SCHEME_KW_MAP[_kw] = _sk


def detect_govt_scheme(query: str) -> str | None:
    """Detect govt scheme queries. Returns scheme key or None."""
    q_lower = query.lower()
    for kw in sorted(_SCHEME_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            return _SCHEME_KW_MAP[kw]
    return None


def build_scheme_context(scheme_key: str) -> str:
    """Format govt scheme card as priority context for LLM."""
    card = _GOVT_SCHEME_CARDS.get(scheme_key)
    if not card:
        return ""
    lines = [
        f"โ”โ”โ” PRIORITY CONTEXT โ€” GOVERNMENT SCHEME (use exact figures below) โ”โ”โ”",
        f"Scheme: {card['scheme']}",
    ]
    for field in ("amount","interest","premium","what_tested","cost","benefit","eligibility",
                  "how_to_apply","how_to_register","claim_process","frequency","helpline","source"):
        if field in card:
            label = field.replace("_"," ").upper()
            lines.append(f"{label}: {card[field]}")
    lines.append("โ”โ”โ” Use EXACT amounts/figures above. Do NOT guess or use approximate figures. โ”โ”โ”")
    return "\n".join(lines) + "\n"


# โ”€โ”€ ICAR Irrigation Knowledge Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Source: ICAR-IARI, AICRP on Micro-irrigation, National Committee on Plasticulture
# Applications in Horticulture (NCPAH), state agriculture universities.
# Water requirements are for Indian semi-arid/sub-humid conditions.

_ICAR_IRRIGATION_CARDS: dict[str, dict] = {
    "tomato": {
        "keywords": ["tomato","tamatar"],
        "drip_lpd": "2โ€“4 litres per plant per day",
        "stages": "Transplanting (1โ€“2 L), Vegetative (2โ€“3 L), Flowering & fruit set (3โ€“4 L โ€” critical, never let it dry)",
        "flood": "8โ€“10 cm depth every 7โ€“10 days in summer; every 12โ€“15 days in winter",
        "critical": "Flowering and fruit set โ€” moisture stress here causes blossom drop and fruit cracking",
        "tip": "Use fertigation (water-soluble NPK 19:19:19 @ 3kg/acre/week through drip) for 20โ€“30% higher yield",
        "source": "ICAR-IIVR / IARI Tomato Package of Practices",
    },
    "chilli": {
        "keywords": ["chilli","chili","mirchi","capsicum","shimla mirch"],
        "drip_lpd": "1.5โ€“2.5 litres per plant per day",
        "stages": "Transplanting (1โ€“1.5 L), Vegetative (1.5โ€“2 L), Flowering (2โ€“2.5 L โ€” critical)",
        "flood": "6โ€“8 cm depth every 7โ€“10 days; avoid waterlogging (causes phytophthora wilt)",
        "critical": "Flowering โ€” drought stress causes flower drop; waterlogging causes wilt and root rot",
        "tip": "Mulching with black polythene (25 micron) reduces water need by 40% and controls weeds",
        "source": "IIHR-Bangalore / NCPAH Chilli Drip Advisory",
    },
    "onion": {
        "keywords": ["onion","pyaz","kanda"],
        "drip_lpd": "1โ€“1.5 litres per plant per day (or 3โ€“4 mm/day total field water)",
        "stages": "Establishment (3โ€“4 mm/day), Bulbing (4โ€“5 mm/day โ€” critical), Maturity (reduce to 2mm, stop 10d before harvest)",
        "flood": "5โ€“6 cm depth every 7โ€“10 days; total 8โ€“10 irrigations per crop",
        "critical": "Bulbing stage (60โ€“90 days after transplanting) โ€” moisture stress reduces bulb size by 30โ€“40%",
        "tip": "Stop irrigation 10 days before harvest โ€” improves storability and reduces neck rot",
        "source": "NHRDF / AICRP Onion and Garlic",
    },
    "wheat": {
        "keywords": ["wheat","gehun","gehu"],
        "drip_lpd": "Not applicable (flood/furrow crop)",
        "flood": "5โ€“7 cm depth at each irrigation; total 5โ€“6 irrigations for timely-sown wheat in north India",
        "stages": "CRI (crown root initiation, 20โ€“25 DAS) โ†’ Tillering (40โ€“45 DAS) โ†’ Jointing (60โ€“65 DAS) โ†’ Booting (75โ€“80 DAS) โ†’ Milking (90โ€“95 DAS). CRI and booting are most critical.",
        "critical": "CRI (20โ€“25 days after sowing) โ€” skipping CRI irrigation reduces yield by 25โ€“40%",
        "tip": "Laser levelling saves 20โ€“25% irrigation water and improves uniformity",
        "source": "ICAR-IARI Wheat Section / AICRPW Advisory",
    },
    "paddy": {
        "keywords": ["paddy","rice","dhan","chawal","dhaan"],
        "drip_lpd": "Not typical; SRI method: intermittent irrigation keeping field moist but not flooded",
        "flood": "Maintain 5 cm standing water during vegetative and reproductive stages. Drain 10 days before harvest.",
        "stages": "Puddling (flood), Transplanting (2โ€“3 cm water), Tillering (5 cm), Panicle initiation (5 cm), Flowering (5โ€“7 cm critical), Grain filling (5 cm), Pre-harvest (drain 10d before)",
        "critical": "Flowering stage โ€” even 1 day of drought causes 30โ€“40% spikelet sterility",
        "tip": "Alternate Wetting and Drying (AWD) technique: allow soil to dry to 15 cm below surface between irrigations โ€” saves 25โ€“30% water with no yield loss",
        "source": "ICAR-NRRI / IRRI AWD Advisory",
    },
    "cotton": {
        "keywords": ["cotton","kapas","karpas"],
        "drip_lpd": "4โ€“6 litres per plant per day (drip-fertigation system)",
        "flood": "6โ€“8 cm depth every 10โ€“15 days; total 5โ€“6 irrigations; avoid waterlogging",
        "stages": "Seedling (light irrigation), Square formation (moderate), Flowering (5โ€“6 L/plant/day โ€” critical), Boll development (5 L/plant), Boll opening (reduce/stop)",
        "critical": "Flowering and early boll development โ€” stress causes boll shedding; excessive water causes boll rot",
        "tip": "Drip-fertigation with 20 kg N + 10 kg Pโ‚‚Oโ‚… + 20 kg Kโ‚‚O per acre split over season doubles water efficiency",
        "source": "CICR-Nagpur / AICRP Cotton Drip Advisory",
    },
    "sugarcane": {
        "keywords": ["sugarcane","ganna","ikh","ganderi"],
        "drip_lpd": "8โ€“12 litres per plant per day in summer (drip)",
        "flood": "8โ€“10 cm depth every 7โ€“10 days in summer; every 15โ€“20 days in winter; total 25โ€“30 irrigations",
        "stages": "Germination (light), Tillering (moderate), Grand growth (8โ€“10 L/day โ€” critical), Maturation (reduce)",
        "critical": "Grand growth period (4โ€“8 months) โ€” accounts for 70% of total water need; drought reduces sucrose content",
        "tip": "Drip irrigation reduces water use by 40โ€“50% vs. flood and increases sugar recovery by 0.5โ€“0.8 units",
        "source": "ICAR-IISR Coimbatore / AICRP Sugarcane",
    },
    "potato": {
        "keywords": ["potato","aloo","alu"],
        "drip_lpd": "2โ€“4 litres per plant per day (drip)",
        "flood": "4โ€“6 cm depth every 7โ€“10 days; avoid waterlogging (causes tuber rot); total 8โ€“10 irrigations",
        "stages": "Planting to emergence (light), Stolon formation (moderate), Tuber initiation (4 L/day โ€” critical), Tuber bulking (4โ€“5 L/day), Maturation (reduce gradually, stop 2 weeks before harvest)",
        "critical": "Tuber initiation and bulking โ€” soil moisture below 50% field capacity reduces tuber size and increases hollow heart",
        "tip": "Sprinkler irrigation for potato reduces blight incidence compared to overhead flood (less leaf wetness)",
        "source": "ICAR-CPRI Shimla / AICRP Potato Advisory",
    },
    "maize": {
        "keywords": ["maize","corn","makka","makki","maka"],
        "drip_lpd": "3โ€“5 litres per plant per day (drip)",
        "flood": "5โ€“7 cm depth; total 4โ€“5 irrigations; critical stages must not be missed",
        "stages": "Sowing (light pre-sowing), Knee-high (5โ€“6 leaf stage), Tasseling, Silking (most critical โ€” 10 days around silking), Grain filling",
        "critical": "Silking (1 week before and after silk emergence) โ€” drought at this stage causes barren cobs and 50โ€“70% yield loss",
        "tip": "Deficit irrigation: if water is scarce, prioritize silking stage over all others",
        "source": "ICAR-IIMR Hyderabad / DWR-AICRP Maize",
    },
    "groundnut": {
        "keywords": ["groundnut","peanut","moongphali","mungfali"],
        "drip_lpd": "2โ€“3 litres per plant per day (drip)",
        "flood": "5โ€“6 cm depth every 10โ€“12 days; critical to avoid waterlogging at pod-fill",
        "stages": "Pre-sowing (5cm), Pegging (5cm), Pod development (critical, 5cm), Pod fill, Maturity (dry down)",
        "critical": "Pegging and pod development โ€” moisture stress causes empty pods; waterlogging causes collar rot",
        "tip": "Avoid irrigation 2 weeks before harvest โ€” allows shells to dry and reduces aflatoxin risk",
        "source": "ICAR-DGR Junagadh / AICRP Groundnut Advisory",
    },
    "soybean": {
        "keywords": ["soybean","soya","soyabean","bhat"],
        "drip_lpd": "2โ€“4 litres per plant per day",
        "flood": "5โ€“6 cm depth; total 3โ€“4 irrigations; pre-sowing + flowering + pod fill",
        "stages": "Pre-sowing (critical for germination), Flowering (R1 โ€” most critical), Pod fill (R4-R5), Grain fill",
        "critical": "Flowering and pod fill stages โ€” drought reduces yield by 30โ€“40%; waterlogging at any stage reduces nodulation",
        "tip": "Furrow irrigation preferred over overhead (reduces disease). Avoid irrigation if rain expected within 3 days.",
        "source": "ICAR-IISR Indore / AICRP Soybean Water Management",
    },
    "brinjal": {
        "keywords": ["brinjal","baingan","eggplant","begun","baigan"],
        "drip_lpd": "2โ€“3 litres per plant per day",
        "flood": "5โ€“6 cm depth every 7โ€“10 days; twice weekly in summer; weekly in winter",
        "stages": "Transplanting (daily for 7 days), Vegetative (2โ€“3 L), Flowering (3 L โ€” critical), Fruiting (3โ€“4 L)",
        "critical": "Flowering โ€” water stress causes flower drop and misshapen fruits. Avoid waterlogging.",
        "tip": "Drip + black plastic mulch reduces water use by 35% and suppresses weeds simultaneously",
        "source": "ICAR-IIHR Bangalore / AICRP Vegetables Irrigation",
    },
    "cauliflower": {
        "keywords": ["cauliflower","phool gobhi","gobi","broccoli"],
        "drip_lpd": "2โ€“3 litres per plant per day",
        "flood": "5โ€“7 cm depth every 7โ€“10 days; must not dry out (shallow roots)",
        "stages": "Transplanting (daily), Leaf development (2โ€“3 L), Curd initiation (3โ€“4 L โ€” critical), Curd development",
        "critical": "Curd initiation โ€” moisture stress causes button heads, brown curds, premature bolting",
        "tip": "Tie outer leaves over curd at 10 cm diameter โ€” prevents browning and frost damage",
        "source": "ICAR-IARI / AICRP Vegetables Water Management",
    },
    "mango": {
        "keywords": ["mango","aam","keri","kairi"],
        "drip_lpd": "10โ€“20 litres per mature tree per day; 5โ€“8 L for young trees (1โ€“3 years)",
        "flood": "Basin irrigation: 100โ€“150 L per adult tree; 15โ€“20 irrigations/year",
        "stages": "Flowering (withheld โ€” promotes flowering), Fruit set (resume), Marble stage (critical), Pre-harvest (stop 2 weeks before)",
        "critical": "DO NOT irrigate during flowering (Novโ€“Jan) โ€” promotes vegetative growth, reduces fruit set. Resume after 80% bloom.",
        "tip": "Withhold irrigation Sepโ€“Oct (2 months before flowering) for better flowering induction in Novโ€“Jan.",
        "source": "ICAR-CISH Lucknow / AICRP Mango Water Management",
    },
    "banana": {
        "keywords": ["banana","kela","plantain"],
        "drip_lpd": "8โ€“12 litres per plant per day (G9/Grand Naine TC); 15 L in peak summer",
        "flood": "Basin/furrow: 15โ€“20 cm depth every 5โ€“7 days in summer; every 10โ€“12 days in winter",
        "stages": "Planting (high), Shooting (high โ€” bunch development), Flowering (critical), Bunch filling (high), Pre-harvest (reduce)",
        "critical": "Bunch filling โ€” water stress reduces bunch weight by 20โ€“30% and finger girth significantly",
        "tip": "Drip + fertigation (NPK 19:19:19 @ 5g/plant/day during bunch fill) gives 20โ€“25% higher yield vs flood",
        "source": "ICAR-NRC Banana Trichy / NCPAH Banana Drip Advisory",
    },
    "mustard": {
        "keywords": ["mustard","sarson","rai","rapeseed"],
        "drip_lpd": "Not typically drip-irrigated; 2โ€“3 flood irrigations only",
        "flood": "5โ€“6 cm depth; 1st at 30d (branching), 2nd at 55d (flowering), 3rd at 80d (pod fill) if dry",
        "stages": "Branching (30d), Flowering (55d โ€” most critical), Pod fill (80d)",
        "critical": "Flowering โ€” moisture stress causes flower drop; excess water at this stage causes Sclerotinia rot",
        "tip": "Mustard is drought-tolerant; 2 irrigations (branching + flowering) are sufficient most years. Over-irrigation promotes aphids.",
        "source": "ICAR-DRMR Bharatpur / AICRP Oilseed Water Management",
    },
}

# โ”€โ”€ ICAR Agronomy Cards โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Covers all 26 crops in the pest model + extras. Fields:
#   seed_rate, spacing, low_rain_variety, low_rain_spacing,
#   waterlog_steps, sowing_window, fertilizer, intercrop, source
# These are injected as PRIORITY CONTEXT for agronomy-type queries.
_ICAR_AGRONOMY_CARDS: dict[str, dict] = {
    "rice": {
        "keywords": ["rice","paddy","dhan","chawal","bodo","basmati","samba","ponni"],
        "seed_rate": "Transplanted: 20โ€“25 kg/acre (nursery); Direct seeded: 8โ€“10 kg/acre",
        "spacing": "Transplanted: 20ร—15 cm; SRI method: 25ร—25 cm (1 seedling/hill)",
        "low_rain_variety": "MTU-1010, Sahbhagi Dhan (120d, aerobic-tolerant); DRR Dhan-44 (drought-tolerant)",
        "low_rain_spacing": "25ร—20 cm โ€” wider spacing reduces water demand by 15โ€“20%",
        "waterlog_steps": [
            "Open field drainage channels immediately โ€” rice tolerates shallow flooding but NOT stagnant water >7 days",
            "If water >30 cm deep for >5 days: drain and assess plant health",
            "Apply potash (MOP 10 kg/acre) after drainage โ€” compensates leaching",
            "Spray Carbendazim 50% WP @ 1g/L if sheath blight symptoms appear post-flood",
        ],
        "sowing_window": "Kharif: Juneโ€“July (transplant 21โ€“25 day seedlings); Rabi (South India): Novโ€“Dec",
        "fertilizer": "Basal: DAP 50 kg/acre + MOP 25 kg/acre. Top dress: Urea 25 kg/acre at tillering + 25 kg/acre at panicle initiation",
        "intercrop": "Azolla as green manure between rows (fixes 20โ€“30 kg N/acre)",
        "source": "ICAR-NRRI Cuttack / CRRI / AICRP Rice Advisory",
    },
    "wheat": {
        "keywords": ["wheat","gehun","gehu","triticum","rabi wheat"],
        "seed_rate": "25โ€“30 kg/acre (timely sown); 35โ€“40 kg/acre (late sown โ€” compensate with higher density)",
        "spacing": "Row spacing: 22.5 cm; seed depth 5 cm",
        "low_rain_variety": "GW-322, K-9107 (drought-tolerant); HD-2781 (heat+drought; 120d); RAJ-3765 for arid zones",
        "low_rain_spacing": "Same row spacing (22.5 cm) but REDUCE seed rate to 22โ€“25 kg/acre โ€” saves moisture for germination",
        "waterlog_steps": [
            "Wheat is very sensitive to waterlogging โ€” act within 24 hours",
            "Open furrows between rows immediately to drain water",
            "Withhold nitrogen fertilizer for 10 days post-flooding (roots cannot absorb when waterlogged)",
            "Apply Zinc Sulphate 5 g/L foliar spray at recovery โ€” waterlogging causes Zn deficiency",
            "If yellowing persists after drainage: check for crown rot (Fusarium) โ€” drench with Carbendazim 1g/L",
        ],
        "sowing_window": "Rabi: Oct 15 โ€“ Nov 25 (Punjab/Haryana); Nov 1โ€“30 (UP/Bihar); Nov 15โ€“Dec 15 (MP/Rajasthan)",
        "fertilizer": "Basal: DAP 50 kg/acre. Top dress: Urea 33 kg/acre at crown root initiation (21d) + 33 kg/acre at tillering (45d)",
        "intercrop": "Wheat + Mustard (9:1 ratio); Wheat + Chickpea in rainfed areas",
        "source": "ICAR-IIWBR Karnal / AICRP Wheat & Barley Advisory",
    },
    "cotton": {
        "keywords": ["cotton","kapas","karpas","narma","bt cotton","desi cotton"],
        "seed_rate": "Bt hybrid: 0.75โ€“1 kg/acre (pelleted seed); Desi/non-Bt: 3โ€“4 kg/acre",
        "spacing": "Hybrid Bt: 90ร—60 cm or 120ร—45 cm; Desi: 60ร—30 cm",
        "low_rain_variety": "Suraj, Khandwa-2, JK Durga (short-duration 150โ€“160d, drought-tolerant); PKV Rajat for Vidarbha",
        "low_rain_spacing": "Wider: 120ร—60 cm โ€” reduces inter-plant competition; plant population 5,500โ€“6,000/acre",
        "waterlog_steps": [
            "Cotton is highly susceptible โ€” drain within 12โ€“24 hours of waterlogging",
            "Create raised bed / broad bed furrow BEFORE sowing to prevent waterlogging",
            "Post-waterlogging: apply Ridomil Gold (Metalaxyl+Mancozeb) @ 2.5g/L to prevent root rot",
            "Withhold fertilizer for 7โ€“10 days; then apply SOP (potash) @ 5g/L foliar to boost recovery",
            "Scout for pink bollworm post-flood โ€” stress increases susceptibility",
        ],
        "sowing_window": "Kharif: May 15 โ€“ June 30 (pre-monsoon planting preferred); latest by July 15",
        "fertilizer": "Basal: 10 kg DAP + 10 kg MOP/acre. Top dress: Urea 25 kg at square formation (45d) + 25 kg at boll development (70d). Boron 0.2% foliar at flowering.",
        "intercrop": "Cotton + Moong (2:1); Cotton + Cowpea; Cotton + Soybean (Vidarbha)",
        "source": "ICAR-CICR Nagpur / AICRP Cotton Advisory",
    },
    "soybean": {
        "keywords": ["soybean","soya","soya bean","bhat","glycine","soybean crop"],
        "seed_rate": "30โ€“35 kg/acre; seed treatment: Rhizobium + PSB + Thiram before sowing",
        "spacing": "45ร—5 cm (row ร— plant); depth 2โ€“3 cm",
        "low_rain_variety": "JS 95-60 (95d, drought-tolerant, Vidarbha/Madhya Pradesh); MACS 450 (90d); NRC 86 (early, 90d)",
        "low_rain_spacing": "45ร—10 cm wider plant spacing โ€” retains soil moisture longer; reduces plant population to 60,000/acre",
        "waterlog_steps": [
            "Soybean drowns in 48 hours โ€” drain immediately via field channels",
            "Withhold ALL fertilizer for 5 days post-drainage",
            "Apply Thiram 75% WS @ 3g/kg seed if re-sowing needed after flood failure",
            "Post-flood foliar: 2% urea spray + Borax 0.2% for recovery",
            "Watch for Phytophthora root rot (brown stems) โ€” drench with Metalaxyl 2g/L",
        ],
        "sowing_window": "Kharif: June 20 โ€“ July 15 (ideal: last week June with pre-monsoon rains)",
        "fertilizer": "Basal: DAP 25 kg/acre + MOP 15 kg/acre (soybean fixes N; no urea at sowing). Foliar: Borax 0.2% at flowering.",
        "intercrop": "Soybean + Pigeonpea (4:2 ratio) โ€” classic Vidarbha system; Soybean + Maize (4:1)",
        "source": "ICAR-IISR Indore / AICRP Soybean Advisory",
    },
    "maize": {
        "keywords": ["maize","corn","makka","makki","maka","bhutta","sweet corn"],
        "seed_rate": "Hybrid: 7โ€“8 kg/acre; Composite: 10โ€“12 kg/acre; Baby corn: 18โ€“20 kg/acre",
        "spacing": "60ร—20 cm (Kharif); 60ร—15 cm (Rabi โ€” denser for yield)",
        "low_rain_variety": "Vivek QPM 9 (75d, drought-tolerant); HQPM-1 (QPM type); Rajkumar (drought-tolerant, 90d)",
        "low_rain_spacing": "75ร—20 cm โ€” wider row spacing conserves soil moisture; sow on ridges to channel water to roots",
        "waterlog_steps": [
            "Drain within 24 hours โ€” maize tassel stage especially vulnerable",
            "If at vegetative stage: spray 2% Urea after drainage for nitrogen recovery",
            "At silking/tasseling: waterlogging causes barren ears โ€” boost with potash (SOP 5g/L foliar)",
            "Check for stem rot (Pythium) post-flood โ€” spray Mancozeb 2.5g/L preventively",
        ],
        "sowing_window": "Kharif: Juneโ€“July; Rabi (South/Central India): Octโ€“Nov; Spring (North): Febโ€“March",
        "fertilizer": "Basal: DAP 50 kg/acre + MOP 20 kg/acre. Top dress: Urea 45 kg/acre in 3 splits (knee-high, tassel, silking)",
        "intercrop": "Maize + Cowpea (3:1); Maize + Groundnut; Maize + Soybean",
        "source": "ICAR-IIMR Hyderabad / AICRP Maize Advisory",
    },
    "mustard": {
        "keywords": ["mustard","sarson","rai","rapeseed","canola","toria","yellow sarson"],
        "seed_rate": "1.5โ€“2 kg/acre (broadcast); 1โ€“1.5 kg/acre (line sowing)",
        "spacing": "30ร—10 cm (line sowing); thinning to 10 cm within rows at 2-leaf stage",
        "low_rain_variety": "Varuna (low water need), RH-749, Pusa Bold (135d, drought-tolerant, Rajasthan/Haryana)",
        "low_rain_spacing": "45ร—10 cm โ€” wider rows save moisture; early sowing critical (Oct 1โ€“15) to escape terminal drought",
        "waterlog_steps": [
            "Mustard is very sensitive โ€” drain within 12 hours",
            "Spray KNO3 (potassium nitrate) 1% foliar for stress recovery",
            "Watch for Sclerotinia stem rot post-flood โ€” spray Carbendazim 0.1%",
        ],
        "sowing_window": "Rabi: Oct 1โ€“25 (optimal); Oct 26โ€“Nov 10 (late โ€” use short-duration variety Toria)",
        "fertilizer": "Basal: DAP 50 kg/acre + Sulphur 8 kg/acre (critical for oil content). Top dress: Urea 25 kg/acre at 30 days.",
        "intercrop": "Mustard + Wheat (2:8 ratio); Mustard + Gram; Mustard + Lentil",
        "source": "ICAR-DRMR Bharatpur / AICRP Rapeseed-Mustard Advisory",
    },
    "gram": {
        "keywords": ["gram","chickpea","chana","chick pea","bengal gram","kabuli","desi chana"],
        "seed_rate": "Desi: 30โ€“35 kg/acre; Kabuli: 40โ€“45 kg/acre (larger seed)",
        "spacing": "30ร—10 cm; depth 5โ€“7 cm",
        "low_rain_variety": "JG-11 (100d, drought-tolerant, MP/Rajasthan); Vihar (early, 90d); KAK-2 (Kabuli, drought)",
        "low_rain_spacing": "30ร—15 cm slightly wider โ€” chickpea is drought-adapted and benefits from less competition",
        "waterlog_steps": [
            "Chickpea is highly intolerant โ€” drain within 12 hours",
            "Post-flood apply Ridomil Gold @ 2.5g/L to prevent Phytophthora collar rot",
            "Boron 0.2% foliar spray at pod fill stage for recovery",
        ],
        "sowing_window": "Rabi: Oct 15 โ€“ Nov 15 (North/Central India); Novโ€“Dec (South India)",
        "fertilizer": "Basal only: DAP 25 kg/acre + MOP 10 kg/acre (chickpea fixes N โ€” no Urea). Rhizobium seed treatment mandatory.",
        "intercrop": "Gram + Safflower (3:1); Gram + Wheat (1:6 ratio); Gram + Linseed",
        "source": "ICAR-IIPR Kanpur / AICRP Chickpea Advisory",
    },
    "moong": {
        "keywords": ["moong","mung","green gram","mungbean","moong dal","golden gram"],
        "seed_rate": "6โ€“8 kg/acre; seed treatment with Rhizobium + Thiram",
        "spacing": "30ร—10 cm; depth 3โ€“4 cm",
        "low_rain_variety": "SML-668, Pusa Vishal, IPM 02-3 (60-65d, drought-tolerant); Meha for dry areas",
        "low_rain_spacing": "30ร—15 cm; moisture conservation via mulching (paddy straw 2t/acre)",
        "waterlog_steps": [
            "Moong is very sensitive โ€” roots rot in 24โ€“36 hours of waterlogging",
            "Drain immediately and apply DAP 10 kg/acre foliar spray (2%) for recovery",
            "Scout for Cercospora leaf spot post-flood โ€” spray Mancozeb 2.5g/L",
        ],
        "sowing_window": "Kharif: Juneโ€“July; Zaid (summer): Marchโ€“April (irrigated); Rabi (South): Octโ€“Nov",
        "fertilizer": "Basal: DAP 15 kg/acre + MOP 10 kg/acre (pulses fix N; no top dressing urea needed)",
        "intercrop": "Moong + Sorghum (1:2); Moong + Sugarcane; Moong + Cotton (bund planting)",
        "source": "ICAR-IIPR Kanpur / AICRP Mungbean Advisory",
    },
    "urad": {
        "keywords": ["urad","black gram","urd","black lentil","urad dal","vigna mungo"],
        "seed_rate": "8โ€“10 kg/acre; Rhizobium + PSB seed treatment",
        "spacing": "30ร—10 cm; depth 3โ€“4 cm",
        "low_rain_variety": "LBG-752, WBU-108, PU-31 (65-70d, tolerates dry spells); Pant U-30",
        "low_rain_spacing": "30ร—15 cm; early sowing (June 15โ€“30) critical to avoid drought at pod fill",
        "waterlog_steps": [
            "Similar sensitivity to moong โ€” drain within 24 hours",
            "Apply Carbendazim 0.1% spray after drainage to prevent collar rot",
            "Foliar: 2% urea for vegetative recovery",
        ],
        "sowing_window": "Kharif: Juneโ€“July; Rabi (South India): Octโ€“Nov",
        "fertilizer": "Basal: DAP 15 kg/acre + MOP 10 kg/acre. No nitrogen top dressing.",
        "intercrop": "Urad + Sorghum (1:2); Urad + Maize; Urad + Sugarcane (bund)",
        "source": "ICAR-IIPR Kanpur / AICRP Black Gram Advisory",
    },
    "pigeonpea": {
        "keywords": ["pigeonpea","arhar","toor","tur","red gram","cajanus"],
        "seed_rate": "Short-duration: 5โ€“6 kg/acre; Long-duration: 4โ€“5 kg/acre",
        "spacing": "Short-duration hybrid: 60ร—20 cm; Long-duration: 90ร—30 cm",
        "low_rain_variety": "Maruti (ICPL-87, 150d), GTH-1 (short 120d, drought-tolerant); Pusa 992 for Central India",
        "low_rain_spacing": "90ร—30 cm โ€” arhar is drought-adapted, wider spacing reduces competition",
        "waterlog_steps": [
            "Arhar is moderately tolerant โ€” can handle brief flooding (2โ€“3 days)",
            "Drain and apply potash (MOP 10 kg/acre top dress) after 3+ days flooding",
            "Watch for Phytophthora stem blight post-flood",
        ],
        "sowing_window": "Kharif: Juneโ€“July (short-duration); Mayโ€“June (long-duration for Jan harvest)",
        "fertilizer": "Basal: DAP 25 kg/acre + MOP 15 kg/acre. Rhizobium seed treatment.",
        "intercrop": "Arhar + Soybean (1:4); Arhar + Sorghum (1:2); Arhar + Groundnut",
        "source": "ICAR-IIPR Kanpur / ICRISAT / AICRP Pigeonpea Advisory",
    },
    "lentil": {
        "keywords": ["lentil","masur","masoor","lentils","masur dal","red lentil"],
        "seed_rate": "12โ€“15 kg/acre (bold seed); 10โ€“12 kg/acre (small seed)",
        "spacing": "22.5ร—5โ€“7 cm; depth 3โ€“5 cm",
        "low_rain_variety": "K-75, Malika, DPL-62 (105โ€“110d, drought-tolerant); PL-406 for low rainfall",
        "low_rain_spacing": "22.5ร—10 cm with wider plant spacing; mulching recommended",
        "waterlog_steps": [
            "Lentil has zero waterlogging tolerance โ€” drain within 12 hours",
            "Apply lime (25 kg/acre) post-flood to correct soil pH disruption",
        ],
        "sowing_window": "Rabi: Oct 20 โ€“ Nov 20 (North India); Novโ€“Dec (East India)",
        "fertilizer": "Basal: DAP 20 kg/acre + MOP 10 kg/acre. Rhizobium seed treatment critical.",
        "intercrop": "Lentil + Mustard (3:1); Lentil + Wheat (1:6)",
        "source": "ICAR-IIPR Kanpur / AICRP Lentil Advisory",
    },
    "groundnut": {
        "keywords": ["groundnut","peanut","moongphali","mungfali","arachis","singdana","shengdana"],
        "seed_rate": "Bold seed: 50โ€“55 kg/acre; Small seed: 40โ€“45 kg/acre (shelled)",
        "spacing": "30ร—10 cm (bunch type); 45ร—15 cm (spreading type); depth 5 cm",
        "low_rain_variety": "TG-37A (105d, drought-tolerant, Gujarat/Rajasthan); JL-24 (100d); ICGV-91114 for dry areas",
        "low_rain_spacing": "30ร—15 cm for bunch type; conserve moisture with in-situ rainwater harvesting",
        "waterlog_steps": [
            "Drain immediately โ€” pod zone waterlogging causes aflatoxin + collar rot",
            "Open furrows between rows for drainage before crop establishment",
            "Post-flood: apply Mancozeb 2g/L for collar rot prevention",
            "DO NOT irrigate for 7 days post-drainage",
        ],
        "sowing_window": "Kharif: June 15 โ€“ July 15; Rabi (South): Octโ€“Nov; Summer: Febโ€“March",
        "fertilizer": "Basal: SSP 100 kg/acre (sulphur critical for oil quality) + Gypsum 100 kg/acre at pegging.",
        "intercrop": "Groundnut + Maize (4:1); Groundnut + Castor (4:2); Groundnut + Pigeonpea",
        "source": "ICAR-DGR Junagadh / AICRP Groundnut Advisory",
    },
    "tomato": {
        "keywords": ["tomato","tamatar","tamater","lycopersicon","lal tamatar"],
        "seed_rate": "100โ€“150 g/acre (nursery); transplant 25-day seedlings",
        "spacing": "60ร—45 cm (staked); 75ร—60 cm (unstaked); transplant in evening/cloudy weather",
        "low_rain_variety": "Arka Rakshak (disease-tolerant + drought-adapted); Pusa Hybrid-4; Naveen F1 for stress conditions",
        "low_rain_spacing": "60ร—60 cm โ€” wider spacing + drip irrigation; mulching with black polythene mandatory",
        "waterlog_steps": [
            "Tomato is very sensitive โ€” drain immediately",
            "Spray Metalaxyl+Mancozeb 2.5g/L preventively for Phytophthora",
            "Remove and destroy plants showing yellowing/wilting at crown",
            "Apply Trichoderma viride @ 5g/L as root drench after drainage",
        ],
        "sowing_window": "Rabi: Augโ€“Sep (nursery) for Octโ€“Nov transplant; Summer: Decโ€“Jan for Febโ€“March transplant",
        "fertilizer": "Basal: FYM 4t/acre + DAP 50 kg + MOP 25 kg. Top dress: Urea 25 kg at 30d + 25 kg at flowering.",
        "intercrop": "Tomato + Basil (repels aphids); Tomato + Marigold (border trap crop for nematodes)",
        "source": "ICAR-IIHR Bangalore / AICRP Vegetables Advisory",
    },
    "potato": {
        "keywords": ["potato","aloo","alu","batata","solanum tuberosum"],
        "seed_rate": "1,000โ€“1,200 kg seed tubers/acre (30โ€“40 g each, 2โ€“3 eyes)",
        "spacing": "60ร—20 cm (ridge planting); earthing up at 20 days and 40 days",
        "low_rain_variety": "Kufri Surya, Kufri Pushkar (heat+drought-tolerant); Kufri Jyoti for rainfed",
        "low_rain_spacing": "Same spacing โ€” potato needs consistent moisture; mulch with paddy straw 2t/acre for moisture conservation",
        "waterlog_steps": [
            "Potato tubers rot within 24โ€“36 hours of waterlogging",
            "Drain immediately via inter-row furrows",
            "Apply Mancozeb 2.5g/L preventively for late blight post-flood",
            "Avoid earthing up for 10 days post-drainage โ€” roots too weak",
            "Check for soft rot (Erwinia) โ€” no chemical fix; remove affected plants",
        ],
        "sowing_window": "Rabi: Octโ€“Nov (North India plains); Kharif: Juneโ€“July (hills)",
        "fertilizer": "High feeder: DAP 75 kg/acre + MOP 50 kg/acre (basal) + Urea 50 kg at earthing up",
        "intercrop": "Potato + Onion; Potato + Garlic; Potato + Fenugreek (winter)",
        "source": "ICAR-CPRI Shimla / AICRP Potato Advisory",
    },
    "onion": {
        "keywords": ["onion","pyaz","piaz","kanda","dungli","allium cepa"],
        "seed_rate": "1โ€“1.5 kg/acre (kharif); 0.75โ€“1 kg/acre (rabi); transplant 6โ€“8 week nursery",
        "spacing": "15ร—10 cm (transplanted); 10ร—7.5 cm (direct seeded)",
        "low_rain_variety": "Arka Kalyan, N-53 (Kharif, dry-tolerant); Agrifound Light Red, Phule Safed (Rabi)",
        "low_rain_spacing": "Same spacing; critical: DO NOT let field dry completely at bulb initiation stage",
        "waterlog_steps": [
            "Onion bulb rots immediately in waterlogged soil",
            "Drain and spray Mancozeb 2.5g/L for purple blotch prevention",
            "Apply Copper Oxychloride 0.3% if bacterial soft rot suspected",
            "Remove affected plants to prevent spread",
        ],
        "sowing_window": "Kharif: Mayโ€“June (nursery) โ†’ Julyโ€“Aug transplant; Rabi: Octโ€“Nov (nursery) โ†’ Novโ€“Dec transplant",
        "fertilizer": "Basal: FYM 3t/acre + DAP 50 kg + MOP 25 kg. Top dress: Urea 25 kg at 30d + 25 kg at bulb initiation.",
        "intercrop": "Onion + Carrot (1:3); Onion + Coriander; Onion + Garlic",
        "source": "ICAR-DOGR Pune / AICRP Vegetables Advisory",
    },
    "chilli": {
        "keywords": ["chilli","chili","mirchi","lal mirch","shimla mirch","capsicum","pepper"],
        "seed_rate": "150โ€“200 g/acre (nursery raised); transplant 5โ€“6 week seedlings",
        "spacing": "60ร—45 cm; transplant in shaded/cloudy weather to reduce transplant shock",
        "low_rain_variety": "LCA-206, Pusa Jwala (drought-adapted); Arka Lohit (disease + drought tolerant)",
        "low_rain_spacing": "60ร—60 cm + black polythene mulch (saves 40% water, controls weeds)",
        "waterlog_steps": [
            "Chilli root system is very shallow โ€” waterlogging causes Phytophthora wilt within 24 hours",
            "Drain via broad bed furrow system immediately",
            "Drench with Metalaxyl 2g/L at root zone after drainage",
            "Remove and destroy wilted plants โ€” no chemical recovery once wilt sets in",
        ],
        "sowing_window": "Kharif: Mayโ€“June (nursery) โ†’ Juneโ€“July transplant; Rabi: Augโ€“Sep nursery โ†’ Sepโ€“Oct transplant",
        "fertilizer": "Basal: FYM 3t/acre + DAP 50 kg + MOP 25 kg. Top dress: Urea 25 kg at 30d + 25 kg at flowering + SOP 5g/L foliar at fruiting.",
        "intercrop": "Chilli + Coriander (3:3); Chilli + Onion; Chilli + Maize (border)",
        "source": "ICAR-IIHR Bangalore / AICRP Chilli Advisory",
    },
    "brinjal": {
        "keywords": ["brinjal","eggplant","baingan","baigan","aubergine","solanum melongena"],
        "seed_rate": "150โ€“200 g/acre (nursery); transplant at 4โ€“5 leaf stage",
        "spacing": "60ร—60 cm (large varieties); 45ร—45 cm (small varieties)",
        "low_rain_variety": "Pusa Purple Long, Arka Nidhi (drought-adapted); Azad Kranti for dry areas",
        "low_rain_spacing": "75ร—60 cm with mulching; drip irrigation preferred",
        "waterlog_steps": [
            "Drain immediately โ€” brinjal tolerates brief waterlogging better than chilli but still vulnerable",
            "Apply Mancozeb 2.5g/L after drainage for fungal disease prevention",
            "Remove yellowed leaves post-flood to prevent secondary infections",
        ],
        "sowing_window": "Year-round in South India; Kharif: Juneโ€“July; Rabi: Sepโ€“Oct; Summer: Janโ€“Feb",
        "fertilizer": "Basal: FYM 3t/acre + DAP 50 kg + MOP 25 kg. Top dress: Urea 25 kg at 30d + 25 kg at fruiting.",
        "intercrop": "Brinjal + Cowpea; Brinjal + Marigold (border); Brinjal + Coriander",
        "source": "ICAR-IIHR Bangalore / AICRP Vegetables Advisory",
    },
    "sugarcane": {
        "keywords": ["sugarcane","ganna","ikshu","ikh","sugar cane","cane"],
        "seed_rate": "2,500โ€“3,000 kg setts/acre (3-bud setts; 6โ€“8 quintals viable setts)",
        "spacing": "90 cm row spacing (flat); 75 cm (paired row system for ratoon); trench depth 20โ€“25 cm",
        "low_rain_variety": "CoJ-64, CoLk-94184 (drought-tolerant); Co-0238 for normal; Co-86032 for waterlogging-prone",
        "low_rain_spacing": "Flat planting at 90 cm; sub-soil moisture conservation with 5 cm mulch in furrow",
        "waterlog_steps": [
            "Sugarcane tolerates short flooding (3โ€“5 days) but NOT chronic waterlogging",
            "Drain via main and lateral field drains immediately",
            "Apply urea 15 kg/acre foliar (1% solution) for nitrogen recovery",
            "For ratoon crop: apply potash to strengthen regrowth",
            "Watch for red rot disease post-flood (Colletotrichum): spray Carbendazim 0.1%",
        ],
        "sowing_window": "Spring: Febโ€“March (main season, 12 months); Autumn: Sepโ€“Oct (11 months); Subtropical: Octโ€“Nov",
        "fertilizer": "Heavy feeder: Basal FYM 6t/acre + DAP 50 kg. Split Urea: 50 kg at 30d + 50 kg at 90d + 50 kg at 150d. MOP 50 kg at earthing up.",
        "intercrop": "Sugarcane + Potato (ratoon + Rabi); Sugarcane + Onion; Sugarcane + Garlic (bund)",
        "source": "ICAR-IISR Lucknow / AICRP Sugarcane Advisory",
    },
    "pearl_millet": {
        "keywords": ["pearl millet","bajra","bajri","sajje","cumbu","sajja","bajura","bajre"],
        "seed_rate": "1.5โ€“2 kg/acre (hybrid); 3โ€“4 kg/acre (composite open-pollinated)",
        "spacing": "45ร—15 cm (hybrid); 45ร—10 cm (composite); depth 2โ€“3 cm",
        "low_rain_variety": "86M86, Pioneer 86M88 (75โ€“80d hybrid, extreme drought-tolerant); Raj 171, ICMH 356 for arid zones",
        "low_rain_spacing": "45ร—20 cm or even 60ร—20 cm โ€” bajra is drought-adapted; wider spacing in very dry conditions",
        "waterlog_steps": [
            "Pearl millet tolerates brief waterlogging (2โ€“3 days) better than other cereals",
            "Drain excess water via field furrows",
            "Downy mildew risk increases post-flood โ€” apply Metalaxyl+Mancozeb 2.5g/L",
        ],
        "sowing_window": "Kharif: Juneโ€“July (monsoon onset); must not sow late โ€” maturity hits before post-monsoon drought",
        "fertilizer": "Basal: DAP 25 kg/acre. Top dress: Urea 25 kg at knee-high stage (30d)",
        "intercrop": "Bajra + Cowpea (3:1); Bajra + Moong (3:1); Bajra + Cluster bean",
        "source": "ICAR-ICMR / AICRP Pearl Millet Advisory",
    },
    "sorghum": {
        "keywords": ["sorghum","jowar","juar","jawar","jwari","cholam","milo"],
        "seed_rate": "4โ€“5 kg/acre (hybrid); 6โ€“8 kg/acre (local variety); depth 3โ€“5 cm",
        "spacing": "45ร—15 cm (hybrid Kharif); 30ร—10 cm (Rabi โ€” denser)",
        "low_rain_variety": "CSH-16, CSV-23 (90d, drought-tolerant Kharif); M-35-1, DSV-4 for Rabi",
        "low_rain_spacing": "45ร—20 cm โ€” sorghum is highly drought-tolerant; deeper sowing (5 cm) helps tap subsoil moisture",
        "waterlog_steps": [
            "Jowar tolerates 2โ€“3 days flooding at vegetative stage",
            "Drain and apply urea 1% foliar spray",
            "Watch for charcoal rot post-stress โ€” no chemical fix; improve drainage for next crop",
        ],
        "sowing_window": "Kharif: Juneโ€“July; Rabi: Sepโ€“Oct (South India); Rabbi: Oct (Marathwada, Vidarbha)",
        "fertilizer": "Basal: DAP 30 kg/acre. Top dress: Urea 25 kg at knee-high (30d)",
        "intercrop": "Jowar + Moong (2:1); Jowar + Cowpea; Jowar + Pigeonpea",
        "source": "ICAR-IIMR Hyderabad / AICRP Sorghum Advisory",
    },
    "okra": {
        "keywords": ["okra","bhindi","lady finger","bhendo","vendaikkai","bamia"],
        "seed_rate": "4โ€“5 kg/acre (direct sown); seed treatment with Imidacloprid 70% WS @ 7g/kg",
        "spacing": "45ร—30 cm; depth 2โ€“3 cm; thin to 1 plant/hill at 2-leaf stage",
        "low_rain_variety": "Pusa A-4, Arka Anamika (short 50-55 days, tolerates mild drought); VRO-6",
        "low_rain_spacing": "45ร—45 cm with mulching โ€” okra is relatively drought-tolerant once established",
        "waterlog_steps": [
            "Okra roots rot within 24 hours of waterlogging",
            "Drain immediately; apply Ridomil Gold @ 2g/L for root rot prevention",
            "Remove yellowed plants; spray Mancozeb 2.5g/L for fungal prevention",
        ],
        "sowing_window": "Kharif: Juneโ€“July; Summer: Febโ€“March; Rabi (South): Octโ€“Nov",
        "fertilizer": "Basal: FYM 2t/acre + DAP 25 kg + MOP 15 kg. Top dress: Urea 25 kg at first picking (45d).",
        "intercrop": "Okra + Cowpea; Okra + Maize (border row)",
        "source": "ICAR-IIHR Bangalore / AICRP Vegetable Advisory",
    },
    "mango": {
        "keywords": ["mango","aam","keri","kairi","mangifera","alphonso","dashehari","langra"],
        "seed_rate": "Grafted seedlings: 40โ€“45 plants/acre (10ร—10 m spacing); High density: 200 plants/acre (5ร—4 m)",
        "spacing": "10ร—10 m (standard); 5ร—4 m (high density planting); Pit size: 1ร—1ร—1 m",
        "low_rain_variety": "Totapuri, Neelam (drought-tolerant, South); Mallika, Amrapali for water-limited areas",
        "low_rain_spacing": "Standard spacing; basin irrigation + mulching with dry grass/leaves at tree base",
        "waterlog_steps": [
            "Mango roots tolerate brief flooding (3โ€“5 days) better than annual crops",
            "Ensure good drainage around tree collar to prevent collar rot",
            "Post-flood: apply Carbendazim 0.1% drench around tree base",
            "Remove accumulated silt/soil from tree collar after flooding recedes",
        ],
        "sowing_window": "Planting: Julyโ€“Aug (monsoon) or Febโ€“March (spring). Flowering: Novโ€“Jan. Fruiting: Marchโ€“June",
        "fertilizer": "Per tree per year: Urea 500g + SSP 300g + MOP 200g (split March + September). FYM 25 kg/tree/year.",
        "intercrop": "Young mango orchards (1โ€“4 years): Moong, Cowpea, Groundnut as inter-crops between rows",
        "source": "ICAR-CISH Lucknow / AICRP Mango Advisory",
    },
    "banana": {
        "keywords": ["banana","kela","plantain","vaazhai","bale","keli","mouz"],
        "seed_rate": "400โ€“450 suckers/acre (tissue culture: 500โ€“550/acre); sword suckers preferred",
        "spacing": "1.8ร—1.5 m (Grand Naine, TC); 1.5ร—1.5 m (Robusta); 2ร—2 m (tall varieties)",
        "low_rain_variety": "Dwarf Cavendish, G9 (TC, 11โ€“12 months, moderate water); Poovan for low-water Kerala/TN",
        "low_rain_spacing": "Same spacing; drip irrigation mandatory for low-rainfall areas (8โ€“10 L/plant/day)",
        "waterlog_steps": [
            "Banana pseudostem rots at base under prolonged waterlogging (>3 days)",
            "Drain using deep furrows between rows",
            "Spray Mancozeb 2.5g/L for Sigatoka leaf spot (intensifies post-flood)",
            "Apply potash (MOP 50g/plant foliar as KNO3 1%) for structural recovery",
        ],
        "sowing_window": "Planting: Juneโ€“July (Kharif) or Febโ€“March (Spring); tissue culture plants year-round",
        "fertilizer": "Heavy feeder: 100g N + 35g P + 300g K per plant per year, split into 4 applications",
        "intercrop": "Young banana: Cowpea, French bean, Turmeric (shade-tolerant crops)",
        "source": "ICAR-NRC Banana Trichy / AICRP Banana Advisory",
    },
    "cauliflower": {
        "keywords": ["cauliflower","phool gobhi","gobhi","phulagobhi","broccoli","cabbage","band gobhi"],
        "seed_rate": "200โ€“250 g/acre; transplant 25โ€“30 day nursery seedlings",
        "spacing": "45ร—30 cm (early season); 60ร—45 cm (main season); 60ร—60 cm (late season)",
        "low_rain_variety": "Pusa Sharad, Pusa Synthetic (main season); Snowball-16 for dryer conditions",
        "low_rain_spacing": "Same spacing; mulching critical โ€” plastic mulch reduces water need by 35%",
        "waterlog_steps": [
            "Cauliflower roots are shallow โ€” drain within 12โ€“24 hours",
            "Apply Trichoderma viride @ 5g/L root drench after drainage",
            "Spray Mancozeb 2.5g/L for damping-off prevention",
            "Curds exposed to excess moisture develop brown discoloration โ€” harvest early if flood imminent",
        ],
        "sowing_window": "Early: Junโ€“Jul nursery โ†’ Augโ€“Sep transplant; Main: Augโ€“Sep โ†’ Sepโ€“Oct transplant; Late: Octโ€“Nov โ†’ Novโ€“Dec",
        "fertilizer": "Basal: FYM 4t/acre + DAP 50 kg + MOP 25 kg. Top dress: Urea 25 kg at 20d + 25 kg at curd initiation. Boron 0.2% foliar for hollow stem prevention.",
        "intercrop": "Cauliflower + Onion; Cauliflower + Spinach; Cauliflower + Garlic",
        "source": "ICAR-IIHR Bangalore / AICRP Vegetables Advisory",
    },
}

# Build keyword lookup map for agronomy crop detection
_AGRO_CROP_KW_MAP: dict[str, str] = {}
for _agk, _agv in _ICAR_AGRONOMY_CARDS.items():
    for _kw in _agv["keywords"]:
        _AGRO_CROP_KW_MAP[_kw] = _agk


def detect_agronomy_query(query: str) -> bool:
    """Return True if this looks like an agronomy/field-management query."""
    _AGRO_TRIGGERS = {
        "seed rate","beej dar","spacing","doori","kab lagaye","kab boya",
        "when to sow","when to plant","row spacing","plant spacing",
        "waterlogged","waterlog","paani bhar","jal bhar","drain",
        "khet mein paani","monsoon delay","monsoon late","baarish nahi",
        "intercrop","mixed crop","earthing","thinning","mulch",
        "variety select","kaunsi variety","which variety","beej kaun sa",
        "seed rate kam","spacing badhaun","doori kam","transplant",
        "sowing depth","bawai","bawai kab","lagane ka samay",
    }
    q = query.lower()
    return any(t in q for t in _AGRO_TRIGGERS)


def build_agronomy_context(query: str, crop: str | None = None) -> str:
    """
    Inject ICAR agronomy card as priority context for field management queries.
    Detects crop from query or detected_crop. Returns empty string if no card found.
    """
    q_lower = query.lower()
    crop_key = None
    # Detect crop from query keywords (longest match first)
    for kw in sorted(_AGRO_CROP_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            crop_key = _AGRO_CROP_KW_MAP[kw]
            break
    # Fallback to detected crop
    if not crop_key and crop:
        cl = crop.lower()
        for kw, ck in _AGRO_CROP_KW_MAP.items():
            if kw in cl:
                crop_key = ck
                break

    if not crop_key:
        return (
            "โ”โ”โ” PRIORITY CONTEXT โ€” ICAR AGRONOMY GUIDELINES โ”โ”โ”\n"
            "No crop-specific card available. General principles:\n"
            "โ€ข Low rainfall: wider row spacing, shorter-duration varieties, early sowing\n"
            "โ€ข Waterlogged field: drain first โ†’ withhold fertilizer โ†’ check roots\n"
            "โ€ข Delayed monsoon (>15 days): switch to short-duration varieties (60-90 days)\n"
            "Ask farmer: which crop + what specific problem (waterlogging/spacing/variety)?\n"
            "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        )

    card = _ICAR_AGRONOMY_CARDS[crop_key]
    return (
        f"โ”โ”โ” PRIORITY CONTEXT โ€” ICAR AGRONOMY DATA FOR {crop_key.upper()} โ”โ”โ”\n"
        f"Seed rate: {card['seed_rate']}\n"
        f"Optimal spacing: {card['spacing']}\n"
        f"Low-rainfall variety: {card['low_rain_variety']}\n"
        f"Low-rainfall spacing adjustment: {card['low_rain_spacing']}\n"
        f"Waterlogging response: {' | '.join(card['waterlog_steps'][:3])}\n"
        f"Sowing window: {card['sowing_window']}\n"
        f"Fertilizer schedule: {card['fertilizer']}\n"
        f"Intercropping options: {card['intercrop']}\n"
        f"Source: {card['source']}\n"
        f"โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        f"Use these EXACT numbers. Ground your answer in these ICAR-validated figures.\n"
    )


# โ”€โ”€ ICAR Crop Decision Profiles (for crop_selection queries) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Purpose: inject ACTUAL agronomic + economic data so LLM gives
# comparison tables with REAL numbers instead of vague textbook answers.
# Data: ICAR-AICSIP, NABARD Crop-wise Reports, State Agri dept handbooks.
# Yield range = rainfed / irrigated. MSP or indicative market price.

_CROP_DECISION_DATA: dict[str, dict] = {
    "soybean": {
        "keywords": ["soybean","soya","bhat"],
        "season": "Kharif (Juneโ€“July sowing)",
        "duration_days": "90โ€“100d",
        "rainfall_needed_mm": "450โ€“700mm",
        "drought_tolerance": "Medium (sensitive at flowering)",
        "waterlog_tolerance": "LOW โ€” drowns in 48h",
        "yield_rainfed_q_acre": "6โ€“10 q/acre",
        "yield_irrigated_q_acre": "10โ€“14 q/acre",
        "soil_best": "Black cotton (Vertisol) / clay loam",
        "soil_avoid": "Sandy / waterlogged / acidic (pH <6)",
        "water_risk": "HIGH if monsoon fails after July 15 (late sowing = yield crash)",
        "profit_potential": "MEDIUM โ€” stable MSP ~Rs 4,600/q; reliable procurement in MP/Maharashtra",
        "input_cost_per_acre": "Rs 8,000โ€“12,000",
        "key_risk": "Yellow mosaic virus (whitefly-spread), Kharif price crash if bumper nationally",
        "suitable_states": "Madhya Pradesh, Maharashtra, Rajasthan, Chhattisgarh",
    },
    "cotton": {
        "keywords": ["cotton","kapas","narma"],
        "season": "Kharif (Mayโ€“June sowing, pre-monsoon preferred)",
        "duration_days": "150โ€“180d (Bt hybrid)",
        "rainfall_needed_mm": "500โ€“800mm (well-distributed)",
        "drought_tolerance": "Medium-High (deep roots)",
        "waterlog_tolerance": "VERY LOW โ€” drain within 12โ€“24h",
        "yield_rainfed_q_acre": "6โ€“10 q lint/acre",
        "yield_irrigated_q_acre": "12โ€“18 q lint/acre",
        "soil_best": "Black cotton (Vertisol) / deep sandy loam",
        "soil_avoid": "Shallow soils, waterlogged fields, saline soils",
        "water_risk": "VERY HIGH โ€” 6-month crop; prolonged drought = full failure",
        "profit_potential": "HIGH โ€” Rs 6,000โ€“8,000/q; long-duration crop locks capital 6 months",
        "input_cost_per_acre": "Rs 15,000โ€“25,000 (high Bt seed + insecticide cost)",
        "key_risk": "Pink bollworm (major), bollworm spray cost, delayed sowing reduces yield 20%",
        "suitable_states": "Gujarat, Maharashtra, Telangana, Andhra Pradesh, Punjab, Haryana",
    },
    "pearl_millet_bajra": {
        "keywords": ["bajra","pearl millet","bajri"],
        "season": "Kharif (Juneโ€“July sowing)",
        "duration_days": "75โ€“85d (hybrid)",
        "rainfall_needed_mm": "200โ€“350mm (extremely drought-tolerant)",
        "drought_tolerance": "VERY HIGH โ€” crop of choice for arid zones",
        "waterlog_tolerance": "Medium (better than soybean)",
        "yield_rainfed_q_acre": "8โ€“12 q/acre (hybrid)",
        "yield_irrigated_q_acre": "14โ€“18 q/acre",
        "soil_best": "Sandy loam / light soils; also grows in poor soils",
        "soil_avoid": "Waterlogged / highly acidic soils; โ›” BLACK COTTON SOIL (Vertisol) โ€” Bajra is NOT suitable for MP/Maharashtra black soil districts like Barwani, Khargone, Vidarbha. Use Soybean or Gram instead.",
        "water_risk": "LOW โ€” survives on 200mm; ideal for delayed/failed monsoon",
        "profit_potential": "MEDIUM-LOW โ€” Rs 2,500โ€“3,500/q; less market prestige but very reliable",
        "input_cost_per_acre": "Rs 4,000โ€“7,000 (lowest input crop)",
        "key_risk": "Downy mildew (use resistant hybrid), price instability in surplus years",
        "suitable_states": "Rajasthan, Gujarat, Haryana, Maharashtra (drought areas)",
    },
    "moong": {
        "keywords": ["moong","mung","green gram"],
        "season": "Kharif (Juneโ€“July) OR Zaid summer (Marchโ€“April, irrigated)",
        "duration_days": "60โ€“65d (short โ€” ideal for delayed monsoon)",
        "rainfall_needed_mm": "250โ€“400mm",
        "drought_tolerance": "High (short duration escapes drought)",
        "waterlog_tolerance": "VERY LOW โ€” roots rot in 24โ€“36h",
        "yield_rainfed_q_acre": "3โ€“5 q/acre",
        "yield_irrigated_q_acre": "6โ€“8 q/acre",
        "soil_best": "Sandy loam / well-drained alluvial; pH 6.5โ€“7.5",
        "soil_avoid": "Clay/waterlogged soils",
        "water_risk": "LOW โ€” short 65-day crop; sow even in late July and harvest before October drought",
        "profit_potential": "HIGH per day โ€” Rs 7,000โ€“9,000/q; quick 65-day turnaround",
        "input_cost_per_acre": "Rs 4,000โ€“6,000",
        "key_risk": "Yellow mosaic virus, pod borer at grain fill; very low input cost = good safety net",
        "suitable_states": "All India; especially UP, Bihar, MP, AP, Rajasthan",
    },
    "sorghum_jowar": {
        "keywords": ["jowar","sorghum","juar"],
        "season": "Kharif (Juneโ€“July) OR Rabi (Sepโ€“Oct, South India)",
        "duration_days": "90โ€“110d (hybrid Kharif); 120d (Rabi)",
        "rainfall_needed_mm": "300โ€“500mm",
        "drought_tolerance": "HIGH โ€” very deep roots, withstands dry spells",
        "waterlog_tolerance": "Medium",
        "yield_rainfed_q_acre": "8โ€“12 q grain + 15โ€“20 q fodder/acre",
        "yield_irrigated_q_acre": "14โ€“18 q grain/acre",
        "soil_best": "Black cotton / medium deep soils",
        "soil_avoid": "Waterlogged / very sandy soils",
        "water_risk": "LOW-MEDIUM โ€” drought-tolerant, but charcoal rot under severe stress",
        "profit_potential": "MEDIUM โ€” dual purpose (grain + fodder); stable local demand",
        "input_cost_per_acre": "Rs 5,000โ€“8,000",
        "key_risk": "Stem borer (major), shoot fly at seedling stage; grain mold in rainy harvest",
        "suitable_states": "Maharashtra (Marathwada/Vidarbha), Karnataka, AP, MP, Rajasthan",
    },
    "wheat": {
        "keywords": ["wheat","gehu","gehun"],
        "season": "Rabi (Octโ€“Nov sowing; Marchโ€“April harvest)",
        "duration_days": "110โ€“130d",
        "rainfall_needed_mm": "Irrigated (4โ€“6 irrigations); rainfed 350โ€“450mm",
        "drought_tolerance": "Low-Medium (needs assured water)",
        "waterlog_tolerance": "LOW at sowing; moderate at tillering",
        "yield_rainfed_q_acre": "8โ€“12 q/acre",
        "yield_irrigated_q_acre": "18โ€“25 q/acre",
        "soil_best": "Alluvial (loam) โ€” Punjab, Haryana, UP, Bihar",
        "soil_avoid": "Sandy / acidic soils",
        "water_risk": "LOW with irrigation; HIGH in rainfed areas (terminal heat stress in March)",
        "profit_potential": "HIGH โ€” guaranteed MSP Rs 2,275/q (2024-25); easy procurement via FCI",
        "input_cost_per_acre": "Rs 8,000โ€“14,000",
        "key_risk": "Rust disease (yellow/brown/stem), heat wave at grain fill; late sowing = 1-2 q/acre loss per week delay",
        "suitable_states": "Punjab, Haryana, UP, MP, Bihar, Rajasthan",
    },
    "mustard": {
        "keywords": ["mustard","sarson","rai","rapeseed"],
        "season": "Rabi (Oct 1โ€“25 optimal sowing; harvest Febโ€“March)",
        "duration_days": "110โ€“130d",
        "rainfall_needed_mm": "250โ€“400mm (low water need โ€” 2-3 irrigations only)",
        "drought_tolerance": "HIGH โ€” short-duration options available",
        "waterlog_tolerance": "VERY LOW โ€” drain within 12h",
        "yield_rainfed_q_acre": "4โ€“6 q/acre",
        "yield_irrigated_q_acre": "7โ€“10 q/acre",
        "soil_best": "Sandy loam / alluvial; Rajasthan, Haryana, UP",
        "soil_avoid": "Waterlogged / heavy clay",
        "water_risk": "LOW โ€” good option for water-scarce Rabi season",
        "profit_potential": "HIGH โ€” Rs 5,000โ€“6,500/q; strong demand for oil; MSP supported",
        "input_cost_per_acre": "Rs 4,000โ€“7,000",
        "key_risk": "Aphid (Febโ€“March), white rust, Sclerotinia; early sowing critical",
        "suitable_states": "Rajasthan, Haryana, UP, MP, Bihar",
    },
    "gram_chickpea": {
        "keywords": ["gram","chana","chickpea","bengal gram"],
        "season": "Rabi (Oct 15โ€“Nov 15 sowing; March harvest)",
        "duration_days": "90โ€“110d (desi); 100โ€“120d (kabuli)",
        "rainfall_needed_mm": "Rainfed 250โ€“400mm OR residual soil moisture only (no irrigation needed)",
        "drought_tolerance": "VERY HIGH โ€” tap root, survives on residual moisture",
        "waterlog_tolerance": "VERY LOW",
        "yield_rainfed_q_acre": "5โ€“8 q/acre (desi)",
        "yield_irrigated_q_acre": "8โ€“12 q/acre (kabuli)",
        "soil_best": "Black cotton / medium loam; pH 6โ€“8",
        "soil_avoid": "Acidic / waterlogged soils",
        "water_risk": "VERY LOW โ€” adapts to low water; excellent for residual moisture fields",
        "profit_potential": "HIGH โ€” Rs 5,200โ€“6,000/q; nitrogen-fixing (saves Rs 1,500/acre in next crop fertilizer)",
        "input_cost_per_acre": "Rs 4,000โ€“6,000 (lowest input cost after pearl millet)",
        "key_risk": "Helicoverpa pod borer (critical), wilt (soil-borne), rust in humid areas",
        "suitable_states": "MP, Rajasthan, Maharashtra, UP, Karnataka, AP",
    },
    "rice_paddy": {
        "keywords": ["rice","paddy","dhan","dhaan","chawal"],
        "season": "Kharif (Juneโ€“July sowing; harvest Octโ€“Nov)",
        "duration_days": "110โ€“135d depending on variety",
        "rainfall_needed_mm": "1,000โ€“1,500mm OR irrigated with 3โ€“4 irrigations/week in dry spells",
        "drought_tolerance": "LOW โ€” requires standing water at tillering to heading",
        "waterlog_tolerance": "HIGH โ€” paddy thrives in standing water (2โ€“5 cm depth)",
        "yield_rainfed_q_acre": "10โ€“15 q/acre (direct-seeded, monsoon)",
        "yield_irrigated_q_acre": "20โ€“30 q/acre (transplanted, irrigated)",
        "soil_best": "Clayey / loam with good water retention; paddy fields with bund",
        "soil_avoid": "Sandy soils (lose water too fast), highly acidic (pH < 5.5)",
        "water_risk": "MEDIUM for eastern states (rainfed); LOW for Punjab/Haryana (canal-irrigated)",
        "profit_potential": "MEDIUM โ€” guaranteed MSP Rs 2,300/q; major staple; assured procurement but low margin",
        "input_cost_per_acre": "Rs 8,000โ€“16,000 (transplanted) / Rs 5,000โ€“8,000 (direct-seeded)",
        "key_risk": "Blast disease (cloudy/rainy), BPH (brown plant hopper), Sheath blight, Flood damage (deep water variety needed)",
        "suitable_states": "West Bengal, UP, Bihar, Odisha, Andhra Pradesh, Tamil Nadu, Chhattisgarh, Punjab (DSR)",
    },
    "tomato": {
        "keywords": ["tomato","tamatar","tamater"],
        "season": "Rabi (Augโ€“Sep nursery โ†’ Octโ€“Nov transplant) OR Summer (Decโ€“Jan โ†’ Febโ€“March)",
        "duration_days": "70โ€“90d (from transplant)",
        "rainfall_needed_mm": "400โ€“600mm well-distributed; drip preferred",
        "drought_tolerance": "LOW โ€” highly sensitive to drought at flowering and fruit set",
        "waterlog_tolerance": "VERY LOW โ€” Phytophthora wilt within 24โ€“48h of flooding",
        "yield_rainfed_q_acre": "40โ€“60 q/acre",
        "yield_irrigated_q_acre": "80โ€“120 q/acre (hybrid, drip + fertigation)",
        "soil_best": "Sandy loam / loam with good drainage; slightly acidic (pH 6โ€“7)",
        "soil_avoid": "Heavy clay / waterlogged / very alkaline soils",
        "water_risk": "HIGH โ€” needs consistent moisture; drought at fruit set = blossom drop; rain at harvest = fruit cracking",
        "profit_potential": "HIGH but VOLATILE โ€” Rs 8โ€“40/kg (huge price swings); can give Rs 50,000+/acre in good season OR near-zero in glut",
        "input_cost_per_acre": "Rs 20,000โ€“35,000 (nursery + transplanting + staking + spray)",
        "key_risk": "Extreme price volatility, Tomato leaf curl virus (TLCuV), Early blight, Late blight in humid weather",
        "suitable_states": "Karnataka, Maharashtra, AP, MP, UP, Bihar, HP (hills)",
    },
    "maize_corn": {
        "keywords": ["maize","corn","makka","makki","bhutta"],
        "season": "Kharif (Juneโ€“July) OR Rabi (Octโ€“Nov, South India) OR Spring (Febโ€“March)",
        "duration_days": "90โ€“110d (hybrid Kharif); 120d (Rabi)",
        "rainfall_needed_mm": "500โ€“800mm well-distributed",
        "drought_tolerance": "Medium (sensitive at tasseling/silking โ€” 7-day window)",
        "waterlog_tolerance": "LOW โ€” tassel stage very sensitive; waterlogging causes barren ears",
        "yield_rainfed_q_acre": "10โ€“16 q/acre (hybrid)",
        "yield_irrigated_q_acre": "20โ€“28 q/acre",
        "soil_best": "Well-drained loam / sandy loam; pH 6โ€“7.5",
        "soil_avoid": "Waterlogged / very heavy clay / highly acidic",
        "water_risk": "HIGH at silking โ€” single week of drought causes 50-70% yield loss; otherwise medium risk",
        "profit_potential": "MEDIUM โ€” Rs 1,800โ€“2,500/q; poultry feed demand is stable; baby corn premium (Rs 4,000+/q)",
        "input_cost_per_acre": "Rs 8,000โ€“14,000",
        "key_risk": "Fall armyworm (new invasive โ€” major threat), stem borer, charcoal rot in drought; not MSP-supported everywhere",
        "suitable_states": "Karnataka, AP, Bihar, MP, UP, Maharashtra, Telangana",
    },
    "sugarcane_ganna": {
        "keywords": ["sugarcane","ganna","ikshu","ikh"],
        "season": "Spring: Febโ€“March planting (main, 12 months); Autumn: Sepโ€“Oct (11 months)",
        "duration_days": "10โ€“14 months (spring); 8โ€“10 months (autumn ratoon)",
        "rainfall_needed_mm": "1,500โ€“2,500mm OR 50โ€“60 irrigations (North India)",
        "drought_tolerance": "LOW โ€” heavy water consumer; needs regular supply",
        "waterlog_tolerance": "Medium โ€” tolerates 3โ€“5 days flooding; NOT chronic waterlogging",
        "yield_rainfed_q_acre": "200โ€“250 q/acre (Maharashtra, good rainfall)",
        "yield_irrigated_q_acre": "350โ€“450 q/acre (North India, irrigated)",
        "soil_best": "Deep loam / clay loam with good water retention; pH 6.5โ€“7.5",
        "soil_avoid": "Shallow soils, waterlogged fields, very sandy",
        "water_risk": "MEDIUM for assured irrigated areas; HIGH for drought-prone Maharashtra",
        "profit_potential": "HIGH but payment DELAYED โ€” Rs 290โ€“340/q (state-announced SAP); mill payment delayed 6โ€“18 months is common",
        "input_cost_per_acre": "Rs 25,000โ€“45,000 (highest input crop โ€” heaviest feeder)",
        "key_risk": "Mill payment delays, ratoon stunting disease, red rot, pink bollworm; capital locked for 12+ months",
        "suitable_states": "UP, Maharashtra, Karnataka, AP, Tamil Nadu, Gujarat, Haryana",
    },
    "arhar_pigeonpea": {
        "keywords": ["arhar","tur","toor","pigeonpea","red gram","cajanus"],
        "season": "Kharif (Mayโ€“June for long-duration; Juneโ€“July for short-duration)",
        "duration_days": "120โ€“150d (short-duration); 200โ€“240d (long-duration Maruti type)",
        "rainfall_needed_mm": "600โ€“1,000mm; deep tap root uses subsoil moisture",
        "drought_tolerance": "HIGH โ€” deep roots access subsoil moisture after rains stop",
        "waterlog_tolerance": "Medium โ€” can tolerate brief 2โ€“3 day flooding at vegetative stage",
        "yield_rainfed_q_acre": "4โ€“7 q/acre (rainfed; highly variable)",
        "yield_irrigated_q_acre": "8โ€“12 q/acre",
        "soil_best": "Medium deep red/black soils; pH 6โ€“8; well-drained",
        "soil_avoid": "Waterlogged / very sandy / acidic soils",
        "water_risk": "LOW-MEDIUM โ€” adapts to dry spells; sensitive only at flowering",
        "profit_potential": "HIGH โ€” Rs 6,000โ€“8,000/q; strong household dal demand; MSP supported; dual use (pods + green manure)",
        "input_cost_per_acre": "Rs 5,000โ€“9,000",
        "key_risk": "Helicoverpa pod borer (most damaging), Phytophthora stem blight post-flood, price crash if national surplus",
        "suitable_states": "Maharashtra, UP, MP, Karnataka, AP, Gujarat, Bihar",
    },
    "groundnut": {
        "keywords": ["groundnut","peanut","moongfali"],
        "season": "Kharif (June 15โ€“July 15) OR Summer (Febโ€“March, irrigated)",
        "duration_days": "100โ€“110d (bunch); 120โ€“130d (spreading)",
        "rainfall_needed_mm": "450โ€“600mm well-distributed",
        "drought_tolerance": "High (deep roots) but sensitive at pegging",
        "waterlog_tolerance": "VERY LOW โ€” pod zone flooding = aflatoxin risk",
        "yield_rainfed_q_acre": "7โ€“10 q pods/acre",
        "yield_irrigated_q_acre": "12โ€“16 q pods/acre",
        "soil_best": "Sandy loam / well-drained light soils; Gujarat, Rajasthan",
        "soil_avoid": "Heavy clay / waterlogged soils",
        "water_risk": "MEDIUM โ€” critical water at pegging; excess rain = aflatoxin contamination",
        "profit_potential": "HIGH โ€” Rs 5,000โ€“7,000/q; confectionery/oil demand; export potential",
        "input_cost_per_acre": "Rs 10,000โ€“16,000 (high seed cost)",
        "key_risk": "Aflatoxin (storage), collar rot (post-flood), late leaf spot, Tikka disease",
        "suitable_states": "Gujarat, Rajasthan, AP, Karnataka, Tamil Nadu",
    },
    "sunflower": {
        "keywords": ["sunflower","surajmukhi","surjmukhi"],
        "season": "Rabi (Octโ€“Nov sowing) OR Zaid/Summer (Febโ€“March, irrigated)",
        "duration_days": "90โ€“100d",
        "rainfall_needed_mm": "400โ€“600mm OR 4โ€“6 irrigations (Rabi/Zaid = irrigated crop)",
        "drought_tolerance": "Medium (deep taproot) โ€” but sensitive at head-filling stage",
        "waterlog_tolerance": "LOW โ€” avoid standing water after sowing",
        "yield_rainfed_q_acre": "5โ€“7 q/acre (Kharif rainfed)",
        "yield_irrigated_q_acre": "8โ€“12 q/acre (Rabi/Zaid irrigated)",
        "soil_best": "Sandy loam / medium loam; pH 6.5โ€“8; well-drained",
        "soil_avoid": "Very heavy clay / saline / waterlogged soils",
        "water_risk": "MEDIUM โ€” critical water at flowering and seed fill; bird damage can cut yield 20โ€“30%",
        "profit_potential": "MEDIUM-HIGH โ€” MSP Rs 7,280/q (2024-25); oil demand growing; reliable procurement in AP/Karnataka",
        "input_cost_per_acre": "Rs 5,500โ€“8,500",
        "key_risk": "Bird damage (major โ€” net/scare needed), head rot (Alternaria), Sclerotinia wilt, price volatile",
        "suitable_states": "Karnataka, Andhra Pradesh, Maharashtra, Odisha, Bihar, Rajasthan",
    },
    "sesame": {
        "keywords": ["sesame","til","gingelly","tilli"],
        "season": "Kharif (Juneโ€“July) OR Zaid/Summer (Febโ€“March, short season)",
        "duration_days": "75โ€“90d",
        "rainfall_needed_mm": "250โ€“400mm (very drought tolerant โ€” MINIMUM water crop)",
        "drought_tolerance": "HIGH โ€” one of India's most drought-tolerant crops",
        "waterlog_tolerance": "VERY LOW โ€” even 24h waterlogging causes total failure",
        "yield_rainfed_q_acre": "3โ€“5 q/acre",
        "yield_irrigated_q_acre": "5โ€“7 q/acre",
        "soil_best": "Sandy loam / loamy sand / light well-drained soils; pH 5.5โ€“8",
        "soil_avoid": "Heavy clay / waterlogged / poorly drained soils โ€” FATAL",
        "water_risk": "LOW (drought side) but VERY HIGH for waterlogging โ€” NEVER sow in low-lying fields",
        "profit_potential": "HIGH โ€” MSP Rs 9,267/q (2024-25; highest oilseed MSP); strong export demand; low input cost",
        "input_cost_per_acre": "Rs 4,000โ€“6,000 (lowest-cost oilseed crop)",
        "key_risk": "Phyllody disease (mycoplasma via leafhopper โ€” use Imidacloprid), very low national average yield, market price volatile",
        "suitable_states": "Rajasthan, Gujarat, Maharashtra, Andhra Pradesh, Odisha, West Bengal, Madhya Pradesh",
    },
}

# Build keyword lookup for crop decision data
_CROP_DECISION_KW_MAP: dict[str, str] = {}
for _cdk, _cdv in _CROP_DECISION_DATA.items():
    for _kw in _cdv["keywords"]:
        _CROP_DECISION_KW_MAP[_kw] = _cdk


def build_crop_decision_context(query: str, detected_state: str | None = None) -> str:
    """
    For crop_selection queries: inject ICAR economic + agronomic profiles
    of candidate crops so LLM gives a comparison table with REAL numbers.
    Injects profiles for crops that appear in query OR are suitable for state.
    """
    q_lower = query.lower()
    # Detect which specific crops are mentioned
    mentioned: list[str] = []
    for kw in sorted(_CROP_DECISION_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            ck = _CROP_DECISION_KW_MAP[kw]
            if ck not in mentioned:
                mentioned.append(ck)

    # If no specific crops mentioned, pick top 4 for the state/season
    if not mentioned:
        # Default comparison set based on common queries
        mentioned = ["soybean", "pearl_millet_bajra", "moong", "gram_chickpea"]

    lines = ["โ”โ”โ” PRIORITY CONTEXT โ€” CROP DECISION DATA (use these EXACT numbers) โ”โ”โ”"]
    for ck in mentioned[:4]:  # max 4 crops in table
        p = _CROP_DECISION_DATA.get(ck)
        if not p:
            continue
        lines.append(
            f"\n๐Ÿ“Š {ck.upper().replace('_',' ')}:\n"
            f"  Season: {p['season']} | Duration: {p['duration_days']}\n"
            f"  Water need: {p['rainfall_needed_mm']} | Drought: {p['drought_tolerance']} | Waterlog: {p['waterlog_tolerance']}\n"
            f"  Yield rainfed: {p['yield_rainfed_q_acre']} | Irrigated: {p['yield_irrigated_q_acre']}\n"
            f"  Best soil: {p['soil_best']}\n"
            f"  Profit: {p['profit_potential']}\n"
            f"  Input cost: {p['input_cost_per_acre']}\n"
            f"  Key risk: {p['key_risk']}\n"
            f"  Best states: {p['suitable_states']}"
        )
    lines.append(
        "\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        "Use these EXACT numbers in your comparison table. "
        "Always include: Duration | Water Need | Drought Tolerance | Yield | Profit Risk | Verdict.\n"
        "Give ONE clear winner at the end with a single-line reason."
    )
    return "\n".join(lines)


# โ”€โ”€ Variety context injector for Before-Sowing AI Advisor โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_VARIETY_CROP_KW: dict[str, str] = {
    # maps query keyword โ†’ _VARIETY_DATA key
    "gehu": "Wheat", "wheat": "Wheat", "เค—เฅ‡เคนเฅ‚เค‚": "Wheat",
    "dhan": "Rice", "paddy": "Rice", "rice": "Rice", "dhaan": "Rice",
    "soybean": "Soybean", "soya": "Soybean", "bhat": "Soybean",
    "cotton": "Cotton", "kapas": "Cotton", "narma": "Cotton",
    "maize": "Maize", "makka": "Maize", "corn": "Maize",
    "mustard": "Mustard", "sarson": "Mustard", "sarson": "Mustard",
    "groundnut": "Groundnut", "moongfali": "Groundnut", "peanut": "Groundnut",
    "arhar": "Arhar", "tur": "Arhar", "pigeon": "Arhar",
    "gram": "Gram", "chana": "Gram", "chickpea": "Gram",
    "bajra": "Bajra", "pearl millet": "Bajra", "bajri": "Bajra",
    "moong": "Moong", "green gram": "Moong", "mung": "Moong",
    "urad": "Urad", "black gram": "Urad", "urd": "Urad",
    "sunflower": "Sunflower", "surajmukhi": "Sunflower", "surjmukhi": "Sunflower",
    "sesame": "Sesame", "til": "Sesame", "gingelly": "Sesame", "tilli": "Sesame",
    "onion": "Onion", "pyaz": "Onion", "kanda": "Onion",
    "tomato": "Tomato", "tamatar": "Tomato",
}

_VARIETY_QUERY_SIGNALS = re.compile(
    r"variety|varieties|variet|beej|seed|kism|เค•เคฟเคธเฅเคฎ|bona|lagaun|lagana|"
    r"konsa lagaun|kya lagaun|best crop|which crop|kaunsi fasal|sow|buwai|"
    r"which variety|konsi variety|sabse achhi|best variety|recommended",
    re.IGNORECASE,
)

def build_variety_context(query: str, detected_state: str | None = None) -> str:
    """
    Injects CURRENT ICAR variety data from _VARIETY_DATA into the LLM prompt
    for Before-Sowing queries. Prevents the LLM from falling back to old KCC
    records with obsolete varieties (Pankaj, Hunga, etc.).
    Called only for crop_selection / variety queries in the Before-Sowing tab.
    """
    q_lower = query.lower()

    # Detect which crops the farmer is asking about
    detected_crops: list[str] = []
    for kw in sorted(_VARIETY_CROP_KW, key=len, reverse=True):
        if kw in q_lower:
            crop = _VARIETY_CROP_KW[kw]
            if crop not in detected_crops:
                detected_crops.append(crop)

    if not detected_crops:
        return ""  # no specific crop mentioned โ€” skip injection

    lines = ["โ”โ”โ” ICAR VARIETY REFERENCE (2024-25) โ€” use ONLY these varieties โ”โ”โ”",
             "โš ๏ธ CRITICAL: The KCC database may contain OLD varieties (pre-2015). "
             "IGNORE them. Use ONLY the varieties listed below.\n"]

    for crop in detected_crops[:3]:  # max 3 crops
        varieties = _VARIETY_DATA.get(crop, [])
        if not varieties:
            continue

        # Filter to state if possible
        if detected_state:
            state_vars = [v for v in varieties
                          if detected_state in v.get("states", [])
                          or "All states" in v.get("states", [])]
            show = state_vars[:3] if state_vars else varieties[:3]
        else:
            show = varieties[:3]

        lines.append(f"๐ŸŒพ {crop.upper()} โ€” Recommended varieties:")
        for v in show:
            lines.append(
                f"  โ€ข {v['name']}: {v['days']} days, yield {v['yield']} q/acre, "
                f"seed rate {v['seed_rate']} | {v['traits']} | Buy: {v['source']}"
            )
        lines.append("")

    if len(lines) <= 3:  # nothing useful added
        return ""

    lines.append("โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”")
    return "\n".join(lines)


# Keyword lookup for irrigation crop detection
_IRR_CROP_KW_MAP: dict[str, str] = {}
for _ick, _icv in _ICAR_IRRIGATION_CARDS.items():
    for _kw in _icv["keywords"]:
        _IRR_CROP_KW_MAP[_kw] = _ick


def detect_irrigation_query(query: str) -> bool:
    """Return True if this is an irrigation/water management query."""
    _IRR_TRIGGERS = {"drip","sinchai","paani dena","pani dena","kitna paani","kitna pani",
                     "irrigation","water requirement","litres per","litre per","sprinkler",
                     "furrow","flood irrigation","per day water","per din paani","pump",
                     "drip line","emitter","micro irrigation","trickle","seepage"}
    q = query.lower()
    return any(t in q for t in _IRR_TRIGGERS)


def build_irrigation_context(query: str, crop: str | None = None) -> str:
    """
    Inject ICAR irrigation knowledge card when farmer asks about water/irrigation.
    Detects crop from query or uses detected_crop. Returns empty string if no card found.
    """
    q_lower = query.lower()
    crop_key = None
    # Try to find crop from irrigation card keywords
    for kw in sorted(_IRR_CROP_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            crop_key = _IRR_CROP_KW_MAP[kw]
            break
    # Fallback to detected_crop
    if not crop_key and crop:
        cl = crop.lower()
        for kw, ck in _IRR_CROP_KW_MAP.items():
            if kw in cl:
                crop_key = ck
                break

    if not crop_key:
        return (
            "โ”โ”โ” PRIORITY CONTEXT โ€” ICAR IRRIGATION GUIDELINES โ”โ”โ”\n"
            "No crop-specific card available. General guidance:\n"
            "Drip irrigation: 2โ€“6 L/plant/day depending on crop and stage.\n"
            "Flood irrigation: 5โ€“8 cm depth; frequency depends on soil and season.\n"
            "IMPORTANT: Ask farmer which crop and irrigation method before giving exact numbers.\n"
            "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        )

    card = _ICAR_IRRIGATION_CARDS[crop_key]
    return (
        f"โ”โ”โ” PRIORITY CONTEXT โ€” ICAR IRRIGATION DATA FOR {crop_key.upper()} โ”โ”โ”\n"
        f"Drip requirement: {card['drip_lpd']}\n"
        f"Stage-wise schedule: {card['stages']}\n"
        f"Flood/furrow: {card['flood']}\n"
        f"Most critical stage: {card['critical']}\n"
        f"Pro tip: {card['tip']}\n"
        f"Source: {card['source']}\n"
        f"โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        f"Use these EXACT numbers. Do NOT estimate or say 'it depends' without numbers.\n"
    )


def detect_named_disease(query: str) -> str | None:
    """
    Detect if the farmer explicitly named a specific disease or pest.
    Returns the disease key (e.g. 'rust', 'aphid') or None.
    Checks longest keyword first to prefer specific matches over generic ones.
    """
    q_lower = query.lower()
    for kw in sorted(_DISEASE_KW_MAP, key=len, reverse=True):
        if kw in q_lower:
            return _DISEASE_KW_MAP[kw]
    return None


def get_icar_priority_context(query: str, crop=None, problem_type=None, state=None) -> str:
    """
    Semantic ICAR retrieval for Tab 1 (Before Sowing) and other tabs.
    Uses the new ICAR KB FAISS index โ€” returns expert context or empty string.
    """
    if not _ICAR_AVAILABLE or _ICAR_RETRIEVER is None:
        return ""
    try:
        results = _ICAR_RETRIEVER.search(query, top_k=2)
        if results:
            return _ICAR_RETRIEVER.format_for_llm(results) + "\n\n"
    except Exception:
        pass
    return ""


def build_icar_context(disease_key: str, crop: str | None = None) -> str:
    """
    Format an ICAR disease card as a priority context block for the LLM prompt.
    This is injected ABOVE retrieved KCC records so the LLM gets ground-truth
    ICAR treatment data even when retrieval returns generic records.
    """
    card = _ICAR_DISEASE_CARDS.get(disease_key)
    if not card:
        return ""
    crop_note = f" in {crop}" if crop else ""
    return (
        f"โ”โ”โ” PRIORITY CONTEXT โ€” ICAR VALIDATED TREATMENT (use this first) โ”โ”โ”\n"
        f"Named condition{crop_note}: {card['diagnosis']}\n"
        f"TREATMENT: {card['treatment']}\n"
        f"DOSE: {card['dose']}\n"
        f"TIMING: {card['timing']}\n"
        f"IPM / Prevention: {card['ipm']}\n"
        f"Source: {card['source']}\n"
        f"PPE MANDATORY: Wear gloves, mask, full-sleeve clothing when spraying.\n"
        f"โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n"
        f"NOTE: The KCC records below are supplementary context. The ICAR card above\n"
        f"takes precedence for the farmer's explicitly named condition.\n"
    )


def detect_crop(query: str) -> str | None:
    """
    Detect the crop name from a query using keyword matching.
    Returns the DB crop name (e.g. 'Wheat') or None if not detected.
    """
    q_lower = query.lower()
    # Check each keyword; longer matches take priority (sorted by length desc)
    for kw in sorted(CROP_KEYWORDS, key=len, reverse=True):
        if kw in q_lower:
            return CROP_KEYWORDS[kw]
    return None


# โ”€โ”€ Language Normalizer (#7) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Appends English translations to Hindi/transliterated terms so the
# multilingual embedding model retrieves more relevant results.

HINDI_NORMALIZER: dict[str, str] = {
    # Symptoms
    "pilli": "yellow leaves",   "peeli": "yellow leaves",   "pili": "yellow leaves",
    "patte": "leaves",          "pattiya": "leaves",        "pattiyan": "leaves",
    "daag": "spots lesions",    "dhabbe": "spots marks",    "dhabb": "spots",
    "murjha": "wilting",        "murjhana": "wilting",      "sukh": "drying",
    "jhad": "falling dropping", "jhadna": "falling",        "girna": "falling",
    "gadna": "rotting",         "sadna": "rotting",         "galna": "rotting",
    "safed": "white",           "kala": "black",            "laal": "red",
    "bhura": "brown",           "peela": "yellow",
    # Pests / insects
    "keeda": "insect pest",     "kide": "insects pests",    "kirmi": "worm",
    "borer": "stem borer insect","sundi": "caterpillar",    "mahu": "aphid",
    "tikka": "spot disease",    "tela": "aphid",            "titli": "moth butterfly",
    "makhi": "fly",             "safed makhi": "whitefly",
    # Diseases
    "rog": "disease",           "bimari": "disease",        "jhulsa": "blight",
    "rust": "rust fungal",      "karela": "bitter gourd",
    # Deficiencies
    "kami": "deficiency",       "poshan": "nutrition",
    # Actions
    "badhana": "increase production yield",
    "bachana": "protect prevention control",
    "control": "control management treatment",
    "upchar": "treatment cure",
    "dawai": "medicine pesticide",
    "spray": "spray application",
    # Weather
    "pala": "frost cold damage", "thand": "cold frost",
    "baarish": "rain flood",     "sukha": "drought dry",
    # Misc
    "fasal": "crop",             "khet": "field farm",
    "mausam": "season weather",  "beej": "seed",
    "khaad": "fertilizer",       "sinchai": "irrigation",
}


def normalize_query(query: str) -> str:
    """
    Append English translations of Hindi agricultural terms to the query.
    The original query is kept intact; English terms are appended so the
    embedding model retrieves more semantically relevant KCC pairs.
    """
    q_lower = query.lower()
    additions: list[str] = []
    for hindi, english in HINDI_NORMALIZER.items():
        if hindi in q_lower and english not in q_lower:
            additions.append(english)
    if additions:
        return query + " " + " ".join(additions)
    return query


# โ”€โ”€ Problem Classifier (#1) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Rule-based (zero LLM cost). Classifies query into problem type so
# safety guardrails and retrieval can be tuned accordingly.

PROBLEM_KEYWORDS: dict[str, list[str]] = {
    "pest": [
        "keeda", "kide", "kirmi", "borer", "sundi", "mahu", "tela", "makhi",
        "safed makhi", "titli", "moth", "aphid", "thrip", "mite", "whitefly",
        "insect", "pest", "caterpillar", "larva", "bug", "เคŸเคฟเคกเฅเคกเฅ€", "เคฎเคพเคนเฅ‚",
        "grasshopper", "locust", "weevil", "nematode",
    ],
    "disease": [
        "rog", "bimari", "daag", "dhabbe", "jhulsa", "blight", "rust", "rot",
        "mildew", "fungal", "fungus", "bacterial", "virus", "tikka", "blast",
        "mosaic", "wilt", "canker", "scab", "smut", "spots", "lesion",
    ],
    "nutrient": [
        "peeli", "pilli", "yellow", "kami", "deficiency", "poshan", "khaad",
        "fertilizer", "urea", "npk", "zinc", "nitrogen", "phosphorus",
        "potassium", "micronutrient", "pallor", "chlorosis",
    ],
    "yield": [
        "paidavar", "utpadan", "increase", "badhana", "improve", "variety",
        "production", "output", "harvest", "paidal", "zyada",
    ],
    "weather": [
        "pala", "thand", "frost", "cold", "baarish", "flood", "drought",
        "sukha", "heat", "garmi", "hail", "ola", "andhi", "storm",
        "waterlogged", "waterlog", "paani bhar", "jal bhar", "drainage",
        "khet mein paani", "water stagnant", "nali", "drain",
    ],
    "organic": [
        "organic","jaivik","jaivik kheti","prakritic kheti","natural farming","zero budget",
        "zbnf","neem khad","vermicompost","gobar gas","cow dung","compost","jeevamrit",
        "beejamrit","panch gavya","panchgavya","bio pesticide","biopesticide","bio fertilizer",
        "trichoderma","pseudomonas","rhizobium","psb","ksb","biological control","biocontrol",
        "neem oil","neem cake","organic certification","pgpr","am fungi","mycorrhiza",
    ],
    "post_harvest": [
        "store","storage","store karna","bhandan","bhndaran","anaj rakhna","godam",
        "warehouse","silage","drying","sukhaana","moisture","aardrata","nami",
        "pest in storage","weevil storage","ghun","storage fungus","aflatoxin",
        "cold storage","refrigerate","grading","sorting","packaging","pack",
        "shelf life","kitne din chalega","kitne mahine","how long to store",
        "gehun store","dhan store","dal store","onion store","potato store",
    ],
    "seed_treatment": [
        "seed treatment","beej upchar","beej uchar","seed treat","seed coating",
        "beej shod","fungicide seed","thiram","captan","carbendazim seed",
        "imidacloprid seed","rhizobium seed","psb seed","trichoderma seed",
        "beej shodhan","before sowing","preplanting treatment","seed dressing",
    ],
    "agronomy": [
        "seed rate", "beej dar", "bij dar", "row spacing", "plant spacing", "doori",
        "kab lagaye", "kab lgaye", "when to sow", "when to plant", "kab boya",
        "sowing depth", "transplant time", "delayed monsoon", "monsoon delay",
        "monsoon late", "monsoon fail", "baarish nahi", "paani kam",
        "intercrop", "mixed crop", "companion crop", "relay crop",
        "thinning", "weeding", "mulching", "earthing up", "earthing",
        "spacing reduce", "spacing increase", "plant population",
        "kab kaate", "when to harvest", "maturity days", "crop duration",
    ],
    "crop_selection": [
        # Hindi/Hinglish
        "kya lagaye", "kya ugaye", "kaunsi fasal", "kya boya",
        "crop selection", "variety selection", "kaun si", "konsi",
        "laga sakte", "laga sakta", "lagana chahiye", "kya lagau",
        "kya ugaun", "kya bohun", "is mausam", "is season", "abhi kya",
        "suitable crop", "konsi kheti", "kaunsa beej",
        "kya fasal", "fasal chunav", "kaun sa bij",
        "behtar fasal", "kaun si kheti", "kaun sa crop",
        "soybean ya", "cotton ya", "gehu ya", "dhan ya",
        "ya soybean", "ya cotton", "ya wheat", "ya paddy",
        # English comparison patterns
        "which crop", "which variety", "which is better",
        "should i go for", "should i grow", "should i plant",
        "better option", "better crop", "crop choice",
        "cotton or soybean", "soybean or cotton", "wheat or mustard",
        "cotton or", "soybean or", "wheat or", "rice or", "maize or",
        "or cotton", "or soybean", "or wheat", "or rice", "or maize",
        "vs cotton", "vs soybean", "vs wheat", "vs rice",
        "cotton vs", "soybean vs", "wheat vs", "paddy vs",
        "best crop for", "suitable for my", "recommend crop",
        "what to grow", "what should i grow", "what to plant",
        "what crop", "go for cotton", "go for soybean", "go for wheat",
        "better profit", "more profit crop", "profitable crop",
        "which gives more", "which is more profitable",
        "switch to", "change to crop", "alternative crop",
    ],
    "irrigation": [
        "sinchai", "drip", "paani dena", "pani dena", "kitna paani", "kitna pani",
        "irrigation", "water requirement", "watering", "flow rate", "litres per day",
        "litre per plant", "sprinkler", "furrow", "flood irrigation", "micro irrigation",
        "per din paani", "per day water", "pump", "drip line", "emitter",
        "drip system", "trickle", "seepage", "suraksha sinchai",
    ],
    "scheme": [
        "pm kisan", "kisan samman", "fasal bima", "pmfby", "scheme", "yojana",
        "subsidy", "subsidi", "anudan", "kisan credit", "kcc loan", "loan",
        "pradhan mantri", "sarkar yojana", "government scheme", "apply kaise",
        "registration kaise", "form kaise", "beneficiary", "kisan vikas patra",
        "soil health card", "mgnrega", "rashtriya krishi", "rkvy", "agriculture loan",
    ],
}


def classify_problem(query: str) -> str:
    """
    Classify the farmer's query into a problem type using keyword matching.
    Returns one of: pest, disease, nutrient, yield, weather, crop_selection, general.
    """
    q_lower = query.lower()
    scores: dict[str, int] = {k: 0 for k in PROBLEM_KEYWORDS}
    for ptype, keywords in PROBLEM_KEYWORDS.items():
        for kw in keywords:
            if kw in q_lower:
                scores[ptype] += 1
    best = max(scores, key=lambda k: scores[k])
    return best if scores[best] > 0 else "general"


# โ”€โ”€ โ˜… Agriculture topic guard โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Fast pre-filter before FAISS retrieval + LLM generation.
# Saves compute and prevents the model from answering non-agriculture questions.

_AGRI_SIGNALS = {
    "crop","plant","seed","soil","fertilizer","pesticide","pest","disease",
    "farm","farmer","kisan","kheti","fasal","gehu","gehun","dhan","kapas",
    "tamatar","aloo","pyaz","mandi","bhav","rate","harvest","sow","sowing",
    "irrigation","spray","insecticide","fungicide","organic","yield",
    "wheat","rice","paddy","cotton","maize","sugarcane","soybean","mustard",
    "chilli","brinjal","onion","tomato","potato","groundnut","gram","arhar",
    "aphid","borer","blight","mildew","rust","wilt","thrips","mite","whitefly",
    "caterpillar","jassid","leaf","rot","fungus","virus","bacteria",
    "khaad","dawai","beej","sinchai","pattiya","keeda","bimari","rog",
    "upchar","khet","paidavar","safed","makhi","tela","mahu","tikda",
    "pala","thand","frost","baarish","drought","sukha","ola","flood",
    "waterlog","waterlogged","jaldhar","drainage","drain","paani bhar gaya",
    "jal bhar","flooding","khet mein paani","field water","nali","water stagnant",
    "kvk","icar","kcc","variety","beej","nursery","transplant","cutting",
    "seed rate","spacing","doori","row spacing","kab lagaye","when to sow",
    "intercrop","mixed crop","mulch","thinning","weeding","earthing",
    "agronomy","agronomic","delayed monsoon","monsoon delay","monsoon late",
    # Organic farming
    "jaivik","prakritic","jeevamrit","beejamrit","panchgavya","vermicompost",
    "neem cake","gobar","cow dung","organic","zero budget","zbnf","trichoderma",
    "rhizobium","psb","ksb","bio pesticide","biopesticide","biocontrol",
    # Post-harvest
    "store","storage","godam","bhandan","anaj rakhna","moisture","nami","aardrata",
    "drying","sukhaana","grading","packaging","shelf life","cold storage",
    "hermetic","pusa bin","fumigation","ghun","weevil","aflatoxin",
    # Seed treatment
    "seed treatment","beej upchar","beej shodhan","thiram","beejamrit",
}

_NON_AGRI_RE = re.compile(
    r"\b(stock market|share market|bitcoin|crypto|politics|election|"
    r"movie|cricket|football|recipe|cooking|exam|bank account|loan|"
    r"insurance claim|marriage|divorce|love|dating|salary|job|"
    r"celebrity|news|tv show|web series|ipl|bollywood|actor|actress|"
    r"girlfriend|boyfriend|password|hack|war|army|defence)\b",
    re.IGNORECASE,
)

# โ”€โ”€ Harmful non-farm query detector โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Catches queries that explicitly say "not for farm use" / "for home use" but
# ask about harmful topics (poisons, chemicals for harming people/animals).
# These pass is_agriculture_query() because "farm" appears in the query,
# but they're NOT legitimate agricultural queries and must be refused.
_HARMFUL_NON_FARM_RE = re.compile(
    r"not\s+for\s+farm|not\s+for\s+agri|not\s+farming|"
    r"ghar\s+ke\s+liye\s+(?:zeher|poison|dawa|chemical)|"
    r"non.agri(?:cultural)?\s+use|outside\s+(?:farm|agriculture|agri)\b",
    re.IGNORECASE,
)
_HARM_SIGNAL_RE = re.compile(
    r"\b(poison|kill|mar|zeher|toxic|deadly|harm|hurt|weapon|murder)\b",
    re.IGNORECASE,
)

# Second arm: mass-harm / collective-kill language โ€” dangerous even WITHOUT
# "not for farm" qualifier (e.g. "maar de", "sab khatam kar", "sabko zeher do")
_MASS_HARM_RE = re.compile(
    r"maar\s+de|sabko\s+(?:maar|zeher|khatam|nasha)|"
    r"sab\s+(?:khatam\s+kar|maar\s+daal|ko\s+zeher)|"
    r"puri\s+(?:abadi|colony|gaon)\s+(?:ko\s+)?(?:maar|zeher|khatam)|"
    r"logo[n]?\s+ko\s+(?:maar|zeher|nasha|jaan\s+se)|"
    r"paan?i\s+mein\s+(?:zeher|poison|dawa)\s+(?:mila(?:na|o)?|dal(?:na|o)?)|"
    r"(?:kuein|kuan|well|water\s+supply|naali)\s+(?:mein\s+)?(?:zeher|poison)|"
    r"zeher\s+(?:dalna|milana|daalo|milao)\s+(?:mein\s+)?(?:pani|paani|kuein|kuan|naali)|"
    r"how\s+to\s+(?:poison\s+(?:a\s+)?(?:well|water|people|village)|kill\s+(?:people|humans|someone|villagers))|"
    r"insaan\s+(?:maar|khatam|zeher)|"
    r"gaon\s+ke\s+(?:log|lok|insaan)\s+(?:maar|zeher|khatam)|"
    r"ghareloo\s+(?:use|istemal|upyog)\s+(?:ke\s+liye\s+)?(?:zeher|aluminium|phosphide|deadly|hanikarak)",
    re.IGNORECASE,
)


def is_harmful_non_farm_query(query: str) -> bool:
    """
    Returns True if the query requests harmful info that is not legitimate farming advice.

    Arm 1 (original): "not for farm use" + harm signal โ†’ e.g. rat poison for home use
    Arm 2 (new):      Mass-harm / collective-kill language โ†’ e.g. "maar de", "sabko zeher"
                      These are dangerous regardless of whether "farm" appears.
    """
    q = query.strip()
    # Arm 1: explicit non-farm qualifier + harm signal
    if _HARMFUL_NON_FARM_RE.search(q) and _HARM_SIGNAL_RE.search(q):
        return True
    # Arm 2: mass-harm / collective-kill language (always dangerous)
    if _MASS_HARM_RE.search(q):
        return True
    return False


OFF_TOPIC_RESPONSE = (
    "๐ŸŒพ Main sirf kheti-baadi, fasal, keed-bimari, khaad, mandi bhav, "
    "aur kisan-sambandhit sawalon ka jawab de sakta hoon.\n\n"
    "Kripya apna kheti-sambandhit sawaal poochhein โ€” main zaroor madad karoonga!"
)


def is_agriculture_query(query: str) -> bool:
    """
    Returns True if the query is agriculture-related.
    Short follow-ups (โ‰ค3 words) always pass โ€” they're conversational replies.
    """
    stripped = query.strip()
    # Very short โ†’ allow (continuation of conversation)
    if len(stripped.split()) <= 3:
        return True

    # Devanagari script โ†’ almost certainly an Indian farmer query
    if any('เค€' <= c <= 'เฅฟ' for c in stripped):
        if _NON_AGRI_RE.search(stripped.lower()):
            return False
        return True

    q_lower = stripped.lower()

    # Hard non-agriculture + no agriculture override โ†’ reject
    if _NON_AGRI_RE.search(q_lower):
        words = set(re.findall(r"\b\w+\b", q_lower))
        if words.intersection(_AGRI_SIGNALS):
            return True   # e.g. "cotton market rate" has both signals
        return False

    # Any agriculture signal โ†’ allow
    words = set(re.findall(r"\b\w+\b", q_lower))
    return bool(words.intersection(_AGRI_SIGNALS))


# โ”€โ”€ Chemical Safety Guardrails (#6) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Injected into the prompt so the LLM knows exactly which chemical class
# is appropriate โ€” preventing the carbendazim-for-stem-borer mistake.

SAFETY_GUARDRAILS: dict[str, str] = {
    "pest": (
        "SAFETY RULE โ€” PEST/INSECT PROBLEM DETECTED: "
        "Recommend INSECTICIDES only (e.g. Chlorpyrifos, Imidacloprid, "
        "Quinalphos, Malathion, Cypermethrin). "
        "DO NOT recommend fungicides (Mancozeb, Carbendazim, Propiconazole, "
        "Copper oxychloride) for controlling the pest."
    ),
    "disease": (
        "SAFETY RULE โ€” FUNGAL/BACTERIAL DISEASE DETECTED: "
        "Recommend FUNGICIDES or bactericides only (e.g. Mancozeb, Carbendazim, "
        "Copper oxychloride, Propiconazole). "
        "DO NOT recommend insecticides for treating the disease."
    ),
    "nutrient": (
        "SAFETY RULE โ€” NUTRIENT DEFICIENCY DETECTED: "
        "Recommend FERTILIZERS and micronutrients only (Urea, DAP, MOP, "
        "Zinc Sulphate, Ferrous Sulphate, Boron). "
        "Do NOT recommend pesticides for nutrient problems."
    ),
    "weather": (
        "SAFETY RULE โ€” WEATHER/ABIOTIC STRESS DETECTED: "
        "Recommend protective measures (light irrigation for frost, drainage for "
        "flooding/waterlogging). Do NOT recommend pesticides for weather damage. "
        "For waterlogged fields: drainage first โ†’ withhold fertilizer โ†’ check roots."
    ),
    "agronomy": (
        "SAFETY RULE โ€” AGRONOMY QUERY: "
        "Give principle-based advice tied to the farmer's soil/water/season context. "
        "Never give generic percentages without explaining why. "
        "For waterlogged fields: drainage is always Step 1. "
        "For delayed monsoon: recommend shorter-duration crop varieties."
    ),
    "crop_selection": (
        "CRITICAL GUARD โ€” CROP SELECTION QUERY: "
        "Farmer is asking WHICH CROP TO GROW โ€” do NOT diagnose any pest or disease. "
        "Do NOT assume farmer already chose a crop. "
        "Do NOT quote โ‚น prices from retrieved KCC context (those are old historical data). "
        "Compare crops using: water need + duration + soil fit + current season window. "
        "Always redirect price questions to the Mandi Prices tab."
    ),
    "organic": (
        "ORGANIC FARMING GUARD: Farmer is asking about organic/natural farming. "
        "DO NOT recommend ANY synthetic chemical pesticide or fertilizer. "
        "Stick to bio-inputs: Neem oil, Jeevamrit, Trichoderma, FYM, vermicompost, PSB, Rhizobium. "
        "For disease: Copper Oxychloride is permitted (it is NPOP-approved). "
        "Mention that chemicals will disqualify organic certification."
    ),
    "post_harvest": (
        "POST-HARVEST GUARD: Give storage-specific advice. "
        "Always mention MOISTURE % target for safe storage. "
        "Mention appropriate container type. "
        "Never recommend raw Aluminium Phosphide to farmer โ€” say 'contact licensed fumigator'. "
        "Duration advice must be specific (months, not vague 'long time')."
    ),
    "seed_treatment": (
        "SEED TREATMENT GUARD: Give the 3-step sequence: chemical โ†’ dry โ†’ biological. "
        "NEVER mix Rhizobium/Trichoderma with Thiram/Captan in same application โ€” chemicals kill bacteria. "
        "Always mention seeds should be sown within 24 hours of treatment. "
        "Treated seeds should NOT be stored โ€” treat just before sowing."
    ),
}



# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# INFRASTRUCTURE FUNCTIONS โ€” reconstructed
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

_RESPONSE_CACHE: dict[str, str] = {}
nn = "\n\n"

_CHATBOT_PRESOW_KEYWORDS = [
    "lagana", "ugana", "beej", "sow", "plant", "grow",
    "konsi fasal", "which crop", "crop selection", "kharif", "rabi",
    "price forecast", "bhav kitna",
]

_CHATBOT_PEST_KEYWORDS = [
    "kida", "rog", "bimari", "pest", "disease", "insect", "fungus",
    "patta", "patti", "daag", "jhulsa", "sukhna", "mahu", "whitefly",
    "thrips", "rust", "blight", "wilt", "borer", "caterpillar",
    "spray karun", "dawai", "dawa", "treatment", "bachao",
]

STATE_COORDS: dict[str, tuple] = {
    "Andhra Pradesh": (15.9129, 79.7400), "Arunachal Pradesh": (28.2180, 94.7278),
    "Assam": (26.2006, 92.9376), "Bihar": (25.0961, 85.3131),
    "Chhattisgarh": (21.2787, 81.8661), "Goa": (15.2993, 74.1240),
    "Gujarat": (22.2587, 71.1924), "Haryana": (29.0588, 76.0856),
    "Himachal Pradesh": (31.1048, 77.1734), "Jharkhand": (23.6102, 85.2799),
    "Karnataka": (15.3173, 75.7139), "Kerala": (10.8505, 76.2711),
    "Madhya Pradesh": (22.9734, 78.6569), "Maharashtra": (19.7515, 75.7139),
    "Manipur": (24.6637, 93.9063), "Meghalaya": (25.4670, 91.3662),
    "Mizoram": (23.1645, 92.9376), "Nagaland": (26.1584, 94.5624),
    "Odisha": (20.9517, 85.0985), "Punjab": (31.1471, 75.3412),
    "Rajasthan": (27.0238, 74.2179), "Sikkim": (27.5330, 88.5122),
    "Tamil Nadu": (11.1271, 78.6569), "Telangana": (18.1124, 79.0193),
    "Tripura": (23.9408, 91.9882), "Uttar Pradesh": (26.8467, 80.9462),
    "Uttarakhand": (30.0668, 79.0193), "West Bengal": (22.9868, 87.8550),
    "Jammu & Kashmir": (33.7782, 76.5762), "Jammu and Kashmir": (33.7782, 76.5762),
    "Ladakh": (34.1526, 77.5770), "Delhi": (28.7041, 77.1025),
}

_WMO_CODES: dict[int, str] = {
    0: "โ˜€๏ธ Clear", 1: "๐ŸŒค๏ธ Mostly Clear", 2: "โ›… Partly Cloudy", 3: "โ˜๏ธ Overcast",
    45: "๐ŸŒซ๏ธ Foggy", 48: "๐ŸŒซ๏ธ Icy Fog", 51: "๐ŸŒฆ๏ธ Light Drizzle", 53: "๐ŸŒฆ๏ธ Drizzle",
    55: "๐ŸŒง๏ธ Heavy Drizzle", 61: "๐ŸŒง๏ธ Light Rain", 63: "๐ŸŒง๏ธ Rain", 65: "๐ŸŒง๏ธ Heavy Rain",
    71: "โ„๏ธ Light Snow", 73: "โ„๏ธ Snow", 75: "โ„๏ธ Heavy Snow",
    80: "๐ŸŒฆ๏ธ Showers", 81: "๐ŸŒฆ๏ธ Moderate Showers", 82: "โ›ˆ๏ธ Heavy Showers",
    95: "โ›ˆ๏ธ Thunderstorm", 96: "โ›ˆ๏ธ Thunderstorm+Hail", 99: "โ›ˆ๏ธ Heavy Thunderstorm",
}

_STATE_SOIL: dict[str, str] = {
    "Punjab": "loamy alluvial (high fertility)", "Haryana": "sandy loam alluvial",
    "Uttar Pradesh": "alluvial (Gangetic plain)", "Bihar": "alluvial + sandy loam",
    "West Bengal": "alluvial + laterite (red)",
    "Madhya Pradesh": "black cotton (Vertisol) + red laterite",
    "Maharashtra": "black cotton (Vertisol) 60% + red laterite 40%",
    "Rajasthan": "sandy / arid + alluvial (eastern)",
    "Gujarat": "black cotton + coastal alluvial + sandy",
    "Karnataka": "red laterite + black cotton (northern)",
    "Andhra Pradesh": "black cotton + red sandy loam",
    "Telangana": "black cotton (Vertisol) dominant",
    "Tamil Nadu": "red laterite + alluvial + black cotton",
    "Kerala": "laterite + sandy coastal + forest loam",
    "Odisha": "red laterite + alluvial river valleys",
    "Chhattisgarh": "red laterite (dongar) + alluvial (plains)",
    "Jharkhand": "red laterite + forest loam",
    "Assam": "alluvial + clay loam + hill soils",
    "Himachal Pradesh": "mountain loam + brown forest soil",
    "Uttarakhand": "mountain loam + bhabhar terai alluvial",
    "Jammu & Kashmir": "mountain alluvial + brown forest soil",
    "Jammu and Kashmir": "mountain alluvial + brown forest soil",
}

_STATE_ALIASES: dict[str, str] = {
    "up": "Uttar Pradesh", "mp": "Madhya Pradesh", "ap": "Andhra Pradesh",
    "tn": "Tamil Nadu", "wb": "West Bengal", "hp": "Himachal Pradesh",
    "uk": "Uttarakhand", "jk": "Jammu & Kashmir", "j&k": "Jammu & Kashmir",
    "uttar pradesh": "Uttar Pradesh", "madhya pradesh": "Madhya Pradesh",
    "andhra pradesh": "Andhra Pradesh", "tamil nadu": "Tamil Nadu",
    "west bengal": "West Bengal", "himachal pradesh": "Himachal Pradesh",
    "himachal": "Himachal Pradesh", "uttarakhand": "Uttarakhand",
    "jammu": "Jammu & Kashmir", "kashmir": "Jammu & Kashmir",
    "maharashtra": "Maharashtra", "rajasthan": "Rajasthan",
    "punjab": "Punjab", "haryana": "Haryana", "gujarat": "Gujarat",
    "karnataka": "Karnataka", "telangana": "Telangana", "kerala": "Kerala",
    "odisha": "Odisha", "orissa": "Odisha", "jharkhand": "Jharkhand",
    "chhattisgarh": "Chhattisgarh", "assam": "Assam", "bihar": "Bihar",
    "goa": "Goa", "manipur": "Manipur", "meghalaya": "Meghalaya",
    "mizoram": "Mizoram", "nagaland": "Nagaland", "sikkim": "Sikkim",
    "tripura": "Tripura", "arunachal pradesh": "Arunachal Pradesh",
    "arunachal": "Arunachal Pradesh",
}


@st.cache_data(ttl=3600, show_spinner=False)
def _fetch_weather(state: str) -> dict:
    coords = STATE_COORDS.get(state)
    if not coords:
        return {}
    lat, lon = coords
    url = (
        f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}"
        "&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,weathercode"
        "&current_weather=true&timezone=Asia%2FKolkata&forecast_days=3"
    )
    try:
        r = requests.get(url, timeout=8)
        if r.ok:
            data = r.json()
            url28 = (
                f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}"
                "&daily=temperature_2m_max,temperature_2m_mean,precipitation_sum"
                "&timezone=Asia%2FKolkata&forecast_days=14&past_days=14"
            )
            try:
                r28 = requests.get(url28, timeout=8)
                if r28.ok:
                    d28 = r28.json().get("daily", {})
                    cl = lambda v: [x for x in v if x is not None]
                    tmean_v = d28.get("temperature_2m_mean", [])
                    tmax_v  = d28.get("temperature_2m_max", [])
                    prec_v  = d28.get("precipitation_sum", [])
                    data["t2m_mean_28d"] = float(np.mean(cl(tmean_v))) if tmean_v else np.nan
                    data["t2m_max_28d"]  = float(np.max(cl(tmax_v)))  if tmax_v  else np.nan
                    data["tp_total_28d"] = float(np.sum(cl(prec_v)))  if prec_v  else np.nan
                    data["tp_mean_7d"]   = float(np.mean(cl(prec_v[-7:]))) if len(prec_v) >= 7 else np.nan
                    ys = cl(tmean_v)
                    data["temp_trend"]   = float(np.polyfit(range(len(ys)), ys, 1)[0]) if len(ys) >= 3 else 0.0
                    data["skt_mean_28d"] = data["t2m_mean_28d"]
                    data["stl1_mean_28d"]= data["t2m_mean_28d"]
            except Exception:
                for k in ["t2m_mean_28d","t2m_max_28d","tp_total_28d","tp_mean_7d",
                          "temp_trend","skt_mean_28d","stl1_mean_28d"]:
                    data.setdefault(k, np.nan)
            return data
    except Exception:
        pass
    return {}


def _format_weather_context(wx: dict, state: str) -> str:
    if not wx or "daily" not in wx:
        return ""
    daily = wx["daily"]
    tmax = (daily.get("temperature_2m_max") or [None])[0]
    tmin = (daily.get("temperature_2m_min") or [None])[0]
    rain = (daily.get("precipitation_sum") or [0])[0] or 0
    code = (daily.get("weathercode") or [0])[0] or 0
    desc = _WMO_CODES.get(code, "")
    lines = [f"WEATHER in {state} (today): {desc} | Max {tmax}ยฐC | Min {tmin}ยฐC | Rain {rain}mm"]
    if rain > 20:
        lines.append("๐ŸŒง๏ธ Heavy rain โ€” avoid spraying. Fungal disease risk elevated.")
    if tmax and tmax > 40:
        lines.append("๐ŸŒก๏ธ Extreme heat โ€” avoid urea application. Volatilisation risk.")
    if tmin and tmin < 5:
        lines.append("๐Ÿฅถ Cold โ€” frost risk. Protect sensitive crops.")
    return "\n".join(lines)


def detect_state(query: str) -> str | None:
    ql = query.lower()
    for alias, state in _STATE_ALIASES.items():
        if alias in ql:
            return state
    return None


def get_current_season() -> dict:
    m = datetime.now().month
    if m in (6, 7, 8, 9):
        return {"name": "Kharif", "months": "Juneโ€“October",
                "crops": "Rice, Cotton, Soybean, Maize, Groundnut, Arhar, Moong, Urad",
                "planning_note": "Kharif sowing: June 15 โ€“ July 31. Scout pests post-monsoon."}
    elif m in (10, 11, 12, 1, 2, 3):
        return {"name": "Rabi", "months": "Octoberโ€“March",
                "crops": "Wheat, Mustard, Gram, Lentil, Peas, Potato, Onion",
                "planning_note": "Rabi sowing: Oct 15 โ€“ Nov 30. Watch aphid in mustard, rust in wheat."}
    else:
        return {"name": "Zaid (Summer)", "months": "Aprilโ€“June",
                "crops": "Moong, Urad, Cucumber, Watermelon, Bitter Gourd, Sunflower",
                "planning_note": "Zaid: 60-65 day window. Irrigation is critical."}


def get_soil_context(state: str) -> str:
    return _STATE_SOIL.get(state, "")


def _extract_location_from_query(query: str) -> str | None:
    words = query.split()
    for i, w in enumerate(words):
        if w.lower() in ("district","tehsil","block","mein","me","ka","ki") and i > 0:
            return words[i-1].title()
    return None


def _apply_state_preference(docs: list, state: str | None) -> list:
    if not state or not docs:
        return docs
    su = state.upper()
    sd = [d for d in docs if su in (getattr(d, "source", "") or "").upper()]
    od = [d for d in docs if d not in sd]
    return sd + od


def _make_cache_key(query: str, crop, state, problem_type: str, season_name: str) -> str:
    raw = f"{query.lower().strip()}|{crop}|{state}|{problem_type}|{season_name}"
    return hashlib.md5(raw.encode()).hexdigest()


def _cache_store(key: str, value: str) -> None:
    if len(_RESPONSE_CACHE) > 500:
        for k in list(_RESPONSE_CACHE.keys())[:100]:
            del _RESPONSE_CACHE[k]
    _RESPONSE_CACHE[key] = value


@st.cache_resource(show_spinner=False)
def _get_gemini_client():
    try:
        import google.generativeai as genai
        genai.configure(api_key=config.GEMINI_API_KEY)
        return genai
    except Exception:
        return None


def _build_prompt(query: str, context: str, history: list,
                  problem_type: str, lang: str,
                  state: str = "", district: str = "",
                  detected_crop: str = "", problem_detail: str = "") -> str:
    lang_rule = (
        f"\n\nMANDATORY LANGUAGE RULE: Reply ONLY in {lang.upper()}. "
        "Do NOT switch languages mid-answer."
    )
    hist_str = ""
    if history:
        hist_str = "\n\nCONVERSATION HISTORY (last 3 turns):\n"
        for msg in history[-6:]:
            role = "Farmer" if msg["role"] == "user" else "Advisor"
            hist_str += f"{role}: {msg['content'][:200]}\n"

    # UPGRADE 2: Farmer location context
    location_str = ""
    if state or district:
        loc_parts = [p for p in [district, state] if p]
        location_str = (
            f"\n\nFARMER LOCATION: {', '.join(loc_parts)} โ€” "
            "prioritize region-specific advice for this area"
        )

    # UPGRADE 3: Verified dose injection
    verified_dose_str = ""
    if detected_crop and problem_detail:
        dose_info = get_verified_dose(detected_crop, problem_detail)
        if dose_info:
            verified_dose_str = (
                f"\n\nVERIFIED DOSE (ICAR/KCC source โ€” use this exactly, do not modify):\n"
                + json.dumps(dose_info, ensure_ascii=False)
            )

    # UPGRADE 2: Forced grounding context header
    grounded_context = (
        "RETRIEVED KCC RECORDS (from 16.5M farmer queries database):\n"
        + context
        + "\n\nMANDATORY GROUNDING RULES:\n"
        "- Your answer MUST be based on the retrieved KCC records above\n"
        "- For any chemical dose, seed rate, or specific agronomy number: use ONLY values from the retrieved records\n"
        "- If the retrieved records don't cover the question: say 'KCC records don't have specific data for this โ€” general guidance: [answer]'\n"
        "- Do NOT use doses or chemicals from your general training if they contradict the retrieved records\n"
        "- The records above represent real answers given to Indian farmers by agricultural experts"
    )

    return (
        SYSTEM_PROMPT + lang_rule + location_str + hist_str
        + "\n\n" + grounded_context
        + verified_dose_str
        + "\n\nFARMER'S QUESTION: " + query
        + "\n\nADVISOR'S ANSWER (be concise, specific, actionable):"
    )


def _stream_llm_response(prompt: str):
    try:
        import groq as _groq
    except ImportError:
        yield "โš ๏ธ groq not installed."
        return
    _GM = [
        getattr(config, "GROQ_MODEL_PRIMARY", "meta-llama/llama-4-scout-17b-16e-instruct"),
        "llama-3.3-70b-versatile",
        "gemma2-9b-it",
    ]
    _GEMS = ["gemini-2.0-flash", "gemini-1.5-flash", "gemma-3-27b-it"]
    gc = _groq.Groq(api_key=config.GROQ_API_KEY)
    for model in _GM:
        try:
            stream = gc.chat.completions.create(
                model=model,
                messages=[{"role": "user", "content": prompt}],
                max_tokens=600, temperature=0.1, stream=True,
            )
            for chunk in stream:
                d = chunk.choices[0].delta.content
                if d: yield d
            return
        except Exception as e:
            if "429" in str(e) or "rate" in str(e).lower():
                time.sleep(2)
            continue
    try:
        import google.generativeai as genai
        genai.configure(api_key=config.GEMINI_API_KEY)
        for gm in _GEMS:
            try:
                resp = genai.GenerativeModel(gm).generate_content(
                    prompt, generation_config={"max_output_tokens": 600, "temperature": 0.1})
                yield resp.text
                return
            except Exception:
                continue
    except Exception:
        pass
    yield "โš ๏ธ AI service temporarily unavailable. Please try again."


def check_banned_pesticides(response: str) -> list[str]:
    """Check for India-banned pesticides โ€” negation-aware.

    A mention inside a negated sentence (e.g. "do NOT use Endosulfan" or
    "Banned chemicals (Endosulfan, Monocrotophos) se bachein") is NOT flagged.
    Whole-sentence scan + 30-char inline window. Backport from v3
    kcc_core/citation_guard.py (2026-05-10).
    Sources: MoA gazette notifications + India CIBRC + ICAR advisory.
    """
    import re as _re
    # Union of v2's original list + v3's CIBRC-aligned set (21 entries total)
    _BANNED = [
        # Organophosphates (banned/severely restricted)
        "monocrotophos","endosulfan","methyl parathion","phorate","triazophos",
        "phosphamidon","dichlorvos","profenofos","chlorpyrifos ethyl",
        "ethyl parathion","fenthion","phosalone","trichlorfon",
        # Carbamates
        "carbofuran","aldicarb","methomyl","carbaryl","carbosulfan",
        # Organochlorines
        "alachlor","dicofol","ddt","bhc","aldrin","dieldrin","heptachlor",
        "lindane","chlordane","tetradifon","pentachlorophenol",
        # Mercury / others (restricted/banned)
        "atrazine","mancozeb blends with zineb","benomyl","captafol",
        "paraquat","2 4 5 t","sodium cyanide","ethyl mercury chloride",
    ]
    _NEGATIONS = (
        "do not use", "don't use", "never use", "avoid", "banned",
        "no use of", "not recommended", "instead of", "rather than",
        "not ", "non-", "no ", "without ",
    )
    _SENT_SPLIT = _re.compile(r"[.!?\n]+")
    rl = response.lower()

    def _is_negated(chem: str) -> bool:
        """True if every occurrence of `chem` is inside a negated sentence."""
        idx = rl.find(chem)
        while idx != -1:
            sent_start = max((m.end() for m in _SENT_SPLIT.finditer(rl, 0, idx)), default=0)
            next_term = _SENT_SPLIT.search(rl, idx)
            sent_end = next_term.start() if next_term else len(rl)
            sentence = rl[sent_start:sent_end]
            window = rl[max(0, idx - 30):idx]
            if any(neg in sentence for neg in _NEGATIONS) or any(neg in window for neg in _NEGATIONS):
                idx = rl.find(chem, idx + len(chem))
                continue
            return False
        return True

    found = []
    for b in _BANNED:
        pattern = r'\b' + _re.escape(b) + r'\b'
        if _re.search(pattern, rl) and not _is_negated(b):
            found.append(b)
    return found


# Chemical-target correctness lookup (ICAR verified)
# Maps disease/pest -> correct chemical mode of action
_CHEMICAL_TARGET_RULES = {
    # Fungal wilts -- soil-borne, sprays DON'T work
    "fusarium wilt":    {"banned_classes": ["metalaxyl","ridomil","mancozeb spray"],
                         "correct": "Carbendazim 50% WP soil drench @ 2g/L OR Trichoderma viride @ 5g/kg seed"},
    "verticillium wilt":{"banned_classes": ["metalaxyl","ridomil"],
                         "correct": "Trichoderma-based soil application; no curative spray exists"},
    # Whitefly -- pyrethroids cause resurgence
    "whitefly":         {"banned_classes": ["bifenthrin","cypermethrin","lambda cyhalothrin","permethrin","deltamethrin"],
                         "correct": "Pyriproxyfen 10% EC @ 0.75ml/L OR Diafenthiuron 50% WP @ 1.5g/L OR Spiromesifen 22.9% SC @ 0.5ml/L"},
    # BPH -- synthetic pyrethroids make it worse
    "brown planthopper":{"banned_classes": ["cypermethrin","deltamethrin","lambda cyhalothrin","bifenthrin"],
                         "correct": "Buprofezin 25% SC @ 1ml/L OR Dinotefuran 20% SG @ 0.4g/L"},
    # Spider mite -- organophosphates ineffective
    "spider mite":      {"banned_classes": ["monocrotophos","chlorpyrifos","malathion"],
                         "correct": "Abamectin 1.9% EC @ 0.5ml/L OR Spiromesifen 22.9% SC @ 0.5ml/L"},
}

def check_chemical_target_match(response: str, query: str) -> str | None:
    """Post-generation check: flag if response recommends wrong chemical class for detected disease."""
    rl = response.lower()
    ql = query.lower()
    warnings = []
    for disease, rules in _CHEMICAL_TARGET_RULES.items():
        if disease in ql or disease in rl:
            for banned_chem in rules["banned_classes"]:
                if banned_chem in rl:
                    warnings.append(
                        f"โš ๏ธ Correction: {banned_chem.title()} is not effective for {disease}. "
                        f"Recommended: {rules['correct']}"
                    )
                    break
    return "\n".join(warnings) if warnings else None


def check_chemical_safety(response: str, problem_type: str) -> str | None:
    if problem_type in ("pest","disease") and "spray" in response.lower():
        if "gloves" not in response.lower():
            return "โš ๏ธ Wear gloves and mask while spraying."
    return None

# โ”€โ”€ UPGRADE 3: Verified dose lookup (ICAR/KCC source) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_DOSE_TABLE: dict | None = None
_DOSE_TABLE_LOADED = False

def _load_dose_table() -> dict:
    """Load kcc_dose_table.json once and cache in module."""
    global _DOSE_TABLE, _DOSE_TABLE_LOADED
    if _DOSE_TABLE_LOADED:
        return _DOSE_TABLE or {}
    _DOSE_TABLE_LOADED = True
    import json as _j
    from pathlib import Path as _P
    dose_path = _P(__file__).parent / "kcc_dose_table.json"
    if dose_path.exists():
        try:
            with open(dose_path, "r", encoding="utf-8") as f:
                _DOSE_TABLE = _j.load(f)
        except Exception as e:
            print(f"[DoseTable] Load error: {e}")
            _DOSE_TABLE = {}
    else:
        _DOSE_TABLE = {}
    return _DOSE_TABLE


def get_verified_dose(crop: str, pest_disease: str) -> dict | None:
    """
    Look up ICAR-verified dose for a crop + pest/disease combination.
    Returns dose dict or None if not found.

    crop         : e.g. "wheat", "cotton", "rice"
    pest_disease : e.g. "aphid", "yellow_rust", "bollworm"
    """
    if not crop or not pest_disease:
        return None
    table = _load_dose_table()
    if not table:
        return None
    # Normalize
    crop_key = crop.strip().lower().replace(" ", "_")
    pest_key = pest_disease.strip().lower().replace(" ", "_")
    crop_data = table.get(crop_key)
    if not crop_data:
        # Try partial match
        for k in table:
            if k in crop_key or crop_key in k:
                crop_data = table[k]
                break
    if not crop_data:
        return None
    # Exact match
    if pest_key in crop_data:
        return crop_data[pest_key]
    # Partial match
    for k in crop_data:
        if k in pest_key or pest_key in k:
            return crop_data[k]
    return None




def _icar_mandatory_supplement(response: str, problem_type: str, detected_crop) -> str:
    banned = check_banned_pesticides(response)
    if banned:
        response += f"\n\nโš ๏ธ Note: {', '.join(b.title() for b in banned)} is banned in India."
    if problem_type in ("pest","disease") and "โš ๏ธ Wear gloves" not in response:
        response += "\n\nโš ๏ธ Wear gloves and mask while spraying."
    # FIX 6: Check chemical-target correctness (query unavailable here; uses response context)
    chem_warning = check_chemical_target_match(response, "")
    if chem_warning:
        response += f"\n\n{chem_warning}"
    return response


def understand_query_llm(query: str) -> dict:
    try:
        import groq as _groq
        client = _groq.Groq(api_key=config.GROQ_API_KEY)
        mini = (f'Farmer query: "{query}"\nReply ONLY with JSON: {{"crop": <name or null>, '
                '"problem": <pest|disease|nutrient|yield|weather|crop_selection|general>}}\nNo explanation.')
        resp = client.chat.completions.create(
            model="gemma2-9b-it",
            messages=[{"role": "user", "content": mini}],
            max_tokens=60, temperature=0,
        )
        text = resp.choices[0].message.content.strip()
        m = re.search(r'\{.*?\}', text, re.DOTALL)
        if m:
            return json.loads(m.group())
    except Exception:
        pass
    return {"crop": None, "problem": "general"}


def rewrite_query_for_retrieval(query: str, crop, problem_type: str, state, season) -> str:
    parts = [query]
    if crop: parts.append(f"{crop} crop")
    if problem_type and problem_type != "general": parts.append(problem_type)
    if state: parts.append(state)
    if season: parts.append(season.get("name",""))
    return " ".join(p for p in parts if p)


def _fetch_mandi_prices(state: str, crop: str) -> dict | None:
    try:
        api_key = getattr(config, "DATA_GOV_API_KEY", None)
        if not api_key: return None
        url = (
            "https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070"
            f"?api-key={api_key}&format=json&limit=5"
            f"&filters[State]={state}&filters[Commodity]={crop}"
        )
        r = requests.get(url, timeout=6)
        if r.ok:
            records = r.json().get("records", [])
            prices = [float(rec["Modal_Price"]) for rec in records if rec.get("Modal_Price")]
            return {"avg": sum(prices)/len(prices), "records": records[:3]} if prices else None
    except Exception:
        pass
    return None


def _build_price_context(season: dict, state: str) -> str:
    cmap = {"Kharif": ["Soybean","Cotton","Rice","Maize","Groundnut"],
            "Rabi": ["Wheat","Gram","Mustard"], "Zaid (Summer)": ["Moong","Urad"]}
    lines = []
    for crop in cmap.get(season.get("name",""), [])[:3]:
        data = _fetch_mandi_prices(state, crop)
        if data:
            lines.append(f"{crop}: \u20b9{data['avg']:,.0f}/quintal ({state} mandi avg)")
    return "CURRENT MANDI PRICES (live):\n" + "\n".join(lines) if lines else ""


def _build_presow_chatbot_context(crop: str, state: str) -> str:
    try:
        from mandi_advisor.enterprise_engine_v2 import get_presow_signal
        sig = get_presow_signal(crop, state)
        if not sig or not sig.get("p50"): return ""
        p25, p50, p75 = sig.get("p25",0), sig["p50"], sig.get("p75",0)
        conf = sig.get("confidence","MEDIUM")
        return (
            f"HARVEST PRICE FORECAST for {crop} in {state} (presow_v4, {conf} confidence):\n"
            f"  Pessimistic (P25): \u20b9{p25:,}/quintal\n"
            f"  Likely (P50):      \u20b9{p50:,}/quintal\n"
            f"  Optimistic (P75):  \u20b9{p75:,}/quintal\n"
            "Use for sowing decision. Do NOT quote old KCC price data."
        )
    except Exception:
        return ""


def _build_pest_risk_chatbot_context(state: str, district: str, crop: str, month: int) -> str:
    try:
        from mandi_advisor.pest_predictor import predict_pest_risk
        results = predict_pest_risk(state, crop, district=district, month=month)
        if not results: return ""
        high = [r for r in results if r.get("risk_score",0) >= 0.5]
        if not high: return ""
        lines = [f"PEST RISK for {crop} in {state} (14-day):"]
        for r in high[:3]:
            lines.append(f"  {r.get('pest_category','?')}: {int(r.get('risk_score',0)*100)}% risk")
        return "\n".join(lines)
    except Exception:
        return ""


def _render_feedback(idx: int, user_q: str, response: str, crop, state, problem: str) -> None:
    col1, col2, _ = st.columns([1, 1, 8])
    fb_key = f"fb_{idx}_{hashlib.md5(response[:50].encode()).hexdigest()[:8]}"
    if fb_key not in st.session_state:
        st.session_state[fb_key] = None
    with col1:
        if st.button("\U0001f44d", key=f"up_{fb_key}", help="Helpful"):
            st.session_state[fb_key] = "up"
    with col2:
        if st.button("\U0001f44e", key=f"dn_{fb_key}", help="Not helpful"):
            st.session_state[fb_key] = "down"


_PROJECT_ROOT = Path(__file__).parent.resolve()
_EW_DATA  = str(_PROJECT_ROOT / "early_warning" / "data")
_EW_MODEL = str(_PROJECT_ROOT / "early_warning" / "model")


@st.cache_data(ttl=86400, show_spinner=False)
def _load_location_lookup() -> "pd.DataFrame":
    try: return pd.read_parquet(f"{_EW_DATA}/location_lookup.parquet")
    except Exception: return pd.DataFrame(columns=["StateName","DistrictName","BlockName","lat","lon"])


@st.cache_data(ttl=86400, show_spinner=False)
def _load_neighbor_lookup() -> dict:
    try:
        df = pd.read_parquet(f"{_EW_DATA}/neighbor_lookup.parquet")
        out: dict = {}
        for r in df.itertuples(index=False):
            out.setdefault(r.BlockName, []).append(r.neighbor_block)
        return out
    except Exception: return {}


@st.cache_data(ttl=86400, show_spinner=False)
def _load_hist_outbreak_rate() -> "pd.DataFrame":
    try: return pd.read_parquet(f"{_EW_DATA}/hist_rate_lookup.parquet")
    except Exception: return pd.DataFrame(columns=["BlockName","Crop","call_month","hist_outbreak_rate"])


@st.cache_data(ttl=86400, show_spinner=False)
def _load_block_top_crops() -> "pd.DataFrame":
    try: return pd.read_parquet(f"{_EW_DATA}/block_top_crops.parquet")
    except Exception: return pd.DataFrame(columns=["BlockName","Crop","count"])


@st.cache_data(ttl=86400, show_spinner=False)
def _load_alerts_lookup() -> "pd.DataFrame":
    try: return pd.read_parquet(f"{_EW_DATA}/block_crop_alerts.parquet")
    except Exception: return pd.DataFrame(columns=["BlockName","Crop","call_month","category","exact_problem","count"])


@st.cache_data(ttl=86400, show_spinner=False)
def _load_national_alerts() -> "pd.DataFrame":
    try: return pd.read_parquet(f"{_EW_DATA}/national_crop_alerts.parquet")
    except Exception: return pd.DataFrame(columns=["Crop","call_month","category","exact_problem","count"])


@st.cache_data(ttl=86400, show_spinner=False)
def _load_wx_baselines() -> dict:
    try:
        df = pd.read_parquet(f"{_EW_DATA}/wx_baselines.parquet")
        return {(float(r.lat_g), float(r.lon_g), int(r.call_month)):
                {"t2m": float(r.t2m_baseline), "tp": float(r.tp_baseline), "trend": float(r.trend_baseline)}
                for r in df.itertuples(index=False)}
    except Exception: return {}


@st.cache_data(ttl=86400, show_spinner=False)
def _load_neighbor_hist_rate() -> dict:
    try:
        df = pd.read_parquet(f"{_EW_DATA}/neighbor_hist_rate.parquet")
        return {(r.BlockName, int(r.call_month)): float(r.neighbor_hist_rate)
                for r in df.itertuples(index=False)}
    except Exception: return {}


@st.cache_data(ttl=86400, show_spinner=False)
def _load_ndvi_baselines() -> dict:
    try:
        df = pd.read_parquet(f"{_EW_DATA}/ndvi_baseline.parquet")
        return {(float(r.lat_g), float(r.lon_g), int(r.call_month)): float(r.ndvi_baseline)
                for r in df.itertuples(index=False)}
    except Exception: return {}


@st.cache_data(ttl=3600, show_spinner=False)
def _load_ndvi_lookup() -> dict:
    try:
        df = pd.read_parquet(f"{_EW_DATA}/ndvi_lookup.parquet")
        return df.groupby(["lat_g","lon_g"])["ndvi"].mean().to_dict()
    except Exception: return {}


@st.cache_resource(show_spinner=False)
def _load_pest_model_v2():
    try:
        import joblib as _jl
        le_s = _jl.load(f"{_EW_MODEL}/v2_le_state.pkl")
        le_c = _jl.load(f"{_EW_MODEL}/v2_le_crop.pkl")
        return le_s, le_c
    except Exception:
        class _DLE:
            classes_ = []
            def transform(self, v): return [0]*len(v)
        return _DLE(), _DLE()


def _get_crop_risk_v2(state: str, district: str, crop: str, month: int) -> tuple:
    try:
        from mandi_advisor.pest_predictor import predict_pest_risk
        results = predict_pest_risk(state, crop, district=district, month=month)
        if results:
            return float(max(r.get("risk_score", 0.0) for r in results)), {}
    except Exception:
        pass
    return 0.3, {}


_CROP_STATE_MAP: dict[str, list] = {
    "Coconut":   ["Kerala","Karnataka","Tamil Nadu","Andhra Pradesh","Goa","Odisha"],
    "Tea":       ["Assam","West Bengal","Kerala","Himachal Pradesh","Uttarakhand","Arunachal Pradesh"],
    "Coffee":    ["Karnataka","Kerala","Tamil Nadu"],
    "Rubber":    ["Kerala","Karnataka","Tamil Nadu","Goa","Andhra Pradesh"],
    "Apple":     ["Himachal Pradesh","Uttarakhand","Jammu & Kashmir","Arunachal Pradesh"],
    "Pineapple": ["Assam","Meghalaya","West Bengal","Kerala","Tripura","Nagaland"],
    "Sugarcane": ["Uttar Pradesh","Maharashtra","Karnataka","Tamil Nadu","Bihar",
                  "Haryana","Punjab","Andhra Pradesh","Gujarat"],
    "Jute":      ["West Bengal","Bihar","Assam","Odisha","Meghalaya"],
    "Cotton":    ["Gujarat","Maharashtra","Telangana","Andhra Pradesh","Punjab",
                  "Haryana","Rajasthan","Madhya Pradesh","Karnataka"],
}


def _is_crop_suitable_for_state(crop: str, state: str) -> bool:
    if crop not in _CROP_STATE_MAP: return True
    return state in _CROP_STATE_MAP[crop]


_TRIGGER_RULES: dict[str, dict] = {
    "late_blight":       {"rain_min": 15, "temp_max": 25},
    "downy_mildew":      {"rain_min": 10, "temp_max": 28},
    "powdery_mildew":    {"rain_max": 5,  "temp_max": 28},
    "blast":             {"rain_min": 10, "temp_min": 20},
    "stem_borer":        {"temp_min": 25},
    "bollworm":          {"temp_min": 25},
    "aphid":             {"temp_max": 25, "rain_max": 5},
    "rust":              {"temp_max": 22, "rain_min": 5},
    "brown_planthopper": {"rain_min": 8,  "temp_min": 26},
    "bacterial_blight":  {"rain_min": 15, "temp_min": 24},
}


def _check_triggers(pest_name: str, wx: dict) -> bool:
    if not wx: return False
    pl = pest_name.lower().replace(" ", "_")
    rules = next((r for k, r in _TRIGGER_RULES.items() if k in pl or pl in k), None)
    if not rules: return False
    try:
        t = float(wx.get("t2m_mean_28d") or 20)
        rain = float(wx.get("tp_total_28d") or 0)
    except (TypeError, ValueError):
        return False
    if "temp_min" in rules and t < rules["temp_min"]: return False
    if "temp_max" in rules and t > rules["temp_max"]: return False
    if "rain_min" in rules and rain < rules["rain_min"]: return False
    if "rain_max" in rules and rain > rules["rain_max"]: return False
    return True


# โ”€โ”€ Multi-Step RAG (#8) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def multi_step_retrieve(
    retriever,
    query: str,
    normalized_query: str,
    detected_crop: "str | None",
    problem_type: str,
    settings: dict,
    state: str = "",
    district: str = "",
) -> "list[RetrievedDoc]":
    """
    Two-step retrieval strategy:
      Step 1 โ€” symptom match:  normalized query + crop filter
      Step 2 โ€” treatment match: query augmented with problem-type keywords
    Results are merged, deduplicated by answer text, re-ranked by score.
    """
    k = settings["top_k"]

    # Step 1: broad symptom / context retrieval
    docs1 = retriever.search(
        normalized_query,
        top_k       = max(k, 4),
        deduplicate = settings["deduplicate"],
        min_score   = settings["min_score"],
        crop_filter = detected_crop,
        state       = state,
        district    = district,
    )

    # Step 2: problem-type focused retrieval (skip if general)
    docs2: list[RetrievedDoc] = []
    if problem_type != "general":
        problem_suffix = {
            "pest":           "insect pest control treatment insecticide",
            "disease":        "disease fungal control treatment fungicide spray",
            "nutrient":       "deficiency fertilizer nutrient dose application",
            "yield":          "production increase variety best practice",
            "weather":        "frost cold heat drought protection remedy",
            "crop_selection": "crop variety season sowing recommendation",
        }.get(problem_type, "")
        if problem_suffix:
            step2_query = f"{normalized_query} {problem_suffix}"
            docs2 = retriever.search(
                step2_query,
                top_k       = max(k, 4),
                deduplicate = settings["deduplicate"],
                min_score   = settings["min_score"],
                crop_filter = detected_crop,
                state       = state,
                district    = district,
            )

    # Merge: prefer step-1 results (more semantically close to query)
    seen_answers: set[str] = set()
    merged: list[RetrievedDoc] = []
    for doc in docs1 + docs2:
        if doc.answer not in seen_answers:
            seen_answers.add(doc.answer)
            merged.append(doc)

    # Re-rank by similarity score, keep top-k
    merged.sort(key=lambda d: d.score, reverse=True)
    merged = merged[:k]
    for i, doc in enumerate(merged):
        doc.rank = i + 1
    return merged


# โ”€โ”€ cached retriever (one load per Streamlit server process) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

class _ProxyRetriever:
    """
    Drop-in replacement for KCCRetriever that calls the retrieval_api.py
    service on port 8502 instead of loading the 6GB FAISS index locally.
    Streamlit stays at ~500MB RAM while retrieval_api.py holds the index.
    Falls back to loading locally if port 8502 is unavailable.
    """
    index_size: int = 16_565_975

    def search(self, query: str, top_k: int = 5, **kwargs):
        import urllib.request as _ur, json as _j
        try:
            data = _j.dumps({"query": query, "top_k": top_k}).encode()
            req  = _ur.Request("http://localhost:8502", data=data,
                               headers={"Content-Type": "application/json"},
                               method="POST")
            with _ur.urlopen(req, timeout=15) as r:
                ctx = _j.loads(r.read())["context"]
            # Return a single fake doc carrying the full context
            from step3_retrieval import RetrievedDoc
            return [RetrievedDoc(score=1.0, answer=ctx,
                                 question="[proxy]", source="retrieval_api")]
        except Exception:
            # Port 8502 not available โ€” fall back to local retriever
            return get_retriever().search(query, top_k=top_k, **kwargs)

    def format_context(self, docs) -> str:
        if not docs:
            return ""
        proxy_docs = [d for d in docs if getattr(d, "question", "") == "[proxy]"]
        real_docs  = [d for d in docs if getattr(d, "question", "") != "[proxy]"]
        parts = []
        seen = set()
        for d in proxy_docs:
            if d.answer not in seen:
                parts.append(d.answer)
                seen.add(d.answer)
        if real_docs:
            parts.append(get_retriever().format_context(real_docs))
        sep = chr(10) + chr(10)
        return sep.join(parts) if parts else get_retriever().format_context(docs)


@st.cache_resource(show_spinner="Connecting to knowledge baseโ€ฆ")
def _load_retriever():
    """Return a _ProxyRetriever if port 8502 is up, else load FAISS locally."""
    import urllib.request as _ur
    try:
        with _ur.urlopen("http://localhost:8502", timeout=3) as r:
            r.read()
        return _ProxyRetriever()
    except Exception:
        return get_retriever()


def _render_early_warning_tab(
        user_state: str | None,
        user_district: str | None,
        user_block: str | None,
        block_lat,
        block_lon,
) -> None:
    """Render early-warning pest risk tab (Tab 5)."""
    if block_lat is None or block_lon is None:
        st.info("๐Ÿ“ Set your location in the sidebar to see the 14-day pest outlook.")
        return
    # โ”€โ”€ Local resource init โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    ndvi_lookup = _load_ndvi_lookup()
    le_state, le_crop = _load_pest_model_v2()
    wx = _fetch_weather(user_state or "")
    if not isinstance(wx, dict): wx = {}
    def _rg(v): return round(round(v / 0.25) * 0.25, 2)
    lat_g = _rg(block_lat); lon_g = _rg(block_lon)
    ndvi_val = ndvi_lookup.get((lat_g, lon_g), np.nan)

    # โ”€โ”€ 5. NEIGHBOR CONTAGION signal โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    neighbor_lookup  = _load_neighbor_lookup()
    hist_df          = _load_hist_outbreak_rate()
    crops_df         = _load_block_top_crops()
    alerts_df        = _load_alerts_lookup()
    nat_alerts       = _load_national_alerts()
    curr_month       = datetime.now().month
    curr_year        = datetime.now().year

    neighbor_blocks  = neighbor_lookup.get(user_block, [])
    block_hist       = hist_df[hist_df["BlockName"] == user_block]
    block_alerts     = alerts_df[alerts_df["BlockName"] == user_block]

    # Neighbor historical outbreak rates for this month (contagion signal)
    neighbor_hist_month = hist_df[
        hist_df["BlockName"].isin(neighbor_blocks) & (hist_df["call_month"] == curr_month)
    ]
    # For each crop, get fraction of neighbors that had high historical outbreak rate
    neighbor_pressure: dict[str, float] = {}
    if len(neighbor_hist_month) > 0:
        n_blocks = len(neighbor_blocks) if neighbor_blocks else 1
        for crop, grp in neighbor_hist_month.groupby("Crop"):
            high_rate = (grp["hist_outbreak_rate"] > 0.4).sum()
            neighbor_pressure[crop] = min(high_rate / n_blocks, 1.0)

    # โ”€โ”€ SEASON FILTER โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    month_hist = block_hist[block_hist["call_month"] == curr_month]
    if len(month_hist) > 0:
        top_crops = (
            month_hist[month_hist["hist_outbreak_rate"] > 0]
            .sort_values("hist_outbreak_rate", ascending=False)
            .head(10)["Crop"].tolist()
        )
        if not top_crops:
            top_crops = month_hist.sort_values("hist_outbreak_rate", ascending=False).head(10)["Crop"].tolist()
    else:
        block_crops = crops_df[crops_df["BlockName"] == user_block]
        top_crops = (block_crops.sort_values("count", ascending=False).head(8)["Crop"].tolist()
                     if len(block_crops) > 0
                     else ["Wheat","Paddy (Dhan)","Chillies","Tomato","Onion","Cotton (Kapas)","Maize (Makka)","Mustard"])

    # โ”€โ”€ Crop relevance filtering (two-layer) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    # Layer 1: Remove generic / uninformative crop categories
    _EXCLUDE_CROPS = {"others", "other", "misc", "miscellaneous", "general"}
    top_crops = [c for c in top_crops if c.lower().strip() not in _EXCLUDE_CROPS]

    # Layer 2: Minimum call count โ€” require at least 5 KCC calls from this block.
    # Crops with 1-2 calls are statistical noise and often geographically wrong.
    block_call_counts = {}
    if len(crops_df) > 0:
        block_crops_all = crops_df[crops_df["BlockName"] == user_block]
        block_call_counts = dict(zip(block_crops_all["Crop"], block_crops_all["count"]))
    MIN_CALL_COUNT = 5
    top_crops = [c for c in top_crops
                 if block_call_counts.get(c, MIN_CALL_COUNT) >= MIN_CALL_COUNT]

    # Layer 3: Geographic suitability โ€” filter crops not grown in this state.
    # Prevents Jack Fruit appearing in MP, coconut in Punjab, etc.
    if user_state:
        geo_filtered = [c for c in top_crops if _is_crop_suitable_for_state(c, user_state)]
        # Only apply if filter didn't remove everything (safety fallback)
        if len(geo_filtered) >= 3:
            top_crops = geo_filtered

    # Encode state
    state_enc_val = int(le_state.transform([user_state])[0]) if user_state in le_state.classes_ else 0

    FEATURES = [
        "lat","lon","call_month","call_season","year",
        "crop_enc","state_enc",
        "t2m_mean_28d","t2m_max_28d","tp_total_28d","tp_mean_7d",
        "skt_mean_28d","stl1_mean_28d","temp_trend",
        "temp_anomaly","rain_anomaly","trend_anomaly",
        "ndvi_t7","ndvi_t14","ndvi_t21","ndvi_t28","ndvi_mean",
        "ndvi_anomaly",
        "hist_outbreak_rate","neighbor_outbreak_rate",
    ]

    # Load v5 lookup tables for anomaly computation
    wx_bl       = _load_wx_baselines()
    nb_hist_map = _load_neighbor_hist_rate()
    ndvi_bl_map = _load_ndvi_baselines()

    # Round GPS to match lookup grid (0.25ยฐ for ERA5, 0.5ยฐ for NDVI)
    lat_g = round(block_lat * 4) / 4   # nearest 0.25ยฐ
    lon_g = round(block_lon * 4) / 4

    # Weather baseline for this location+month
    bl_key = (lat_g, lon_g, curr_month)
    bl = wx_bl.get(bl_key, {})
    temp_baseline  = bl.get("t2m",   wx.get("t2m_mean_28d", np.nan))
    rain_baseline  = bl.get("tp",    wx.get("tp_total_28d", np.nan))
    trend_baseline = bl.get("trend", wx.get("temp_trend",   np.nan))
    ndvi_baseline  = ndvi_bl_map.get(bl_key, ndvi_val)

    # Anomaly = current forecast - climatological baseline
    temp_anomaly  = wx.get("t2m_mean_28d", np.nan)  - temp_baseline
    rain_anomaly  = wx.get("tp_total_28d", np.nan)   - rain_baseline
    trend_anomaly = wx.get("temp_trend",   np.nan)   - trend_baseline
    ndvi_anomaly  = ndvi_val - ndvi_baseline if (ndvi_val and not np.isnan(ndvi_val)) else np.nan

    # Historical neighbor outbreak rate for this block+month
    nb_rate = nb_hist_map.get((user_block, curr_month), 0.5)

    # Build feature rows; also track hist_rate per crop for delta computation
    rows = []
    hist_rates_map: dict[str, float] = {}
    for crop in top_crops:
        crop_enc_val = int(le_crop.transform([crop])[0]) if crop in le_crop.classes_ else 0
        sub = block_hist[(block_hist["Crop"] == crop) & (block_hist["call_month"] == curr_month)]
        hist_rate = float(sub["hist_outbreak_rate"].mean()) if len(sub) > 0 else 0.0
        hist_rates_map[crop] = hist_rate
        rows.append({
            "lat": block_lat, "lon": block_lon,
            "call_month": curr_month, "call_season": (curr_month % 12) // 3,
            "year": curr_year, "crop_enc": crop_enc_val, "state_enc": state_enc_val,
            **{k: wx.get(k, np.nan) for k in ["t2m_mean_28d","t2m_max_28d","tp_total_28d",
                                               "tp_mean_7d","skt_mean_28d","stl1_mean_28d","temp_trend"]},
            "temp_anomaly":  temp_anomaly,
            "rain_anomaly":  rain_anomaly,
            "trend_anomaly": trend_anomaly,
            "ndvi_t7": ndvi_val, "ndvi_t14": ndvi_val,
            "ndvi_t21": ndvi_val, "ndvi_t28": ndvi_val, "ndvi_mean": ndvi_val,
            "ndvi_anomaly": ndvi_anomaly,
            "hist_outbreak_rate":    hist_rate,
            "neighbor_outbreak_rate": nb_rate,
        })

    # โ”€โ”€ 2. Score crops using district v2 stacking model (AUC 0.937) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    # Replaces v5 LightGBM (which returned near-identical scores for all crops).
    # v2 uses 81 crop+weather+history features โ†’ meaningful differentiation per crop.
    results = []
    with st.spinner("Computing outbreak risk with v2 stacking modelโ€ฆ"):
        for crop in top_crops:
            cal_p, _ = _get_crop_risk_v2(
                user_state or "", user_district or "", crop, curr_month
            )
            hist_rate = hist_rates_map.get(crop, 0.0)
            delta     = cal_p - hist_rate
            nb_press  = neighbor_pressure.get(crop, 0.0)
            results.append((crop, cal_p, delta, hist_rate, nb_press))

    results.sort(key=lambda x: x[1], reverse=True)

    def _risk_label(p: float):
        if p >= 0.70: return "๐Ÿ”ด HIGH", "red"
        if p >= 0.45: return "๐ŸŸก MEDIUM", "orange"
        if p >= 0.20: return "๐ŸŸข LOW", "green"
        return "โšช MINIMAL", "gray"

    _CATEGORY_ICON = {"pest": "๐Ÿ›", "disease": "๐Ÿฆ ", "nutrient": "๐ŸŒฟ"}

    def _fmt_problem(name: str) -> str:
        return name.replace("_", " ").title()

    def _get_pest_alerts(crop: str, month: int):
        sub = block_alerts[(block_alerts["Crop"] == crop) & (block_alerts["call_month"] == month)]
        if len(sub) > 0:
            return sub.sort_values("count", ascending=False)[["category","exact_problem"]].values.tolist()[:3]
        nat = nat_alerts[(nat_alerts["Crop"] == crop) & (nat_alerts["call_month"] == month)]
        return nat.sort_values("count", ascending=False)[["category","exact_problem"]].values.tolist()[:3]

    st.markdown("### ๐ŸŒพ Crop Outbreak Risk โ€” Next 14 Days")
    st.caption(f"Based on 14-day weather forecast + satellite data for {user_block} | {len(neighbor_blocks)} neighboring blocks monitored")

    for crop, cal_p, delta, hist_rate, nb_press in results:
        label, _ = _risk_label(cal_p)
        pct = int(cal_p * 100)

        col_a, col_b, col_c = st.columns([3, 1, 2])
        col_a.markdown(f"**{crop}**")
        col_b.markdown(f"`{pct}%`")

        # โ”€โ”€ 3. Delta label โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        if abs(delta) >= 0.05:
            arrow = "โ†‘" if delta > 0 else "โ†“"
            delta_str = f"{arrow}{abs(delta)*100:.0f}% vs normal"
            col_c.markdown(f"{label}  <span style='font-size:0.8em;color:{'tomato' if delta>0 else 'steelblue'}'>{delta_str}</span>",
                           unsafe_allow_html=True)
        else:
            col_c.markdown(f"{label}  <span style='font-size:0.8em;color:gray'>โ‰ˆ normal</span>",
                           unsafe_allow_html=True)
        st.progress(min(pct, 100))

        # Specific pest/disease + trigger check
        pest_alerts = _get_pest_alerts(crop, curr_month)
        info_parts = []

        # โ”€โ”€ 4. TRIGGER RULES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        triggered = []
        for cat, pname in pest_alerts:
            icon = _CATEGORY_ICON.get(cat, "โš ๏ธ")
            display = f"{icon} {_fmt_problem(pname)}"
            if _check_triggers(pname, wx):
                display += " โšก"   # lightning = weather conditions are ACTIVE right now
                triggered.append(_fmt_problem(pname))
            info_parts.append(display)

        if info_parts and pct >= 20:
            st.caption(f"Likely threats: {' ยท '.join(info_parts)}"
                       + ("  *(โšก = forecast conditions actively favour this)*" if triggered else ""))

        # โ”€โ”€ 5. NEIGHBOR CONTAGION โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        if nb_press >= 0.15 and pct >= 20:
            st.caption(f"๐Ÿ”— Contagion signal: {nb_press*100:.0f}% of nearby blocks have high {crop} outbreak history this month.")

        # โ”€โ”€ 6. EXPLAINABILITY โ€” WHY this risk score โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        # B2B requirement: show the specific factors driving the prediction.
        # Surface top 2-3 drivers so agronomists can validate the model's reasoning.
        if pct >= 20:
            why_parts = []
            t_mean = wx.get("t2m_mean_28d", np.nan)
            rh_mean = wx.get("_rh_mean", np.nan)
            rain14  = wx.get("tp_total_28d", np.nan)
            t_anom  = temp_anomaly if not (isinstance(temp_anomaly, float) and np.isnan(temp_anomaly)) else None
            r_anom  = rain_anomaly if not (isinstance(rain_anomaly, float) and np.isnan(rain_anomaly)) else None

            # Temperature driver
            if t_mean and not np.isnan(t_mean):
                if t_anom is not None and abs(t_anom) >= 1.5:
                    direction = "above" if t_anom > 0 else "below"
                    why_parts.append(
                        f"๐ŸŒก๏ธ Temp **{t_mean:.1f}ยฐC** ({'+' if t_anom>0 else ''}{t_anom:.1f}ยฐC {direction} normal) "
                        f"{'โ†’ warm/humid = disease-favourable' if t_anom > 0 else 'โ†’ cooler than normal'}"
                    )
                else:
                    why_parts.append(f"๐ŸŒก๏ธ Forecast temp: **{t_mean:.1f}ยฐC** (near normal)")

            # Humidity driver
            if rh_mean and not np.isnan(rh_mean) and rh_mean > 0:
                rh_label = "๐Ÿ”ด high" if rh_mean >= 75 else ("๐ŸŸก moderate" if rh_mean >= 55 else "๐ŸŸข low")
                why_parts.append(f"๐Ÿ’ง Humidity: **{rh_mean:.0f}%** ({rh_label}) โ†’ {'favours fungal diseases' if rh_mean>=75 else 'within normal range'}")

            # Rainfall driver
            if rain14 and not np.isnan(rain14):
                if r_anom is not None and abs(r_anom) >= 10:
                    direction = "more" if r_anom > 0 else "less"
                    why_parts.append(
                        f"๐ŸŒง๏ธ Rain forecast: **{rain14:.0f}mm/14d** ({'+' if r_anom>0 else ''}{r_anom:.0f}mm {direction} than usual)"
                        + (" โ†’ excess moisture = blight/rot risk" if r_anom > 20 else "")
                    )
                elif rain14 > 0:
                    why_parts.append(f"๐ŸŒง๏ธ Rain forecast: **{rain14:.0f}mm/14d** (normal range)")

            # Historical context driver
            if hist_rate > 0.3:
                why_parts.append(
                    f"๐Ÿ“‹ History: **{hist_rate*100:.0f}% of past years** this block had {crop} problems in this month"
                )
            elif hist_rate > 0.1:
                why_parts.append(
                    f"๐Ÿ“‹ History: {hist_rate*100:.0f}% historical outbreak rate for {crop} this month in this area"
                )

            # Neighbor pressure driver
            if nb_press >= 0.3:
                why_parts.append(f"๐Ÿ—บ๏ธ Spread risk: **{nb_press*100:.0f}% of neighboring blocks** historically have outbreaks this month")

            if why_parts:
                with st.expander(f"๐Ÿ” Why {pct}% for {crop}? (click to expand)", expanded=False):
                    for part in why_parts:
                        st.markdown(f"- {part}")
                    st.caption(
                        "Model: Stacking ensemble (LGB + XGB + CatBoost) | "
                        "81 features | AUC 0.937 | 19 years training data"
                    )

    st.markdown("---")

    # Alert summary for high-risk crops
    high_risk = [(c, p, d, hr, nb) for c, p, d, hr, nb in results if p >= 0.45]
    if high_risk:
        st.markdown("### โš ๏ธ Alert Summary")
        for crop, cal_p, delta, hist_rate, nb_press in high_risk[:4]:
            label, _ = _risk_label(cal_p)
            pest_alerts = _get_pest_alerts(crop, curr_month)
            triggered_pests = [_fmt_problem(pn) for _, pn in pest_alerts if _check_triggers(pn, wx)]
            all_pests = [f"{_CATEGORY_ICON.get(cat,'โš ๏ธ')} **{_fmt_problem(ep)}**" for cat, ep in pest_alerts]

            delta_note = ""
            if abs(delta) >= 0.05:
                delta_note = f" (โ†‘{delta*100:.0f}% above normal)" if delta > 0 else f" (โ†“{abs(delta)*100:.0f}% below normal)"

            body = (f"**{crop}** โ€” {label} ({int(cal_p*100)}%){delta_note} in **{user_block}** over next 14 days.\n\n")
            if all_pests:
                body += f"Likely threats: {', '.join(all_pests)}\n\n"
            if triggered_pests:
                body += f"โšก **Weather conditions right now actively favour: {', '.join(triggered_pests)}** โ€” act soon.\n\n"
            if nb_press >= 0.15:
                body += f"๐Ÿ”— {nb_press*100:.0f}% of nearby blocks show high historical outbreak pressure.\n\n"

            # Inline WHY for alert summary (B2B explainability requirement)
            t_mean = wx.get("t2m_mean_28d", np.nan)
            rh_mean = wx.get("_rh_mean", 0)
            rain14 = wx.get("tp_total_28d", np.nan)
            why_inline = []
            if t_mean and not np.isnan(t_mean):
                t_anom_v = temp_anomaly if not (isinstance(temp_anomaly, float) and np.isnan(temp_anomaly)) else 0
                if abs(t_anom_v) >= 1.5:
                    why_inline.append(f"temp {t_mean:.1f}ยฐC ({'+' if t_anom_v>0 else ''}{t_anom_v:.1f}ยฐC vs normal)")
                else:
                    why_inline.append(f"temp {t_mean:.1f}ยฐC")
            if rh_mean and rh_mean > 0:
                why_inline.append(f"humidity {rh_mean:.0f}%")
            if rain14 and not np.isnan(rain14):
                why_inline.append(f"rain {rain14:.0f}mm/14d")
            if hist_rate > 0.2:
                why_inline.append(f"{hist_rate*100:.0f}% historical rate")
            if why_inline:
                body += f"๐Ÿ“Š **Key factors:** {' ยท '.join(why_inline)}\n\n"

            st.warning(body)

            # โ”€โ”€ One-click "Ask chatbot" button โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            threat_str_btn = (", ".join(_fmt_problem(ep) for _, ep in pest_alerts)
                              if pest_alerts else "pest outbreak")
            btn_label = f"๐Ÿ’ฌ How to protect my {crop} from {threat_str_btn.split(',')[0]}?"
            if st.button(btn_label, key=f"ew_ask_{crop}", use_container_width=True):
                # Pre-fill the chatbot with a specific actionable question
                st.session_state["prefill_query"] = (
                    f"Early warning system ne alert kiya hai ki {user_block} mein "
                    f"{crop} mein {threat_str_btn} ka khatra hai agle 14 dinon mein. "
                    f"Abhi se kya kadam uthaun? Konsi dawai lagaun aur kab?"
                )
                st.rerun()

        # Store in session for chatbot context
        alert_parts = []
        for c, p, d, hr, nb in high_risk[:3]:
            pests = _get_pest_alerts(c, curr_month)
            threat_str = ", ".join(_fmt_problem(ep) for _, ep in pests) if pests else "outbreak"
            alert_parts.append(f"{c}: {threat_str} ({int(p*100)}%)")
        st.session_state["ew_alert"] = (
            f"Early warning detected for {user_block}, {user_district}, {user_state} โ€” next 14 days: "
            + "; ".join(alert_parts)
        )
    else:
        st.success("โœ… No significant outbreaks predicted for this location in the next 14 days.")
        st.session_state.pop("ew_alert", None)

    st.markdown("---")
    st.caption(
        f"โš™๏ธ Stacking Ensemble (LGB + XGB + CatBoost) v2 | 81 features: weather + crop ร— pest baselines + spatial contagion | "
        f"AUC 0.937 | 475 districts ยท 26 crop groups ยท 19-year training data (2007โ€“2025) | "
        f"1-month early warning | {len(neighbor_blocks)} neighbor blocks ยท {len(top_crops)} in-season crops shown"
    )


# โ”€โ”€ Streamlit UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def _sidebar(retriever: KCCRetriever) -> dict:
    """Render sidebar controls; return settings dict."""
    # Branded sidebar header
    st.sidebar.markdown("""
<div style="background:rgba(255,255,255,0.1);border-radius:12px;padding:14px 16px;
            margin-bottom:16px;border:1px solid rgba(255,255,255,0.2);text-align:center;">
    <div style="font-size:1.4rem;font-weight:700;color:#FFFFFF;letter-spacing:-0.01em;">๐ŸŒพ Farm Advisor</div>
    <div style="font-size:0.72rem;color:#A5D6A7;letter-spacing:0.08em;text-transform:uppercase;margin-top:3px;">
        AI Farm Advisor
    </div>
</div>
""", unsafe_allow_html=True)

    # System status (no technical details visible to clients)
    st.sidebar.markdown('<p style="color:#A5D6A7;font-size:0.78rem;font-weight:600;letter-spacing:0.07em;text-transform:uppercase;margin:0 0 8px 2px;">โšก System Status</p>', unsafe_allow_html=True)
    st.sidebar.markdown('<div style="background:rgba(165,214,167,0.15);border-radius:8px;padding:10px 12px;border:1px solid rgba(165,214,167,0.3);"><div style="color:#FFFFFF;font-size:0.88rem;font-weight:600;">โœ… All Systems Online</div><div style="color:#A5D6A7;font-size:0.78rem;margin-top:4px;">Knowledge base ready ยท AI active</div></div>', unsafe_allow_html=True)

    st.sidebar.markdown('<hr style="border-color:rgba(255,255,255,0.12);margin:12px 0;">', unsafe_allow_html=True)
    st.sidebar.markdown('<p style="color:#A5D6A7;font-size:0.78rem;font-weight:600;letter-spacing:0.07em;text-transform:uppercase;margin:0 0 8px 2px;">๐Ÿ“ Your Location</p>', unsafe_allow_html=True)
    st.sidebar.caption("Set once โ€” weather, crops, prices all update for your area")

    # Load location data for cascading dropdowns
    loc_df = _load_location_lookup()
    all_states = sorted(loc_df["StateName"].unique().tolist()) if len(loc_df) else []

    # State dropdown
    state_options = ["โ€” Select State โ€”"] + all_states
    prev_state = st.session_state.get("user_state_sel", "โ€” Select State โ€”")
    state_sel = st.sidebar.selectbox("State", state_options,
                                     index=state_options.index(prev_state)
                                     if prev_state in state_options else 0,
                                     key="user_state_sel")
    user_state_loc = None if state_sel == "โ€” Select State โ€”" else state_sel

    # District dropdown (filtered by state)
    user_district_loc = None
    user_block_loc    = None
    block_lat = block_lon = None

    if user_state_loc:
        dist_df = loc_df[loc_df["StateName"] == user_state_loc]
        districts = ["โ€” Select District โ€”"] + sorted(dist_df["DistrictName"].unique().tolist())
        prev_dist = st.session_state.get("user_district_sel", "โ€” Select District โ€”")
        dist_sel = st.sidebar.selectbox("District", districts,
                                        index=districts.index(prev_dist)
                                        if prev_dist in districts else 0,
                                        key="user_district_sel")
        user_district_loc = None if dist_sel == "โ€” Select District โ€”" else dist_sel

        # Block dropdown (filtered by state + district)
        if user_district_loc:
            block_df = dist_df[dist_df["DistrictName"] == user_district_loc]
            blocks = ["โ€” Select Block โ€”"] + sorted(block_df["BlockName"].unique().tolist())
            prev_block = st.session_state.get("user_block_sel", "โ€” Select Block โ€”")
            block_sel = st.sidebar.selectbox("Block", blocks,
                                             index=blocks.index(prev_block)
                                             if prev_block in blocks else 0,
                                             key="user_block_sel")
            user_block_loc = None if block_sel == "โ€” Select Block โ€”" else block_sel

            if user_block_loc:
                row = block_df[block_df["BlockName"] == user_block_loc].iloc[0]
                block_lat = float(row["lat"])
                block_lon = float(row["lon"])
                st.sidebar.success(f"๐Ÿ“ {user_block_loc}")
                st.sidebar.caption(f"GPS: {block_lat:.3f}ยฐN, {block_lon:.3f}ยฐE")

    st.sidebar.markdown('<hr style="border-color:rgba(255,255,255,0.12);margin:12px 0;">', unsafe_allow_html=True)
    st.sidebar.markdown('<p style="color:#A5D6A7;font-size:0.78rem;font-weight:600;letter-spacing:0.07em;text-transform:uppercase;margin:0 0 8px 2px;">โš™๏ธ Retrieval Settings</p>', unsafe_allow_html=True)

    top_k = st.sidebar.slider(
        "Results to retrieve (top-K)",
        min_value=1, max_value=15, value=config.TOP_K, step=1,
    )
    min_score = st.sidebar.slider(
        "Min. similarity score",
        min_value=0.0, max_value=1.0, value=0.0, step=0.05,
        help="Discard results below this cosine similarity threshold.",
    )
    deduplicate = st.sidebar.checkbox("Deduplicate identical answers", value=True)
    show_sources = st.sidebar.checkbox("Show sources after each answer", value=True)

    # Store location in session state for use across tabs
    st.session_state["user_state_loc"]    = user_state_loc
    st.session_state["user_district_loc"] = user_district_loc
    st.session_state["user_block_loc"]    = user_block_loc
    st.session_state["block_lat"]         = block_lat
    st.session_state["block_lon"]         = block_lon

    # Also set active_state for existing weather/mandi features
    manual_state = user_state_loc

    # Live weather widget in sidebar
    wx_state = manual_state or st.session_state.get("active_state")
    if wx_state and wx_state in STATE_COORDS:
        with st.sidebar:
            wx = _fetch_weather(wx_state)
            daily = wx.get("daily", {})
            if daily.get("time"):
                tmax = daily["temperature_2m_max"]
                rain = daily.get("precipitation_sum", [0])
                code = daily.get("weathercode", [0])
                desc = _WMO_CODES.get(code[0] if code else 0, "")
                st.sidebar.markdown(f"**๐ŸŒค๏ธ Today โ€” {wx_state}**")
                st.sidebar.markdown(
                    f"{desc} | ๐ŸŒก๏ธ {tmax[0]}ยฐC | ๐ŸŒง๏ธ {rain[0]:.1f}mm rain"
                )

    st.sidebar.markdown('<hr style="border-color:rgba(255,255,255,0.12);margin:12px 0;">', unsafe_allow_html=True)
    if st.sidebar.button("๐Ÿ—‘๏ธ  Clear Chat History"):
        st.session_state.messages       = []
        st.session_state.retrieval      = []
        st.session_state.active_crop    = None
        st.session_state.active_problem = "general"
        st.session_state.active_state   = None
        st.session_state.topic_origin   = None
        st.rerun()

    return {
        "top_k": top_k,
        "min_score": min_score,
        "deduplicate": deduplicate,
        "show_sources": show_sources,
        "manual_state": manual_state,
    }


def _render_sources(docs: List[RetrievedDoc]) -> None:
    """Render retrieved source documents in an expander."""
    if not docs:
        return
    # Warn if majority of sources are pre-2015 (outdated pesticide registrations)
    years = []
    for doc in docs:
        try:
            years.append(int(doc.year))
        except (ValueError, TypeError):
            pass
    old_count = sum(1 for y in years if y < 2015)
    if years and old_count > len(years) // 2:
        st.caption(
            f"โš ๏ธ **Note**: {old_count}/{len(docs)} sources are from before 2015. "
            "Pesticide registrations and doses may have been updated โ€” verify with "
            "your local agriculture extension officer or KVK."
        )

    with st.expander(f"๐Ÿ“š Sources ({len(docs)} retrieved Q&A pairs)", expanded=False):
        for doc in docs:
            try:
                yr = int(doc.year)
                year_tag = f"๐Ÿ“… {doc.year}" + (" โš ๏ธ *old*" if yr < 2015 else "")
            except (ValueError, TypeError):
                year_tag = f"๐Ÿ“… {doc.year}"
            st.markdown(
                f"**#{doc.rank}** โ€” "
                f"๐ŸŽฏ {doc.score_pct} similarity | "
                f"๐Ÿ“ {doc.state} | ๐ŸŒฟ {doc.crop} | {year_tag}"
            )
            st.markdown(f"> **Q:** {doc.query}")
            st.markdown(f"> **A:** {doc.answer[:500]}{'โ€ฆ' if len(doc.answer) > 500 else ''}")
            st.markdown("---")


_IMAGE_VISION_PROMPT = """You are an expert plant pathologist. Carefully examine this crop photo.

Return ONLY a valid JSON object with these exact fields:
{
  "crop": "<crop name in English, e.g. Wheat, Rice, Cotton, Tomato>",
  "condition": "<disease/pest/deficiency name, or 'Healthy' if no problem>",
  "problem_type": "<one of: disease, pest, nutrient, healthy>",
  "confidence": "<High / Medium / Low>",
  "visible_symptoms": "<1-2 sentence description of what you see in the image>"
}

Rules:
- crop: identify from leaf/plant shape, colour, structure
- condition: be specific (e.g. "Yellow Rust", "Stem Borer", "Nitrogen Deficiency")
- If the image is unclear, set confidence to "Low" and do your best
- Return ONLY the JSON, absolutely no other text"""


@st.cache_data(ttl=3600, show_spinner=False)
def _diagnose_image_gemini(image_bytes: bytes) -> dict:
    """
    Use Gemini Vision (multimodal) to diagnose crop disease from an image.
    Replaces the HuggingFace classifier which:
      - doesn't support Indian crops (wheat, rice, cotton missing from PlantVillage)
      - has cold-start / 503 issues on the free inference API
    Gemini Vision supports ANY crop and is always available on our existing API key.
    Cached by image content hash (ttl=1h) โ€” won't re-call Gemini on tab switch.
    Returns dict: {crop, condition, problem_type, confidence, visible_symptoms}
    """
    client = _get_gemini_client()
    if client is None:
        return {}
    try:
        from google.genai import types as _gtypes
        # Detect image mime type from magic bytes
        if image_bytes[:3] == b'\xff\xd8\xff':
            mime = "image/jpeg"
        elif image_bytes[:8] == b'\x89PNG\r\n\x1a\n':
            mime = "image/png"
        elif image_bytes[:4] == b'RIFF' and image_bytes[8:12] == b'WEBP':
            mime = "image/webp"
        else:
            mime = "image/jpeg"  # fallback

        response = client.models.generate_content(
            model    = config.GEMINI_MODEL,
            contents = [
                _gtypes.Part.from_bytes(data=image_bytes, mime_type=mime),
                _IMAGE_VISION_PROMPT,
            ],
        )
        text = response.text.strip()
        match = re.search(r'\{.*\}', text, re.DOTALL)
        if match:
            return json.loads(match.group())
        return {}
    except Exception as e:
        # Return error in dict so @st.cache_data (which forbids session_state) can propagate it
        return {"_error": str(e)}


def _render_image_diagnosis(retriever: KCCRetriever, settings: dict) -> None:
    """Render the image diagnosis tab (Gemini Vision powered)."""
    st.markdown("### ๐Ÿ“ท Upload a leaf/plant photo for instant disease diagnosis")
    st.caption(
        "Powered by Gemini Vision โ€” supports **any Indian crop**: "
        "Wheat, Rice, Cotton, Tomato, Potato, Sugarcane, and more."
    )

    uploaded = st.file_uploader(
        "Upload crop photo", type=["jpg", "jpeg", "png", "webp"],
        help="Take a close-up photo of the affected leaf or plant part."
    )

    if uploaded is None:
        st.info("Upload a photo to get started. Gemini AI will identify the crop, disease, and suggest treatment.")
        return

    # Read bytes FIRST โ€” st.image() advances the file pointer to EOF,
    # so uploaded.read() after st.image() returns b"" (empty).
    image_bytes = uploaded.read()

    col1, col2 = st.columns([1, 1])
    with col1:
        st.image(image_bytes, caption="Uploaded image", use_container_width=True)

    with col2:
        with st.spinner("๐Ÿ”ฌ Gemini Vision analyzing your crop photoโ€ฆ"):
            diagnosis = _diagnose_image_gemini(image_bytes)

        if diagnosis.get("_error"):
            st.error(f"Could not analyze image. Error: {diagnosis['_error']}")
            return
        if not diagnosis or not diagnosis.get("crop"):
            st.error("Could not analyze image. Gemini returned an unexpected response. Please try another photo.")
            return

        crop_name    = diagnosis.get("crop", "Unknown")
        condition    = diagnosis.get("condition", "Unknown")
        problem_type = diagnosis.get("problem_type", "disease")
        confidence   = diagnosis.get("confidence", "Medium")
        symptoms     = diagnosis.get("visible_symptoms", "")

        # Confidence colour
        conf_color = {"High": "๐ŸŸข", "Medium": "๐ŸŸก", "Low": "๐Ÿ”ด"}.get(confidence, "๐ŸŸก")

        st.markdown(f"### ๐ŸŒฟ Crop: **{crop_name}**")
        st.markdown(f"### ๐Ÿฆ  Condition: **{condition}**")
        st.markdown(f"{conf_color} Confidence: **{confidence}**")
        if symptoms:
            st.caption(f"*{symptoms}*")

        if problem_type == "healthy" or condition.lower() == "healthy":
            st.success("โœ… Your plant looks healthy! No disease or pest detected.")
            return

        # Auto-query RAG for treatment
        st.markdown("---")
        st.markdown("**๐Ÿ’Š Getting treatment advice from KCC knowledge baseโ€ฆ**")

        auto_query = f"{crop_name} {condition} control treatment"
        with st.spinner("Retrieving KCC advisoryโ€ฆ"):
            docs = multi_step_retrieve(
                retriever, auto_query, normalize_query(auto_query),
                crop_name, problem_type, settings,
            )

        if docs:
            context = retriever.format_context(docs)
            safety  = SAFETY_GUARDRAILS.get(problem_type, "")
            meta = (
                f"DETECTED CROP: {crop_name}\n"
                f"DETECTED CONDITION: {condition} (Gemini Vision, confidence: {confidence})\n"
                f"PROBLEM TYPE: {problem_type.upper()}\n"
                f"VISIBLE SYMPTOMS: {symptoms}\n"
                f"{safety}\n\n"
            )
            prompt = _build_prompt(
                f"My {crop_name} has {condition}. What should I do?",
                meta + context, [], problem_type, "Hindi"
            )
            with st.chat_message("assistant", avatar="๐Ÿค–"):
                img_response = st.write_stream(_stream_llm_response(prompt))
            for w in check_banned_pesticides(img_response):
                st.error(w)
            _render_sources(docs)
        else:
            st.warning("No specific KCC advice found. Consult your local KVK or call KCC helpline 1551.")


# โ”€โ”€ MSP 2025-26 (Ministry of Agriculture, GoI) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Source: PIB โ€” Kharif MSP 2025-26 (Jun 2025) + Rabi MSP 2025-26 (Oct 2024)
_MSP_2025: dict[str, int] = {
    # Rabi 2025-26
    "Wheat":     2425,   # PIB Oct 2024 (unchanged)
    "Gram":      5650,   # was โ‚น5,440
    "Mustard":   6200,   # was โ‚น5,950
    # Kharif 2025-26
    "Rice":      2425,   # was โ‚น2,300
    "Maize":     2400,   # was โ‚น2,225
    "Sorghum":   3590,   # was โ‚น3,371
    "Bajra":     2735,   # was โ‚น2,625
    "Arhar":     7550,   # unchanged
    "Moong":     8682,   # was โ‚น8,558
    "Urad":      7800,   # was โ‚น7,400
    "Groundnut": 7011,   # was โ‚น6,783
    "Soybean":   4892,   # unchanged
    "Sunflower":  7280,  # unchanged
    "Sesame":    9574,   # was โ‚น9,267
    "Cotton":    7121,   # medium staple (long: โ‚น7,521)
}

# โ”€โ”€ ICAR Variety Recommender Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Source: ICAR-CIMMYT, State Agriculture Universities, Seed Authority of India
# Format: crop โ†’ list of {name, maturity_days, yield_q_acre, states, traits, seed_rate, source}
_VARIETY_DATA: dict[str, list[dict]] = {
    "Soybean": [
        {"name": "JS 9305", "days": "95-100", "yield": "8-12", "seed_rate": "30-35 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra", "Rajasthan"],
         "traits": "High yielding, YMV tolerant, suitable for late sowing",
         "source": "JNKVV Jabalpur / state seed corp"},
        {"name": "NRC 37", "days": "100-105", "yield": "10-14", "seed_rate": "30-35 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra"],
         "traits": "ICAR-IISR variety, charcoal rot tolerant, high protein",
         "source": "ICAR-IISR Indore / NSC"},
        {"name": "JS 335", "days": "95-100", "yield": "8-10", "seed_rate": "30-35 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra", "Rajasthan", "Gujarat"],
         "traits": "Most popular, wide adaptability, good for rain-fed",
         "source": "State seed corp / private dealers"},
    ],
    "Wheat": [
        {"name": "HD 3086 (Pusa Samridhi)", "days": "120-125", "yield": "18-22", "seed_rate": "40-45 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "West Bengal", "Madhya Pradesh"],
         "traits": "High yield, rust resistant, ICAR-IARI variety",
         "source": "ICAR-IARI Delhi / NSC / state corp"},
        {"name": "PBW 725", "days": "155-160", "yield": "20-25", "seed_rate": "40-45 kg/acre",
         "states": ["Punjab", "Haryana", "Himachal Pradesh"],
         "traits": "Best for Punjab-Haryana, lodging resistant, chapati quality",
         "source": "PAU Ludhiana / Punjab Seed Corp"},
        {"name": "DBW 187", "days": "145-150", "yield": "20-24", "seed_rate": "40 kg/acre",
         "states": ["Haryana", "Uttar Pradesh", "Rajasthan", "Madhya Pradesh"],
         "traits": "Yellow rust resistant, high protein, good chapati quality",
         "source": "ICAR-IIWBR Karnal / state seed corp"},
    ],
    "Rice": [
        {"name": "Pusa Basmati 1121", "days": "140-145", "yield": "16-18", "seed_rate": "5-6 kg/acre (nursery)",
         "states": ["Punjab", "Haryana", "Uttar Pradesh"],
         "traits": "Premium export basmati, extra-long grain, 20-25% price premium",
         "source": "ICAR-IARI / state seed corp"},
        {"name": "Swarna (MTU 7029)", "days": "145-155", "yield": "22-26", "seed_rate": "5-6 kg/acre",
         "states": ["West Bengal", "Odisha", "Andhra Pradesh", "Bihar", "Assam"],
         "traits": "Most popular non-basmati, flood tolerant, good for Eastern India",
         "source": "ANGRAU / state seed corp"},
        {"name": "IR 64", "days": "125-130", "yield": "20-24", "seed_rate": "5-6 kg/acre",
         "states": ["Karnataka", "Tamil Nadu", "Maharashtra", "Andhra Pradesh"],
         "traits": "Blast resistant, short duration, widely available",
         "source": "State seed corporations"},
    ],
    "Cotton": [
        {"name": "MRC 7031 (Bt)", "days": "160-175", "yield": "12-16 q lint", "seed_rate": "1 packet/acre (450g)",
         "states": ["Maharashtra", "Madhya Pradesh", "Telangana"],
         "traits": "Bt hybrid, CLCuV tolerant, good staple length, medium bollworm resistance",
         "source": "Mahyco / agri dealers"},
        {"name": "RCH 650 (Bt)", "days": "155-165", "yield": "10-14 q lint", "seed_rate": "1 packet/acre",
         "states": ["Gujarat", "Rajasthan", "Punjab"],
         "traits": "Compact plant, suitable for high density planting, drought tolerant",
         "source": "Rasi Seeds / agri dealers"},
        {"name": "Ajeet 155 (Non-Bt)", "days": "150-160", "yield": "10-12 q lint", "seed_rate": "1 packet/acre",
         "states": ["Telangana", "Andhra Pradesh", "Karnataka"],
         "traits": "Non-Bt option for organic farming, good fibre quality",
         "source": "Ajeet Seeds / agri dealers"},
    ],
    "Maize": [
        {"name": "DKC 9144", "days": "95-105", "yield": "25-30", "seed_rate": "8-10 kg/acre",
         "states": ["Karnataka", "Andhra Pradesh", "Bihar", "Rajasthan"],
         "traits": "High yield hybrid, good in kharif + rabi both, widely available",
         "source": "Dekalb-Bayer / agri dealers"},
        {"name": "P3401 (Pioneer)", "days": "90-100", "yield": "28-35", "seed_rate": "8-10 kg/acre",
         "states": ["Punjab", "Haryana", "Madhya Pradesh", "Rajasthan"],
         "traits": "Best yield potential, lodging resistant, early maturity",
         "source": "Corteva Agriscience / agri dealers"},
        {"name": "Shaktiman 1 (OPV)", "days": "90-100", "yield": "18-22", "seed_rate": "8-10 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "West Bengal", "Madhya Pradesh"],
         "traits": "Open-pollinated, cheaper seed, farmer can save seed",
         "source": "VPKAS / state seed corp"},
    ],
    "Mustard": [
        {"name": "Pusa Bold", "days": "120-125", "yield": "8-10", "seed_rate": "1.5-2 kg/acre",
         "states": ["Rajasthan", "Haryana", "Uttar Pradesh", "Madhya Pradesh"],
         "traits": "Most popular, high oil content (42%), bold seed",
         "source": "ICAR-IARI / state seed corp / NSC"},
        {"name": "RH 749", "days": "120-125", "yield": "9-11", "seed_rate": "1.5 kg/acre",
         "states": ["Haryana", "Punjab", "Rajasthan"],
         "traits": "Aphid tolerant, Alternaria tolerant, high oil content",
         "source": "HAU Hisar / Haryana seed corp"},
        {"name": "Giriraj", "days": "125-130", "yield": "8-10", "seed_rate": "1.5-2 kg/acre",
         "states": ["Rajasthan", "Gujarat", "Madhya Pradesh"],
         "traits": "Drought tolerant, good for arid zones, consistent yield",
         "source": "IARI / state seed corp"},
    ],
    "Groundnut": [
        {"name": "GG 20", "days": "110-115", "yield": "15-18", "seed_rate": "60-65 kg/acre",
         "states": ["Gujarat", "Rajasthan"],
         "traits": "ICRISAT variety, high oil, tolerant to dry spells",
         "source": "GAU Junagadh / Gujarat seed corp"},
        {"name": "TAG 24", "days": "105-110", "yield": "14-16", "seed_rate": "60-65 kg/acre",
         "states": ["Andhra Pradesh", "Karnataka", "Tamil Nadu"],
         "traits": "Tolerant to late leaf spot, drought tolerant, widely grown South India",
         "source": "ICRISAT / state seed corp"},
        {"name": "TG 37A", "days": "100-105", "yield": "14-18", "seed_rate": "55-60 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra", "Rajasthan"],
         "traits": "Early maturing, suitable for short season, good oil content",
         "source": "ICRISAT / agri dealers"},
    ],
    "Arhar": [
        {"name": "Asha (ICPL 87119)", "days": "155-165", "yield": "8-12", "seed_rate": "6-8 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra", "Gujarat", "Uttar Pradesh"],
         "traits": "Wilt resistant, medium duration, widely adaptable",
         "source": "ICRISAT / state seed corp"},
        {"name": "Maruti", "days": "150-160", "yield": "8-10", "seed_rate": "6-8 kg/acre",
         "states": ["Maharashtra", "Karnataka", "Andhra Pradesh"],
         "traits": "Early maturing for medium duration, wilt tolerant",
         "source": "UAS Dharwad / state seed corp"},
    ],
    "Gram": [
        {"name": "JG 11", "days": "100-110", "yield": "8-10", "seed_rate": "25-30 kg/acre",
         "states": ["Madhya Pradesh", "Maharashtra", "Rajasthan"],
         "traits": "JNKVV variety, Fusarium wilt resistant, widely grown in MP",
         "source": "JNKVV Jabalpur / state seed corp"},
        {"name": "Pusa 362", "days": "115-120", "yield": "8-10", "seed_rate": "25-30 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "Haryana", "Rajasthan"],
         "traits": "ICAR-IARI variety, ascochyta blight resistant",
         "source": "ICAR-IARI / state seed corp / NSC"},
    ],
    "Bajra": [
        {"name": "HHB 67 Improved", "days": "65-70", "yield": "10-14", "seed_rate": "1.5-2 kg/acre",
         "states": ["Haryana", "Rajasthan", "Gujarat", "Punjab"],
         "traits": "Downy mildew resistant, drought tolerant, best for arid/semi-arid zones",
         "source": "HAU Hisar / AICSIP / state seed corp"},
        {"name": "ICMH 356 (hybrid)", "days": "70-75", "yield": "14-18", "seed_rate": "1.5 kg/acre",
         "states": ["Rajasthan", "Gujarat", "Maharashtra", "Andhra Pradesh"],
         "traits": "High-yield hybrid, ICRISAT variety, dual purpose (grain + fodder)",
         "source": "ICRISAT / Kaveri Seeds / agri dealers"},
        {"name": "Pusa Composite 383", "days": "75-80", "yield": "10-12", "seed_rate": "2 kg/acre",
         "states": ["All states"],
         "traits": "OPV variety, farmer can save seed, wide adaptability, consistent yield",
         "source": "ICAR-IARI / NSC / state seed corp"},
    ],
    "Moong": [
        {"name": "Pusa Vishal (MH 421)", "days": "65-70", "yield": "5-7", "seed_rate": "10-12 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "Haryana", "Rajasthan", "Punjab"],
         "traits": "ICAR-IARI variety, MYMV tolerant (Yellow Mosaic), Kharif + Zaid both seasons",
         "source": "ICAR-IARI / state seed corp / NSC"},
        {"name": "SML 668", "days": "60-65", "yield": "5-6", "seed_rate": "10-12 kg/acre",
         "states": ["Punjab", "Haryana", "Rajasthan", "Madhya Pradesh"],
         "traits": "Short duration, ideal for Zaid/summer sowing, disease tolerant",
         "source": "PAU Ludhiana / state seed corp"},
        {"name": "HUM 16 (Pant Moong 4)", "days": "65-70", "yield": "4-6", "seed_rate": "10-12 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "Madhya Pradesh", "West Bengal"],
         "traits": "Yellow mosaic resistant, suitable for late Kharif, good germination",
         "source": "GBPUAT Pantnagar / state seed corp"},
    ],
    "Urad": [
        {"name": "Pant U 35", "days": "70-75", "yield": "5-7", "seed_rate": "10-12 kg/acre",
         "states": ["Uttar Pradesh", "Bihar", "Madhya Pradesh", "West Bengal"],
         "traits": "ICAR variety, powdery mildew tolerant, bold grain, good cooking quality",
         "source": "GBPUAT Pantnagar / state seed corp"},
        {"name": "LBG 752", "days": "65-70", "yield": "5-6", "seed_rate": "8-10 kg/acre",
         "states": ["Andhra Pradesh", "Karnataka", "Tamil Nadu", "Telangana"],
         "traits": "Short duration, suitable for South India, YMD tolerant",
         "source": "ANGRAU / state seed corp"},
        {"name": "KU 300", "days": "70-75", "yield": "5-6", "seed_rate": "10-12 kg/acre",
         "states": ["Rajasthan", "Gujarat", "Madhya Pradesh"],
         "traits": "Drought tolerant, suitable for rainfed Kharif, medium season",
         "source": "SKN Agriculture University / state seed corp"},
    ],
    "Sunflower": [
        {"name": "KBSH 44 (hybrid)", "days": "90-95", "yield": "8-10", "seed_rate": "2-2.5 kg/acre",
         "states": ["Karnataka", "Andhra Pradesh", "Maharashtra", "Tamil Nadu"],
         "traits": "Best yield hybrid, large head size, high oil content (42%), bird-resistant variety",
         "source": "UAS Dharwad / state seed corp / agri dealers"},
        {"name": "MSFH 17 (hybrid)", "days": "90-100", "yield": "8-12", "seed_rate": "2-2.5 kg/acre",
         "states": ["Rajasthan", "Maharashtra", "Odisha", "Bihar"],
         "traits": "Tolerant to Sclerotinia wilt, uniform maturity, good for Rabi/Zaid season",
         "source": "State seed corp / agri dealers"},
        {"name": "EC 68415 (OPV)", "days": "95-100", "yield": "6-8", "seed_rate": "2.5-3 kg/acre",
         "states": ["All states"],
         "traits": "Open-pollinated, farmer can save seed, wide adaptability, lower cost",
         "source": "NSC / state seed corp"},
    ],
    "Sesame": [
        {"name": "TMV 7", "days": "75-80", "yield": "4-5", "seed_rate": "1.5-2 kg/acre",
         "states": ["Tamil Nadu", "Andhra Pradesh", "Karnataka", "Telangana"],
         "traits": "TNAU variety, phyllody tolerant, high oil content (52%), best for South India",
         "source": "TNAU / state seed corp"},
        {"name": "RT 351", "days": "80-85", "yield": "3-5", "seed_rate": "1.5-2 kg/acre",
         "states": ["Rajasthan", "Gujarat", "Madhya Pradesh", "Maharashtra"],
         "traits": "Drought tolerant, rust resistant, suitable for arid/semi-arid zones",
         "source": "CAZRI / RAU / state seed corp"},
        {"name": "Pragati (OPV)", "days": "75-80", "yield": "3-4", "seed_rate": "1.5-2 kg/acre",
         "states": ["All states"],
         "traits": "Wide adaptability, branching type, farmer can save seed, reliable",
         "source": "ICAR-DRMR / NSC / state seed corp"},
    ],
    "Onion": [
        {"name": "Bhima Shakti", "days": "110-120", "yield": "100-120", "seed_rate": "3-4 kg seed (for nursery) โ†’ 50,000-60,000 seedlings/acre",
         "states": ["Maharashtra", "Karnataka", "Andhra Pradesh", "Madhya Pradesh"],
         "traits": "DOGR variety, pink colour, good storage, high yield",
         "source": "ICAR-DOGR Pune / NHRDF Nasik"},
        {"name": "Agrifound Dark Red", "days": "110-120", "yield": "80-100", "seed_rate": "3-4 kg seed (for nursery) โ†’ 50,000-60,000 seedlings/acre",
         "states": ["All states"],
         "traits": "Dark red skin, good export quality, 3-4 months storage",
         "source": "NHRDF Nasik / state seed corp"},
    ],
    "Tomato": [
        {"name": "Arka Rakshak (hybrid)", "days": "70-80", "yield": "100-120", "seed_rate": "15-20g seed (nursery) โ†’ 5,000-6,000 seedlings/acre",
         "states": ["Karnataka", "Andhra Pradesh", "Maharashtra", "Tamil Nadu"],
         "traits": "IIHR variety, TYLCV resistant, determinate, high lycopene",
         "source": "ICAR-IIHR Bangalore / Syngenta"},
        {"name": "NS 501", "days": "65-75", "yield": "80-100", "seed_rate": "15-20g seed (nursery) โ†’ 5,000-6,000 seedlings/acre",
         "states": ["All states"],
         "traits": "Popular hybrid, long shelf life, good for market",
         "source": "Namdhari Seeds / agri dealers"},
    ],
}

# โ”€โ”€ Input Cost + Profit Calculator Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Per-acre estimates (2024-25 market rates, district-level variation ยฑ20%)
# Source: CACP (Commission for Agricultural Costs and Prices) reports + state govt data
_INPUT_COST: dict[str, dict] = {
    "Soybean":    {"seed": 1800, "fertilizer": 1500, "pesticide": 800, "labor": 4000, "irrigation": 500,  "misc": 500,  "yield_q": 10, "season": "Kharif"},
    "Wheat":      {"seed": 1600, "fertilizer": 3000, "pesticide": 600, "labor": 5000, "irrigation": 2500, "misc": 500,  "yield_q": 20, "season": "Rabi"},
    "Rice":       {"seed": 800,  "fertilizer": 2500, "pesticide": 1000,"labor": 7000, "irrigation": 2000, "misc": 700,  "yield_q": 22, "season": "Kharif"},
    "Cotton":     {"seed": 800,  "fertilizer": 3500, "pesticide": 2500,"labor": 8000, "irrigation": 1500, "misc": 1000, "yield_q": 14, "season": "Kharif"},
    "Maize":      {"seed": 1200, "fertilizer": 2500, "pesticide": 600, "labor": 4000, "irrigation": 1000, "misc": 400,  "yield_q": 28, "season": "Kharif/Rabi"},
    "Mustard":    {"seed": 400,  "fertilizer": 2000, "pesticide": 400, "labor": 3000, "irrigation": 1500, "misc": 300,  "yield_q": 9,  "season": "Rabi"},
    "Groundnut":  {"seed": 4000, "fertilizer": 1500, "pesticide": 800, "labor": 5000, "irrigation": 1000, "misc": 500,  "yield_q": 15, "season": "Kharif"},
    "Gram":       {"seed": 1500, "fertilizer": 1200, "pesticide": 400, "labor": 3000, "irrigation": 800,  "misc": 300,  "yield_q": 8,  "season": "Rabi"},
    "Arhar":      {"seed": 800,  "fertilizer": 1500, "pesticide": 600, "labor": 4500, "irrigation": 600,  "misc": 400,  "yield_q": 8,  "season": "Kharif"},
    "Moong":      {"seed": 1200, "fertilizer": 800,  "pesticide": 400, "labor": 2500, "irrigation": 600,  "misc": 200,  "yield_q": 5,  "season": "Kharif/Zaid"},
    "Urad":       {"seed": 1000, "fertilizer": 800,  "pesticide": 400, "labor": 2500, "irrigation": 600,  "misc": 200,  "yield_q": 5,  "season": "Kharif"},
    "Onion":      {"seed": 2000, "fertilizer": 3000, "pesticide": 1500,"labor": 8000, "irrigation": 2000, "misc": 1000, "yield_q": 100,"season": "Rabi/Kharif"},
    "Tomato":     {"seed": 2500, "fertilizer": 3500, "pesticide": 2000,"labor": 8000, "irrigation": 2000, "misc": 1500, "yield_q": 100,"season": "All"},
    "Bajra":      {"seed": 400,  "fertilizer": 1500, "pesticide": 300, "labor": 2500, "irrigation": 400,  "misc": 200,  "yield_q": 12, "season": "Kharif"},
    "Sunflower":  {"seed": 600,  "fertilizer": 2000, "pesticide": 500, "labor": 3000, "irrigation": 1200, "misc": 300,  "yield_q": 8,  "season": "Rabi/Zaid"},
    "Sesame":     {"seed": 300,  "fertilizer": 1200, "pesticide": 400, "labor": 2500, "irrigation": 600,  "misc": 200,  "yield_q": 4,  "season": "Kharif/Zaid"},
}

# โ”€โ”€ Soil Prep Checklist Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_SOIL_PREP: dict[str, list[dict]] = {
    "cereal": [  # wheat, rice, maize, bajra, sorghum
        {"days_before": 30, "task": "Deep ploughing", "detail": "1 deep plough (25-30cm) to break hardpan + expose soil pests to sun. Use MB plough or chisel plough."},
        {"days_before": 21, "task": "FYM / Compost application", "detail": "Apply 5-8 tonnes FYM or 2-3 tonnes vermicompost per acre. Mix well into soil."},
        {"days_before": 14, "task": "Soil Health Card check", "detail": "Get SHC from KVK โ€” apply fertilizer as per SHC recommendation only (saves 20-30% cost)."},
        {"days_before": 10, "task": "Field levelling", "detail": "Level field for uniform irrigation. Laser levelling saves 30% water."},
        {"days_before": 7,  "task": "Basal fertilizer (DAP/SSP)", "detail": "Apply DAP 50kg/acre (or as per SHC). Mix into soil before sowing."},
        {"days_before": 1,  "task": "Seed treatment (MANDATORY)", "detail": "Treat seed with Carbendazim 50%WP @2g/kg + Imidacloprid 70%WS @5g/kg to prevent early disease and sucking pests."},
        {"days_before": 0,  "task": "Sowing at correct depth + irrigation plan", "detail": "Wheat: 5-6cm depth (Crown Root Irrigation at 20-25 DAS is CRITICAL โ€” do not miss). Rice: transplant 3-4 week nursery seedlings, maintain 5cm water. Maize: 5cm depth; irrigate at knee-high (25 DAS), tasselling (55 DAS), grain fill (75 DAS). Bajra: 2-3cm depth; first irrigation at 3-4 leaf stage if rains fail."},
    ],
    "oilseed": [  # soybean, mustard, groundnut, sunflower, sesame
        {"days_before": 30, "task": "Deep ploughing + lime if needed", "detail": "If soil pH < 6.0 (soybean): apply lime 200-400kg/acre. Plough to 20-25cm."},
        {"days_before": 21, "task": "FYM + phosphate fertilizer", "detail": "FYM 4-5 tonnes/acre + SSP 100-150kg/acre as basal โ€” critical for oilseed pod setting."},
        {"days_before": 14, "task": "Soil Health Card + micronutrient test", "detail": "Oilseeds are sensitive to Sulphur and Zinc deficiency. Apply Gypsum 200kg/acre if Sulphur deficient."},
        {"days_before": 7,  "task": "Field preparation + ridges (groundnut)", "detail": "For groundnut: make ridges 30cm apart. For soybean: flat or slight ridge. Ensure good drainage."},
        {"days_before": 1,  "task": "Rhizobium + PSB seed treatment", "detail": "MANDATORY for soybean/groundnut: Soybean โ†’ Bradyrhizobium japonicum @200g/10kg. Groundnut โ†’ Bradyrhizobium sp. (Arachis) @200g/10kg. PSB @200g/10kg for all. Apply Carbendazim fungicide FIRST (dry 30 min), THEN biofertilizers โ€” never mix simultaneously. Mustard/Sunflower: only fungicide (Thiram @3g/kg), no Rhizobium needed."},
        {"days_before": 0,  "task": "Sowing depth + irrigation schedule", "detail": "Soybean: 3-4cm depth, 30-35kg/acre โ€” irrigate at branching (30 DAS) + flowering (45 DAS) + pod fill (60 DAS) if rains fail. Groundnut: 5-7cm, 55-65kg/acre โ€” critical irrigation at pegging (35 DAS) + pod development (60 DAS). Mustard: 1-2cm, 1.5-2kg/acre โ€” irrigate at branching (30 DAS) + flowering (60 DAS). Sunflower: 3-4cm, 2-3kg/acre."},
    ],
    "pulse": [  # gram, arhar, moong, urad, lentil
        {"days_before": 21, "task": "Light ploughing + FYM", "detail": "Pulses prefer fine tilth. 2 cross ploughings + FYM 4 tonnes/acre. Do NOT over-fertilize N โ€” pulses fix their own N."},
        {"days_before": 14, "task": "Soil moisture check", "detail": "Pulses need dry seedbed. Sow only when top 5cm soil is moist but not wet โ€” prevents damping off."},
        {"days_before": 7,  "task": "Phosphate fertilizer", "detail": "DAP 25-30kg/acre as starter dose โ€” do not apply urea (pulses fix N via Rhizobium)."},
        {"days_before": 1,  "task": "Rhizobium + Carbendazim treatment", "detail": "Use CROP-SPECIFIC strain: Gram/Lentil โ†’ Mesorhizobium ciceri | Arhar โ†’ Bradyrhizobium sp. (Cajanus) | Moong/Urad โ†’ Bradyrhizobium sp. (Vigna). Dose: @200g/10kg seed. Apply Carbendazim @2g/kg first, dry 30 min, then Rhizobium. Sow same day."},
        {"days_before": 0,  "task": "Sowing depth + inter-row spacing", "detail": "Gram: 5-7cm depth, 30ร—10cm spacing | Arhar: 5cm, 60-70ร—20cm | Moong/Urad: 3-4cm, 30ร—10cm. Germination test โ‰ฅ80% mandatory. Irrigate at: branching (25 DAS), flowering (45 DAS), pod fill (65 DAS) โ€” SKIP if soil has moisture."},
    ],
    "vegetable": [  # tomato, onion, chilli, brinjal, etc.
        {"days_before": 30, "task": "Nursery preparation (transplanted crops)", "detail": "Prepare raised nursery bed (15cm height, 1m width). Apply FYM 5-10kg/mยฒ + Carbendazim 50%WP @2g/kg soil drench for damping off."},
        {"days_before": 21, "task": "Main field deep ploughing + FYM", "detail": "3-4 ploughings to fine tilth. FYM 8-10 tonnes/acre or vermicompost 3-4 tonnes/acre. Mix well."},
        {"days_before": 14, "task": "Apply lime + micronutrients", "detail": "pH 6.0-7.0 is ideal. Apply Borax 2kg/acre + ZnSO4 10kg/acre if deficient (check SHC)."},
        {"days_before": 7,  "task": "Bed preparation + drip/mulch if planned", "detail": "Install drip system before transplanting (saves 40-50% water). Black polythene mulch (25 micron) controls weeds + retains moisture."},
        {"days_before": 1,  "task": "Seedling hardening + fungicide drench", "detail": "Stop watering nursery 2 days before transplanting (hardening). Drench seedlings with Carbendazim @1g/L or Metalaxyl+Mancozeb @2.5g/L before lifting."},
        {"days_before": 0,  "task": "Transplanting in evening + immediate irrigation", "detail": "Transplant in evening (cooler). Water immediately after transplanting. Plant to plant distance: Tomato 45ร—60cm | Onion 10ร—15cm | Chilli 45ร—45cm."},
    ],
}

def _get_crop_category(crop: str) -> str:
    """Map crop to soil prep category."""
    _CEREAL  = {"wheat","rice","maize","bajra","sorghum","barley","oat","jowar"}
    _OILSEED = {"soybean","mustard","groundnut","sunflower","sesame","linseed","castor","safflower"}
    _PULSE   = {"gram","arhar","moong","urad","lentil","pea","cowpea","moth bean","pigeonpea"}
    _VEG     = {"tomato","onion","potato","chilli","brinjal","okra","cauliflower","cabbage","carrot"}
    c = crop.lower().replace("/","").strip()
    for word in c.split():
        if word in _CEREAL:  return "cereal"
        if word in _OILSEED: return "oilseed"
        if word in _PULSE:   return "pulse"
        if word in _VEG:     return "vegetable"
    return "cereal"  # default fallback


def _render_variety_recommender(active_state: str | None) -> None:
    """Section: ICAR Variety Recommender โ€” select crop โ†’ get top varieties for state."""
    st.markdown("### ๐ŸŒฑ Variety Recommender โ€” Which Seed to Buy?")
    st.caption("ICAR-recommended varieties for your state ยท Seed rate ยท Where to buy")

    col_crop, col_info = st.columns([1, 2])
    with col_crop:
        crop_list = sorted(_VARIETY_DATA.keys())
        sel_crop = st.selectbox("Select your crop", crop_list, key="vr_crop_sel")

    varieties = _VARIETY_DATA.get(sel_crop, [])
    if not varieties:
        st.info(f"Variety data for {sel_crop} coming soon. Ask the AI Crop Advisor below.")
        return

    # Filter to state if possible, else show all
    state_varieties = [v for v in varieties if not active_state or active_state in v.get("states", []) or "All states" in v.get("states", [])]
    if not state_varieties:
        state_varieties = varieties  # show all if no state match

    # Display as cards
    num = len(state_varieties)
    cols = st.columns(num) if num <= 3 else st.columns(3)
    for i, v in enumerate(state_varieties[:3]):
        with cols[i % 3]:
            st.markdown(f"**๐ŸŒฟ {v['name']}**")
            st.markdown(f"โฑ๏ธ **{v['days']} days** maturity")
            st.markdown(f"๐Ÿ“ฆ Yield: **{v['yield']} q/acre**")
            st.markdown(f"๐ŸŒฑ Seed rate: {v['seed_rate']}")
            st.caption(f"โœ… {v['traits']}")
            st.caption(f"๐Ÿช Source: {v['source']}")
            if active_state and active_state not in v.get("states", []) and "All states" not in v.get("states", []):
                st.warning(f"โš ๏ธ Best in: {', '.join(v['states'][:2])}")

    with st.expander("๐Ÿ’ก Seed Treatment (do this before every sowing)"):
        category = _get_crop_category(sel_crop)
        if category == "oilseed" and sel_crop.lower() in ("soybean","groundnut","arhar","moong","urad"):
            st.markdown("""
**Step 1 โ€” Fungicide treatment:**
`Carbendazim 50%WP @ 2g per kg seed` โ€” mix well, shade dry 30 min

**Step 2 โ€” Rhizobium inoculant (legumes ONLY):**
`Rhizobium @200g + PSB @200g per 10kg seed` โ€” apply last (after fungicide), shade dry, sow immediately
โš ๏ธ Do NOT mix Rhizobium with fungicide in same step โ€” kills bacteria
""")
        elif category == "cereal":
            st.markdown("""
**Fungicide treatment:**
`Carbendazim 50%WP @2g/kg seed` (prevents seed-borne diseases)
`Imidacloprid 70%WS @5g/kg seed` (prevents aphid/BPH early attack)
Mix dry, shade dry 30 min, sow same day.
""")
        else:
            st.markdown("""
**Rhizobium + Fungicide treatment:**
`Carbendazim 50%WP @2g/kg seed` (fungicide first, dry 30 min)
`Rhizobium specific strain @200g/10kg seed` (apply last)
Never expose treated seed to direct sunlight.
""")


# โ”€โ”€ Presow price model โ€” AGMARKNET commodity name mapping โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_PRESOW_CROP_MAP: dict[str, str] = {
    "Wheat":     "Wheat",
    "Rice":      "Rice",
    "Cotton":    "Cotton",
    "Bajra":     "Bajra",
    "Moong":     "Green Gram (Moong)(Whole)",
    "Urad":      "Black Gram (Urad)",
    "Sunflower": "Sunflower",
    "Sesame":    "Sesamum(Sesame,Gingelly,Til)",
    "Soybean":   "Soyabean",
    "Maize":     "Maize",
    "Mustard":   "Mustard",
    "Groundnut": "Groundnut",
    "Arhar":     "Arhar(Tur)",  # Whole grain as traded at mandi (not processed dal)
    "Gram":      "Chana/Bengal Gram",
    "Onion":     "Onion",
    "Tomato":    "Tomato",
}


@st.cache_data(ttl=3600, show_spinner=False)
def _get_presow_price(crop: str, state: str) -> dict:
    """Cached wrapper โ€” fetch harvest-price forecast (presow_v4, 87% accuracy / 96.2% stable crops).
    Tries multiple AGMARKNET commodity name variants for resilience."""
    _ARHAR_ALIASES = ["Arhar(Tur)", "Arhar", "Tur/Arhar", "Arhar/Tur", "Arhar Dal(Tur Dal)"]
    _ALIASES: dict[str, list[str]] = {
        "Arhar": _ARHAR_ALIASES,
        "Gram":  ["Gram", "Chana/Bengal Gram", "Bengal Gram(Gram)(Whole)", "Chickpea"],
        "Moong": ["Green Gram (Moong)(Whole)", "Green Gram", "Moong"],
        "Urad":  ["Black Gram (Urad)", "Urad", "Black Gram"],
    }
    try:
        from mandi_advisor.enterprise_engine_v2 import get_presow_signal
        primary = _PRESOW_CROP_MAP.get(crop, crop)
        aliases = _ALIASES.get(crop, [primary])
        if primary not in aliases:
            aliases = [primary] + aliases
        for name in aliases:
            try:
                result = get_presow_signal(name, state or "India")
                if result and result.get("p50") and float(result["p50"]) > 500:
                    return result  # valid price (>โ‚น500/q)
            except Exception:
                continue
        return {"error": "No valid forecast found", "confidence": "LOW"}
    except Exception as e:
        return {"error": str(e), "confidence": "LOW"}


def _render_input_cost_calculator(active_state: str | None) -> None:
    """Input Cost + Profit Estimator with presow_v4 price forecast (87% accuracy within +/-15%).

    Shows pessimistic/likely/optimistic harvest-price scenarios from AGMARKNET 16yr data.
    """
    st.markdown("### ๐Ÿ’ฐ Input Cost & Profit Estimator")
    st.caption("Approximate 2024-25 rates ยท Actual costs vary ยฑ20% by district ยท Consult local KVK for exact prices")

    col1, col2 = st.columns(2)
    with col1:
        # Filter to crops actually grown/traded in selected state
        _cidx = _load_state_crop_index()
        _cstate = _normalise_state_name(active_state or "")
        _cstate_crops = {e["crop"] for e in _cidx.get(_cstate, [])}
        _state_matched = sorted([c for c in _INPUT_COST if c in _cstate_crops])
        # Only restrict if we have a meaningful match (>= 4 crops)
        # For hill/NE states with mostly horticulture, show full list
        if len(_state_matched) >= 4:
            crop_list = _state_matched
            st.caption(f"๐Ÿ“Š Showing crops traded in {active_state or 'your state'} mandis ยท Select any crop to see all")
        else:
            crop_list = sorted(_INPUT_COST.keys())
            if active_state and _state_matched:
                st.caption(f"๐Ÿ“Š {', '.join(_state_matched)} are commonly traded in {active_state} ยท Showing all crops")
        sel_crop  = st.selectbox("Select crop", crop_list, key="ic_crop")
    with col2:
        acres = st.number_input("Acreage (acres)", min_value=0.5, max_value=100.0,
                                value=2.0, step=0.5, key="ic_acres")

    data = _INPUT_COST.get(sel_crop, {})
    if not data:
        return

    msp_price     = _MSP_2025.get(sel_crop)
    _NO_MSP_CROPS = {"Onion", "Tomato", "Potato", "Sesame", "Sunflower"}

    # ---- Presow price forecast -----------------------------------------------
    presow       = _get_presow_price(sel_crop, active_state or "India") if sel_crop in _PRESOW_CROP_MAP else {}
    has_forecast = bool(presow and "p50" in presow and not presow.get("error"))

    if has_forecast:
        p25 = int(presow["p25"])
        p50 = int(presow["p50"])
        p75 = int(presow["p75"])
        conf = presow.get("confidence", "MEDIUM")
        hw   = presow.get("harvest_window", "---")
        pp   = presow.get("profit_probability", "MEDIUM")
        conf_em = {"HIGH": "๐ŸŸข", "MEDIUM": "๐ŸŸก", "LOW": "๐Ÿ”ด"}.get(conf, "๐ŸŸก")
        pp_em   = {"HIGH": "๐ŸŸข", "MEDIUM": "๐ŸŸก", "LOW": "๐Ÿ”ด"}.get(pp,   "๐ŸŸก")

        with st.expander("๐Ÿ“Š Harvest Price Forecast (presow_v4 ยท AGMARKNET 16yr data)", expanded=True):
            st.caption(
                f"Crop: **{sel_crop}** | State: **{active_state or 'India'}** | "
                f"Expected harvest: **{hw}** | "
                f"Model confidence: {conf_em} **{conf}** | "
                f"Profit probability vs MSP: {pp_em} **{pp}** | "
                f"Accuracy: 87% within ยฑ15%"
            )
            fa, fb, fc = st.columns(3)
            def _vs_msp(px):
                if not msp_price:
                    return None, "off"
                diff = px - msp_price
                return (f"+{diff:,}" if diff >= 0 else f"{diff:,}"), ("normal" if diff >= 0 else "inverse")
            d25, dc25 = _vs_msp(p25)
            d50, dc50 = _vs_msp(p50)
            d75, dc75 = _vs_msp(p75)
            fa.metric("๐Ÿ“‰ Pessimistic (p25)", f"โ‚น{p25:,}/q", delta=d25, delta_color=dc25)
            fb.metric("๐Ÿ“Š Likely (p50 median)", f"โ‚น{p50:,}/q", delta=d50, delta_color=dc50)
            fc.metric("๐Ÿ“ˆ Optimistic (p75)", f"โ‚น{p75:,}/q", delta=d75, delta_color=dc75)
            if msp_price:
                st.caption(f"๐Ÿ›๏ธ MSP 2024-25: โ‚น{msp_price:,}/q")
            # Historical mandi benchmark from actual data
            _hidx = _load_state_crop_index()
            _hstate = _normalise_state_name(active_state or "")
            _hent = next((e for e in _hidx.get(_hstate, []) if e["crop"] == sel_crop), None)
            if _hent and _hent.get("avg_modal_price_per_quintal", 0) > 0:
                _hp = _hent["avg_modal_price_per_quintal"]
                st.caption(f"๐Ÿ“ˆ Historical mandi avg 2018โ€“2025 ({active_state}): โ‚น{_hp:,}/q")
        # Never show a price below MSP as default โ€” MSP is the floor
        default_price = max(p50, msp_price) if msp_price else p50
        if p50 < (msp_price or 0):
            st.caption(
                f"โ„น๏ธ ML forecast for this region (โ‚น{p50:,}/q) is below MSP (โ‚น{msp_price:,}/q). "
                "Using MSP as default โ€” adjust if your local market pays differently."
            )
    else:
        default_price = msp_price or 3000
        if sel_crop in _PRESOW_CROP_MAP:
            st.caption("โ„น๏ธ Price forecast unavailable for this combination โ€” using MSP as reference.")

    sell_price = st.number_input(
        "Expected sell price (โ‚น/quintal) โ€” adjust if needed",
        min_value=500, max_value=50000,
        value=int(default_price), step=50, key="ic_price"
    )

    # ---- Cost breakdown ------------------------------------------------------
    total_seed  = data["seed"]        * acres
    total_fert  = data["fertilizer"]  * acres
    total_pest  = data["pesticide"]   * acres
    total_labor = data["labor"]       * acres
    total_irr   = data["irrigation"]  * acres
    total_misc  = data["misc"]        * acres
    total_input = total_seed + total_fert + total_pest + total_labor + total_irr + total_misc

    est_yield  = data["yield_q"] * acres
    breakeven  = total_input / est_yield if est_yield > 0 else 0

    st.markdown(f"**Cost breakdown for {acres:.1f} acre(s) of {sel_crop}** *(Season: {data.get('season','---')})*")
    cost_rows = [
        ("๐ŸŒฑ Seed / planting material",           f"โ‚น{total_seed:,.0f}"),
        ("๐Ÿงช Fertilizer (NPK + micronutrients)",  f"โ‚น{total_fert:,.0f}"),
        ("๐Ÿ’Š Pesticide / fungicide",              f"โ‚น{total_pest:,.0f}"),
        ("๐Ÿ‘ท Labour (sowing + weeding + harvest)", f"โ‚น{total_labor:,.0f}"),
        ("๐Ÿ’ง Irrigation",                         f"โ‚น{total_irr:,.0f}"),
        ("๐Ÿ“ฆ Misc (transport, bags, etc.)",        f"โ‚น{total_misc:,.0f}"),
    ]
    for label, val in cost_rows:
        c1, c2 = st.columns([3, 1])
        c1.markdown(label)
        c2.markdown(f"**{val}**")
    st.markdown(f"**๐Ÿ”ด Total Input Cost: โ‚น{total_input:,.0f}**")
    st.markdown("---")

    # ---- Profit scenarios ----------------------------------------------------
    if has_forecast:
        st.markdown("**๐Ÿ“Š Profit Scenarios at Harvest** *(based on price forecast)*")
        sc_cols = st.columns(4)
        sc_cols[0].metric("๐Ÿ“ฆ Expected Yield", f"{est_yield:.0f} q")
        for col_idx, (label, px) in enumerate([
            ("๐Ÿ“‰ Pessimistic", p25),
            ("๐Ÿ“Š Likely",      p50),
            ("๐Ÿ“ˆ Optimistic",  p75),
        ]):
            rev  = est_yield * px
            prof = rev - total_input
            sc_cols[col_idx + 1].metric(
                f"{label} @ โ‚น{px:,}/q",
                f"โ‚น{prof:,.0f}",
                delta="Profit" if prof >= 0 else "Loss",
                delta_color="normal" if prof >= 0 else "inverse",
            )
    else:
        revenue = est_yield * sell_price
        profit  = revenue  - total_input
        m1, m2, m3, m4 = st.columns(4)
        m1.metric("๐Ÿ“ฆ Expected Yield",  f"{est_yield:.0f} q")
        m2.metric("๐Ÿ’ต Revenue",          f"โ‚น{revenue:,.0f}")
        m3.metric("๐Ÿ“ˆ Net Profit",       f"โ‚น{profit:,.0f}",
                  delta="Profit" if profit >= 0 else "Loss",
                  delta_color="normal" if profit >= 0 else "inverse")
        m4.metric("โš–๏ธ Break-even",         f"โ‚น{breakeven:.0f}/q")

    st.caption(f"โš–๏ธ Break-even price: **โ‚น{breakeven:.0f}/q** โ€” you must sell above this to recover all costs.")

    # ---- MSP / no-MSP messaging ----------------------------------------------
    if msp_price:
        if sell_price < msp_price:
            st.warning(f"โš ๏ธ Your expected price (โ‚น{sell_price}) is **below MSP (โ‚น{msp_price})**. "
                       f"Contact nearest APMC or call 1800-180-1551 for MSP procurement.")
        elif sell_price >= msp_price * 1.2:
            st.success(f"โœ… Selling at โ‚น{sell_price} = **{((sell_price/msp_price)-1)*100:.0f}% above MSP**. Good market timing!")
    elif sel_crop in _NO_MSP_CROPS:
        st.info(f"โ„น๏ธ **{sel_crop} has no government MSP** โ€” market price is set by demand/supply. "
                f"Monitor AGMARKNET (agmarknet.gov.in) or call **1800-270-0224** for daily mandi rates. "
                f"Consider price hedging via FPO/contract farming.")

    # ---- PMFBY insurance premium ---------------------------------------------
    if msp_price:
        _season_str = data.get("season", "")
        premium_pct  = 0.02 if "Kharif" in _season_str else (0.015 if "Rabi" in _season_str else 0.05)
        sum_insured  = msp_price * data["yield_q"]
        premium_per_acre = sum_insured * premium_pct * acres
        st.info(f"๐Ÿ›ก๏ธ **PMFBY Insurance:** For {acres:.1f} acre of {sel_crop} โ€” "
                f"your premium = **โ‚น{premium_per_acre:,.0f}** ({premium_pct*100:.1f}% of sum insured). "
                f"Enroll before cut-off date at nearest bank.")
    elif sel_crop in _NO_MSP_CROPS:
        st.caption("๐Ÿ›ก๏ธ PMFBY available for vegetable crops in select states โ€” check with your nearest bank branch.")


def _render_soil_prep_checklist(sel_crop: str | None) -> None:
    """Section: Soil Preparation Checklist โ€” 30โ†’0 days before sowing."""
    st.markdown("### ๐Ÿ“‹ Soil Prep Checklist โ€” Before You Sow")
    st.caption("Tick off each step as you complete it ยท Done right = 15-25% higher yield")

    if not sel_crop:
        sel_crop = st.selectbox("Select crop for checklist",
                                sorted(_INPUT_COST.keys()), key="sp_crop")

    category = _get_crop_category(sel_crop)
    steps    = _SOIL_PREP.get(category, _SOIL_PREP["cereal"])

    for step in steps:
        days = step["days_before"]
        label = f"**{step['task']}**" + (f" *(~{days} days before sowing)*" if days > 0 else " *(Sowing day)*")
        done = st.checkbox(label, key=f"sp_{sel_crop}_{days}_{step['task'][:10]}")
        if done:
            st.caption(f"   โœ… Done")
        else:
            st.caption(f"   โ„น๏ธ {step['detail']}")


def _render_weather_compact(active_state: str | None) -> None:
    """Compact 3-day weather cards for the Before Sowing tab."""
    # Use sidebar location โ€” no duplicate selector here
    sel_state = active_state
    if not sel_state:
        st.info("๐Ÿ“ Select your **State โ†’ District โ†’ Block** in the sidebar to see local weather and crop recommendations.")
        return

    with st.spinner(f"Fetching forecast for {sel_state}โ€ฆ"):
        wx = _fetch_weather(sel_state)

    if not wx or "daily" not in wx:
        st.warning("Could not fetch weather. Check internet connection.")
        return

    daily = wx["daily"]
    dates = daily.get("time", [])[:3]
    tmax  = daily.get("temperature_2m_max", [])
    tmin  = daily.get("temperature_2m_min", [])
    rain  = daily.get("precipitation_sum", [])
    codes = daily.get("weathercode", [])

    cols = st.columns(3)
    labels = ["Today", "Tomorrow", "Day 3"]
    for i, col in enumerate(cols):
        if i >= len(dates):
            break
        with col:
            desc = _WMO_CODES.get(codes[i] if i < len(codes) else 0, "")
            r = rain[i] if i < len(rain) else 0
            col.metric(f"{labels[i]} ({dates[i]})",
                       f"{tmax[i] if i < len(tmax) else '?'}ยฐC",
                       f"Min {tmin[i] if i < len(tmin) else '?'}ยฐC")
            col.caption(f"{desc} | ๐ŸŒง๏ธ {r:.1f}mm")

    # Agricultural alerts
    ctx = _format_weather_context(wx, sel_state)
    alerts = [l for l in ctx.split("\n") if l.strip().startswith(("๐ŸŒง๏ธ", "๐Ÿฅถ", "๐ŸŒก๏ธ"))]
    if alerts:
        for a in alerts:
            st.warning(a)
    else:
        st.success("โœ… Weather looks favourable for sowing preparations.")

    return sel_state  # caller can use this



import json as _json_mod

@st.cache_data(ttl=86400)
def _load_state_crop_index() -> dict:
    """Load mandi-derived per-state crop index. Returns {} on failure."""
    _idx_path = str(Path(__file__).parent / "mandi_advisor" / "state_crop_index.json")
    try:
        with open(_idx_path, "r", encoding="utf-8") as _f:
            return _json_mod.load(_f)
    except Exception:
        return {}


def _normalise_state_name(state: str) -> str:
    """Map app state names to AGMARKNET state names used in mandi index."""
    _ALIAS = {
        "Jammu & Kashmir": "Jammu and Kashmir",
        "J&K": "Jammu and Kashmir",
        "Uttarakhand": "Uttarakhand",
        "Orissa": "Odisha",
    }
    return _ALIAS.get(state, state)



@st.cache_data(ttl=86400, show_spinner=False)
def _load_district_crop_index() -> dict:
    """Load district-level crop index built from mandi_data_clean.parquet."""
    _path = str(Path(__file__).parent / "mandi_advisor" / "district_crop_index.json")
    try:
        import json as _j
        with open(_path, "r", encoding="utf-8") as _f:
            return _j.load(_f)
    except Exception:
        return {}


def _get_district_crops_for_season(
        state: str, district: str, season_name: str, top_n: int = 8) -> list:
    """Return top crops for a specific district+season from district mandi index."""
    idx = _load_district_crop_index()
    if not idx:
        return []

    # Try exact match first, then normalised
    key = f"{state}|{district}"
    entries = idx.get(key, [])
    if not entries:
        # Try case-insensitive match
        key_lower = key.lower()
        for k, v in idx.items():
            if k.lower() == key_lower:
                entries = v
                break

    if not entries:
        return []

    # Filter by season using commodity-to-season mapping
    _KHARIF_CROPS = {
        "Rice", "Paddy", "Cotton", "Soybean", "Soyabean", "Maize", "Groundnut",
        "Arhar", "Tur", "Moong", "Green Gram", "Urad", "Black Gram", "Bajra",
        "Sorghum", "Jowar", "Sesame", "Sunflower", "Sugarcane", "Okra",
        "Bitter Gourd", "Cucumber", "Watermelon", "Cowpea", "Castor"
    }
    _RABI_CROPS = {
        "Wheat", "Gram", "Chickpea", "Bengal Gram", "Mustard", "Rapeseed",
        "Lentil", "Masur", "Pea", "Potato", "Onion", "Tomato", "Barley",
        "Coriander", "Cumin", "Fenugreek", "Garlic"
    }
    _ZAID_CROPS = {
        "Moong", "Green Gram", "Urad", "Black Gram", "Maize", "Watermelon",
        "Cucumber", "Muskmelon", "Bitter Gourd", "Bottle Gourd", "Pumpkin",
        "Sesame", "Sunflower", "Cowpea"
    }

    season_set = {"Kharif": _KHARIF_CROPS, "Rabi": _RABI_CROPS,
                  "Zaid (Summer)": _ZAID_CROPS}.get(season_name, set())

    filtered = []
    unmatched = []
    for e in entries:
        crop = e["crop"]
        if any(kw in crop for kw in season_set):
            filtered.append(e)
        else:
            unmatched.append(e)

    # If nothing season-matched, return all (better than empty)
    result = filtered if filtered else unmatched
    return result[:top_n]

def _get_state_crops_for_season(
        state, season_name: str, index: dict, top_n: int = 8) -> list:
    """
    Return top crops for a state+season from the mandi index.
    Each entry: {crop, count, avg_modal_price_per_quintal, season}
    """
    if not state or not index:
        return []
    lookup = _normalise_state_name(state)
    crops = index.get(lookup) or index.get("_national") or []

    SEASON_TOKENS = {
        "Kharif":        {"Kharif"},
        "Rabi":          {"Rabi"},
        "Zaid (Summer)": {"Zaid", "Summer", "Perennial"},
    }
    tokens = SEASON_TOKENS.get(season_name, set())

    matched, perennials = [], []
    for entry in crops:
        s = entry.get("season", "")
        if any(t in s for t in tokens - {"Perennial"}):
            matched.append(entry)
        elif "Perennial" in s and "Perennial" in tokens:
            perennials.append(entry)

    seen, result = set(), []
    for e in matched + perennials:
        if e["crop"] not in seen:
            seen.add(e["crop"])
            result.append(e)
        if len(result) >= top_n:
            break
    return result


def _render_sowing_calendar(active_state: str | None, season: dict) -> None:
    """Show what to sow now based on state + season."""
    state_crops: dict[str, list] = {
        "Kharif": {
            "Madhya Pradesh": ["Soybean โ˜…", "Cotton", "Maize", "Arhar", "Moong"],
            "Maharashtra": ["Soybean โ˜…", "Cotton โ˜…", "Sugarcane", "Tur", "Sorghum"],
            "Punjab": ["Rice/Paddy โ˜…", "Maize", "Cotton", "Sugarcane"],
            "Haryana": ["Rice/Paddy โ˜…", "Maize", "Cotton", "Bajra"],
            "Uttar Pradesh": ["Rice/Paddy โ˜…", "Maize", "Arhar", "Sugarcane", "Cotton"],
            "Rajasthan": ["Bajra โ˜…", "Moong", "Moth Bean", "Sesame", "Groundnut"],
            "Gujarat": ["Cotton โ˜…", "Groundnut โ˜…", "Maize", "Castor", "Arhar"],
            "Bihar": ["Rice/Paddy โ˜…", "Maize", "Arhar", "Moong"],
            "West Bengal": ["Rice/Paddy โ˜…", "Jute", "Maize"],
            "Andhra Pradesh": ["Rice/Paddy โ˜…", "Cotton", "Maize", "Groundnut"],
            "Telangana": ["Rice/Paddy โ˜…", "Cotton โ˜…", "Maize", "Soybean"],
            "Karnataka": ["Rice/Paddy โ˜…", "Sugarcane", "Cotton", "Ragi", "Groundnut"],
            "Tamil Nadu": ["Rice/Paddy โ˜…", "Sugarcane", "Groundnut", "Cotton"],
        },
        "Rabi": {
            "Madhya Pradesh": ["Wheat โ˜…", "Gram โ˜…", "Mustard", "Lentil", "Potato"],
            "Punjab": ["Wheat โ˜…", "Mustard", "Potato"],
            "Haryana": ["Wheat โ˜…", "Mustard โ˜…", "Barley", "Potato"],
            "Uttar Pradesh": ["Wheat โ˜…", "Mustard", "Gram", "Potato", "Pea"],
            "Rajasthan": ["Wheat โ˜…", "Mustard โ˜…", "Gram", "Barley", "Cumin"],
            "Maharashtra": ["Gram โ˜…", "Wheat", "Sorghum", "Onion"],
            "Gujarat": ["Wheat โ˜…", "Mustard", "Gram", "Cumin"],
            "Bihar": ["Wheat โ˜…", "Mustard", "Lentil", "Potato"],
        },
        "Zaid (Summer)": {
            "Jammu & Kashmir": ["Peas โ˜…", "Potato", "Rajma", "Maize", "French Beans"],
            "Himachal Pradesh": ["Peas โ˜…", "Potato โ˜…", "Maize", "Rajma", "Tomato"],
            "Uttarakhand": ["Potato โ˜…", "Peas", "Soybean", "Maize", "Tomato"],
            "Assam": ["Jute โ˜…", "Maize", "Sesame", "Moong", "Cucumber"],
            "Meghalaya": ["Potato โ˜…", "Maize", "Ginger", "Pineapple", "Tomato"],
            "Manipur": ["Rice (Boro) โ˜…", "Maize", "Potato", "Sesame"],
            "Nagaland": ["Maize โ˜…", "Potato", "Ginger", "Soybean"],
            "Mizoram": ["Maize โ˜…", "Potato", "Ginger", "Sesame"],
            "Tamil Nadu": ["Rice (Kuruvai) โ˜…", "Groundnut", "Sesame", "Bitter Gourd", "Cucumber"],
            "Karnataka": ["Groundnut โ˜…", "Sesame", "Sunflower", "Cucumber", "Watermelon"],
            "Andhra Pradesh": ["Groundnut โ˜…", "Sesame", "Sunflower", "Watermelon", "Cucumber"],
            "Telangana": ["Sunflower โ˜…", "Groundnut", "Sesame", "Watermelon"],
            "Kerala": ["Sesame โ˜…", "Cowpea", "Bitter Gourd", "Snake Gourd"],
            "Gujarat": ["Groundnut โ˜…", "Sesame", "Watermelon", "Cucumber", "Moong"],
            "Rajasthan": ["Moong โ˜… (60d)", "Moth Bean", "Watermelon", "Cucumber"],
            "All States": ["Moong โ˜… (60d)", "Urad (65d)", "Cucumber", "Watermelon", "Bitter Gourd"],
        },
    }.get(season["name"], {})

    # โ”€โ”€ Guard: require state before showing crops โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    if not active_state:
        st.info(
            "๐Ÿ“ **Select your State โ†’ District โ†’ Block** in the sidebar to see "
            "hyperlocal crop recommendations based on actual mandi trading data "
            "for your district."
        )
        return

    # โ”€โ”€ 1. Try district-level data first (most accurate) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    user_district = st.session_state.get("user_district_loc")
    _district_crops = []
    if user_district:
        _district_crops = _get_district_crops_for_season(
            active_state, user_district, season["name"], top_n=8
        )

    # โ”€โ”€ 2. Fall back to state-level mandi index โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    _midx = _load_state_crop_index()
    _mcrops = _get_state_crops_for_season(active_state, season["name"], _midx, top_n=8)

    # โ”€โ”€ 3. Last resort: hardcoded seasonal list (no "All States" fallback) โ”€โ”€
    crops_for_state = state_crops.get(active_state, [])

    if _district_crops:
        st.markdown(
            f'<p style="font-size:0.78rem;color:#5A7A5A;margin:0 0 6px 0;">'
            f'๐Ÿ“ <b>District-specific</b> โ€” based on mandi arrivals in <b>{user_district}, {active_state}</b>'
            ' (2018โ€“2025) ยท sorted by trading volume</p>',
            unsafe_allow_html=True)
        _show = _district_crops[:4]
        cols = st.columns(len(_show))
        for _i, _entry in enumerate(_show):
            _cname = _entry["crop"]
            _price = _entry.get("avg_price", 0)
            _pstr = f"avg \u20b9{_price:,}/q" if _price > 0 else ""
            cols[_i].success(f"\U0001f33f {_cname}\n{_pstr}")
        if len(_district_crops) > 4:
            st.caption("Also common in your district: " + " ยท ".join(
                e["crop"] for e in _district_crops[4:]))
        st.caption(
            f"โฌ†๏ธ These crops are actually traded at mandis in {user_district} district โ€” "
            "not generic regional suggestions."
        )
    elif _mcrops:
        st.markdown(
            f'<p style="font-size:0.78rem;color:#5A7A5A;margin:0 0 6px 0;">'
            f'๐Ÿ“Š Based on <b>mandi arrival data 2018โ€“2025</b> for <b>{active_state}</b>'
            ' &nbsp;ยท&nbsp; Select District for more specific data</p>',
            unsafe_allow_html=True)
        _show = _mcrops[:4]
        cols = st.columns(len(_show))
        for _i, _entry in enumerate(_show):
            _cname = _entry["crop"]
            _price = _entry.get("avg_modal_price_per_quintal", 0)
            _pstr = f"avg \u20b9{_price:,}/q" if _price > 0 else ""
            cols[_i].success(f"\U0001f33f {_cname}\n{_pstr}")
        if len(_mcrops) > 4:
            st.caption("Also common: " + " ยท ".join(e["crop"] for e in _mcrops[4:]))
    elif crops_for_state:
        st.markdown(f"**๐Ÿ“… Recommended for {season['name']} in {active_state}:**")
        cols = st.columns(min(5, len(crops_for_state)))
        for i, crop in enumerate(crops_for_state[:5]):
            cols[i].success(f"\U0001f33f {crop}")
    else:
        st.info(f"No crop data available for {active_state} in {season['name']} season.")


# โ”€โ”€ After Harvest tab: crop โ†’ AGMARKNET commodity name mapping โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
_MANDI_CROP_MAP: dict[str, str] = {
    "Wheat":    "Wheat",
    "Rice":     "Paddy(Common)",
    "Maize":    "Maize",
    "Soybean":  "Soyabean",
    "Cotton":   "Cotton",
    "Mustard":  "Rapeseed/Mustard(Toria)",
    "Chickpea": "Bengal Gram(Gram)(Whole)",
    "Arhar":    "Arhar (Tur/Red Gram)(Whole)",
    "Moong":    "Green Gram(Whole)",
    "Urad":     "Black Gram (Urad Whole)",
    "Groundnut":"Groundnut",
    "Onion":    "Onion",
    "Tomato":   "Tomato",
    "Potato":   "Potato",
    "Bajra":    "Bajra(Pearl Millet/Cumbu)",
    "Gram":     "Bengal Gram(Gram)(Whole)",
    "Sunflower":"Sunflower",
    "Sesame":   "Sesamum(Sesame,Gingelly,Til)",
}


def _hold_sell_recommendation(crop: str, modal_price: float, state: str = "India") -> str:
    """
    Enhanced hold/sell recommendation using:
    1. presow_v4 ML forecast (p25/p50/p75) as price benchmark
    2. MSP as safety floor
    3. Seasonal timing logic
    """
    msp = _MSP_2025.get(crop)

    # Try presow_v4 forecast for data-backed benchmark
    presow_p50 = None
    presow_p75 = None
    presow_p25 = None
    forecast_note = ""
    try:
        from mandi_advisor.enterprise_engine_v2 import get_presow_signal
        _SELL_CROP_MAP = {
            "Wheat": "Wheat", "Rice": "Paddy(Desi)(Common)", "Cotton": "Cotton(Lint)(Long Staple)",
            "Soybean": "Soyabean", "Mustard": "Rapeseed/Mustard(Toria)", "Maize": "Maize",
            "Chickpea": "Bengal Gram(Gram)(Whole)", "Arhar": "Arhar (Tur/Red Gram)(Whole)",
            "Groundnut": "Groundnut", "Onion": "Onion", "Tomato": "Tomato",
            "Bajra": "Bajra(Pearl Millet/Cumbu)",
        }
        agmkt = _SELL_CROP_MAP.get(crop)
        if agmkt:
            sig = get_presow_signal(agmkt, state)
            if sig and not sig.get("error") and "p50" in sig:
                presow_p25 = int(sig.get("p25", 0))
                presow_p50 = int(sig.get("p50", 0))
                presow_p75 = int(sig.get("p75", 0))
                conf = sig.get("confidence", "MEDIUM")
                conf_emoji = {"HIGH": "๐ŸŸข", "MEDIUM": "๐ŸŸก", "LOW": "๐Ÿ”ด"}.get(conf, "๐ŸŸก")
                forecast_note = (
                    chr(10) + "**๐Ÿ“Š ML Price Forecast (presow_v4):** "
                    + "Pessimistic โ‚น" + str(presow_p25) + "/q | "
                    + "Likely โ‚น" + str(presow_p50) + "/q | "
                    + "Optimistic โ‚น" + str(presow_p75) + "/q "
                    + conf_emoji + " " + conf
                )
    except Exception:
        pass

    # Use presow_p50 as benchmark if available, else MSP
    benchmark = presow_p50 if presow_p50 else msp
    benchmark_label = "ML forecast" if presow_p50 else "MSP"

    if not benchmark:
        return (
            "๐Ÿ’ก No price benchmark available for " + crop + ". "
            "Check agmarknet.gov.in for historical prices before deciding."
            + forecast_note
        )

    gap_pct = (modal_price - benchmark) / benchmark * 100

    if msp and modal_price < msp:
        result = (
            "โš ๏ธ **Price โ‚น" + str(int(modal_price)) + "/q is BELOW MSP โ‚น" + str(msp) + "/q** for " + crop + "." + chr(10)
            + "โ†’ Do NOT sell below MSP. Check nearest APMC / FCI procurement centre." + chr(10)
            + "โ†’ Call Kisan Helpline **1800-180-1551** for MSP procurement info."
        )
    elif gap_pct < -10:
        result = (
            "๐Ÿ”ด **Price โ‚น" + str(int(modal_price)) + "/q is " + str(abs(int(gap_pct))) + "% BELOW " + benchmark_label + " โ‚น" + str(benchmark) + "/q**." + chr(10)
            + "โ†’ Prices are lower than expected. **Hold if storage is good** (12-14% moisture)." + chr(10)
            + "โ†’ Wait 4-6 weeks for seasonal price recovery."
        )
    elif gap_pct < 5:
        result = (
            "๐ŸŸก **Price โ‚น" + str(int(modal_price)) + "/q is near " + benchmark_label + " โ‚น" + str(benchmark) + "/q** (" + ("+" if gap_pct >= 0 else "") + str(int(gap_pct)) + "%)." + chr(10)
            + "โ†’ If storage is good, **hold 3-4 weeks** " + chr(0x2014) + " prices typically rise post-harvest." + chr(10)
            + "โ†’ If storage is poor or you need cash, sell now."
        )
    elif gap_pct < 20:
        result = (
            "โœ… **Price โ‚น" + str(int(modal_price)) + "/q is +" + str(int(gap_pct)) + "% above " + benchmark_label + "** " + chr(0x2014) + " good price." + chr(10)
            + "โ†’ **Sell 50-70% now** to lock in profit. Hold rest for possible further rise." + chr(10)
            + (("โ†’ Optimistic forecast is โ‚น" + str(presow_p75) + "/q " + chr(0x2014) + " potential upside if you hold." + chr(10)) if presow_p75 and modal_price < presow_p75 else "")
        )
    else:
        result = (
            "๐ŸŸข **Excellent! โ‚น" + str(int(modal_price)) + "/q is +" + str(int(gap_pct)) + "% above " + benchmark_label + "** " + chr(0x2014) + " very good." + chr(10)
            + "โ†’ **Sell now** " + chr(0x2014) + " this is significantly above expected price." + chr(10)
            + "โ†’ Do not wait unless you have a firm buyer at higher price."
        )

    return result + forecast_note


def _render_before_sowing_tab(retriever: "KCCRetriever", settings: dict) -> None:
    """Tab 1: Before Sowing โ€” Weather + Sowing Calendar + AI Crop Advisor + Price Preview."""
    active_state = (st.session_state.get("user_state_loc")
                    or st.session_state.get("active_state"))
    season = get_current_season()

    # โ”€โ”€ Section 1: Weather โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    st.markdown("### ๐ŸŒค๏ธ Weather Forecast โ€” Will Conditions Support Sowing?")
    sel_state = _render_weather_compact(active_state)
    # sel_state may be None if function returns early on error
    if sel_state:
        active_state = sel_state

    st.markdown("---")

    # โ”€โ”€ Section 2: Sowing Calendar โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    st.markdown("### ๐Ÿ“… What to Sow This Season?")
    _render_sowing_calendar(active_state, season)

    st.markdown("---")

    # โ”€โ”€ Section 3: Variety Recommender โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    _render_variety_recommender(active_state)

    st.markdown("---")

    # โ”€โ”€ Section 4: Soil Prep Checklist โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    # Pass the crop selected in variety recommender if available, else None
    _soil_sel_crop = st.session_state.get("vr_crop_sel")
    _render_soil_prep_checklist(_soil_sel_crop)

    st.markdown("---")

    # โ”€โ”€ Section 5: Input Cost & Profit Calculator โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    _render_input_cost_calculator(active_state)

    st.markdown("---")

    # โ”€โ”€ Section 6: AI Crop Advisor โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    st.markdown("### ๐Ÿค– AI Crop Advisor")
    st.caption(
        "Ask: *'Cotton ya soybean kya lagaun Barwani mein?'* or "
        "*'Which crop for black soil with 650mm rainfall?'* โ€” "
        "AI gives comparison with ICAR data + profit/risk analysis."
    )

    # Init Before-Sowing chat session state (separate from main chatbot)
    if "bs_messages" not in st.session_state:
        st.session_state.bs_messages = []
    if "bs_state" not in st.session_state:
        st.session_state.bs_state = active_state

    # Render conversation history
    for msg in st.session_state.bs_messages:
        with st.chat_message(msg["role"], avatar="๐Ÿ‘จโ€๐ŸŒพ" if msg["role"] == "user" else "๐ŸŒพ"):
            st.markdown(msg["content"])

    if bs_query := st.chat_input(
        "Ask about crop selection, variety, sowing time, soil prepโ€ฆ",
        key="bs_chat_input"
    ):
        with st.chat_message("user", avatar="๐Ÿ‘จโ€๐ŸŒพ"):
            st.markdown(bs_query)
        st.session_state.bs_messages.append({"role": "user", "content": bs_query})

        # Force crop_selection problem type for this tab
        detected_state = detect_state(bs_query) or active_state
        # Allow the AI to handle any pre-sowing query (crop selection, agronomy, weather, scheme)
        problem_type = classify_problem(bs_query)
        # If general, assume crop_selection context for this tab
        if problem_type == "general":
            problem_type = "crop_selection"

        normalized_q  = normalize_query(bs_query)
        rewritten_q   = rewrite_query_for_retrieval(
            normalized_q, None, problem_type, detected_state, season
        )
        docs = retriever.search(
            rewritten_q, top_k=5,
            crop_filter=None,
        )
        docs = _apply_state_preference(docs, detected_state)

        icar_ctx     = get_icar_priority_context(bs_query, None, problem_type, detected_state)
        crop_ctx     = build_crop_decision_context(bs_query, detected_state)
        variety_ctx  = build_variety_context(bs_query, detected_state)   # NEW: pins current ICAR varieties
        if crop_ctx:
            icar_ctx = icar_ctx + "\n" + crop_ctx
        if variety_ctx:
            icar_ctx = variety_ctx + "\n" + icar_ctx  # variety context goes FIRST โ€” highest priority

        kcc_context = retriever.format_context(docs)
        meta_lines  = []
        season_line = (
            f"CURRENT AGRICULTURAL SEASON: {season['name']} "
            f"({season['months']}) โ€” major crops: {season['crops']}"
        )
        if season.get("planning_note"):
            season_line += f"\n{season['planning_note']}"
        meta_lines.append(season_line)

        if detected_state:
            soil_ctx = get_soil_context(detected_state)
            detected_district = _extract_location_from_query(bs_query)
            if detected_district:
                state_line = (
                    f"FARMER LOCATION (from query): {detected_district}, {detected_state} "
                    f"(MANDATORY โ€” tailor advice to this specific district)"
                )
            else:
                state_line = f"FARMER'S STATE: {detected_state} (MANDATORY โ€” do NOT assume a different state)"
            if soil_ctx:
                state_line += f" | {soil_ctx}"
            meta_lines.append(state_line)

        meta = "\n".join(meta_lines)
        full_ctx = (
            f"{icar_ctx}\n\nRECORDS FROM KCC DATABASE:\n{kcc_context}\n\n"
            f"CONTEXT:\n{meta}"
        )

        lang = detect_language(bs_query)
        prompt = _build_prompt(bs_query, full_ctx, [], problem_type, lang)

        with st.chat_message("assistant", avatar="๐ŸŒพ"):
            response = st.write_stream(_stream_llm_response(prompt))

        st.session_state.bs_messages.append({"role": "assistant", "content": response})
        if detected_state:
            st.session_state.active_state = detected_state

    st.markdown("---")

    # โ”€โ”€ Section 7: Price Preview for top 3 Kharif/Rabi crops โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    st.markdown("### ๐Ÿ’ฐ MSP Reference Prices (2025โ€“26)")
    st.caption("Minimum Support Price set by Govt of India โ€” your floor price guarantee.")
    season_crops = {
        "Kharif": ["Soybean", "Cotton", "Rice", "Maize", "Groundnut", "Arhar", "Moong"],
        "Rabi": ["Wheat", "Gram", "Mustard", "Lentil"],
        "Zaid (Summer)": ["Moong", "Urad"],
    }.get(season["name"], [])

    msp_data = {c: _MSP_2025[c] for c in season_crops if c in _MSP_2025}
    if msp_data:
        cols = st.columns(min(4, len(msp_data)))
        for i, (crop, msp) in enumerate(list(msp_data.items())[:4]):
            cols[i].metric(f"๐ŸŒพ {crop} MSP", f"โ‚น{msp:,}/q")
    st.caption(
        "๐Ÿ“Œ If market price falls below MSP, contact your nearest APMC or call "
        "**1800-180-1551** for MSP procurement."
    )


def _render_after_harvest_tab() -> None:
    """Tab 3: After Harvest โ€” Sell Smart (presow_v4 price engine + mandi API + hold/sell)."""
    try:
        import plotly.graph_objects as go
        _PLOTLY_OK = True
    except ImportError:
        _PLOTLY_OK = False

    st.markdown(
        """<div style="background:linear-gradient(135deg,#7B3F00,#C0702A);border-radius:12px;
        padding:14px 20px;margin-bottom:18px;">
        <span style="color:#fff;font-size:1.1rem;font-weight:700;">๐Ÿ“ฆ Sell Smart โ€” After Harvest</span>
        <span style="color:#FFD580;font-size:0.8rem;margin-left:12px;">
        Price Forecast ยท Best Mandi ยท Hold or Sell ยท Storage Guide</span></div>""",
        unsafe_allow_html=True,
    )

    _active_state = (
        st.session_state.get("user_state_loc")
        or st.session_state.get("active_state")
    )

    col_crop, col_state = st.columns(2)
    with col_crop:
        sell_crop = st.selectbox("๐ŸŒพ Your Crop", list(_MANDI_CROP_MAP.keys()), key="tab3_crop")
    with col_state:
        state_list = sorted(STATE_COORDS.keys())
        default_si = state_list.index(_active_state) if _active_state in state_list else 0
        sell_state = st.selectbox("๐Ÿ“ Your State", state_list, index=default_si, key="tab3_state")

    st.divider()

    # โ•โ• A. PRICE INTELLIGENCE โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    st.markdown("### ๐Ÿ“Š A. Price Intelligence (ML Forecast)")

    agmkt_name = _MANDI_CROP_MAP.get(sell_crop, sell_crop)
    presow_sig = {}
    try:
        from mandi_advisor.enterprise_engine_v2 import get_presow_signal
        presow_sig = get_presow_signal(agmkt_name, sell_state) or {}
    except Exception:
        pass

    msp_val = _MSP_2025.get(sell_crop)

    if presow_sig and presow_sig.get("p50"):
        p25 = int(presow_sig.get("p25") or 0)
        p50 = int(presow_sig.get("p50") or 0)
        p75 = int(presow_sig.get("p75") or 0)
        conf = presow_sig.get("confidence", "MEDIUM")
        harvest = presow_sig.get("harvest_window", "upcoming season")
        conf_col = {"HIGH": "#28a745", "MEDIUM": "#ffc107", "LOW": "#dc3545"}.get(conf, "#ffc107")
        conf_emoji = {"HIGH": "๐ŸŸข", "MEDIUM": "๐ŸŸก", "LOW": "๐Ÿ”ด"}.get(conf, "๐ŸŸก")

        if _PLOTLY_OK:
            import plotly.graph_objects as go
            gauge_ref = msp_val if msp_val else p50
            fig = go.Figure(go.Indicator(
                mode="gauge+number+delta",
                value=p50,
                title={"text": f"{sell_crop} โ€” Expected Harvest Price ({harvest})", "font": {"size": 13}},
                delta={"reference": gauge_ref, "prefix": "vs MSP " if msp_val else ""},
                gauge={
                    "axis": {"range": [int(p25 * 0.85), int(p75 * 1.15)]},
                    "bar": {"color": conf_col},
                    "steps": [
                        {"range": [int(p25 * 0.85), p25], "color": "#f8d7da"},
                        {"range": [p25, p50], "color": "#fff3cd"},
                        {"range": [p50, p75], "color": "#d4edda"},
                        {"range": [p75, int(p75 * 1.15)], "color": "#cce5ff"},
                    ],
                    "threshold": {
                        "line": {"color": "black", "width": 3},
                        "thickness": 0.85,
                        "value": msp_val if msp_val else p50,
                    },
                },
                number={"prefix": "โ‚น", "suffix": "/q"},
            ))
            fig.update_layout(height=260, margin=dict(t=60, b=10, l=20, r=20))
            st.plotly_chart(fig, use_container_width=True)

        c1, c2, c3 = st.columns(3)
        def _delta(px):
            if not msp_val:
                return None
            d = (px - msp_val) / msp_val * 100
            return f"{d:+.1f}% vs MSP"

        c1.metric("๐Ÿ˜Ÿ Pessimistic (P25)", f"โ‚น{p25:,}/q", _delta(p25))
        c2.metric("๐Ÿ“ˆ Likely Price (P50)", f"โ‚น{p50:,}/q", _delta(p50))
        c3.metric("๐Ÿš€ Optimistic (P75)", f"โ‚น{p75:,}/q", _delta(p75))

        if msp_val:
            if p50 < msp_val:
                st.warning(
                    f"โš ๏ธ Forecast โ‚น{p50:,}/q is BELOW MSP โ‚น{msp_val:,}/q โ€” "
                    "sell via MSP procurement or wait for seasonal price rise. "
                    "Call **1800-180-1551** for nearest procurement centre."
                )
            else:
                st.success(
                    f"โœ… Forecast โ‚น{p50:,}/q is โ‚น{p50 - msp_val:,} above MSP โ€” "
                    "open-market sale looks profitable."
                )

        st.caption(
            f"{conf_emoji} Forecast confidence: **{conf}** | "
            f"16-yr AGMARKNET data, presow_v4 model (87% accuracy) | "
            f"Harvest window: **{harvest}**"
        )
    else:
        if msp_val:
            st.metric(f"๐Ÿ“‹ MSP 2025โ€“26 for {sell_crop}", f"โ‚น{msp_val:,}/quintal",
                      "Government floor price")
        st.info(
            f"Price forecast model not available for {sell_crop} in {sell_state}. "
            "Check agmarknet.gov.in for live prices."
        )

    st.divider()

    # โ•โ• B. LIVE MANDI PRICES โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    st.markdown("### ๐Ÿช B. Live Mandi Prices")

    if not getattr(config, 'DATA_GOV_API_KEY', None):
        st.info(
            "๐Ÿ”‘ Live mandi prices require a free API key from "
            "[data.gov.in](https://data.gov.in/user/register). "
            "Add it to config.py as DATA_GOV_API_KEY. "
            "ML price forecast above works without any API key."
        )
    else:
        if st.button("๐Ÿ” Fetch Live Prices", key="tab3_fetch", use_container_width=False):
            commodity = _MANDI_CROP_MAP.get(sell_crop, sell_crop)
            with st.spinner(f"Fetching {sell_crop} prices in {sell_state}โ€ฆ"):
                _mandi_res = _fetch_mandi_prices(sell_state, commodity)
                live_records = _mandi_res["records"] if _mandi_res else []

            if not live_records:
                st.warning(
                    f"No live data today for **{sell_crop}** in **{sell_state}**. "
                    "API coverage is partial โ€” use ML forecast above as benchmark."
                )
            else:
                rows = []
                for r in live_records:
                    try:
                        modal = float(r.get("Modal_Price") or r.get("modal_price") or 0)
                    except (ValueError, TypeError):
                        modal = 0.0
                    rows.append({
                        "Market": r.get("Market") or r.get("market", ""),
                        "District": r.get("District") or r.get("district", ""),
                        "Min โ‚น/q": r.get("Min_Price") or r.get("min_price", ""),
                        "Modal โ‚น/q": r.get("Modal_Price") or r.get("modal_price", ""),
                        "Max โ‚น/q": r.get("Max_Price") or r.get("max_price", ""),
                        "Date": r.get("Arrival_Date") or r.get("arrival_date", ""),
                        "_modal": modal,
                    })
                df = pd.DataFrame(rows).sort_values("_modal", ascending=False).drop(
                    columns=["_modal"]).reset_index(drop=True)
                if not df.empty:
                    best = df.iloc[0]
                    st.success(
                        f"๐Ÿฅ‡ Best price today: **{best['Market']}** ({best['District']}) "
                        f"โ€” Modal **โ‚น{best['Modal โ‚น/q']}/q**"
                    )
                st.dataframe(df, use_container_width=True)
                st.caption("Source: Ministry of Agriculture via data.gov.in")

    st.divider()

    # โ•โ• C. TRANSPORT COST CALCULATOR โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    with st.expander("๐Ÿš› C. Transport Calculator โ€” Is the farther mandi worth it?"):
        col1, col2, col3 = st.columns(3)
        with col1:
            local_price = st.number_input("Local mandi price (โ‚น/q)", 0, 50000, 0, 50, key="tc_local")
        with col2:
            far_price = st.number_input("Farther mandi price (โ‚น/q)", 0, 50000, 0, 50, key="tc_far")
        with col3:
            qty_qtl = st.number_input("Quantity (quintals)", 1, 1000, 20, 1, key="tc_qty")
        distance_km = st.slider("Distance to farther mandi (km)", 10, 300, 60, key="tc_dist")
        transport_rate = st.slider("Transport rate (โ‚น/q/km)", 0.5, 3.0, 1.2, 0.1, key="tc_rate")

        if local_price > 0 and far_price > 0:
            transport_cost = distance_km * transport_rate
            net_per_q = far_price - local_price - transport_cost
            total = net_per_q * qty_qtl
            cg, ct, cn = st.columns(3)
            cg.metric("Price gain", f"โ‚น{far_price - local_price:,}/q")
            ct.metric("Transport cost", f"โ‚น{transport_cost:,.1f}/q")
            cn.metric("Net gain", f"โ‚น{net_per_q:,.1f}/q", f"Total: โ‚น{total:,.0f}")
            if net_per_q > 0:
                st.success(f"โœ… Go to the farther mandi โ€” you gain โ‚น{total:,.0f} after transport.")
            elif net_per_q > -30:
                st.warning("โš ๏ธ Marginal โ€” decide based on road condition and urgency.")
            else:
                st.error(f"โŒ Stay local โ€” farther mandi costs โ‚น{abs(net_per_q):,.1f}/q more than you gain.")

    # โ•โ• D. HOLD OR SELL โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    with st.expander("๐Ÿค” D. Hold or Sell? (AI recommendation for your price)"):
        col_p, col_s2 = st.columns(2)
        with col_p:
            manual_price = st.number_input("Your local modal price (โ‚น/q)", 0, 50000, 0, 50, key="tab3_mp")
        with col_s2:
            sl2 = sorted(STATE_COORDS.keys())
            si2 = sl2.index(sell_state) if sell_state in sl2 else 0
            manual_state2 = st.selectbox("State", sl2, index=si2, key="tab3_ms2")
        if manual_price > 0:
            st.markdown(_hold_sell_recommendation(sell_crop, float(manual_price), manual_state2))

    # โ•โ• E. MSP PROCUREMENT GUIDE โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    if sell_crop in _MSP_2025:
        with st.expander(f"๐Ÿ›๏ธ E. MSP Procurement โ€” How to sell {sell_crop} at โ‚น{_MSP_2025.get(sell_crop, 0):,}/q govt. price"):
            msp_v = _MSP_2025.get(sell_crop, 0)
            st.markdown(f"""
**MSP 2025โ€“26 for {sell_crop}: โ‚น{msp_v:,}/quintal** โ€” guaranteed by Government of India

**Step-by-step MSP procurement:**
1. **Register** on PM-KISAN / e-NAM portal โ†’ [enam.gov.in](https://www.enam.gov.in)
2. **Contact your APMC** (Agricultural Produce Market Committee) โ€” usually 10โ€“30 km from your block
3. **FCI / NAFED centres** โ€” for Wheat, Rice, Pulses, Oilseeds
4. **Documents needed:** Khasra number, bank passbook copy, Aadhaar card, crop registration slip
5. **Payment:** Direct bank transfer within 72 hours of procurement

๐Ÿ“ž **Helplines (toll-free):**
- Kisan Helpline: **1800-180-1551**
- PM-KISAN: **155261** or **011-23381092**
- e-NAM: **1800-270-0224**
""")

    # โ•โ• F. STORAGE ADVISORY โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    with st.expander("๐Ÿ“ฆ F. Post-Harvest Storage Guide (ICAR 2024โ€“25)"):
        _STORAGE_GUIDE = {
            "Wheat":     ("12โ€“14%", "Hermetic bags (PUSA ZEC) or metal bins. Fumigate with Aluminium Phosphide 3 tablets/tonne. Check every 15 days for weevils. Lasts 12+ months."),
            "Rice":      ("14%", "Gunny bags in dry ventilated godown. Fumigate if >3 months. Wooden pallets โ€” avoid floor contact. Stack max 8โ€“10 bags high."),
            "Maize":     ("12โ€“13%", "CRITICAL: Dry to <13% before storing โ€” aflatoxin risk at high moisture. Hermetic bags strongly recommended. Moisture meter test mandatory."),
            "Soybean":   ("11โ€“12%", "Dry to 11%. Jute bags, cool dry store. Check weevils monthly. Oil degrades fast above 12% moisture."),
            "Chickpea":  ("9โ€“10%", "Airtight containers with neem leaves. Moisture <10% critical. Fumigate bulk with Aluminium Phosphide tablet."),
            "Gram":      ("9โ€“10%", "Airtight with neem leaves. Check monthly for pulse beetle (ghun/weevil)."),
            "Arhar":     ("10%", "Gunny bags + neem leaves. Monthly inspection for ghun. Max 6-month storage."),
            "Groundnut": ("8โ€“9%", "CRITICAL: Dry pods to <8%. High moisture = aflatoxin (cancer-causing). Use Aflasafe biological treatment."),
            "Mustard":   ("7โ€“8%", "Airtight metal bins. Avoid mixing oils. High moisture = rancidity within weeks."),
            "Cotton":    ("8%", "Press bales, dry conditions. Keep away from moisture and fire. Inspect for residual bollworm contamination."),
            "Onion":     ("65โ€“70% RH", "Jali godown (ventilated). 12โ€“15ยฐC ideal. Grade before storage โ€” remove damaged bulbs. Weekly rot check."),
            "Tomato":    ("90โ€“95% RH", "Cold storage 10โ€“13ยฐC. Cannot be stored >1 week without cold chain. Sell within 2โ€“3 days for best price."),
            "Potato":    ("85โ€“90% RH", "Cold storage 2โ€“4ยฐC, dark. Avoid light (solanine). Monthly soft-rot inspection. Cold storage cost: โ‚น150โ€“200/q/month."),
            "Bajra":     ("10โ€“12%", "Gunny bags or metal bins + neem leaves. Watch for storage pests in humid conditions."),
            "Moong":     ("10%", "Airtight bins with neem / Aluminium Phosphide. Pulse beetle very common. Fumigate if >2 months."),
            "Urad":      ("10%", "Same as Moong. Do not mix old and new stock. Airtight critical."),
            "Sunflower": ("8โ€“9%", "Metal bins or hermetic bags. High oil content = rapid rancidity at high moisture."),
            "Sesame":    ("6โ€“8%", "Extremely sensitive โ€” must dry to <6%. Metal tins. Any moisture = rapid oil oxidation."),
        }
        stor_crop = st.selectbox(
            "Select crop for storage guidance",
            list(_STORAGE_GUIDE.keys()),
            index=list(_STORAGE_GUIDE.keys()).index(sell_crop) if sell_crop in _STORAGE_GUIDE else 0,
            key="stor_crop_tab3",
        )
        moist, guide = _STORAGE_GUIDE.get(stor_crop, ("", "No data available."))
        if moist:
            st.metric("Safe moisture level for storage", moist)
        st.info(guide)
        st.caption("Source: ICAR Post-Harvest Technology Division 2024โ€“25")



def _render_b2b_intelligence_tab() -> None:
    """Tab 4: B2B Intelligence โ€” enterprise dashboard with password gate."""
    import config as _b2b_cfg
    _B2B_PWD = _b2b_cfg.B2B_DEMO_PASSWORD

    # โ”€โ”€ Password gate (protects enterprise data from public access) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    if "b2b_authenticated" not in st.session_state:
        st.session_state["b2b_authenticated"] = False

    if not st.session_state["b2b_authenticated"]:
        st.markdown("""
        <div style="background:linear-gradient(135deg,#1a1a2e,#16213e);
             border-radius:16px;padding:40px;text-align:center;margin:20px 0;">
          <h2 style="color:#FFD700;margin-bottom:8px;">๐Ÿข B2B Intelligence Dashboard</h2>
          <p style="color:#aaa;font-size:0.95rem;">
            Enterprise-grade pest risk maps, price opportunity scanner,<br>
            and district-level input demand signals for agri-businesses.
          </p>
        </div>""", unsafe_allow_html=True)
        col1, col2, col3 = st.columns([1, 2, 1])
        with col2:
            pwd = st.text_input("๐Ÿ” Enter access password", type="password",
                                placeholder="Contact us for demo access",
                                key="b2b_pwd_input")
            if st.button("Access Dashboard โ†’", use_container_width=True, type="primary"):
                if pwd == _B2B_PWD:
                    st.session_state["b2b_authenticated"] = True
                    st.rerun()
                else:
                    st.error("Incorrect password. Contact the team for access.")
            st.markdown("""
            <div style="text-align:center;margin-top:20px;">
              <p style="color:#666;font-size:0.8rem;">
                For enterprise demo access, contact:<br>
                <strong style="color:#FFD700;">AgriAdvisor Enterprise Sales</strong>
              </p>
            </div>""", unsafe_allow_html=True)
        return

    # โ”€โ”€ Authenticated โ€” show dashboard โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    """Tab 4: B2B Intelligence โ€” District-level pest/price signals for enterprise clients."""
    from datetime import datetime
    try:
        import plotly.graph_objects as go
        import plotly.express as px
        _PLOTLY_OK = True
    except ImportError:
        _PLOTLY_OK = False

    st.markdown(
        """<div style="background:linear-gradient(135deg,#1a1a2e,#16213e,#0f3460);
        border-radius:12px;padding:16px 22px;margin-bottom:20px;">
        <span style="color:#e94560;font-size:1.15rem;font-weight:700;">๐Ÿข B2B Intelligence Dashboard</span>
        <span style="color:#a8b2d8;font-size:0.82rem;margin-left:14px;">
        District targeting ยท Pest outbreak map ยท Price opportunity signals ยท Input demand forecast</span><br>
        <span style="color:#64748b;font-size:0.75rem;margin-top:4px;display:block;">
        Powered by AUC 0.937 Pest Model ยท presow_v4 Price Engine ยท 16.5M KCC Records</span></div>""",
        unsafe_allow_html=True,
    )

    # โ”€โ”€ Client type selector โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    client_type = st.selectbox(
        "๐Ÿ‘” I am a...",
        [
            "๐ŸŒพ Agri-Input Company (Pesticides / Fertilizers / Seeds)",
            "๐Ÿฆ Bank / NBFC / Insurance Company",
            "๐Ÿค FPO / Cooperative / Aggregator",
            "๐Ÿช Commodity Trader / Exporter",
            "๐Ÿ”ฌ Research / Policy / Government",
        ],
        key="b2b_client_type",
    )

    month = datetime.now().month
    month_name = datetime(2026, month, 1).strftime("%B")

    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    # SECTION 1 โ€” PEST OUTBREAK RISK MAP
    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    st.markdown("---")
    st.markdown(f"### ๐ŸฆŸ 1. Pest Outbreak Risk Map โ€” {month_name} 2026")
    st.caption("Live predictions from AUC 0.937 stacking ensemble (LightGBM + XGBoost + CatBoost). 1-month early warning.")

    col_crop_ew, col_refresh = st.columns([3, 1])
    with col_crop_ew:
        ew_crop = st.selectbox(
            "Select crop for risk scan",
            ["Wheat", "Rice", "Cotton", "Soybean", "Maize", "Mustard",
             "Groundnut", "Arhar", "Gram", "Onion", "Bajra", "Sugarcane"],
            key="b2b_ew_crop",
        )
    with col_refresh:
        st.markdown("<br>", unsafe_allow_html=True)
        run_scan = st.button("๐Ÿ”„ Run Risk Scan", key="b2b_run_scan", use_container_width=True)

    # Key states for the scan
    _SCAN_STATES = [
        "Uttar Pradesh", "Maharashtra", "Punjab", "Madhya Pradesh",
        "Rajasthan", "Gujarat", "Haryana", "Karnataka", "Andhra Pradesh",
        "Telangana", "Bihar", "West Bengal",
    ]

    if run_scan or st.session_state.get("b2b_scan_done"):
        st.session_state["b2b_scan_done"] = True
        st.session_state["b2b_scan_crop"] = ew_crop

        with st.spinner(f"Running pest risk scan for {ew_crop} across 12 major statesโ€ฆ"):
            from mandi_advisor.pest_predictor import predict_pest_risk
            scan_results = []
            for state in _SCAN_STATES:
                try:
                    risks = predict_pest_risk(state, ew_crop, month=month)
                    if risks:
                        top = max(risks, key=lambda x: x.get("risk_score", 0))
                        high_risks = [r for r in risks if r.get("risk_score", 0) >= 60]
                        scan_results.append({
                            "State": state,
                            "Top Pest": top.get("pest", "Unknown")[:35],
                            "Risk Score": top.get("risk_score", 0),
                            "Risk Level": top.get("risk_level", "UNKNOWN"),
                            "High-Risk Pests": len(high_risks),
                            "Action": top.get("recommended_action", "")[:60],
                            "Spray": top.get("spray", "")[:60],
                        })
                except Exception:
                    pass

        if scan_results:
            scan_results.sort(key=lambda x: x["Risk Score"], reverse=True)

            # Risk level color coding
            def _risk_color(level):
                return {
                    "CRITICAL": "#dc3545",
                    "HIGH": "#fd7e14",
                    "MEDIUM": "#ffc107",
                    "LOW": "#28a745",
                    "NEGLIGIBLE": "#6c757d",
                }.get(level, "#6c757d")

            # Summary cards
            critical = [r for r in scan_results if r["Risk Level"] in ("CRITICAL", "HIGH")]
            medium   = [r for r in scan_results if r["Risk Level"] == "MEDIUM"]
            low_neg  = [r for r in scan_results if r["Risk Level"] in ("LOW", "NEGLIGIBLE")]

            cc, cm, cl = st.columns(3)
            cc.metric("๐Ÿ”ด Critical / High Risk", f"{len(critical)} states",
                      "Immediate action needed" if critical else "")
            cm.metric("๐ŸŸก Medium Risk", f"{len(medium)} states",
                      "Monitor closely" if medium else "")
            cl.metric("๐ŸŸข Low / Negligible", f"{len(low_neg)} states", "")

            # Horizontal bar chart
            if _PLOTLY_OK:
                import plotly.express as px
                import pandas as pd
                df_scan = pd.DataFrame(scan_results)
                color_map = {
                    "CRITICAL": "#dc3545", "HIGH": "#fd7e14",
                    "MEDIUM": "#ffc107", "LOW": "#28a745", "NEGLIGIBLE": "#adb5bd",
                }
                fig = px.bar(
                    df_scan.head(12),
                    x="Risk Score", y="State",
                    color="Risk Level",
                    color_discrete_map=color_map,
                    orientation="h",
                    text="Risk Score",
                    title=f"{ew_crop} Pest Risk Score by State โ€” {month_name} 2026",
                    labels={"Risk Score": "Risk Score (0โ€“100)", "State": ""},
                )
                fig.update_traces(texttemplate="%{text}", textposition="outside")
                fig.update_layout(
                    height=420, yaxis={"categoryorder": "total ascending"},
                    margin=dict(l=10, r=40, t=50, b=10),
                    legend_title="Risk Level",
                )
                st.plotly_chart(fig, use_container_width=True)

            # Full table with formatting
            import pandas as pd
            df_display = pd.DataFrame([{
                "State": r["State"],
                "Risk Level": r["Risk Level"],
                "Score": r["Risk Score"],
                "Top Threat": r["Top Pest"],
                "High-Risk Pests": r["High-Risk Pests"],
                "Recommended Spray": r["Spray"],
            } for r in scan_results])
            st.dataframe(df_display, use_container_width=True, hide_index=True)

            # B2B Insight box
            if critical:
                top_states = ", ".join([r["State"] for r in critical[:4]])
                top_pest = critical[0]["Top Pest"]
                st.error(
                    f"๐ŸŽฏ **B2B Targeting Signal:** {ew_crop} in **{top_states}** shows "
                    f"CRITICAL/HIGH {top_pest} risk this month. "
                    f"**{len(critical)} states** need preventive spray campaigns NOW โ€” "
                    f"3โ€“4 weeks lead time before outbreak peak."
                )
                with st.expander("๐Ÿ“‹ Generate District Targeting Brief"):
                    st.markdown(f"""
**Market Intelligence Brief โ€” {month_name} 2026**
**Crop:** {ew_crop} | **Primary Threat:** {top_pest}

**High-Priority States for Immediate Input Push:**
""")
                    for r in critical[:5]:
                        st.markdown(
                            f"- **{r['State']}** โ€” Risk Score {r['Risk Score']}/100 "
                            f"| Recommended: {r['Spray']}"
                        )
                    st.markdown(f"""
**Recommended Actions for Agri-Input Partners:**
1. Pre-position stock of {critical[0]['Spray'].split(' or ')[0].split('@')[0].strip()} in high-risk states
2. Activate dealer network in {top_states} for the next 2 weeks
3. Run SMS/WhatsApp advisory campaign to registered farmers
4. Coordinate with KVK/ATMA offices in critical districts

*Model confidence: AUC 0.937 | Lead time: 3โ€“4 weeks | Data: 16.5M KCC + 26yr weather*
""")

    st.markdown("---")

    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    # SECTION 2 โ€” PRICE OPPORTUNITY SCANNER
    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    st.markdown(f"### ๐Ÿ’ฐ 2. Price Opportunity Scanner โ€” Harvest Season Forecast")
    st.caption("presow_v4 model (87% accuracy, 96.2% for stable crops). Compare forecast vs MSP across crops.")

    if st.button("๐Ÿ“Š Scan Price Opportunities", key="b2b_price_scan"):
        _PRICE_SCAN = [
            ("Wheat",     "Uttar Pradesh",    "Wheat"),
            ("Wheat",     "Punjab",           "Wheat"),
            ("Wheat",     "Haryana",          "Wheat"),
            ("Rice",      "West Bengal",      "Paddy(Desi)(Common)"),
            ("Rice",      "Andhra Pradesh",   "Paddy(Desi)(Common)"),
            ("Cotton",    "Maharashtra",      "Cotton"),
            ("Cotton",    "Gujarat",          "Cotton"),
            ("Soybean",   "Madhya Pradesh",   "Soyabean"),
            ("Soybean",   "Maharashtra",      "Soyabean"),
            ("Mustard",   "Rajasthan",        "Mustard"),
            ("Mustard",   "Haryana",          "Mustard"),
            ("Maize",     "Karnataka",        "Maize"),
            ("Groundnut", "Gujarat",          "Groundnut"),
            ("Arhar",     "Maharashtra",      "Arhar (Tur/Red Gram)(Whole)"),
            ("Bajra",     "Rajasthan",        "Bajra(Pearl Millet/Cumbu)"),
        ]

        from mandi_advisor.enterprise_engine_v2 import get_presow_signal
        price_rows = []
        with st.spinner("Fetching price forecasts for 15 crop-state combinationsโ€ฆ"):
            for crop, state, agmkt in _PRICE_SCAN:
                try:
                    sig = get_presow_signal(agmkt, state)
                    if sig and sig.get("p50"):
                        p50 = int(sig["p50"])
                        msp = _MSP_2025.get(crop, 0) or 0
                        conf = sig.get("confidence", "MEDIUM")
                        harvest = sig.get("harvest_window", "")
                        vs_msp = p50 - msp if msp else None
                        profit_prob = sig.get("profit_probability", "UNKNOWN")
                        price_rows.append({
                            "Crop": crop,
                            "State": state,
                            "P50 Forecast": p50,
                            "MSP": msp if msp else "No MSP",
                            "vs MSP": vs_msp,
                            "Confidence": conf,
                            "Harvest": harvest,
                            "Profit Outlook": profit_prob,
                        })
                except Exception:
                    pass

        if price_rows:
            import pandas as pd
            df_price = pd.DataFrame(price_rows).sort_values("P50 Forecast", ascending=False)

            if _PLOTLY_OK:
                import plotly.express as px
                df_chart = df_price[df_price["vs MSP"].notna()].copy()
                df_chart["vs_msp_num"] = df_chart["vs MSP"].astype(float)
                df_chart["Label"] = df_chart["Crop"] + "\n" + df_chart["State"]
                df_chart["Outlook Color"] = df_chart["vs_msp_num"].apply(
                    lambda x: "Above MSP" if x > 0 else "Below MSP"
                )
                fig2 = px.bar(
                    df_chart,
                    x="Label", y="vs_msp_num",
                    color="Outlook Color",
                    color_discrete_map={"Above MSP": "#28a745", "Below MSP": "#dc3545"},
                    title="Harvest Price Forecast vs MSP (โ‚น/quintal)",
                    labels={"vs_msp_num": "โ‚น vs MSP", "Label": ""},
                    text="vs_msp_num",
                )
                fig2.update_traces(texttemplate="%{text:+,.0f}", textposition="outside")
                fig2.update_layout(height=400, margin=dict(t=50, b=80, l=10, r=10))
                st.plotly_chart(fig2, use_container_width=True)

            st.dataframe(df_price, use_container_width=True, hide_index=True)

            # Opportunity signals
            above_msp = [r for r in price_rows if isinstance(r["vs MSP"], (int, float)) and r["vs MSP"] > 200]
            below_msp = [r for r in price_rows if isinstance(r["vs MSP"], (int, float)) and r["vs MSP"] < -100]

            if above_msp:
                crops_above = list({r["Crop"] for r in above_msp})
                st.success(
                    f"๐Ÿ“ˆ **Opportunity:** {', '.join(crops_above)} forecast ABOVE MSP โ€” "
                    "farmers will expand acreage next season. "
                    "Push seeds, fertilizers, and insurance for these crops."
                )
            if below_msp:
                crops_below = list({r["Crop"] for r in below_msp})
                st.warning(
                    f"๐Ÿ“‰ **Risk:** {', '.join(crops_below)} forecast BELOW MSP โ€” "
                    "farmers may shift to alternatives. "
                    "Banks: watch for loan stress. Insurers: higher claim probability."
                )

    st.markdown("---")

    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    # SECTION 3 โ€” INPUT DEMAND SIGNALS
    # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    st.markdown("### ๐Ÿงช 3. Input Demand Signals โ€” What Farmers Will Buy This Month")
    st.caption("Based on pest risk model + seasonal crop calendar. Use for stock pre-positioning.")

    _INPUT_DEMAND = {
        "Wheat": {
            "Octโ€“Nov": [("Seed treatment", "Thiram + Carbendazim 50WP @ 2.5g/kg"), ("Basal fertilizer", "DAP + MOP")],
            "Janโ€“Feb": [("Fungicide", "Propiconazole 25EC โ€” rust risk"), ("Weedicide", "Sulfosulfuron 75WG")],
            "Marโ€“Apr": [("Insecticide", "Imidacloprid 17.8SL โ€” aphid"), ("Urea", "Top-dress 50kg/acre")],
        },
        "Cotton": {
            "Junโ€“Jul": [("Seed", "Bt Cotton โ€” sow with onset of monsoon"), ("Basal", "DAP + Urea split")],
            "Augโ€“Sep": [("Insecticide", "Spinosad 45SC โ€” bollworm"), ("Fungicide", "Copper Oxychloride โ€” blight")],
            "Octโ€“Nov": [("Insecticide", "Imidacloprid โ€” whitefly + CLCuV management"), ("Potash", "SOP 2 bags/acre")],
        },
        "Rice": {
            "Junโ€“Jul": [("Nursery seed", "Certified paddy seed"), ("Basal", "DAP + Zinc sulfate")],
            "Augโ€“Sep": [("Insecticide", "Chlorpyrifos 20EC โ€” stem borer, BPH"), ("Weedicide", "Bispyribac Na โ€” post-emergence")],
            "Oct": [("Fungicide", "Tricyclazole 75WP โ€” blast"), ("Urea", "Panicle initiation split")],
        },
        "Soybean": {
            "Junโ€“Jul": [("Seed treatment", "Rhizobium + PSB + Thiram"), ("Basal", "SSP + DAP")],
            "Aug": [("Fungicide", "Chlorothalonil โ€” anthracnose, target spot"), ("Insecticide", "Lambda-cyhalothrin โ€” girdle beetle")],
            "Sep": [("Insecticide", "Thiacloprid โ€” whitefly + YMV"), ("Micronutrient", "Boron spray โ€” pod fill")],
        },
    }

    _curr_season_crops = {
        "Kharif (Junโ€“Oct)": ["Cotton", "Rice", "Soybean", "Maize", "Groundnut", "Arhar"],
        "Rabi (Octโ€“Mar)": ["Wheat", "Gram", "Mustard", "Potato", "Onion"],
        "Zaid (Marโ€“Jun)": ["Moong", "Urad", "Maize", "Watermelon"],
    }
    curr_month = datetime.now().month
    if curr_month in (6, 7, 8, 9, 10):
        curr_season_key = "Kharif (Junโ€“Oct)"
    elif curr_month in (11, 12, 1, 2, 3):
        curr_season_key = "Rabi (Octโ€“Mar)"
    else:
        curr_season_key = "Zaid (Marโ€“Jun)"

    season_crops = _curr_season_crops[curr_season_key]
    st.markdown(f"**Current season: {curr_season_key}** โ€” active crops: {', '.join(season_crops)}")

    # Month-wise demand calendar
    _MONTHLY_DEMAND = {
        5:  {"Cotton": "๐ŸŒฑ Seed + Soil prep", "Wheat": "๐ŸŒพ Post-harvest storage", "Groundnut": "๐ŸŒฑ Seed + Rhizobium"},
        6:  {"Cotton": "๐ŸŒฑ BT Seed + Basal fert", "Rice": "๐ŸŒฑ Nursery + seed treatment", "Soybean": "๐ŸŒฑ Seed + Rhizobium"},
        7:  {"Cotton": "๐Ÿšฟ Weedicide + Zinc", "Rice": "๐ŸŒพ Transplant + DAP", "Soybean": "๐Ÿ”ฌ Fungicide (anthracnose)"},
        8:  {"Cotton": "๐ŸฆŸ Bollworm spray", "Rice": "๐ŸฆŸ Stem borer + BPH", "Soybean": "๐ŸฆŸ Girdle beetle"},
        9:  {"Cotton": "๐ŸฆŸ Whitefly Imidacloprid", "Rice": "๐Ÿ”ฌ Blast fungicide", "Soybean": "๐ŸฆŸ Thiacloprid + Boron"},
        10: {"Cotton": "๐ŸŒฟ Potash top-dress", "Rice": "๐ŸŒพ Harvest prep", "Wheat": "๐ŸŒฑ Seed procurement"},
        11: {"Wheat": "๐ŸŒฑ Seed treatment + DAP", "Mustard": "๐ŸŒฑ Sow + Basal", "Gram": "๐ŸŒฑ Rhizobium + sow"},
        12: {"Wheat": "๐Ÿšฟ Crown root irrigation", "Mustard": "๐ŸฆŸ Aphid watch", "Potato": "๐Ÿ”ฌ Late blight spray"},
        1:  {"Wheat": "๐Ÿ”ฌ Rust fungicide (Propiconazole)", "Mustard": "๐ŸฆŸ Aphid Dimethoate", "Gram": "๐Ÿ”ฌ Botrytis watch"},
        2:  {"Wheat": "๐ŸฆŸ Aphid (Imidacloprid)", "Mustard": "๐ŸŒพ Pod fill โ€” Boron", "Onion": "๐Ÿ”ฌ Purple blotch spray"},
        3:  {"Wheat": "๐ŸŒพ Harvest prep", "Moong": "๐ŸŒฑ Zaid sowing + Rhizobium", "Urad": "๐ŸŒฑ Zaid sowing"},
        4:  {"Moong": "๐ŸฆŸ YMV โ€” whitefly spray", "Urad": "๐ŸฆŸ Aphid watch", "Maize": "๐ŸŒฑ Zaid maize โ€” DAP"},
    }

    curr_demands = _MONTHLY_DEMAND.get(curr_month, {})
    if curr_demands:
        st.markdown(f"**๐Ÿ—“๏ธ Input Demand Signals for {month_name}:**")
        demand_cols = st.columns(min(4, len(curr_demands)))
        for i, (crop, action) in enumerate(curr_demands.items()):
            demand_cols[i % 4].info(f"**{crop}**\n\n{action}")

    # Client-specific insights
    st.markdown("---")
    st.markdown("### ๐Ÿ’ก 4. Client-Specific Intelligence")

    if "Input Company" in client_type:
        st.markdown("""
**๐ŸŒพ For Agri-Input Companies (Pesticides / Seeds / Fertilizers):**

| Signal | Implication | Action |
|---|---|---|
| HIGH pest risk states identified | Demand spike in 2โ€“3 weeks | Pre-position stock at depot level |
| Crop acreage expanding (above MSP forecast) | Higher seed + input demand | Increase dealer inventory |
| Price below MSP forecast | Farmer may reduce acreage | Adjust forecasting for next season |
| Whitefly CRITICAL in cotton states | Imidacloprid demand spike | Alert regional teams |
""")
        st.info(
            "๐Ÿ“ก **API Available:** Integrate our pest risk endpoint into your CRM for "
            "real-time district-level targeting. Contact us for enterprise API access."
        )

    elif "Bank" in client_type or "Insurance" in client_type:
        st.markdown("""
**๐Ÿฆ For Banks / NBFC / Insurance Companies:**

| Signal | Risk Implication | Action |
|---|---|---|
| CRITICAL pest risk + forecast below MSP | High crop loss + income stress | Flag for loan restructuring |
| HIGH pest risk in Kharif districts | Elevated insurance claim probability | Adjust reserve provisioning |
| MSP forecast shortfall >10% | Farmer may default on crop loan | Proactive engagement with farmers |
| Multiple consecutive LOW forecast years | Persistent distress zone | KCC/KCC loan review |
""")
        st.warning(
            "โš ๏ธ **Risk Alert:** Our model identifies districts where BOTH pest risk is HIGH "
            "AND price forecast is BELOW MSP โ€” these are double-stress zones requiring proactive intervention."
        )

    elif "FPO" in client_type or "Cooperative" in client_type:
        st.markdown("""
**๐Ÿค For FPOs / Cooperatives / Aggregators:**

| Signal | Opportunity | Action |
|---|---|---|
| Price forecast ABOVE MSP | Favorable selling season | Aggregate and sell in bulk |
| Pest risk HIGH in neighboring district | Demand for collective spray services | Organize FPO custom hiring services |
| Price forecast LOW | Negotiate forward contracts early | Lock in MSP procurement contracts |
| Storage-sensitive crops | Hold vs sell decision | Leverage FPO cold storage |
""")

    elif "Trader" in client_type or "Exporter" in client_type:
        st.markdown("""
**๐Ÿช For Commodity Traders / Exporters:**

| Signal | Opportunity |
|---|---|
| Price P75 significantly above historical | Potential forward buy opportunity |
| Multiple states with HIGH risk same crop | Supply disruption risk โ€” build inventory |
| Price P25 below MSP in major states | Govt. procurement will absorb supply โ€” limited open market |
| LOW confidence crops | Avoid forward contracts โ€” high price volatility |
""")

    elif "Research" in client_type or "Government" in client_type:
        st.markdown("""
**๐Ÿ”ฌ For Research / Policy / Government:**

**Model Specifications:**
- Pest Model: Stacking ensemble (LightGBM + XGBoost + CatBoost + Logistic Regression) | AUC 0.937
- Coverage: 26 crop-pest combinations ร— 475 districts ร— 2007โ€“2025 data
- Price Model: LightGBM quantile regression (P25/P50/P75) | 87% overall, 96.2% stable crops
- Coverage: 290 crops ร— 36 states ร— 2001โ€“2026 AGMARKNET data (71.6M rows)
- Chatbot: 16.5M KCC records, FAISS+BM25 hybrid retrieval, 99/99 eval score

**Potential Policy Applications:**
1. District-level early warning for state agriculture departments
2. Pre-positioning of agricultural inputs at FCI/NAFED warehouses
3. MSP procurement planning based on price forecast shortfalls
4. Crop insurance actuarial modelling with pest risk integration
""")

    st.markdown("---")
    st.caption(
        "๐Ÿค– Powered by: Pest Model AUC 0.937 ยท presow_v4 price engine (87% accuracy) ยท "
        "16.5M KCC records ยท ICAR 2024โ€“25 agronomic database | "
        "For enterprise API access and white-label integration: contact Dhaat"
    )



def _inject_css() -> None:
    css = """
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

html, body, [class*="css"], .stApp {
    font-family: 'Inter', sans-serif !important;
}
.main .block-container {
    padding-top: 0.5rem !important;
    padding-bottom: 2rem !important;
    max-width: 1440px !important;
}
[data-testid="stAppViewContainer"] > .main {
    background: #F2F5F2 !important;
}
#MainMenu, footer, [data-testid="stToolbar"],
[data-testid="stDecoration"] { display: none !important; }
[data-testid="stHeader"] { background: transparent !important; height: 0 !important; }

[data-testid="stSidebar"] {
    background: linear-gradient(175deg, #1B4332 0%, #2D6A4F 60%, #40916C 100%) !important;
    border-right: none !important;
    box-shadow: 4px 0 20px rgba(0,0,0,0.12);
}
[data-testid="stSidebar"] > div:first-child { padding-top: 0 !important; }
[data-testid="stSidebar"] label,
[data-testid="stSidebar"] .stMarkdown p,
[data-testid="stSidebar"] .stMarkdown li,
[data-testid="stSidebar"] .stCaption { color: #C8E6C9 !important; font-size: 0.85rem; }
[data-testid="stSidebar"] .stMarkdown h1,
[data-testid="stSidebar"] .stMarkdown h2,
[data-testid="stSidebar"] .stMarkdown h3,
[data-testid="stSidebar"] .stMarkdown strong { color: #FFFFFF !important; }
[data-testid="stSidebar"] [data-testid="stMetric"] {
    background: rgba(255,255,255,0.1) !important;
    border-radius: 8px !important;
    padding: 8px 12px !important;
    border: 1px solid rgba(255,255,255,0.15) !important;
    margin-bottom: 6px !important;
}
[data-testid="stSidebar"] [data-testid="stMetricLabel"] { color: #A5D6A7 !important; font-size: 0.75rem !important; }
[data-testid="stSidebar"] [data-testid="stMetricValue"] { color: #FFFFFF !important; font-size: 1rem !important; font-weight: 600 !important; }
[data-testid="stSidebar"] .stSelectbox > div > div,
[data-testid="stSidebar"] .stMultiSelect > div > div {
    background: rgba(255,255,255,0.12) !important;
    border: 1px solid rgba(255,255,255,0.25) !important;
    border-radius: 8px !important;
}
[data-testid="stSidebar"] .stButton > button {
    background: rgba(255,255,255,0.15) !important;
    color: #FFFFFF !important;
    border: 1px solid rgba(255,255,255,0.3) !important;
    border-radius: 8px !important;
    width: 100% !important;
    font-weight: 500 !important;
}
[data-testid="stSidebar"] .stButton > button:hover {
    background: rgba(255,255,255,0.25) !important;
}
[data-testid="stSidebar"] .stSuccess {
    background: rgba(165,214,167,0.2) !important;
    border: 1px solid rgba(165,214,167,0.4) !important;
    border-radius: 8px !important;
}
[data-testid="stSidebar"] hr { border-color: rgba(255,255,255,0.15) !important; margin: 12px 0 !important; }

.stTabs [data-baseweb="tab-list"] {
    background: #FFFFFF !important;
    border-radius: 14px !important;
    padding: 5px !important;
    gap: 4px !important;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08) !important;
    border: 1px solid #E8EDE8 !important;
}
.stTabs [data-baseweb="tab"] {
    border-radius: 10px !important;
    padding: 10px 28px !important;
    font-weight: 500 !important;
    font-size: 0.93rem !important;
    color: #5A7A5A !important;
    background: transparent !important;
    transition: all 0.25s ease !important;
    border: none !important;
}
.stTabs [data-baseweb="tab"]:hover { background: #F0F7F0 !important; color: #2D6A4F !important; }
.stTabs [aria-selected="true"] {
    background: linear-gradient(135deg, #1B4332 0%, #40916C 100%) !important;
    color: #FFFFFF !important;
    box-shadow: 0 3px 10px rgba(27,67,50,0.3) !important;
}
.stTabs [data-baseweb="tab-highlight"],
.stTabs [data-baseweb="tab-border"] { display: none !important; }
.stTabs [data-baseweb="tab-panel"] { padding-top: 1.25rem !important; }

.main .stButton > button {
    background: linear-gradient(135deg, #2D6A4F 0%, #40916C 100%) !important;
    color: #FFFFFF !important;
    border: none !important;
    border-radius: 9px !important;
    font-weight: 600 !important;
    font-size: 0.9rem !important;
    padding: 0.55rem 1.6rem !important;
    transition: all 0.22s ease !important;
    box-shadow: 0 2px 8px rgba(45,106,79,0.25) !important;
}
.main .stButton > button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 5px 15px rgba(45,106,79,0.35) !important;
}

.main [data-testid="stTextInput"] > div > div,
.main [data-testid="stTextArea"] > div > div {
    border-radius: 10px !important;
    border: 1.5px solid #D4E4D4 !important;
    background: #FFFFFF !important;
}
.main [data-testid="stTextInput"] > div > div:focus-within,
.main [data-testid="stTextArea"] > div > div:focus-within {
    border-color: #40916C !important;
    box-shadow: 0 0 0 3px rgba(64,145,108,0.12) !important;
}
.main .stSelectbox > div > div { border-radius: 10px !important; border: 1.5px solid #D4E4D4 !important; }

.main [data-testid="stMetric"] {
    background: #FFFFFF !important;
    border-radius: 12px !important;
    padding: 18px 20px !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06) !important;
    border: 1px solid #E8EDE8 !important;
    border-left: 4px solid #40916C !important;
}
.main [data-testid="stMetricLabel"] { color: #6B8F6B !important; font-size: 0.78rem !important; text-transform: uppercase; letter-spacing: 0.05em; }
.main [data-testid="stMetricValue"] { color: #1B4332 !important; font-size: 1.6rem !important; font-weight: 700 !important; }

.main [data-testid="stExpander"] {
    background: #FFFFFF !important;
    border-radius: 12px !important;
    border: 1px solid #E0EAE0 !important;
    box-shadow: 0 1px 6px rgba(0,0,0,0.05) !important;
    margin-bottom: 0.75rem !important;
    overflow: hidden !important;
}

[data-testid="stChatMessage"] {
    border-radius: 14px !important;
    margin-bottom: 10px !important;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06) !important;
    border: 1px solid rgba(0,0,0,0.05) !important;
}
[data-testid="stChatInputContainer"] {
    border-radius: 14px !important;
    border: 2px solid #40916C !important;
    box-shadow: 0 3px 16px rgba(64,145,108,0.15) !important;
    background: #FFFFFF !important;
}

.main [data-testid="stDataFrame"] {
    border-radius: 12px !important;
    overflow: hidden !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06) !important;
    border: 1px solid #E0EAE0 !important;
}

hr { border-color: #E0EAE0 !important; margin: 1rem 0 !important; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: #F2F5F2; }
::-webkit-scrollbar-thumb { background: #A8C9A8; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #40916C; }
</style>
"""
    st.markdown(css, unsafe_allow_html=True)


def _render_header(user_block_loc, user_district_loc):
    """Branded gradient header bar."""
    loc_html = ""
    if user_block_loc and user_district_loc:
        loc_html = (
            '<span style="background:rgba(255,255,255,0.18);border:1px solid rgba(255,255,255,0.3);'
            'border-radius:20px;padding:4px 14px;font-size:0.8rem;color:#E8F5E9;">'
            "&#128205; " + user_block_loc + ", " + user_district_loc + "</span>"
        )

    html = (
        '<div style="display:flex;align-items:center;justify-content:space-between;'
        'background:linear-gradient(135deg,#1B4332 0%,#2D6A4F 50%,#40916C 100%);'
        'border-radius:16px;padding:16px 28px;margin-bottom:1.2rem;'
        'box-shadow:0 4px 20px rgba(27,67,50,0.25);flex-wrap:wrap;gap:12px;">'

        '<div style="display:flex;align-items:center;gap:14px;">'
        '<div style="background:rgba(255,255,255,0.15);border-radius:12px;padding:10px;'
        'border:1px solid rgba(255,255,255,0.2);font-size:1.6rem;line-height:1;">&#127807;</div>'
        '<div>'
        '<div style="font-family:Inter,sans-serif;font-size:1.55rem;font-weight:700;'
        'color:#FFFFFF;letter-spacing:-0.02em;line-height:1.1;">AI Farm Advisor</div>'
        '<div style="font-family:Inter,sans-serif;font-size:0.72rem;color:#A5D6A7;'
        'letter-spacing:0.1em;text-transform:uppercase;">AI Farm Advisor</div>'
        '</div></div>'

        '<div>' + loc_html + '</div>'

        '<div style="display:flex;gap:8px;flex-wrap:wrap;">'
        '<span style="background:rgba(255,255,255,0.12);border:1px solid rgba(255,255,255,0.2);'
        'border-radius:20px;padding:4px 12px;font-size:0.78rem;color:#C8E6C9;">&#129302; Llama-4 Scout</span>'
        '<span style="background:rgba(255,255,255,0.12);border:1px solid rgba(255,255,255,0.2);'
        'border-radius:20px;padding:4px 12px;font-size:0.78rem;color:#C8E6C9;">&#128218; 16.5M Records</span>'
        '<span style="background:rgba(255,255,255,0.12);border:1px solid rgba(255,255,255,0.2);'
        'border-radius:20px;padding:4px 12px;font-size:0.78rem;color:#C8E6C9;">&#127760; Hindi &middot; English</span>'
        '</div>'
        '</div>'
    )
    st.markdown(html, unsafe_allow_html=True)


def main() -> None:
    # โ”€โ”€ CSS injection (must be first) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    _inject_css()

    # โ”€โ”€ load retriever โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    try:
        retriever = _load_retriever()
    except FileNotFoundError as e:
        st.error(
            "**FAISS index not found.**\n\n"
            f"{e}\n\n"
            "Run `step2_embeddings.py` to build the index first."
        )
        st.stop()

    # โ”€โ”€ sidebar โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    settings = _sidebar(retriever)
    # active_state = state set in sidebar location picker
    active_state = st.session_state.get("user_state_loc") or st.session_state.get("active_state")

    # โ”€โ”€ main area โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    user_block_loc    = st.session_state.get("user_block_loc")
    user_district_loc = st.session_state.get("user_district_loc")
    _render_header(user_block_loc, user_district_loc)

    tab_before, tab_during, tab_after, tab_b2b = st.tabs([
        "๐ŸŒฑ  Sow Smart",
        "๐ŸŒฟ  Grow Strong",
        "๐Ÿ“ฆ  Sell Better",
        "๐Ÿข  B2B Intelligence",
    ])

    with tab_before:
        _render_before_sowing_tab(retriever, settings)

    with tab_after:
        _render_after_harvest_tab()

    with tab_b2b:
        _render_b2b_intelligence_tab()

    with tab_during:
        # โ”€โ”€ Session state init โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        if "messages"        not in st.session_state: st.session_state.messages        = []
        if "retrieval"       not in st.session_state: st.session_state.retrieval       = []
        if "active_crop"     not in st.session_state: st.session_state.active_crop     = None
        if "active_problem"  not in st.session_state: st.session_state.active_problem  = "general"
        if "active_state"    not in st.session_state: st.session_state.active_state    = None
        if "topic_origin"    not in st.session_state: st.session_state.topic_origin    = None
        if "prefill_query"   not in st.session_state: st.session_state.prefill_query   = None

        # โ”€โ”€ Two-column layout: EW on left, Chat on right โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        col_ew, col_chat = st.columns([1, 1], gap="large")

        with col_ew:
            st.markdown("""<div style="background:linear-gradient(135deg,#1B4332,#40916C);border-radius:12px;padding:12px 18px;margin-bottom:14px;"><span style="color:#fff;font-size:1.05rem;font-weight:700;">๐Ÿšจ Pest Early Warning</span><span style="color:#A5D6A7;font-size:0.78rem;margin-left:10px;">14-day forecast</span></div>""", unsafe_allow_html=True)
            ew_loc = st.session_state.get("user_block_loc")
            if not ew_loc:
                st.info(
                    "๐Ÿ“ **Select your location** in the sidebar (State โ†’ District โ†’ Block) "
                    "to see 14-day pest outbreak predictions for your farm."
                )
                st.caption("The Early Warning uses weather forecast + 16.5M historical KCC records + satellite NDVI.")
            else:
                _render_early_warning_tab(
                    st.session_state.get("user_state_loc"),
                    st.session_state.get("user_district_loc"),
                    st.session_state.get("user_block_loc"),
                    st.session_state.get("block_lat"),
                    st.session_state.get("block_lon"),
                )

        with col_chat:
            st.markdown("""<div style="background:linear-gradient(135deg,#1B4332,#40916C);border-radius:12px;padding:12px 18px;margin-bottom:4px;"><span style="color:#fff;font-size:1.05rem;font-weight:700;">๐Ÿ’ฌ Crop Doctor</span><span style="color:#A5D6A7;font-size:0.78rem;margin-left:10px;">Ask in Hindi ยท English ยท Regional</span></div>""", unsafe_allow_html=True)
            st.caption("Pest ยท Disease ยท Fertilizer ยท Spray dose ยท Timing | Hindi / English / Any language")

            # โ”€โ”€ EW alert banner (cross-column) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            ew_alert = st.session_state.get("ew_alert")
            if ew_alert and st.session_state.get("user_block_loc"):
                # Extract first high-risk crop name for pre-fill suggestion
                first_crop = ew_alert.split(":")[1].split("(")[0].strip() if ":" in ew_alert else ""
                st.warning(f"๐Ÿšจ **Alert from Early Warning:** {ew_alert[:120]}โ€ฆ")
                if first_crop:
                    if st.button(f"๐Ÿ’ฌ Ask: How to protect my {first_crop}?",
                                 key="ew_prefill_btn", use_container_width=True):
                        st.session_state.prefill_query = (
                            f"Early warning ne bataya hai ki mere {first_crop} mein "
                            f"pest outbreak ka khatra hai. Bachaav ke liye kya karun?"
                        )
                        st.rerun()

            # โ”€โ”€ Clear chat button โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            if st.session_state.messages:
                if st.button("๐Ÿ—‘๏ธ Clear conversation", key="clear_chat"):
                    st.session_state.messages      = []
                    st.session_state.retrieval     = []
                    st.session_state.active_crop   = None
                    st.session_state.active_problem = "general"
                    st.session_state.topic_origin  = None
                    st.session_state.prefill_query = None
                    st.rerun()

        # Render chat history (inside col_chat context โ€” Streamlit renders to last open column)
        with col_chat:
            for i, msg in enumerate(st.session_state.messages):
                with st.chat_message(msg["role"],
                                     avatar="๐Ÿ‘จโ€๐ŸŒพ" if msg["role"] == "user" else "๐Ÿค–"):
                    st.markdown(msg["content"])
                if msg["role"] == "assistant":
                    src_idx = i // 2
                    if (settings["show_sources"]
                            and src_idx < len(st.session_state.retrieval)
                            and st.session_state.retrieval[src_idx]):
                        _render_sources(st.session_state.retrieval[src_idx])
                    user_q = st.session_state.messages[i - 1]["content"] if i > 0 else ""
                    _render_feedback(
                        i, user_q, msg["content"],
                        st.session_state.active_crop,
                        st.session_state.active_state,
                        st.session_state.active_problem,
                    )

        # โ”€โ”€ chat input (must be top-level, not inside column) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        # Handle EW pre-fill: inject as if user typed it
        _prefill = st.session_state.pop("prefill_query", None)
        if _prefill:
            user_query = _prefill
        elif user_query_raw := st.chat_input("Ask about pest, disease, spray, fertilizerโ€ฆ"):
            user_query = user_query_raw
        else:
            user_query = None

        if user_query:

            with st.chat_message("user", avatar="๐Ÿ‘จโ€๐ŸŒพ"):
                st.markdown(user_query)
            st.session_state.messages.append({"role": "user", "content": user_query})

            with st.chat_message("assistant", avatar="๐Ÿค–"):

                # โ”€โ”€ โ˜… STEP 0: Topic guard โ€” block non-agriculture queries โ”€โ”€โ”€โ”€โ”€โ”€
                # Fast regex check before any FAISS or LLM call.
                if not is_agriculture_query(user_query):
                    st.markdown(OFF_TOPIC_RESPONSE)
                    st.session_state.messages.append(
                        {"role": "assistant", "content": OFF_TOPIC_RESPONSE}
                    )
                    st.session_state.retrieval.append([])
                    st.stop()

                # โ”€โ”€ โ˜… STEP 0b: Harmful non-farm guard โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                # Catches "not for farm use" harmful queries that slip through
                # is_agriculture_query() because "farm" appears in the text.
                if is_harmful_non_farm_query(user_query):
                    st.markdown(OFF_TOPIC_RESPONSE)
                    st.session_state.messages.append(
                        {"role": "assistant", "content": OFF_TOPIC_RESPONSE}
                    )
                    st.session_state.retrieval.append([])
                    st.stop()

                # โ”€โ”€ Step 1: Pre-processing + multi-turn context โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

                # 1a. Language detection โ€” short follow-ups (yes/ha/nahi/เคฆเฅ‹เคฎเคŸ)
                # inherit the conversation language instead of mis-detecting.
                reply_language = detect_language(user_query)
                if len(user_query.split()) <= 2 and st.session_state.messages:
                    # Find last user message that was longer (more reliable signal)
                    for _prev in reversed(st.session_state.messages):
                        if _prev["role"] == "user" and len(_prev["content"].split()) > 2:
                            reply_language = detect_language(_prev["content"])
                            break

                # 1b. Season + state detection
                season         = get_current_season()
                detected_state = (
                    settings.get("manual_state")
                    or detect_state(user_query)
                    or st.session_state.active_state
                )

                # 1c. Keyword-based crop + problem detection
                detected_crop  = detect_crop(user_query)
                problem_type   = classify_problem(user_query)
                normalized_q   = normalize_query(user_query)

                # 1d. Query rewriting for short/ambiguous queries
                #     Expands "wheat pilli patti" โ†’ detailed English retrieval query
                rewritten_q = rewrite_query_for_retrieval(
                    normalized_q, detected_crop, problem_type,
                    detected_state, season,
                )
                retrieval_q = rewritten_q  # used for FAISS search

                # โ”€โ”€ Cache check (skip RAG + LLM entirely on hit) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                _ck = _make_cache_key(
                    user_query, detected_crop, detected_state,
                    problem_type, season["name"]
                )
                if _ck in _RESPONSE_CACHE:
                    full_response = _RESPONSE_CACHE[_ck]
                    st.caption("โšก Instant answer (cached)")
                    st.write(full_response)
                    st.session_state.messages.append(
                        {"role": "assistant", "content": full_response}
                    )
                    st.session_state.retrieval.append([])
                    st.rerun()

                # 1c. LLM understanding โ€” only when keywords fail
                #     Avoids extra API call for clear-cut queries
                _kw_crop_found    = detected_crop is not None
                _kw_problem_found = problem_type != "general"
                if not _kw_crop_found or not _kw_problem_found:
                    with st.spinner("๐Ÿง  Understanding queryโ€ฆ"):
                        llm_info = understand_query_llm(user_query)
                    if not _kw_crop_found and llm_info.get("crop"):
                        detected_crop = llm_info["crop"]
                    if not _kw_problem_found and llm_info.get("problem", "general") != "general":
                        problem_type = llm_info["problem"]

                # 1d. Smart crop inheritance:
                #     Only inherit previous crop if the new query doesn't
                #     clearly introduce a DIFFERENT topic (crop_selection resets context)
                new_crop_in_query = detect_crop(user_query) is not None or (
                    not _kw_crop_found and detected_crop is not None
                )
                if detected_crop is None:
                    # No crop in this query โ€” inherit previous
                    detected_crop = st.session_state.active_crop
                # else: use the newly detected crop (don't inherit)

                # 1e. Problem type inheritance โ€” but crop_selection always wins
                if problem_type == "general":
                    problem_type = st.session_state.active_problem
                # crop_selection question resets context (farmer is starting fresh)
                if problem_type == "crop_selection":
                    st.session_state.active_crop    = None
                    st.session_state.active_problem = "crop_selection"
                    st.session_state.topic_origin   = None
                    detected_crop = None  # don't filter for crop_selection

                # 1f. Persist newly detected values for next turn
                if new_crop_in_query:
                    st.session_state.active_crop = detected_crop
                if classify_problem(user_query) != "general" or (
                    not _kw_problem_found and problem_type != "general"
                ):
                    st.session_state.active_problem = problem_type

                # Record original topic on first substantive turn
                if st.session_state.topic_origin is None and problem_type != "general":
                    st.session_state.topic_origin = {
                        "query": user_query,
                        "crop":  detected_crop,
                        "problem": problem_type,
                    }

                # โ”€โ”€ Step 2: Multi-step retrieval โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                with st.spinner("๐Ÿ” Searching KCC knowledge baseโ€ฆ"):
                    t_ret = time.perf_counter()
                    # UPGRADE 1: pass location for boosted retrieval
                    _r_state    = st.session_state.get("user_state_loc", "") or detected_state or ""
                    _r_district = st.session_state.get("user_district_loc", "") or ""
                    docs  = multi_step_retrieve(
                        retriever, retrieval_q, retrieval_q,
                        detected_crop, problem_type, settings,
                        state=_r_state, district=_r_district,
                    )
                    # Soft re-rank: prefer results from detected state
                    docs = _apply_state_preference(docs, detected_state)
                    ret_ms = (time.perf_counter() - t_ret) * 1000

                # โ”€โ”€ Step 3: Confidence check โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                top_score      = docs[0].score if docs else 0.0
                low_confidence = top_score < LOW_CONF_THRESHOLD

                # โ”€โ”€ Step 3b: Named disease / pest + irrigation detection โ”€โ”€โ”€โ”€โ”€โ”€
                named_disease_key = detect_named_disease(user_query)
                icar_priority_ctx = ""
                if named_disease_key:
                    icar_priority_ctx = build_icar_context(named_disease_key, detected_crop)
                    low_confidence = False

                # Irrigation: inject ICAR validated water-requirement numbers
                if detect_irrigation_query(user_query):
                    irr_ctx = build_irrigation_context(user_query, detected_crop)
                    if irr_ctx:
                        icar_priority_ctx = icar_priority_ctx + irr_ctx
                        low_confidence = False

                # Agronomy: inject ICAR seed rate / spacing / waterlogging / variety data
                if detect_agronomy_query(user_query) or problem_type == "agronomy":
                    agro_ctx = build_agronomy_context(user_query, detected_crop)
                    if agro_ctx:
                        icar_priority_ctx = icar_priority_ctx + agro_ctx
                        low_confidence = False

                # Crop selection: inject crop decision profiles (yield, water, risk, profit)
                if problem_type == "crop_selection":
                    crop_sel_ctx = build_crop_decision_context(user_query, detected_state)
                    if crop_sel_ctx:
                        icar_priority_ctx = icar_priority_ctx + "\n" + crop_sel_ctx
                        low_confidence = False

                # Nutrient deficiency: inject ICAR nutrient cards
                nutrient_key = detect_nutrient_deficiency(user_query)
                if nutrient_key:
                    nutr_ctx = build_nutrient_context(nutrient_key)
                    if nutr_ctx:
                        icar_priority_ctx = icar_priority_ctx + nutr_ctx
                        low_confidence = False

                # Post-harvest storage: inject post-harvest cards
                ph_key = detect_postharvest_query(user_query)
                if ph_key:
                    ph_ctx = build_postharvest_context(ph_key)
                    if ph_ctx:
                        icar_priority_ctx = icar_priority_ctx + ph_ctx
                        low_confidence = False

                # Government schemes: inject scheme cards with exact figures
                scheme_key = detect_govt_scheme(user_query)
                if scheme_key:
                    scheme_ctx = build_scheme_context(scheme_key)
                    if scheme_ctx:
                        icar_priority_ctx = icar_priority_ctx + scheme_ctx
                        low_confidence = False

                # โ”€โ”€ Step 3f: Semantic ICAR catch-all (undetected conditions) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                if not icar_priority_ctx and _ICAR_AVAILABLE and _ICAR_RETRIEVER is not None:
                    try:
                        _sem = _ICAR_RETRIEVER.search(user_query, top_k=2)
                        if _sem:
                            icar_priority_ctx = _ICAR_RETRIEVER.format_for_llm(_sem) + nn
                            low_confidence = False
                    except Exception:
                        pass

                # โ”€โ”€ Step 4: Build enriched context โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                kcc_context = retriever.format_context(docs)
                # ICAR cards go first so LLM sees validated data before KCC records
                context    = (icar_priority_ctx + kcc_context) if icar_priority_ctx else kcc_context

                # UPGRADE 5: Golden set โ€” prepend verified answer if high confidence match
                try:
                    from step3_retrieval import get_golden_retriever as _get_gr
                    _golden_ret = _get_gr()
                    if _golden_ret.size > 0:
                        _golden_hits = _golden_ret.lookup(user_query, top_k=2)
                        if _golden_hits and _golden_hits[0]["score"] > 0.85:
                            _gh = _golden_hits[0]
                            context = (
                                "[VERIFIED KCC ANSWER โ€” high confidence match]\n"
                                f"Q: {_gh['query']}\n"
                                f"A: {_gh['answer']}\n"
                                f"(Crop: {_gh.get('crop','')}, State: {_gh.get('state','')}, "
                                f"Match score: {_gh['score']:.2f})\n\n"
                                + context
                            )
                except Exception:
                    pass  # Golden retriever errors never block main flow

                meta_lines: list[str] = []

                if st.session_state.topic_origin:
                    t = st.session_state.topic_origin
                    meta_lines.append(
                        f"โš ๏ธ CONVERSATION CONTEXT (CRITICAL): Farmer is continuing a "
                        f"conversation about {t['problem'].upper()} in "
                        f"{t['crop'] or 'their crop'}. Original question: \"{t['query']}\". "
                        f"This is a follow-up reply โ€” do NOT change the topic. "
                        f"Answer specifically about {t['crop'] or 'the crop'} {t['problem']}."
                    )
                if detected_crop:
                    meta_lines.append(
                        f"โš ๏ธ DETECTED CROP (MANDATORY): {detected_crop} โ€” "
                        f"ALL recommendations must be for {detected_crop} only."
                    )
                if problem_type != "general":
                    meta_lines.append(f"PROBLEM TYPE: {problem_type.upper()}")
                safety_note = SAFETY_GUARDRAILS.get(problem_type, "")
                if safety_note:
                    meta_lines.append(safety_note)
                if low_confidence:
                    meta_lines.append(
                        f"LOW CONFIDENCE (top score: {top_score*100:.0f}%) โ€” "
                        "ask the farmer 1-2 clarifying questions instead of guessing."
                    )

                # โ”€โ”€ Enrich context: state + season + soil + weather + mandi โ”€โ”€
                season_line = (
                    f"CURRENT AGRICULTURAL SEASON: {season['name']} "
                    f"({season['months']}) โ€” major crops: {season['crops']}"
                )
                if season.get("planning_note"):
                    season_line += f"\n{season['planning_note']}"
                meta_lines.append(season_line)
                if detected_state:
                    soil_ctx = get_soil_context(detected_state)
                    detected_district = _extract_location_from_query(user_query)
                    if detected_district:
                        state_line = (
                            f"FARMER LOCATION (from query): {detected_district}, {detected_state} "
                            f"(MANDATORY โ€” tailor advice to this specific district)"
                        )
                    else:
                        state_line = f"FARMER'S STATE: {detected_state} (MANDATORY โ€” do NOT assume a different state)"
                    if soil_ctx:
                        state_line += f" | {soil_ctx}"
                    meta_lines.append(state_line)

                    # Weather context for weather-type queries
                    if problem_type == "weather":
                        wx = _fetch_weather(detected_state)
                        if wx:
                            wx_ctx = _format_weather_context(wx, detected_state)
                            if wx_ctx:
                                meta_lines.append(wx_ctx)

                    # Mandi price context for crop selection queries
                    if problem_type == "crop_selection" and config.DATA_GOV_API_KEY:
                        with st.spinner("๐Ÿ’ฐ Fetching live mandi pricesโ€ฆ"):
                            price_ctx = _build_price_context(season, detected_state)
                        if price_ctx:
                            meta_lines.append(price_ctx)

                    # -- 3-MODEL: Presow price forecast (presow_v4)
                    _is_sowing_q = any(kw in user_query.lower() for kw in _CHATBOT_PRESOW_KEYWORDS)
                    if (problem_type == "crop_selection" or _is_sowing_q) and detected_crop:
                        try:
                            _presow_ctx = _build_presow_chatbot_context(
                                detected_crop, detected_state or "India"
                            )
                            if _presow_ctx:
                                meta_lines.append(_presow_ctx)
                        except Exception:
                            pass

                    # -- 3-MODEL: Pest risk early warning (AUC 0.937)
                    _is_pest_sow_q = (
                        problem_type in ("crop_selection", "pest_disease")
                        or any(kw in user_query.lower() for kw in _CHATBOT_PEST_KEYWORDS)
                    )
                    if _is_pest_sow_q and detected_crop and detected_state:
                        try:
                            _pest_ctx = _build_pest_risk_chatbot_context(
                                detected_state,
                                st.session_state.get("user_district_loc") or "",
                                detected_crop,
                                datetime.now().month,
                            )
                            if _pest_ctx:
                                meta_lines.append(_pest_ctx)
                        except Exception:
                            pass

                # โ”€โ”€ Confidence signal: summarize retrieved evidence quality โ”€โ”€
                if docs:
                    doc_states  = list({d.state for d in docs if d.state and d.state not in ("", "UNKNOWN")})
                    doc_years   = []
                    for d in docs:
                        try: doc_years.append(int(d.year))
                        except (ValueError, TypeError): pass
                    yr_str = (f"{min(doc_years)}โ€“{max(doc_years)}"
                              if doc_years else "various years")
                    state_str = ", ".join(doc_states[:3]) + ("โ€ฆ" if len(doc_states) > 3 else "")
                    conf_label = ("HIGH" if top_score > 0.75 else
                                  "MEDIUM" if top_score > LOW_CONF_THRESHOLD else "LOW")
                    old_src_warn = ""
                    if doc_years and max(doc_years) < 2016:
                        old_src_warn = (" โš ๏ธ ALL retrieved sources are pre-2016 โ€” "
                                        "chemical doses / varieties may be outdated. "
                                        "Prefer ICAR reference doses over retrieved context.")
                    meta_lines.append(
                        f"EVIDENCE SUMMARY: {len(docs)} similar KCC cases found "
                        f"(confidence: {conf_label}, top similarity: {top_score*100:.0f}%). "
                        f"States covered: {state_str or 'various'}. "
                        f"Data period: {yr_str}.{old_src_warn} "
                        "Use this evidence to give a grounded, specific answer."
                    )

                if meta_lines:
                    context = "\n".join(meta_lines) + "\n\n" + context

                # โ”€โ”€ Step 5: Build prompt + generate โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                history = [
                    m for m in st.session_state.messages[-6:]
                    if "user" in m["role"] or "assistant" in m["role"]
                ]
                history_pairs = []
                for j in range(0, len(history), 2):
                    if j + 1 < len(history):
                        history_pairs.append({
                            "user":      history[j]["content"],
                            "assistant": history[j + 1]["content"],
                        })
                # UPGRADE 2: pass location + crop+problem for dose lookup
                _farmer_state    = st.session_state.get("user_state_loc", "") or detected_state or ""
                _farmer_district = st.session_state.get("user_district_loc", "") or ""
                prompt = _build_prompt(
                    user_query, context, history_pairs,
                    problem_type, reply_language,
                    state=_farmer_state, district=_farmer_district,
                    detected_crop=detected_crop or "",
                    problem_detail=problem_type,
                )

                badges = []
                if detected_crop:             badges.append(f"๐ŸŒฟ {detected_crop}")
                if detected_state:            badges.append(f"๐Ÿ“ {detected_state}")
                if problem_type != "general": badges.append(f"๐Ÿ”ฌ {problem_type}")
                if low_confidence:            badges.append("โš ๏ธ Low confidence")
                badge_str = " | " + " | ".join(badges) if badges else ""
                st.caption(f"๐Ÿ” Retrieved {len(docs)} sources in {ret_ms:.0f} ms{badge_str}")

                full_response = st.write_stream(_stream_llm_response(prompt))

                # โ”€โ”€ Confidence card (Improvement #5) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                # Show a visible, colour-coded confidence signal AFTER the
                # response โ€” tells B2B clients and agronomists how much to
                # trust this answer without reading retrieval internals.
                avg_score = (
                    float(np.mean([d.score for d in docs])) if docs else 0.0
                )
                if top_score >= 0.85:
                    conf_emoji, conf_txt, conf_colour = (
                        "๐ŸŸข", "High confidence", "success"
                    )
                    conf_detail = (
                        f"Top match score: **{top_score:.2f}** ยท "
                        f"Avg score: {avg_score:.2f} โ€” "
                        "answer grounded in closely-matching KCC records."
                    )
                elif top_score >= LOW_CONF_THRESHOLD:
                    conf_emoji, conf_txt, conf_colour = (
                        "๐ŸŸก", "Moderate confidence", "warning"
                    )
                    conf_detail = (
                        f"Top match score: **{top_score:.2f}** ยท "
                        f"Avg score: {avg_score:.2f} โ€” "
                        "good match found; minor details may vary by location."
                    )
                else:
                    conf_emoji, conf_txt, conf_colour = (
                        "๐Ÿ”ด", "Low confidence", "error"
                    )
                    conf_detail = (
                        f"Top match score: **{top_score:.2f}** ยท "
                        f"Avg score: {avg_score:.2f} โ€” "
                        "no close KCC match found. "
                        "**Please verify this advice with your local KVK or agriculture officer (1800-180-1551).**"
                    )

                # Only show the card for pest/disease/nutrient (high-stakes).
                # For scheme / mandi queries the score is less meaningful.
                if problem_type in ("pest", "disease", "nutrient", "agronomy",
                                    "crop_selection") or low_confidence:
                    _msg = f"{conf_emoji} **{conf_txt}** โ€” {conf_detail}"
                    if conf_colour == "success":
                        st.success(_msg)
                    elif conf_colour == "warning":
                        st.warning(_msg)
                    else:
                        st.error(_msg)

            # โ”€โ”€ Post-generation safety checks โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            safety_violations = check_chemical_safety(full_response, problem_type)
            for v in safety_violations:
                st.warning(v)
            for v in check_banned_pesticides(full_response):
                st.error(v)  # red โ€” banned chemicals are critical safety issue

            # โ”€โ”€ ICAR mandatory chemical supplement โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            # If LLM skipped a mandatory ICAR chemical, show yellow info box
            _user_query = st.session_state.messages[-1]["content"] if st.session_state.messages else ""
            for _note in _icar_mandatory_supplement(_user_query, full_response):
                st.info(_note)

            # โ”€โ”€ Stale price scanner: catch old KCC โ‚น data shown as current โ”€โ”€
            # KCC records contain historical prices (2006-2024). If LLM quotes
            # them despite RULE #1, add a clear disclaimer so farmer isn't misled.
            _PRICE_PATTERN = re.compile(
                r'(โ‚น\s*[\d,]+\s*/\s*(quintal|kg|mt|ton|q)'
                r'|Rs\.?\s*[\d,]+\s*/?\s*(quintal|kg)'
                r'|modal\s+price'
                r'|[\d,]{3,}\s*(rupees?|เคฐเฅเคชเค|เคฐเฅเคชเคฏเฅ‡)\s+per\s+(quintal|kg)'
                r'|\bprice\b.{0,30}\bquintal\b)',
                re.IGNORECASE,
            )
            if _PRICE_PATTERN.search(full_response):
                st.warning(
                    "โš ๏ธ **Price Alert**: The figures above are from historical KCC records "
                    "(2006โ€“2024) and are NOT today's market prices. "
                    "For live rates, check the **Mandi Prices** tab or agmarknet.gov.in."
                )

            # โ”€โ”€ Completeness validator (pest/disease only) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            if problem_type in ("pest", "disease", "nutrient") and len(full_response) > 80:
                resp_lower = full_response.lower()
                missing = []
                # Check for dose/quantity mention
                has_dose = bool(re.search(
                    r'\b(\d+\s*(ml|g|kg|gm|gram|litre|liter|oz|ml/|g/|%)'
                    r'|\d+\s*-\s*\d+\s*(ml|g|kg)|per\s*(acre|litre|liter|hectare)'
                    r'|dose|matra|khuraak|concentration)',
                    resp_lower))
                # Check for timing mention
                has_timing = bool(re.search(
                    r'\b(spray|chhidkav|application|apply|din|days?|week|saptah'
                    r'|morning|evening|subah|sham|before|after|interval|repeat'
                    r'|baar|times?|season|mausam)',
                    resp_lower))
                # Check for chemical/treatment mention
                has_treatment = bool(re.search(
                    r'\b(fungicide|insecticide|pesticide|dawai|dawa|spray|neem'
                    r'|chlorpyrifos|mancozeb|copper|sulphur|sulfur|emamectin'
                    r'|imidacloprid|thiamethoxam|profenofos|trichoderma)',
                    resp_lower))
                if not has_dose:
                    missing.append("dose/quantity (matra)")
                if not has_timing:
                    missing.append("timing/frequency (kab aur kitni baar)")
                if not has_treatment and problem_type in ("pest", "disease"):
                    missing.append("treatment/chemical name")
                if missing:
                    st.caption(
                        f"โ„น๏ธ **Tip**: Ask the chatbot to also provide โ€” "
                        + ", ".join(missing)
                        + " โ€” for a complete recommendation."
                    )

            # โ”€โ”€ Cache the response โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            _cache_store(_ck, full_response)

            # โ”€โ”€ Persist state + update session โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            if detected_state:
                st.session_state.active_state = detected_state

            st.session_state.messages.append(
                {"role": "assistant", "content": full_response}
            )
            st.session_state.retrieval.append(
                docs if settings["show_sources"] else []
            )
            if settings["show_sources"] and docs:
                _render_sources(docs)
            st.rerun()


if __name__ == "__main__":
    main()