{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "021ac129",
   "metadata": {},
   "source": [
    "# 🛡️ Palladium — Setup & Connection\n",
    "\n",
    "**Start here.** This notebook gets you from a fresh clone to a working Athena connection.\n",
    "\n",
    "| Where things live | |\n",
    "|---|---|\n",
    "| `00_setup.ipynb` | ← you are here: credentials + connection check |\n",
    "| `studies/<slug>/notebooks/` | the actual TEI work, numbered `00_provision` → `04_export` |\n",
    "| `core/` | shared logic (API client, financial math) — you rarely edit this |\n",
    "| `app/` | Streamlit data-entry UI: `make app` or `streamlit run app/main.py` |\n",
    "| `.env` | your Athena URL + API key (gitignored; created below) |\n",
    "\n",
    "Run cells top to bottom. Re-run any time — every step is idempotent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "53fcc345",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Palladium(root='palladium', athena='not tested')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Bootstrap — finds the repo root, loads .env, builds the API client.\n",
    "import sys, pathlib                       # path shim: works on a fresh kernel\n",
    "for _p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents]:\n",
    "    if (_p / \"pyproject.toml\").exists():\n",
    "        sys.path.insert(0, str(_p)); break\n",
    "\n",
    "from core.bootstrap import init, save_credentials\n",
    "\n",
    "pal = init(connect=False)\n",
    "pal"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca43976",
   "metadata": {},
   "source": [
    "## 1 · Credentials\n",
    "\n",
    "Stored in `<repo>/.env` (gitignored). The cell below only prompts if no key is\n",
    "configured yet — paste the key at the prompt and it's saved for every future\n",
    "session, notebook, the CLI, and the Streamlit app.\n",
    "\n",
    "Current target: **https://athena.ouranos.helu.ca** (Ouranos sandbox — safe to experiment, no production data)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "853aaab8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ Credentials already configured for https://athena.ouranos.helu.ca\n",
      "   (To rotate the key: save_credentials(api_key='new-key'))\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "\n",
    "if not os.getenv(\"ATHENA_API_KEY\"):\n",
    "    key = getpass(\"Athena API key (input hidden): \")\n",
    "    path = save_credentials(api_key=key)\n",
    "    print(f\"Saved → {path}\")\n",
    "else:\n",
    "    print(f\"✅ Credentials already configured for {os.getenv('ATHENA_BASE_URL')}\")\n",
    "    print(\"   (To rotate the key: save_credentials(api_key='new-key'))\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa7464fd",
   "metadata": {},
   "source": [
    "## 2 · Test the connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9b7fcc97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ Athena connected — https://athena.ouranos.helu.ca (1 report templates visible)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'status': 'ok',\n",
       " 'base_url': 'https://athena.ouranos.helu.ca',\n",
       " 'authenticated': True,\n",
       " 'reports_found': 1,\n",
       " 'timestamp': '2026-06-10T07:08:06.947037'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pal = init()   # builds the client and pings /api/v1/tei/reports/\n",
    "client = pal.client\n",
    "pal.connection"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6877d6ae",
   "metadata": {},
   "source": [
    "## 3 · What's in this Athena instance?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "83edbe4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>vendor</th>\n",
       "      <th>version</th>\n",
       "      <th>status</th>\n",
       "      <th>analysis_period_years</th>\n",
       "      <th>discount_rate</th>\n",
       "      <th>field_count</th>\n",
       "      <th>instance_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>xsUTbjh4iDnJ</td>\n",
       "      <td>Amazon Connect 2026</td>\n",
       "      <td>AWS</td>\n",
       "      <td>1.0</td>\n",
       "      <td>active</td>\n",
       "      <td>3</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             id                 name vendor version  status  \\\n",
       "0  xsUTbjh4iDnJ  Amazon Connect 2026    AWS     1.0  active   \n",
       "\n",
       "   analysis_period_years discount_rate  field_count  instance_count  \n",
       "0                      3        0.1000           11               0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "reports = client.list_reports()\n",
    "if reports:\n",
    "    display(pd.DataFrame(reports)[\n",
    "        [c for c in (\"id\", \"name\", \"vendor\", \"version\", \"status\",\n",
    "                     \"analysis_period_years\", \"discount_rate\",\n",
    "                     \"field_count\", \"instance_count\") if c in reports[0]]\n",
    "    ])\n",
    "else:\n",
    "    print(\"No TEI report templates yet — studies/202602_AmazonConnect/notebooks/00_provision.ipynb creates one.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a247bedd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No TEI tool instances yet.\n"
     ]
    }
   ],
   "source": [
    "tools = client.list_tools()\n",
    "if tools:\n",
    "    display(pd.DataFrame(tools)[\n",
    "        [c for c in (\"id\", \"name\", \"status\", \"current_version\") if c in tools[0]]\n",
    "    ])\n",
    "else:\n",
    "    print(\"No TEI tool instances yet.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33114d67",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "\n",
    "1. **Provision the Amazon Connect study** → open\n",
    "   [`studies/202602_AmazonConnect/notebooks/00_provision.ipynb`](studies/202602_AmazonConnect/notebooks/00_provision.ipynb).\n",
    "   It creates the report template + fields in the sandbox, creates a tool,\n",
    "   seeds the Forrester values, calculates, and verifies the published totals\n",
    "   (NPV \\$78.7M · ROI 342% · payback <6 months).\n",
    "2. **Work the study** → notebooks `01_benefits` → `04_export` in the same folder.\n",
    "3. **Interactive data entry** → `make app` (or `streamlit run app/main.py`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d20d824f-e464-4ff7-8191-10c2495842a0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "630ee935-7c7b-47e5-9c13-6285316823e2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eba3877-8e51-443f-9953-9d0a48425f9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
