from http import HTTPStatus
from typing import List

from fastapi import APIRouter, Depends, Query, Response
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlmodel import func, select

from icon_governance.db import get_session
from icon_governance.models.delegations import Delegation

router = APIRouter()


@router.get("/governance/delegations/{address}")
async def get_delegations(
    response: Response,
    address: str,
    skip: int = Query(0),
    limit: int = Query(100, gt=0, lt=101),
    session: AsyncSession = Depends(get_session),
) -> List[Delegation]:
    """Return list of delegations."""
    query = (
        select(Delegation)
        .where(Delegation.address == address)
        .offset(skip)
        .limit(limit)
        .order_by(Delegation.value.desc())
    )

    result = await session.execute(query)
    delegations = result.scalars().all()

    # Check if exists
    if len(delegations) == 0:
        return Response(status_code=HTTPStatus.NO_CONTENT.value)

    # Return the count in header
    query_count = select([func.count(Delegation.address)]).where(Delegation.address == address)
    result_count = await session.execute(query_count)
    total_count = str(result_count.scalars().all()[0])
    response.headers["x-total-count"] = total_count

    return delegations


@router.get("/governance/votes/{address}")
async def get_delegations(
    address: str,
    response: Response,
    skip: int = Query(0),
    limit: int = Query(100, gt=0, lt=101),
    session: AsyncSession = Depends(get_session),
) -> List[Delegation]:
    """Return list of votes."""
    query = (
        select(Delegation)
        .where(Delegation.prep_address == address)
        .offset(skip)
        .limit(limit)
        .order_by(Delegation.value.desc())
    )

    result = await session.execute(query)
    delegations = result.scalars().all()

    # Check if exists
    if len(delegations) == 0:
        return Response(status_code=HTTPStatus.NO_CONTENT.value)

    # Return the count in header
    query_count = select([func.count(Delegation.address)]).where(Delegation.prep_address == address)

    result_count = await session.execute(query_count)
    total_count = str(result_count.scalars().all()[0])
    response.headers["x-total-count"] = total_count

    return delegations

fastapi.Query fastapi.Depends icon_governance.models.delegations.Delegation.value.desc sqlmodel.func.count fastapi.Response sqlmodel.select fastapi.APIRouter
[((340, 351), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (349, 351), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((488, 496), 'fastapi.Query', 'Query', (['(0)'], {}), '(0)\n', (493, 496), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((515, 539), 'fastapi.Query', 'Query', (['(100)'], {'gt': '(0)', 'lt': '(101)'}), '(100, gt=0, lt=101)\n', (520, 539), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((569, 589), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (576, 589), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((1479, 1487), 'fastapi.Query', 'Query', (['(0)'], {}), '(0)\n', (1484, 1487), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((1506, 1530), 'fastapi.Query', 'Query', (['(100)'], {'gt': '(0)', 'lt': '(101)'}), '(100, gt=0, lt=101)\n', (1511, 1530), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((1560, 1580), 'fastapi.Depends', 'Depends', (['get_session'], {}), '(get_session)\n', (1567, 1580), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((801, 824), 'icon_governance.models.delegations.Delegation.value.desc', 'Delegation.value.desc', ([], {}), '()\n', (822, 824), False, 'from icon_governance.models.delegations import Delegation\n'), ((984, 1033), 'fastapi.Response', 'Response', ([], {'status_code': 'HTTPStatus.NO_CONTENT.value'}), '(status_code=HTTPStatus.NO_CONTENT.value)\n', (992, 1033), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((1791, 1814), 'icon_governance.models.delegations.Delegation.value.desc', 'Delegation.value.desc', ([], {}), '()\n', (1812, 1814), False, 'from icon_governance.models.delegations import Delegation\n'), ((1974, 2023), 'fastapi.Response', 'Response', ([], {'status_code': 'HTTPStatus.NO_CONTENT.value'}), '(status_code=HTTPStatus.NO_CONTENT.value)\n', (1982, 2023), False, 'from fastapi import APIRouter, Depends, Query, Response\n'), ((1094, 1124), 'sqlmodel.func.count', 'func.count', (['Delegation.address'], {}), '(Delegation.address)\n', (1104, 1124), False, 'from sqlmodel import func, select\n'), ((2084, 2114), 'sqlmodel.func.count', 'func.count', (['Delegation.address'], {}), '(Delegation.address)\n', (2094, 2114), False, 'from sqlmodel import func, select\n'), ((674, 692), 'sqlmodel.select', 'select', (['Delegation'], {}), '(Delegation)\n', (680, 692), False, 'from sqlmodel import func, select\n'), ((1659, 1677), 'sqlmodel.select', 'select', (['Delegation'], {}), '(Delegation)\n', (1665, 1677), False, 'from sqlmodel import func, select\n')]