{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# agrobr - Demo\n",
    "\n",
    "Dados agrícolas brasileiros em uma linha de código.\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/bruno-portfolio/agrobr/blob/main/examples/demo_colab.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Instalação"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install agrobr -q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. CEPEA - Preço Diário\n",
    "\n",
    "Indicadores de preço do CEPEA/ESALQ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agrobr.sync import cepea\n",
    "\n",
    "df = cepea.indicador(\"soja\")\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.figure(figsize=(12, 4))\n",
    "plt.plot(df[\"data\"], df[\"valor\"])\n",
    "plt.title(\"Preço da Soja - CEPEA/ESALQ\")\n",
    "plt.xlabel(\"Data\")\n",
    "plt.ylabel(\"R$/saca 60kg\")\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. CONAB - Safras\n",
    "\n",
    "Estimativas de safra da CONAB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agrobr.sync import conab\n",
    "\n",
    "df = conab.safras(\"soja\", safra=\"2024/25\")\n",
    "df[[\"uf\", \"area_plantada\", \"producao\", \"produtividade\"]].head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. IBGE - PAM\n",
    "\n",
    "Produção Agrícola Municipal do IBGE."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agrobr.sync import ibge\n",
    "\n",
    "df = ibge.pam(\"soja\", ano=2023, nivel=\"uf\")\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Camada Semântica (v0.6.0)\n",
    "\n",
    "Datasets padronizados com fallback automático entre fontes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agrobr.sync import datasets\n",
    "\n",
    "df = datasets.preco_diario(\"soja\")\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df, meta = datasets.preco_diario(\"soja\", return_meta=True)\n",
    "\n",
    "print(f\"Source: {meta.source}\")\n",
    "print(f\"Dataset: {meta.dataset}\")\n",
    "print(f\"Contract: {meta.contract_version}\")\n",
    "print(f\"Records: {meta.records_count}\")\n",
    "print(f\"From cache: {meta.from_cache}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Listar Datasets e Produtos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Datasets disponíveis:\", datasets.list_datasets())\n",
    "print(\"Produtos preco_diario:\", datasets.list_products(\"preco_diario\"))\n",
    "print(\"Info:\", datasets.info(\"preco_diario\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "source": "## 7. Novas Fontes v0.7.0\n\n### INMET - Dados Meteorológicos",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "source": "from agrobr.sync import inmet\n\n# Dados climáticos mensais de Mato Grosso\ndf_clima = inmet.clima_uf(\"MT\", ano=2024)\ndf_clima.head()",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": "### BCB/SICOR - Crédito Rural",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "source": "from agrobr.sync import bcb\n\n# Crédito rural para soja, safra 2024/25\ndf_credito = bcb.credito_rural(produto=\"soja\", safra=\"2024/25\", finalidade=\"custeio\")\ndf_credito.head()",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": "### ComexStat - Exportações",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "source": "from agrobr.sync import comexstat\n\n# Exportações mensais de soja em 2024\ndf_exp = comexstat.exportacao(\"soja\", ano=2024, agregacao=\"mensal\")\ndf_exp.head()",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": "### CONAB - Custo de Produção",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "source": "# Custo Operacional Efetivo (COE) da soja em MT\ndf_custo = conab.custo_producao(cultura=\"soja\", uf=\"MT\", safra=\"2024/25\")\ndf_custo.head(10)",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": "## 8. MetaInfo — Proveniência (v0.7.0)\n\nTodas as fontes agora retornam `MetaInfo` com rastreabilidade completa.",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "source": "df_exp, meta = comexstat.exportacao(\"soja\", ano=2024, agregacao=\"mensal\", return_meta=True)\n\nprint(\"=== MetaInfo Proveniência ===\")\nprint(f\"  source:            {meta.source}\")\nprint(f\"  selected_source:   {meta.selected_source}\")\nprint(f\"  attempted_sources: {meta.attempted_sources}\")\nprint(f\"  source_method:     {meta.source_method}\")\nprint(f\"  parser_version:    {meta.parser_version}\")\nprint(f\"  records_count:     {meta.records_count}\")\nprint(f\"  fetch_duration_ms: {meta.fetch_duration_ms}\")\nprint(f\"  parse_duration_ms: {meta.parse_duration_ms}\")\nprint(f\"  from_cache:        {meta.from_cache}\")\nprint(f\"  agrobr_version:    {meta.agrobr_version}\")\nprint(f\"  fetch_timestamp:   {meta.fetch_timestamp}\")",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
