from typing import List

from fastapi import FastAPI

from src.inference import AlarmDataModel, InferenceModel
from src.logger import setup_logger

logger = setup_logger(__name__)
app = FastAPI(title="Alarm Prediction API", description="API for detecting relevant alarms from irrelevant ones")


@app.post("/predict")
async def predict(data: AlarmDataModel | List[AlarmDataModel] | dict | List[dict]) -> dict:
    model = InferenceModel()
    if isinstance(data, AlarmDataModel):
        data = [data]
    elif isinstance(data, dict):
        data = [AlarmDataModel(**data)]

    predictions = model.run_inference(data)
    return predictions.to_dict(orient="list")


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="localhost", port=8000)

    # curl -X POST "http://127.0.0.1:8000/predict" -H "Content-Type: application/json" -d '[
    #     {
    # "Severity": "Critical",
    # "Alarm Name": "NodeB Unavailable",
    # "Technical ID": "22214",
    # "Site Name": "Dummar",
    # "Site ID": "12345",
    # "NE Name": "NodeB_123",
    # "Source": "SourceA",
    # "NE Type": "TypeA",
    # "Technology": "4G",
    # "Alarm Category": "CategoryA",
    # "Parent Node": "NodeParent",
    # "First Occurence": "12/01/2015 02:41:22 PM",
    # "Last Occurence": "12/01/2015 02:41:22 PM"
    # },
    # {
    # "severity": "Major",
    # "alarm_name": "NodeB Unavailable",
    # "technical_ID": "22214",
    # "site_name": "Dummar",
    # "site_ID": "12345",
    # "NE_name": "NodeB_123",
    # "source": "SourceA",
    # "NE_type": "TypeA",
    # "technology": "4G",
    # "alarm_category": "CategoryA",
    # "parent_node": "NodeParent",
    # "first_occurence": "12/01/2015 02:41:22 PM",
    # "last_occurence": "12/01/2015 02:41:22 PM"
    # }]'
