"""
Node related APIs.
"""

import logging
from datetime import datetime
from typing import List, Optional

from fastapi import APIRouter, Depends
from sqlmodel import Session, SQLModel, select

from datajunction.models.column import ColumnType
from datajunction.models.node import Node, NodeType
from datajunction.utils import get_session

_logger = logging.getLogger(__name__)
router = APIRouter()


class SimpleColumn(SQLModel):
    """
    A simplified column schema, without ID or dimensions.
    """

    name: str
    type: ColumnType


class NodeMetadata(SQLModel):
    """
    A node with information about columns and if it is a metric.
    """

    id: int
    name: str
    description: str = ""

    created_at: datetime
    updated_at: datetime

    type: NodeType
    expression: Optional[str] = None

    columns: List[SimpleColumn]


@router.get("/nodes/", response_model=List[NodeMetadata])
def read_nodes(*, session: Session = Depends(get_session)) -> List[NodeMetadata]:
    """
    List the available nodes.
    """
    return session.exec(select(Node)).all()

fastapi.Depends sqlmodel.select logging.getLogger fastapi.APIRouter
[((351, 378), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (368, 378), False, 'import logging\n'), ((388, 399), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (397, 399), False, 'from fastapi import APIRouter, Depends\n'), ((946, 966), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (953, 966), False, 'from fastapi import APIRouter, Depends\n'), ((1061, 1073), 'sqlmodel.select', 'select', (['Node'], {}), '(Node)\n', (1067, 1073), False, 'from sqlmodel import Session, SQLModel, select\n')]