from __future__ import annotations

from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.types_probability import TypesProbability


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


@_attrs_define
class TypesFunnelStage:
    """
    Attributes:
        field_id (str | Unset):
        avg_duration (int | Unset): Average duration of deals in this stage in days
        close_prior_projects (bool | Unset): Should we clear prior projects for this deal when it enters this stage?
        manual_close_probability (float | Unset): Let users set probability manually to get some initial estimations
        name (str | Unset):
        owner (str | Unset): Deal owner when stage is reached
        probabilities (list[TypesProbability] | Unset): Fine-tune true probabilities of various outcomes with actual
            data to model out future sales
        template (str | Unset): Launch a project for the deal when it enters a stage?
    """

    field_id: str | Unset = UNSET
    avg_duration: int | Unset = UNSET
    close_prior_projects: bool | Unset = UNSET
    manual_close_probability: float | Unset = UNSET
    name: str | Unset = UNSET
    owner: str | Unset = UNSET
    probabilities: list[TypesProbability] | Unset = UNSET
    template: str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        avg_duration = self.avg_duration

        close_prior_projects = self.close_prior_projects

        manual_close_probability = self.manual_close_probability

        name = self.name

        owner = self.owner

        probabilities: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.probabilities, Unset):
            probabilities = []
            for probabilities_item_data in self.probabilities:
                probabilities_item = probabilities_item_data.to_dict()
                probabilities.append(probabilities_item)

        template = self.template

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if field_id is not UNSET:
            field_dict["_id"] = field_id
        if avg_duration is not UNSET:
            field_dict["avg_duration"] = avg_duration
        if close_prior_projects is not UNSET:
            field_dict["close_prior_projects"] = close_prior_projects
        if manual_close_probability is not UNSET:
            field_dict["manual_close_probability"] = manual_close_probability
        if name is not UNSET:
            field_dict["name"] = name
        if owner is not UNSET:
            field_dict["owner"] = owner
        if probabilities is not UNSET:
            field_dict["probabilities"] = probabilities
        if template is not UNSET:
            field_dict["template"] = template

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.types_probability import TypesProbability

        d = dict(src_dict)
        field_id = d.pop("_id", UNSET)

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

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

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

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

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

        _probabilities = d.pop("probabilities", UNSET)
        probabilities: list[TypesProbability] | Unset = UNSET
        if _probabilities is not UNSET:
            probabilities = []
            for probabilities_item_data in _probabilities:
                probabilities_item = TypesProbability.from_dict(probabilities_item_data)

                probabilities.append(probabilities_item)

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

        types_funnel_stage = cls(
            field_id=field_id,
            avg_duration=avg_duration,
            close_prior_projects=close_prior_projects,
            manual_close_probability=manual_close_probability,
            name=name,
            owner=owner,
            probabilities=probabilities,
            template=template,
        )

        types_funnel_stage.additional_properties = d
        return types_funnel_stage

    @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
