import asyncio
import logging
import os
import uuid
from datetime import datetime, timezone

from dotenv import load_dotenv

from optexity.examples.fadv import fadv_test
from optexity.examples.i94 import automation
from optexity.examples.pshpgeorgia_medicaid import (
    pshpgeorgia_login_test,
    pshpgeorgia_medicaid_test,
)
from optexity.examples.shein import shein_test
from optexity.examples.supabase_login import supabase_login_test
from optexity.inference.core.run_automation import run_automation
from optexity.inference.infra.browser import Browser
from optexity.schema.memory import Memory, Variables
from optexity.schema.task import Task

load_dotenv()


logger = logging.getLogger(__name__)
logging.getLogger(__name__).setLevel(logging.DEBUG)


async def run_supabase_login_test():
    logger.debug("Starting Supabase login test")
    browser = Browser()
    memory = Memory(
        variables=Variables(
            input_variables={
                "username": ["test@test.com"],
                "password": ["password"],
            }
        )
    )

    await browser.start()
    logger.info("Browser started")
    logger.info("Navigating to Supabase")
    await browser.go_to_url("https://supabase.com")
    logger.info("Navigated to Supabase")
    logger.info("Sleeping for 5 seconds")
    await asyncio.sleep(2)

    logger.info("Running automation")
    await run_automation(supabase_login_test, memory, browser)
    logger.info("Automation finished")
    await asyncio.sleep(5)

    await browser.stop()


async def run_pshpgeorgia_test():
    try:
        logger.debug("Starting PSHP Georgia test")
        browser = Browser()
        memory = Memory(
            variables=Variables(
                input_variables={
                    "username": [os.environ.get("USERNAME")],
                    "password": [os.environ.get("PASSWORD")],
                    "plan_type": [os.environ.get("PLAN_TYPE")],
                    "member_id": [os.environ.get("MEMBER_ID")],
                    "dob": [os.environ.get("DOB")],
                }
            )
        )

        await browser.start()
        logger.debug("Browser started")
        logger.debug("Navigating to PSHP Georgia")
        await browser.go_to_url(
            "https://sso.entrykeyid.com/as/authorization.oauth2?response_type=code&client_id=f6a6219c-be42-421b-b86c-e4fc509e2e87&scope=openid%20profile&state=_igWklSsnrkO5DQfjBMMuN41ksMJePZQ_SM_61wTJlA%3D&redirect_uri=https://provider.pshpgeorgia.com/careconnect/login/oauth2/code/pingcloud&code_challenge_method=S256&nonce=xG41TJjco_x7Vs_MQgcS3bw5njLiJsXCqvO-V8THmY0&code_challenge=ZTaVHaZCNFTejXNJo51RlJ3Kv9dH0tMODPTqO7hiP3A&app_origin=https://provider.pshpgeorgia.com/careconnect/login/oauth2/code/pingcloud&brand=pshpgeorgia"
        )
        logger.debug("Navigated to PSHP Georgia")

        logger.debug("Running login test")
        await run_automation(pshpgeorgia_login_test, memory, browser)
        logger.debug("Login test finished")

        logger.debug("Running Medicaid test")
        await run_automation(pshpgeorgia_medicaid_test, memory, browser)
        logger.debug("Medicaid test finished")

        await asyncio.sleep(5)
        await browser.stop()
    except Exception as e:
        logger.error(f"Error running PSHP Georgia test: {e}")
        raise e
    finally:
        await browser.stop()


async def run_i94_test():
    try:
        logger.debug("Starting I-94 test")
        browser = Browser(stealth=True)
        memory = Memory(
            variables=Variables(
                input_variables={
                    "last_name": [os.environ.get("LAST_NAME")],
                    "first_name": [os.environ.get("FIRST_NAME")],
                    "nationality": [os.environ.get("NATIONALITY")],
                    "date_of_birth": [os.environ.get("DATE_OF_BIRTH")],
                    "document_number": [os.environ.get("DOCUMENT_NUMBER")],
                }
            )
        )

        await browser.start()
        logger.debug("Browser started")
        logger.debug("Navigating to I-94")
        await browser.go_to_url(automation.url)
        logger.debug("Navigated to I-94")

        logger.debug("Running I-94 test")
        await asyncio.sleep(5)
        await run_automation(automation, memory, browser)
        logger.debug("I-94 test finished")

        await asyncio.sleep(5)
        await browser.stop()
    except Exception as e:
        logger.error(f"Error running I-94 test: {e}")
        raise e
    finally:
        await browser.stop()


async def run_shein_test():

    try:
        logger.debug("Starting Shein test")
        task = Task(
            task_id=str(uuid.uuid4()),
            user_id=str(uuid.uuid4()),
            recording_id=str(uuid.uuid4()),
            automation=shein_test,
            input_parameters={},
            unique_parameter_names=[],
            created_at=datetime.now(timezone.utc),
            status="queued",
        )
        await run_automation(task, 0)
    except Exception as e:
        logger.error(f"Error running Shein test: {e}")
        raise e
    finally:

        logger.debug("Remaining tasks:")
        for task in asyncio.all_tasks():
            if task is not asyncio.current_task():
                logger.debug(f"Remaining task: {task.get_coro()}")

    logger.debug("Shein test finished")


async def run_fadv_test():
    try:
        logger.debug("Starting FADV test task")
        task = Task(
            task_id=str(uuid.uuid4()),
            user_id=str(uuid.uuid4()),
            recording_id=str(uuid.uuid4()),
            automation=fadv_test,
            input_parameters={
                "client_id": [os.environ.get("client_id")],
                "user_id": [os.environ.get("user_id")],
                "password": [os.environ.get("password")],
                "secret_answer": [os.environ.get("secret_answer")],
                "start_date": [os.environ.get("start_date")],
            },
            unique_parameter_names=[],
            created_at=datetime.now(timezone.utc),
            status="queued",
        )
        await run_automation(task, 0)
        await asyncio.sleep(5)
    except Exception as e:
        logger.error(f"Error running FADV test: {e}")
        raise e
    finally:
        logger.debug("Remaining tasks:")
        for task in asyncio.all_tasks():
            if task is not asyncio.current_task():
                logger.debug(f"Remaining task: {task.get_coro()}")
    logger.debug("FADV test finished")


if __name__ == "__main__":

    # asyncio.run(run_supabase_login_test())
    # asyncio.run(run_pshpgeorgia_test())
    # asyncio.run(run_i94_test())
    asyncio.run(run_fadv_test())
    # asyncio.run(run_shein_test())
