from asyncio import Condition

import httpx
from datetime import datetime, timedelta

from sqlalchemy import select

from app.models.base import async_session
from app.models.user_oauth import UserOAuth
from app.models.user_oauth_app import UserOAuthApp
from app.tasks.async_runner import run_async
from app.tasks.celery_app import celery_app


async def _update_oauth_accounts(account_id: str, account_userid: str, current_user_id: str, db: async_session):
    conditions = [UserOAuth.user_id == current_user_id, UserOAuth.deleted_at.is_(None)]
    if account_id:
        conditions.append(UserOAuth.account_id == account_id)    
    if account_userid:
        conditions.append(UserOAuth.account_userid == account_userid)
    result = await db.execute(
        select(UserOAuth).where(
            *conditions
        )
    )
    oauth_records = result.scalars().all()
    if not oauth_records:
        return True
    for oauth in oauth_records:
        pass

if celery_app:
    @celery_app.task(name="user_oauth.update_oauth_accounts", bind=True, max_retries=3, default_retry_delay=60)
    def update_oauth_accounts(self, account_id: str, account_userid: str, current_user_id: str, db):
        return run_async(_update_oauth_accounts(account_id, account_userid, current_user_id, db))
else:
    class _DisabledTask:
        def delay(self, *args, **kwargs):
            pass

        def apply_async(self, *args, **kwargs):
            pass

    update_oauth_accounts = _DisabledTask()