from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import sys
import os

# Add root directory to sys.path so we can import 'core' and 'agents'
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from core.saksh import Saksh
from core.adapters import CrewAINetra
from agents.qwen_agent import get_qwen_agent
from agents.gpt_agent import get_gpt_agent

app = FastAPI(title="Saksh Observability")

# Enable CORS (Allows local development flexibility)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# --- INITIALIZE SAKSH ---
# We instantiate the observer here. It acts as a standalone monitor.
observer = Saksh()
qwen = get_qwen_agent()
gpt = get_gpt_agent()

# Register Eyes
observer.open_eye(CrewAINetra(qwen, "Qwen-Custom-01"))
observer.open_eye(CrewAINetra(gpt, "OpenAI-GPT4-01"))

# --- API ENDPOINTS ---

@app.get("/api/status")
def get_status():
    """Returns the JSON health matrix for the UI to consume"""
    return observer.observe()

# --- SERVE STATIC FILES (HTML/JS/CSS) ---
app.mount("/", StaticFiles(directory="dashboard/static", html=True), name="static")

if __name__ == "__main__":
    print("👁️ SAKSH Dashboard running at http://localhost:2604")
    uvicorn.run(app, host="0.0.0.0", port=2604)