# Code generated by python/tools/generate_jira_sprint_models.py. DO NOT EDIT.
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict, Optional

from atlassian.errors import SerializationError

JIRA_SPRINT_BY_ID_QUERY = """query JiraSprintById(
  $id: ID!
) {
  sprintById(id: $id) {
    sprintId
    name
    state
    startDate
    endDate
    completionDate
  }
}
"""


def _expect_dict(obj: Any, path: str) -> Dict[str, Any]:
    if not isinstance(obj, dict):
        raise SerializationError(f"Expected object at {path}")
    return obj


def _expect_str(obj: Any, path: str) -> str:
    if not isinstance(obj, str):
        raise SerializationError(f"Expected string at {path}")
    return obj


def _expect_optional_str(obj: Any, path: str) -> Optional[str]:
    if obj is None:
        return None
    if not isinstance(obj, str):
        raise SerializationError(f"Expected string at {path}")
    if not obj:
        raise SerializationError(f"Expected non-empty string at {path}")
    return obj


@dataclass(frozen=True)
class JiraSprintNode:
    sprint_id: str
    name: Optional[str]
    state: Optional[str]
    start_date: Optional[str]
    end_date: Optional[str]
    completion_date: Optional[str]

    @staticmethod
    def from_dict(obj: Any, path: str) -> "JiraSprintNode":
        raw = _expect_dict(obj, path)
        return JiraSprintNode(
            sprint_id=_expect_str(raw.get("sprintId"), f"{path}.sprintId"),
            name=_expect_optional_str(raw.get("name"), f"{path}.name"),
            state=_expect_optional_str(raw.get("state"), f"{path}.state"),
            start_date=_expect_optional_str(raw.get("startDate"), f"{path}.startDate"),
            end_date=_expect_optional_str(raw.get("endDate"), f"{path}.endDate"),
            completion_date=_expect_optional_str(raw.get("completionDate"), f"{path}.completionDate"),
        )


def parse_jira_sprint_by_id(data: Any) -> JiraSprintNode:
    root = _expect_dict(data, "data")
    sprint = root.get("sprintById")
    if sprint is None:
        raise SerializationError("Missing data.sprintById")
    return JiraSprintNode.from_dict(sprint, "data.sprintById")
