from typing import NotRequired, TypedDict

import requests

from config import DOCUSEAL_API, DOCUSEAL_KEY

headers = {
    'X-Auth-Token': DOCUSEAL_KEY,
}

Submitter = TypedDict(
    'Submitter',
    {
        'role': str,
        'name': str,
        'email': str,
        'external_id': NotRequired[str],
    },
)
EmailMessage = TypedDict(
    'EmailMessage',
    {
        'subject': str,
        'body': str,
    },
)


def create_submission_from_pdf(
    filename: str,
    file: str,
    submitters: list[Submitter],
    email_message: EmailMessage,
    **kwargs,
):
    r = requests.post(
        url=f'{DOCUSEAL_API}/api/submissions/pdf',
        json={
            'name': filename,
            'documents': [
                {
                    'name': filename,
                    'file': file,
                }
            ],
            'message': email_message,
            'submitters': submitters,
            **kwargs,
        },
        headers=headers,
        timeout=6,
    )
    r.raise_for_status()

    return True
