import os
from typing import Optional

from pydantic import BaseModel

from tests.functional.pages.abstract import WebDriverT
from tests.functional.pages.sign_in import SignInPage
from tests.functional.pages.sign_up import SignUpPage
from tests.functional.util.consts import URL_LANDING
from tests.functional.util.consts import URL_SIGN_IN
from tests.functional.util.consts import URL_SIGN_UP
from tests.functional.util.util import validate_redirect


class Credentials(BaseModel):
    email: Optional[str] = None
    password: Optional[str] = None
    username: Optional[str] = None


def sign_up(browser: WebDriverT = None) -> Credentials:
    # current_url = browser.current_url
    credentials = Credentials()

    try:
        page = SignUpPage(browser, URL_SIGN_UP)

        nonce = os.urandom(8).hex()

        page.email = credentials.email = f"<EMAIL>"
        page.password = credentials.password = f"<PASSWORD>+"
        page.password_confirmation = <PASSWORD>
        page.username = credentials.username = f"test_user_{nonce}"

        page.sign_up.click()

        validate_redirect(page, URL_LANDING)

    except Exception:
        pass

    # finally:
    #     browser.get(current_url)
    #
    # validate_redirect(page, current_url)

    return credentials


def sign_in(browser: WebDriverT, credentials: Credentials) -> None:
    current_url = browser.current_url

    try:
        page = SignInPage(browser, f"{URL_SIGN_IN}?next={current_url}")

        page.password = <PASSWORD> = <PASSWORD>
        page.username = credentials.username = credentials.username

        page.sign_in.click()

        validate_redirect(page, current_url)

    finally:
        browser.get(current_url)

    validate_redirect(page, current_url)

import os
from typing import Optional

from pydantic import BaseModel

from tests.functional.pages.abstract import WebDriverT
from tests.functional.pages.sign_in import SignInPage
from tests.functional.pages.sign_up import SignUpPage
from tests.functional.util.consts import URL_LANDING
from tests.functional.util.consts import URL_SIGN_IN
from tests.functional.util.consts import URL_SIGN_UP
from tests.functional.util.util import validate_redirect


class Credentials(BaseModel):
    email: Optional[str] = None
    password: Optional[str] = None
    username: Optional[str] = None


def sign_up(browser: WebDriverT = None) -> Credentials:
    # current_url = browser.current_url
    credentials = Credentials()

    try:
        page = SignUpPage(browser, URL_SIGN_UP)

        nonce = os.urandom(8).hex()

        page.email = credentials.email = f"<EMAIL>"
        page.password = credentials.password = f"<PASSWORD>+"
        page.password_confirmation = <PASSWORD>
        page.username = credentials.username = f"test_user_{nonce}"

        page.sign_up.click()

        validate_redirect(page, URL_LANDING)

    except Exception:
        pass

    # finally:
    #     browser.get(current_url)
    #
    # validate_redirect(page, current_url)

    return credentials


def sign_in(browser: WebDriverT, credentials: Credentials) -> None:
    current_url = browser.current_url

    try:
        page = SignInPage(browser, f"{URL_SIGN_IN}?next={current_url}")

        page.password = <PASSWORD> = <PASSWORD>
        page.username = credentials.username = credentials.username

        page.sign_in.click()

        validate_redirect(page, current_url)

    finally:
        browser.get(current_url)

    validate_redirect(page, current_url)

# current_url = browser.current_url # finally: #     browser.get(current_url) # # validate_redirect(page, current_url)