{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Transit Accessibility Bar Chart\n",
    "\n",
    "Shows transit stops per 1,000 residents across Toronto neighbourhoods."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Data Reference\n",
    "\n",
    "### Source Tables\n",
    "\n",
    "| Table | Grain | Key Columns |\n",
    "|-------|-------|-------------|\n",
    "| `mart_neighbourhood_amenities` | neighbourhood × year | transit_per_1000, transit_index, transit_count |\n",
    "\n",
    "### SQL Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "import os\n",
    "\n",
    "engine = create_engine(os.environ.get('DATABASE_URL', 'postgresql://portfolio:portfolio@localhost:5432/portfolio'))\n",
    "\n",
    "query = \"\"\"\n",
    "SELECT\n",
    "    neighbourhood_name,\n",
    "    transit_per_1000,\n",
    "    transit_index,\n",
    "    transit_count,\n",
    "    population,\n",
    "    amenity_tier\n",
    "FROM public_marts.mart_neighbourhood_amenities\n",
    "WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_amenities)\n",
    "  AND transit_per_1000 IS NOT NULL\n",
    "ORDER BY transit_per_1000 DESC\n",
    "\"\"\"\n",
    "\n",
    "df = pd.read_sql(query, engine)\n",
    "print(f\"Loaded {len(df)} neighbourhoods\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformation Steps\n",
    "\n",
    "1. Sort by transit accessibility\n",
    "2. Select top 20 for visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = df.head(20).to_dict('records')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[['neighbourhood_name', 'transit_per_1000', 'transit_index', 'transit_count']].head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Data Visualization\n",
    "\n",
    "### Figure Factory\n",
    "\n",
    "Uses `create_horizontal_bar` from `portfolio_app.figures.bar_charts`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../..')\n",
    "\n",
    "from portfolio_app.figures.bar_charts import create_horizontal_bar\n",
    "\n",
    "fig = create_horizontal_bar(\n",
    "    data=data,\n",
    "    name_column='neighbourhood_name',\n",
    "    value_column='transit_per_1000',\n",
    "    title='Top 20 Neighbourhoods by Transit Accessibility',\n",
    "    color='#00BCD4',\n",
    "    value_format='.2f',\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transit Statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f\"City-wide Transit Statistics:\")\n",
    "print(f\"  Total Transit Stops: {df['transit_count'].sum():,.0f}\")\n",
    "print(f\"  Average per 1,000 pop: {df['transit_per_1000'].mean():.2f}\")\n",
    "print(f\"  Median per 1,000 pop: {df['transit_per_1000'].median():.2f}\")\n",
    "print(f\"  Best Access: {df['transit_per_1000'].max():.2f} per 1,000\")\n",
    "print(f\"  Worst Access: {df['transit_per_1000'].min():.2f} per 1,000\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
