from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.schemas.wallet import WalletResponse, DepositRequest, WithdrawalRequest, TransactionResponse
from app.routers.auth import get_current_user
from app.crud.wallet import get_user_wallet, get_wallet_transactions
from app.services.wallet_service import deposit_funds, withdraw_funds
from app.models import User

router = APIRouter(prefix="/api/v1/wallet", tags=["wallet"])


@router.get("", response_model=WalletResponse)
async def get_wallet(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db)
):
    wallet = await get_user_wallet(db, current_user.id)
    return wallet


@router.post("/deposit", response_model=WalletResponse)
async def deposit(
    deposit_data: DepositRequest,
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db)
):
    wallet = await deposit_funds(db, current_user.id, deposit_data.amount)
    return wallet


@router.post("/withdraw", response_model=WalletResponse)
async def withdraw(
    withdrawal_data: WithdrawalRequest,
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db)
):
    wallet = await withdraw_funds(db, current_user.id, withdrawal_data.amount)
    return wallet


@router.get("/transactions", response_model=list[TransactionResponse])
async def get_transactions(
    limit: int = 50,
    offset: int = 0,
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db)
):
    transactions = await get_wallet_transactions(db, current_user.id, limit, offset)
    return transactions
