from typing import Optional
from typing import Dict
from typing import Any


def update_draft(
    access_key: str,
    url: str,
    owner: str,
    dataset: str,
    *,
    draft_number: int,
    state: Optional[str] = None,
    title: Optional[str] = None,
    description: Optional[str] = None,
) -> Dict[str, Any]:
    """Execute the OpenAPI `PATCH /v2/datasets/{owner}/{dataset}/drafts/{draft_number}`.

    Arguments:
        access_key: User's access key.
        url: The URL of the graviti website.
        owner: The owner of the dataset.
        dataset: Name of the dataset, unique for a user.
        draft_number: The updated draft number.
        state: The updated draft state which could be "CLOSED" or None.
            Where None means no change in state.
        title: The draft title.
        description: The draft description.

    Returns:
        The response of OpenAPI.

    Examples:
        Update the title or description of the draft:

        >>> update_draft(
        ...     "ACCESSKEY-********",
        ...     "https://api.graviti.com",
        ...     "MNIST",
        ...     draft_number=2,
        ...     title="draft-3"
        ... )
        {
           "number": 2,
           "title": "draft-3",
           "description": "",
           "branch": "main",
           "state": "OPEN",
           "parent_commit_id": "85c57a7f03804ccc906632248dc8c359",
           "creator": "graviti-example",
           "created_at": "2021-03-03T18:58:10Z",
           "updated_at": "2021-03-04T18:58:10Z"
        }

        Close the draft:

        >>> update_draft(
        ...     "ACCESSKEY-********",
        ...     "https://api.graviti.com",
        ...     "MNIST",
        ...     draft_number=2,
        ...     state="CLOSED"
        ... )
        {
           "number": 2,
           "title": "draft-3",
           "description": "",
           "branch": "main",
           "state": "CLOSED",
           "parent_commit_id": "85c57a7f03804ccc906632248dc8c359",
           "creator": "graviti-example",
           "created_at": "2021-03-03T18:58:10Z",
           "updated_at": "2021-03-05T18:58:10Z"
        }

    """
    url = f"{url}/v2/datasets/{owner}/{dataset}/drafts/{draft_number}"
    patch_data: Dict[str, Any] = {"draft_number": draft_number}

    if state:
        patch_data["state"] = state
    if title is not None:
        patch_data["title"] = title
    if description is not None:
        patch_data["description"] = description

    return open_api_do(  # type: ignore[no-any-return]
        "PATCH", access_key, url, json=patch_data
    ).json()