"""
Subgraph quota endpoint.
"""

from fastapi import APIRouter, Depends, HTTPException, Path, status
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session

from robosystems.config.graph_tier import get_tier_max_subgraphs
from robosystems.database import get_async_db_session
from robosystems.logger import api_logger, log_metric, logger
from robosystems.middleware.auth.dependencies import get_current_user_with_graph
from robosystems.middleware.graph.types import GRAPH_ID_PATTERN
from robosystems.middleware.otel.metrics import endpoint_metrics_decorator
from robosystems.models.api.common import RESOURCE_ERROR_RESPONSES
from robosystems.models.api.graphs.subgraphs import SubgraphQuotaResponse
from robosystems.models.core.graph import Graph
from robosystems.models.core.user import User

from .utils import (
  circuit_breaker,
  handle_circuit_breaker_check,
  record_operation_metrics,
  record_operation_start,
  verify_parent_graph_access,
)

router = APIRouter()


@router.get(
  "/quota",
  response_model=SubgraphQuotaResponse,
  operation_id="getSubgraphQuota",
  summary="Get Subgraph Quota",
  description="Tier capacity: Standard 3, Large 10, XLarge 25 subgraphs. Use the list endpoint for per-subgraph sizes.",
  responses={**RESOURCE_ERROR_RESPONSES},
)
@endpoint_metrics_decorator(
  "/v1/graphs/{graph_id}/subgraphs/quota", business_event_type="subgraph_quota_checked"
)
async def get_subgraph_quota(
  graph_id: str = Path(
    ..., description="Parent graph identifier", pattern=GRAPH_ID_PATTERN
  ),
  current_user: User = Depends(get_current_user_with_graph),
  session: Session = Depends(get_async_db_session),
) -> SubgraphQuotaResponse:
  start_time = record_operation_start()

  # Check circuit breaker
  handle_circuit_breaker_check(graph_id, "subgraph_quota")

  try:
    # Verify parent graph access (read access required)
    parent_graph = verify_parent_graph_access(
      graph_id, current_user, session, required_role="read"
    )

    # Get current subgraphs
    subgraphs = Graph.get_subgraphs(graph_id, session)
    current_count = len(subgraphs)

    # Get max subgraphs from tier configuration
    max_allowed = get_tier_max_subgraphs(parent_graph.graph_tier)

    remaining = None
    if max_allowed is not None:
      remaining = max(0, max_allowed - current_count)

    # TODO: Calculate actual sizes
    total_size_mb = None
    max_size_mb = None

    # Log successful quota check
    api_logger.info(
      f"Checked subgraph quota for parent {graph_id} "
      f"by user {current_user.id}: {current_count}/{max_allowed if max_allowed else 'unlimited'}"
    )

    # Record metrics
    record_operation_metrics(
      start_time, "quota_check", graph_id, {"tier": parent_graph.graph_tier}
    )
    log_metric("subgraph_quota_usage", current_count, {"parent_graph": graph_id})

    # Mark circuit breaker success
    circuit_breaker.record_success(graph_id, "subgraph_quota")

    return SubgraphQuotaResponse(
      parent_graph_id=graph_id,
      tier=parent_graph.graph_tier,
      current_count=current_count,
      max_allowed=max_allowed,
      remaining=remaining,
      total_size_mb=total_size_mb,
      max_size_mb=max_size_mb,
    )

  except HTTPException:
    raise
  except SQLAlchemyError as e:
    logger.error(f"Database error getting subgraph quota: {e}")
    # Record failure metric
    log_metric("subgraph_quota_check_failed", 1, {"error_type": "database"})
    # Mark circuit breaker failure
    circuit_breaker.record_failure(graph_id, "subgraph_quota")
    raise HTTPException(
      status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
      detail="Failed to get subgraph quota due to database error",
    )
  except Exception as e:
    logger.error(f"Unexpected error getting subgraph quota: {e}", exc_info=True)
    # Record failure metric
    log_metric("subgraph_quota_check_failed", 1, {"error_type": "unexpected"})
    # Mark circuit breaker failure
    circuit_breaker.record_failure(graph_id, "subgraph_quota")
    raise HTTPException(
      status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
      detail="Failed to get subgraph quota.",
    )
