# generated by datamodel-codegen:
#   filename:  https://api-test.openepi.io/deforestation/openapi.json
#   timestamp: 2024-01-11T08:35:53+00:00

from __future__ import annotations

from typing import Any, List, Optional, Union

from pydantic import BaseModel, Field, RootModel
from typing_extensions import Literal


class LossYear(BaseModel):
    year: int = Field(
        ...,
        description="Year when the loss was detected.",
        examples=[2022],
        title="Year",
    )
    area: float = Field(
        ...,
        description="Total tree cover loss within the basin polygon, in square kilometers.",
        examples=[8.5095],
        title="Area",
    )
    relative_area: float = Field(
        ...,
        description="Tree cover loss within the basin polygon relative to the total area of the polygon.",
        examples=[0.0036],
        title="Relative Area",
    )


class Coordinate(RootModel[List[Any]]):
    root: List[Any]


class MultiPolygon(BaseModel):
    type: Literal["MultiPolygon"] = Field(..., title="Type")
    coordinates: List[List[List[Coordinate]]] = Field(..., title="Coordinates")


class Polygon(BaseModel):
    type: Literal["Polygon"] = Field(..., title="Type")
    coordinates: List[List[Coordinate]] = Field(..., title="Coordinates")


class ValidationError(BaseModel):
    loc: List[Union[str, int]] = Field(..., title="Location")
    msg: str = Field(..., title="Message")
    type: str = Field(..., title="Error Type")


class BasinProperties(BaseModel):
    downstream_id: int = Field(
        ...,
        description="Id of the next downstream polygon for the current basin polygon. The value 0 means that there is no downstream connection.",
        examples=[1071114980],
        title="Downstream Id",
    )
    basin_area: float = Field(
        ...,
        description="Area of the basin polygon in square kilometers.",
        examples=[2350.0],
        title="Basin Area",
    )
    upstream_area: float = Field(
        ...,
        description="Total upstream area in square kilometers, including the current polygon.",
        examples=[29444.1],
        title="Upstream Area",
    )
    start_year: int = Field(..., examples=[2020], title="Start Year")
    end_year: int = Field(..., examples=[2022], title="End Year")
    daterange_tot_treeloss: float = Field(
        ...,
        description="Total tree cover loss, in square kilometers, within the basin polygon over the time period from start_year to end_year (inclusive)",
        examples=[35.7217],
        title="Daterange Tot Treeloss",
    )
    daterange_rel_treeloss: float = Field(
        ...,
        description="Tree cover loss within the basin polygon relative to the total area of the polygon, over the time period from start_year to end_year (inclusive). Equivalent to `daterange_tot_treeloss / basin_area`.",
        examples=[0.0152],
        title="Daterange Rel Treeloss",
    )
    treeloss_per_year: List[LossYear] = Field(..., title="Treeloss Per Year")


class DeforestationBasinFeature(BaseModel):
    id: int = Field(
        ...,
        description="Unique basin polygon identifier.",
        examples=[1071119930],
        title="Id",
    )
    type: Literal["Feature"] = Field(..., title="Type")
    properties: BasinProperties
    geometry: Union[Polygon, MultiPolygon] = Field(..., title="Geometry")


class DeforestationBasinGeoJSON(BaseModel):
    type: Literal["FeatureCollection"] = Field(..., title="Type")
    features: List[DeforestationBasinFeature] = Field(..., title="Features")


class HTTPValidationError(BaseModel):
    detail: Optional[List[ValidationError]] = Field(None, title="Detail")
