from __future__ import annotations

from collections.abc import Mapping
from typing import Any, TypeVar, cast
from uuid import UUID

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..models.artifact_type import ArtifactType
from ..types import UNSET, Unset

T = TypeVar("T", bound="ArtifactDownloadRequest")


@_attrs_define
class ArtifactDownloadRequest:
    """Request for artifact download URL.

    Attributes:
        artifact_type (ArtifactType): Type of artifact for download.
        agent_version_id (UUID): Agent version ID
        eval_run_id (None | Unset | UUID): Run ID for run-scoped artifacts
        problem_id (None | Unset | UUID): Problem ID for per-problem artifact downloads
    """

    artifact_type: ArtifactType
    agent_version_id: UUID
    eval_run_id: None | Unset | UUID = UNSET
    problem_id: None | Unset | UUID = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        artifact_type = self.artifact_type.value

        agent_version_id = str(self.agent_version_id)

        eval_run_id: None | str | Unset
        if isinstance(self.eval_run_id, Unset):
            eval_run_id = UNSET
        elif isinstance(self.eval_run_id, UUID):
            eval_run_id = str(self.eval_run_id)
        else:
            eval_run_id = self.eval_run_id

        problem_id: None | str | Unset
        if isinstance(self.problem_id, Unset):
            problem_id = UNSET
        elif isinstance(self.problem_id, UUID):
            problem_id = str(self.problem_id)
        else:
            problem_id = self.problem_id

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "artifact_type": artifact_type,
                "agent_version_id": agent_version_id,
            }
        )
        if eval_run_id is not UNSET:
            field_dict["eval_run_id"] = eval_run_id
        if problem_id is not UNSET:
            field_dict["problem_id"] = problem_id

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        artifact_type = ArtifactType(d.pop("artifact_type"))

        agent_version_id = UUID(d.pop("agent_version_id"))

        def _parse_eval_run_id(data: object) -> None | Unset | UUID:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                eval_run_id_type_0 = UUID(data)

                return eval_run_id_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | Unset | UUID, data)

        eval_run_id = _parse_eval_run_id(d.pop("eval_run_id", UNSET))

        def _parse_problem_id(data: object) -> None | Unset | UUID:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                problem_id_type_0 = UUID(data)

                return problem_id_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(None | Unset | UUID, data)

        problem_id = _parse_problem_id(d.pop("problem_id", UNSET))

        artifact_download_request = cls(
            artifact_type=artifact_type,
            agent_version_id=agent_version_id,
            eval_run_id=eval_run_id,
            problem_id=problem_id,
        )

        artifact_download_request.additional_properties = d
        return artifact_download_request

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
