""" Routing Rules API — CRUD for inbound routing rules and per-device DND. """ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from api.deps import get_gateway from core.gateway import AIPSTNGateway from db.database import Device as DeviceDB from db.database import get_db from models.routing import ( RoutingRule, RoutingRuleCreate, RoutingRuleUpdate, ) router = APIRouter() @router.get("/rules", response_model=list[RoutingRule]) async def list_rules(gateway: AIPSTNGateway = Depends(get_gateway)): if gateway._routing is None: raise HTTPException(status_code=503, detail="Routing service not ready") return sorted(gateway._routing.rules, key=lambda r: (r.priority, r.id)) @router.post("/rules", response_model=RoutingRule, status_code=201) async def create_rule( payload: RoutingRuleCreate, gateway: AIPSTNGateway = Depends(get_gateway), ): if gateway._routing is None: raise HTTPException(status_code=503, detail="Routing service not ready") return await gateway._routing.create_rule(payload) @router.put("/rules/{rule_id}", response_model=RoutingRule) async def update_rule( rule_id: str, payload: RoutingRuleUpdate, gateway: AIPSTNGateway = Depends(get_gateway), ): if gateway._routing is None: raise HTTPException(status_code=503, detail="Routing service not ready") rule = await gateway._routing.update_rule(rule_id, payload) if rule is None: raise HTTPException(status_code=404, detail=f"Rule {rule_id} not found") return rule @router.delete("/rules/{rule_id}") async def delete_rule( rule_id: str, gateway: AIPSTNGateway = Depends(get_gateway), ): if gateway._routing is None: raise HTTPException(status_code=503, detail="Routing service not ready") ok = await gateway._routing.delete_rule(rule_id) if not ok: raise HTTPException(status_code=404, detail=f"Rule {rule_id} not found") return {"status": "deleted", "rule_id": rule_id} @router.patch("/devices/{device_id}/dnd") async def set_device_dnd( device_id: str, payload: dict, gateway: AIPSTNGateway = Depends(get_gateway), db: AsyncSession = Depends(get_db), ): """Toggle Do-Not-Disturb on a device.""" enabled = bool(payload.get("enabled", True)) device = gateway.devices.get(device_id) if not device: raise HTTPException(status_code=404, detail=f"Device {device_id} not found") device.dnd = enabled result = await db.execute(select(DeviceDB).where(DeviceDB.id == device_id)) row = result.scalar_one_or_none() if row is not None: row.dnd = enabled return {"device_id": device_id, "dnd": enabled}