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

from attrs import define as _attrs_define
from dateutil.parser import isoparse

from ..models.fixture_pbp_external_model_period_id import FixturePbpExternalModelPeriodId
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.fixture_pbp_external_model_event_details import FixturePbpExternalModelEventDetails
    from ..models.fixture_pbp_external_model_fixture import FixturePbpExternalModelFixture
    from ..models.fixture_pbp_external_model_organization import FixturePbpExternalModelOrganization


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


@_attrs_define
class FixturePbpExternalModel:
    """
    Attributes:
        organization_id (Union[Unset, str]): The unique identifier of the organization Example: 9.
        organization (Union[Unset, FixturePbpExternalModelOrganization]): The organization that this
            fixture_pbp_external belongs to
        fixture_id (Union[Unset, UUID]): The unique identifier of the match Example:
            009e9276-5c80-11e8-9c2d-fa7ae01bbebc.
        fixture (Union[Unset, FixturePbpExternalModelFixture]): The match
        period_id (Union[Unset, FixturePbpExternalModelPeriodId]): The identifier for the period
            >- `1` Period 1
            >- `2` Period 2
            >- `10` Extra time 1 period 1
            >- `11` Extra time 1 period 2
            >- `12` Extra time 2 period 1
            >- `13` Extra time 2 period 2
            >- `14` Shoot Out
        section (Union[Unset, str]): The section of the period (sub-period)
        events (Union['FixturePbpExternalModelEventDetails', None, Unset]): Event details
        updated (Union[Unset, datetime.datetime]): Date/time last modified. In UTC
        added (Union[Unset, datetime.datetime]): Date/time added. In UTC
    """

    organization_id: Union[Unset, str] = UNSET
    organization: Union[Unset, "FixturePbpExternalModelOrganization"] = UNSET
    fixture_id: Union[Unset, UUID] = UNSET
    fixture: Union[Unset, "FixturePbpExternalModelFixture"] = UNSET
    period_id: Union[Unset, FixturePbpExternalModelPeriodId] = UNSET
    section: Union[Unset, str] = UNSET
    events: Union["FixturePbpExternalModelEventDetails", None, Unset] = UNSET
    updated: Union[Unset, datetime.datetime] = UNSET
    added: Union[Unset, datetime.datetime] = UNSET

    def to_dict(self) -> dict[str, Any]:
        from ..models.fixture_pbp_external_model_event_details import FixturePbpExternalModelEventDetails

        organization_id = self.organization_id

        organization: Union[Unset, dict[str, Any]] = UNSET
        if not isinstance(self.organization, Unset):
            organization = self.organization.to_dict()

        fixture_id: Union[Unset, str] = UNSET
        if not isinstance(self.fixture_id, Unset):
            fixture_id = str(self.fixture_id)

        fixture: Union[Unset, dict[str, Any]] = UNSET
        if not isinstance(self.fixture, Unset):
            fixture = self.fixture.to_dict()

        period_id: Union[Unset, int] = UNSET
        if not isinstance(self.period_id, Unset):
            period_id = self.period_id.value

        section = self.section

        events: Union[None, Unset, dict[str, Any]]
        if isinstance(self.events, Unset):
            events = UNSET
        elif isinstance(self.events, FixturePbpExternalModelEventDetails):
            events = self.events.to_dict()
        else:
            events = self.events

        updated: Union[Unset, str] = UNSET
        if not isinstance(self.updated, Unset):
            updated = self.updated.isoformat()

        added: Union[Unset, str] = UNSET
        if not isinstance(self.added, Unset):
            added = self.added.isoformat()

        field_dict: dict[str, Any] = {}

        field_dict.update({})
        if organization_id is not UNSET:
            field_dict["organizationId"] = organization_id
        if organization is not UNSET:
            field_dict["organization"] = organization
        if fixture_id is not UNSET:
            field_dict["fixtureId"] = fixture_id
        if fixture is not UNSET:
            field_dict["fixture"] = fixture
        if period_id is not UNSET:
            field_dict["periodId"] = period_id
        if section is not UNSET:
            field_dict["section"] = section
        if events is not UNSET:
            field_dict["events"] = events
        if updated is not UNSET:
            field_dict["updated"] = updated
        if added is not UNSET:
            field_dict["added"] = added

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.fixture_pbp_external_model_event_details import FixturePbpExternalModelEventDetails
        from ..models.fixture_pbp_external_model_fixture import FixturePbpExternalModelFixture
        from ..models.fixture_pbp_external_model_organization import FixturePbpExternalModelOrganization

        d = dict(src_dict)
        organization_id = d.pop("organizationId", UNSET)

        _organization = d.pop("organization", UNSET)
        organization: Union[Unset, FixturePbpExternalModelOrganization]
        if isinstance(_organization, Unset):
            organization = UNSET
        else:
            organization = FixturePbpExternalModelOrganization.from_dict(_organization)

        _fixture_id = d.pop("fixtureId", UNSET)
        fixture_id: Union[Unset, UUID]
        if isinstance(_fixture_id, Unset):
            fixture_id = UNSET
        else:
            fixture_id = UUID(_fixture_id)

        _fixture = d.pop("fixture", UNSET)
        fixture: Union[Unset, FixturePbpExternalModelFixture]
        if isinstance(_fixture, Unset):
            fixture = UNSET
        else:
            fixture = FixturePbpExternalModelFixture.from_dict(_fixture)

        _period_id = d.pop("periodId", UNSET)
        period_id: Union[Unset, FixturePbpExternalModelPeriodId]
        if isinstance(_period_id, Unset):
            period_id = UNSET
        else:
            period_id = FixturePbpExternalModelPeriodId(_period_id)

        section = d.pop("section", UNSET)

        def _parse_events(data: object) -> Union["FixturePbpExternalModelEventDetails", None, Unset]:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                events_type_0 = FixturePbpExternalModelEventDetails.from_dict(data)

                return events_type_0
            except:  # noqa: E722
                pass
            return cast(Union["FixturePbpExternalModelEventDetails", None, Unset], data)

        events = _parse_events(d.pop("events", UNSET))

        _updated = d.pop("updated", UNSET)
        updated: Union[Unset, datetime.datetime]
        if isinstance(_updated, Unset):
            updated = UNSET
        else:
            updated = isoparse(_updated)

        _added = d.pop("added", UNSET)
        added: Union[Unset, datetime.datetime]
        if isinstance(_added, Unset):
            added = UNSET
        else:
            added = isoparse(_added)

        fixture_pbp_external_model = cls(
            organization_id=organization_id,
            organization=organization,
            fixture_id=fixture_id,
            fixture=fixture,
            period_id=period_id,
            section=section,
            events=events,
            updated=updated,
            added=added,
        )

        return fixture_pbp_external_model
