# !!!
# WARNING: This file is autogenerated
# Only modify code within MANUAL() sections
# or your changes may be overwritten later!
# !!!

from typing import Any, Dict, Optional

from stytch.b2b.models.passwords_email import ResetResponse, ResetStartResponse
from stytch.core.api_base import ApiBase
from stytch.core.http.client import AsyncClient, SyncClient


class Email:
    def __init__(
        self,
        api_base: ApiBase,
        sync_client: SyncClient,
        async_client: AsyncClient,
    ) -> None:
        self.api_base = api_base
        self.sync_client = sync_client
        self.async_client = async_client

    @property
    def sub_url(self) -> str:
        return "passwords/email"

    def reset_start(
        self,
        organization_id: str,
        email_address: str,
        login_redirect_url: Optional[str] = None,
        reset_password_redirect_url: Optional[str] = None,
        reset_password_expiration_minutes: Optional[int] = None,
        locale: Optional[str] = None,
        reset_password_template_id: Optional[str] = None,
    ) -> ResetStartResponse:
        """[Stytch docs](https://stytch.com/docs/api/password-email-reset-start)

        Initiates a password reset for the email address provided. This will trigger an email to be sent to the address, containing a magic link that will allow them to set a new password and authenticate.

        Parameters:

        - `organization_id`: Globally unique UUID that identifies a specific Organization. The organization_id is critical to perform operations on an Organization, so be sure to preserve this value.

        - `email_address`: The email of the Member

        - `reset_password_redirect_url`: The URL that the Member clicks from the reset password link

        - `login_redirect_url`: The URL that the Member clicks from the reset without password link

        - `reset_password_template_id`: Use a custom template for reset password emails.

        - `locale`: Used to determine which language to use when sending the member an email.

        - `reset_password_expiration_minutes`: Sets a time limit after which the email link to reset the member's password will no longer be valid.

        - `code_challenge`: A base64url encoded SHA256 hash of a one time secret used to validate that the request starts and ends on the same device.
        """  # noqa

        payload: Dict[str, Any] = {
            "organization_id": organization_id,
            "email_address": email_address,
        }

        if login_redirect_url is not None:
            payload["login_redirect_url"] = login_redirect_url
        if reset_password_redirect_url is not None:
            payload["reset_password_redirect_url"] = reset_password_redirect_url
        if reset_password_expiration_minutes is not None:
            payload[
                "reset_password_expiration_minutes"
            ] = reset_password_expiration_minutes
        if locale is not None:
            payload["locale"] = locale
        if reset_password_template_id is not None:
            payload["reset_password_template_id"] = reset_password_template_id

        url = self.api_base.route_with_sub_url(self.sub_url, "reset/start")

        res = self.sync_client.post(url, json=payload)
        return ResetStartResponse.from_json(res.response.status_code, res.json)

    async def reset_start_async(
        self,
        organization_id: str,
        email_address: str,
        login_redirect_url: Optional[str] = None,
        reset_password_redirect_url: Optional[str] = None,
        reset_password_expiration_minutes: Optional[int] = None,
        locale: Optional[str] = None,
        reset_password_template_id: Optional[str] = None,
    ) -> ResetStartResponse:
        """[Stytch docs](https://stytch.com/docs/api/password-email-reset-start)

        Initiates a password reset for the email address provided. This will trigger an email to be sent to the address, containing a magic link that will allow them to set a new password and authenticate.

        Parameters:

        - `organization_id`: Globally unique UUID that identifies a specific Organization. The organization_id is critical to perform operations on an Organization, so be sure to preserve this value.

        - `email_address`: The email of the Member

        - `reset_password_redirect_url`: The URL that the Member clicks from the reset password link

        - `login_redirect_url`: The URL that the Member clicks from the reset without password link

        - `reset_password_template_id`: Use a custom template for reset password emails.

        - `locale`: Used to determine which language to use when sending the member an email.

        - `reset_password_expiration_minutes`: Sets a time limit after which the email link to reset the member's password will no longer be valid.

        - `code_challenge`: A base64url encoded SHA256 hash of a one time secret used to validate that the request starts and ends on the same device.
        """  # noqa

        payload: Dict[str, Any] = {
            "organization_id": organization_id,
            "email_address": email_address,
        }

        if login_redirect_url is not None:
            payload["login_redirect_url"] = login_redirect_url
        if reset_password_redirect_url is not None:
            payload["reset_password_redirect_url"] = reset_password_redirect_url
        if reset_password_expiration_minutes is not None:
            payload[
                "reset_password_expiration_minutes"
            ] = reset_password_expiration_minutes
        if locale is not None:
            payload["locale"] = locale
        if reset_password_template_id is not None:
            payload["reset_password_template_id"] = reset_password_template_id

        url = self.api_base.route_with_sub_url(self.sub_url, "reset/start")

        res = await self.async_client.post(url, json=payload)
        return ResetStartResponse.from_json(res.response.status, res.json)

    def reset(
        self,
        password_reset_token: str,
        password: str,
        session_token: Optional[str] = None,
        session_jwt: Optional[str] = None,
        session_duration_minutes: Optional[int] = None,
        session_custom_claims: Optional[Dict[str, Any]] = None,
    ) -> ResetResponse:
        """Reset the member's password and authenticate them. This endpoint checks that the password reset token is valid, hasn’t expired, or already been used.

        The provided password needs to meet our password strength requirements, which can be checked in advance with the password strength endpoint. If the token and password are accepted, the password is securely stored for future authentication and the user is authenticated.

        Parameters:

        - `password_reset_token`: The password reset token to authenticate.

        - `password`: The password to authenticate

        - `session_token`: A secret token for a given Stytch Session. Read more about session_token in our Session Management guide.

        - `session_jwt`: The JSON Web Token (JWT) for a given Stytch Session. Read more about session_token in our Session Management guide.

        - `session_duration_minutes`: The Session lifetime of this many minutes from now; minimum of 5 and a maximum of 129600 minutes (90 days). Note that a successful authentication will continue to extend the Session this many minutes.

        - `session_custom_claims`: Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in Session duration minutes. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.
          Custom claims made with reserved claims ("iss", "sub", "aud", "exp", "nbf", "iat", "jti") will be ignored. Total custom claims size cannot exceed four kilobytes

        - `code_verifier`: A base64url encoded one time secret used to validate that the request starts and ends on the same device.
        """  # noqa

        payload: Dict[str, Any] = {
            "password_reset_token": password_reset_token,
            "password": password,
        }

        if session_token is not None:
            payload["session_token"] = session_token
        if session_jwt is not None:
            payload["session_jwt"] = session_jwt
        if session_duration_minutes is not None:
            payload["session_duration_minutes"] = session_duration_minutes
        if session_custom_claims is not None:
            payload["session_custom_claims"] = session_custom_claims

        url = self.api_base.route_with_sub_url(self.sub_url, "reset")

        res = self.sync_client.post(url, json=payload)
        return ResetResponse.from_json(res.response.status_code, res.json)

    async def reset_async(
        self,
        password_reset_token: str,
        password: str,
        session_token: Optional[str] = None,
        session_jwt: Optional[str] = None,
        session_duration_minutes: Optional[int] = None,
        session_custom_claims: Optional[Dict[str, Any]] = None,
    ) -> ResetResponse:
        """Reset the member's password and authenticate them. This endpoint checks that the password reset token is valid, hasn’t expired, or already been used.

        The provided password needs to meet our password strength requirements, which can be checked in advance with the password strength endpoint. If the token and password are accepted, the password is securely stored for future authentication and the user is authenticated.

        Parameters:

        - `password_reset_token`: The password reset token to authenticate.

        - `password`: The password to authenticate

        - `session_token`: A secret token for a given Stytch Session. Read more about session_token in our Session Management guide.

        - `session_jwt`: The JSON Web Token (JWT) for a given Stytch Session. Read more about session_token in our Session Management guide.

        - `session_duration_minutes`: The Session lifetime of this many minutes from now; minimum of 5 and a maximum of 129600 minutes (90 days). Note that a successful authentication will continue to extend the Session this many minutes.

        - `session_custom_claims`: Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in Session duration minutes. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.
          Custom claims made with reserved claims ("iss", "sub", "aud", "exp", "nbf", "iat", "jti") will be ignored. Total custom claims size cannot exceed four kilobytes

        - `code_verifier`: A base64url encoded one time secret used to validate that the request starts and ends on the same device.
        """  # noqa

        payload: Dict[str, Any] = {
            "password_reset_token": password_reset_token,
            "password": password,
        }

        if session_token is not None:
            payload["session_token"] = session_token
        if session_jwt is not None:
            payload["session_jwt"] = session_jwt
        if session_duration_minutes is not None:
            payload["session_duration_minutes"] = session_duration_minutes
        if session_custom_claims is not None:
            payload["session_custom_claims"] = session_custom_claims

        url = self.api_base.route_with_sub_url(self.sub_url, "reset")

        res = await self.async_client.post(url, json=payload)
        return ResetResponse.from_json(res.response.status, res.json)
