from __future__ import annotations

from datetime import UTC, datetime
from typing import Any, Literal

import pandas as pd
import structlog

from agrobr.datasets.base import BaseDataset, DatasetInfo, DatasetSource
from agrobr.datasets.deterministic import get_snapshot
from agrobr.models import MetaInfo

logger = structlog.get_logger()


async def _fetch_ibge_silvicultura(
    produto: str, **kwargs: Any
) -> tuple[pd.DataFrame, MetaInfo | None]:
    from agrobr import ibge

    ano = kwargs.get("ano")
    nivel = kwargs.get("nivel", "uf")
    uf = kwargs.get("uf")
    variavel = kwargs.get("variavel", "quantidade_produzida")

    result = await ibge.silvicultura(
        produto, ano=ano, nivel=nivel, uf=uf, variavel=variavel, return_meta=True
    )

    if isinstance(result, tuple):
        return result[0], result[1]
    return result, None


SILVICULTURA_INFO = DatasetInfo(
    name="silvicultura",
    description="Produção e área de silvicultura (eucalipto, pinus) por UF ou município",
    sources=[
        DatasetSource(
            name="ibge_silvicultura",
            priority=1,
            fetch_fn=_fetch_ibge_silvicultura,
            description="IBGE PEVS Silvicultura",
        ),
    ],
    products=[
        "carvao",
        "lenha",
        "madeira_tora",
        "madeira_celulose",
        "madeira_outras_finalidades",
        "acacia_negra",
        "eucalipto_folha",
        "resina",
    ],
    contract_version="1.0",
    update_frequency="yearly",
    typical_latency="Y+1",
    source_url="https://sidra.ibge.gov.br",
    source_institution="IBGE",
    min_date="1986-01-01",
    unit="Toneladas / Metros cúbicos / Hectares",
    license="livre",
)


class SilviculturaDataset(BaseDataset):
    info = SILVICULTURA_INFO

    async def fetch(  # type: ignore[override]
        self,
        produto: str,
        ano: int | None = None,
        nivel: Literal["brasil", "uf", "municipio"] = "uf",
        uf: str | None = None,
        variavel: str = "quantidade_produzida",
        return_meta: bool = False,
        **kwargs: Any,
    ) -> pd.DataFrame | tuple[pd.DataFrame, MetaInfo]:
        logger.info("dataset_fetch", dataset="silvicultura", produto=produto, ano=ano)

        snapshot = get_snapshot()
        if snapshot and ano is None:
            ano = int(snapshot[:4]) - 1

        df, source_name, source_meta, attempted = await self._try_sources(
            produto, ano=ano, nivel=nivel, uf=uf, variavel=variavel, **kwargs
        )

        self._validate_contract(df)

        if return_meta:
            now = datetime.now(UTC)
            meta = MetaInfo(
                source=f"datasets.silvicultura/{source_name}",
                source_url=source_meta.source_url if source_meta else "",
                source_method="dataset",
                fetched_at=source_meta.fetched_at if source_meta else now,
                records_count=len(df),
                columns=df.columns.tolist(),
                from_cache=False,
                parser_version=source_meta.parser_version if source_meta else 1,
                dataset="silvicultura",
                contract_version=self.info.contract_version,
                snapshot=snapshot,
                attempted_sources=attempted,
                selected_source=source_name,
                fetch_timestamp=now,
            )
            return df, meta

        return df


_silvicultura = SilviculturaDataset()

from agrobr.datasets.registry import register  # noqa: E402

register(_silvicultura)


async def silvicultura(
    produto: str,
    ano: int | None = None,
    nivel: Literal["brasil", "uf", "municipio"] = "uf",
    uf: str | None = None,
    variavel: str = "quantidade_produzida",
    return_meta: bool = False,
    **kwargs: Any,
) -> pd.DataFrame | tuple[pd.DataFrame, MetaInfo]:
    return await _silvicultura.fetch(
        produto, ano=ano, nivel=nivel, uf=uf, variavel=variavel, return_meta=return_meta, **kwargs
    )
