from fastapi import APIRouter, HTTPException from pydantic import BaseModel import json import os from app.core.rag import rag_engine router = APIRouter(tags=["Portal Controls"]) class PolicyUpdatePayload(BaseModel): policy_json: dict # --- User Routes --- @router.get("/user/{member_id}/wallet") async def get_wallet_limit(member_id: str): """Mocks database retrieval of a user's wallet limit.""" return { "member_id": member_id, "total_limit": 50000.00, "available_limit": 35000.00 # Example remaining balance } # --- Admin Routes --- @router.get("/admin/policy") async def get_current_policy(): """Fetches the current policy JSON for the admin editor.""" if not os.path.exists(rag_engine.policy_file): raise HTTPException(status_code=404, detail="Policy file not found.") with open(rag_engine.policy_file, "r") as f: return json.load(f) @router.post("/admin/policy/update") async def update_policy(payload: PolicyUpdatePayload): """Overwrites the policy file and instantly rebuilds the AI's RAG memory.""" with open(rag_engine.policy_file, "w") as f: json.dump(payload.policy_json, f, indent=2) # CRITICAL: Rebuild the local FAISS vector database rag_engine.initialize() return {"status": "success", "message": "Policy updated. AI memory successfully refreshed."}