# !!!
# 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.magic_links_email import InviteResponse, LoginOrSignupResponse
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 "magic_links/email"

    def login_or_signup(
        self,
        organization_id: str,
        email_address: str,
        create_member_as_pending: Optional[bool] = None,
        login_redirect_url: Optional[str] = None,
        signup_redirect_url: Optional[str] = None,
        pkce_code_challenge: Optional[str] = None,
        login_template_id: Optional[str] = None,
        signup_template_id: Optional[str] = None,
    ) -> LoginOrSignupResponse:
        """Send either a login or signup magic link to a Member. A new or pending Member will receive a signup Email Magic Link. An active Member will receive a login Email Magic Link.

        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 who will receive the Email Magic Link for login or signup.

        - `create_member_as_pending`: Flag for whether or not to save a Member as pending vs active in Stytch. Defaults to false. If true, new Members will be created with status pending in Stytch's backend. Their status will remain pending and they will continue to receive sign-up magic links until a magic link is authenticated for that Member. If false, new Members will be created with status active. They will receive a sign-up magic link for their first magic link but subsequent magic links will use the login email template, even if the Member never authenticated their initial magic link.

        - `login_redirect_url`: The URL that Member clicks from the login email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default login redirect URL that you set in your Dashboard is used. If you have not set a default login redirect URL, an error is returned.

        - `signup_redirect_url`: The url the Member clicks from the sign-up email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default sign-up redirect URL that you set in your Dashboard is used. If you have not set a default sign-up redirect URL, an error is returned.

        - `pkce_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. See the PKCE OAuth guide for usage instructions.

        - `login_template_id`: Use a custom template for login emails. By default, it will use your default email template The template must be a template using our built-in customizations or a custom HTML email for Magic links - Login.

        - `signup_template_id`: Use a custom template for sign-up emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Sign-up.
        """  # noqa

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

        if create_member_as_pending is not None:
            payload["create_member_as_pending"] = create_member_as_pending
        if login_redirect_url is not None:
            payload["login_redirect_url"] = login_redirect_url
        if signup_redirect_url is not None:
            payload["signup_redirect_url"] = signup_redirect_url
        if pkce_code_challenge is not None:
            payload["pkce_code_challenge"] = pkce_code_challenge
        if login_template_id is not None:
            payload["login_template_id"] = login_template_id
        if signup_template_id is not None:
            payload["signup_template_id"] = signup_template_id

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

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

    async def login_or_signup_async(
        self,
        organization_id: str,
        email_address: str,
        create_member_as_pending: Optional[bool] = None,
        login_redirect_url: Optional[str] = None,
        signup_redirect_url: Optional[str] = None,
        pkce_code_challenge: Optional[str] = None,
        login_template_id: Optional[str] = None,
        signup_template_id: Optional[str] = None,
    ) -> LoginOrSignupResponse:
        """Send either a login or signup magic link to a Member. A new or pending Member will receive a signup Email Magic Link. An active Member will receive a login Email Magic Link.

        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 who will receive the Email Magic Link for login or signup.

        - `create_member_as_pending`: Flag for whether or not to save a Member as pending vs active in Stytch. Defaults to false. If true, new Members will be created with status pending in Stytch's backend. Their status will remain pending and they will continue to receive sign-up magic links until a magic link is authenticated for that Member. If false, new Members will be created with status active. They will receive a sign-up magic link for their first magic link but subsequent magic links will use the login email template, even if the Member never authenticated their initial magic link.

        - `login_redirect_url`: The URL that Member clicks from the login email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default login redirect URL that you set in your Dashboard is used. If you have not set a default login redirect URL, an error is returned.

        - `signup_redirect_url`: The url the Member clicks from the sign-up email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default sign-up redirect URL that you set in your Dashboard is used. If you have not set a default sign-up redirect URL, an error is returned.

        - `pkce_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. See the PKCE OAuth guide for usage instructions.

        - `login_template_id`: Use a custom template for login emails. By default, it will use your default email template The template must be a template using our built-in customizations or a custom HTML email for Magic links - Login.

        - `signup_template_id`: Use a custom template for sign-up emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Sign-up.
        """  # noqa

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

        if create_member_as_pending is not None:
            payload["create_member_as_pending"] = create_member_as_pending
        if login_redirect_url is not None:
            payload["login_redirect_url"] = login_redirect_url
        if signup_redirect_url is not None:
            payload["signup_redirect_url"] = signup_redirect_url
        if pkce_code_challenge is not None:
            payload["pkce_code_challenge"] = pkce_code_challenge
        if login_template_id is not None:
            payload["login_template_id"] = login_template_id
        if signup_template_id is not None:
            payload["signup_template_id"] = signup_template_id

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

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

    def invite(
        self,
        organization_id: str,
        email_address: str,
        invite_redirect_url: Optional[str] = None,
        invite_by_member_id: Optional[str] = None,
        name: Optional[str] = None,
        trusted_metadata: Optional[Dict[str, Any]] = None,
        untrusted_metadata: Optional[Dict[str, Any]] = None,
        invite_template_id: Optional[str] = None,
    ) -> InviteResponse:
        """Send an invite email to a new Member to join an Organization. The Member will be created with a pending status until they successfully authenticate.

        Parameters:

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

        - `email_address`: The email address of the Member.

        - `invite_redirect_url`: The URL that Members click from the login email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default login redirect URL that you set in your Dashboard is used. If you have not set a default login redirect URL, an error is returned.

        - `invite_by_member_id`: The member_id of the Member who sent the invite.

        - `name`: The name of the invited Member.

        - `trusted_metadata`: An arbitrary JSON object for storing application-specific or identity-provider-specific data.

        - `untrusted_metadata`: The untrusted_metadata field contains an arbitrary JSON object of application-specific data. Untrusted metadata can be edited by end Members directly via the SDK, and cannot be used to store critical information. See the Metadata reference for complete field behavior details.

        - `invite_template_id`: Use a custom template for invite emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Invite.
        """  # noqa

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

        if invite_redirect_url is not None:
            payload["invite_redirect_url"] = invite_redirect_url
        if invite_by_member_id is not None:
            payload["invite_by_member_id"] = invite_by_member_id
        if name is not None:
            payload["name"] = name
        if trusted_metadata is not None:
            payload["trusted_metadata"] = trusted_metadata
        if untrusted_metadata is not None:
            payload["untrusted_metadata"] = untrusted_metadata
        if invite_template_id is not None:
            payload["invite_template_id"] = invite_template_id

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

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

    async def invite_async(
        self,
        organization_id: str,
        email_address: str,
        invite_redirect_url: Optional[str] = None,
        invite_by_member_id: Optional[str] = None,
        name: Optional[str] = None,
        trusted_metadata: Optional[Dict[str, Any]] = None,
        untrusted_metadata: Optional[Dict[str, Any]] = None,
        invite_template_id: Optional[str] = None,
    ) -> InviteResponse:
        """Send an invite email to a new Member to join an Organization. The Member will be created with a pending status until they successfully authenticate.

        Parameters:

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

        - `email_address`: The email address of the Member.

        - `invite_redirect_url`: The URL that Members click from the login email magic link. This URL should be an endpoint in the backend server that verifies the request by querying Stytch's authenticate endpoint and finishes the login. If this value is not passed, the default login redirect URL that you set in your Dashboard is used. If you have not set a default login redirect URL, an error is returned.

        - `invite_by_member_id`: The member_id of the Member who sent the invite.

        - `name`: The name of the invited Member.

        - `trusted_metadata`: An arbitrary JSON object for storing application-specific or identity-provider-specific data.

        - `untrusted_metadata`: The untrusted_metadata field contains an arbitrary JSON object of application-specific data. Untrusted metadata can be edited by end Members directly via the SDK, and cannot be used to store critical information. See the Metadata reference for complete field behavior details.

        - `invite_template_id`: Use a custom template for invite emails. By default, it will use your default email template. The template must be a template using our built-in customizations or a custom HTML email for Magic links - Invite.
        """  # noqa

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

        if invite_redirect_url is not None:
            payload["invite_redirect_url"] = invite_redirect_url
        if invite_by_member_id is not None:
            payload["invite_by_member_id"] = invite_by_member_id
        if name is not None:
            payload["name"] = name
        if trusted_metadata is not None:
            payload["trusted_metadata"] = trusted_metadata
        if untrusted_metadata is not None:
            payload["untrusted_metadata"] = untrusted_metadata
        if invite_template_id is not None:
            payload["invite_template_id"] = invite_template_id

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

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