from __future__ import annotations

from sqlite3 import Connection

from fastapi import APIRouter, Depends

from jarvis_finance.api.dependencies import get_db
from jarvis_finance.api.schemas.positions import CryptoActionConfirmRequest, CryptoActionPreviewRequest, CryptoPosition, CryptoPositionDetail, ConfirmResponse, PreviewResponse, PricePoint, WalletDetail, WalletSummary
from jarvis_finance.services.crypto_service import get_crypto_position, get_wallet, list_crypto_positions, list_wallets
from jarvis_finance.services.manual_entry_service import confirm_crypto_action, crypto_price_history, preview_crypto_action

router = APIRouter(tags=["crypto"])


@router.get("/crypto/positions", response_model=list[CryptoPosition])
def crypto_positions(conn: Connection = Depends(get_db)) -> list[CryptoPosition]:
    return list_crypto_positions(conn)


@router.get("/crypto/positions/{asset_id}", response_model=CryptoPositionDetail)
def crypto_position(asset_id: str, conn: Connection = Depends(get_db)) -> CryptoPositionDetail:
    return get_crypto_position(conn, asset_id)


@router.get("/wallets", response_model=list[WalletSummary])
def wallets(conn: Connection = Depends(get_db)) -> list[WalletSummary]:
    return list_wallets(conn)


@router.get("/wallets/{wallet_id}", response_model=WalletDetail)
def wallet(wallet_id: str, conn: Connection = Depends(get_db)) -> WalletDetail:
    return get_wallet(conn, wallet_id)


@router.get("/crypto/prices/{asset_id}/history", response_model=list[PricePoint])
def crypto_history(asset_id: str, conn: Connection = Depends(get_db)) -> list[PricePoint]:
    return crypto_price_history(conn, asset_id)


@router.post("/crypto/actions/preview", response_model=PreviewResponse)
def crypto_action_preview(request: CryptoActionPreviewRequest, conn: Connection = Depends(get_db)) -> PreviewResponse:
    return preview_crypto_action(conn, request)


@router.post("/crypto/actions/confirm", response_model=ConfirmResponse)
def crypto_action_confirm(request: CryptoActionConfirmRequest, conn: Connection = Depends(get_db)) -> ConfirmResponse:
    return confirm_crypto_action(conn, request)
