    return {"accounts": [_account_out(a) for a in rows], "count": len(rows)}


@router.post("/accounts/connect")
async def connect_start(user: User = Depends(get_current_user),
                        db: AsyncSession = Depends(get_db)):
    """Return the Google authorization URL for this user to open (real OAuth start)."""
    try:
        gmail_service.resolve_client_config()
    except gmail_service.GmailOauthError:
        raise HTTPException(400, "No Google OAuth client configuration imported yet. "
                                 "Use 'Import Gmail Configuration' first.")
    from app.api.routers.auth import google_start_impl
    return await google_start_impl(user)


@router.post("/accounts/{account_id}/test")
async def test_account(account_id: int, user: User = Depends(get_current_user),
                       db: AsyncSession = Depends(get_db)):
    """Real connection test: verify OAuth creds and Gmail API profile."""
    account = await db.get(GmailAccount, account_id)
