{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Affordability Index Choropleth Map\n",
    "\n",
    "Displays housing affordability across Toronto's 158 neighbourhoods. Index of 100 = city average."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Data Reference\n",
    "\n",
    "### Source Tables\n",
    "\n",
    "| Table | Grain | Key Columns |\n",
    "|-------|-------|-------------|\n",
    "| `mart_neighbourhood_housing` | neighbourhood × year | affordability_index, rent_to_income_pct, avg_rent_2bed, geometry |\n",
    "\n",
    "### SQL Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import pandas as pd\n",
    "from dotenv import load_dotenv\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "# Load .env from project root\n",
    "load_dotenv(\"../../.env\")\n",
    "\n",
    "engine = create_engine(os.environ[\"DATABASE_URL\"])\n",
    "\n",
    "query = \"\"\"\n",
    "SELECT\n",
    "    neighbourhood_id,\n",
    "    neighbourhood_name,\n",
    "    geometry,\n",
    "    year,\n",
    "    affordability_index,\n",
    "    rent_to_income_pct,\n",
    "    avg_rent_2bed,\n",
    "    median_household_income,\n",
    "    is_affordable\n",
    "FROM public_marts.mart_neighbourhood_housing\n",
    "WHERE year = (SELECT MAX(year) FROM public_marts.mart_neighbourhood_housing)\n",
    "ORDER BY affordability_index ASC\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. Filter to most recent year\n",
    "2. Convert geometry to GeoJSON\n",
    "3. Lower index = more affordable (inverted for visualization clarity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "import geopandas as gpd\n",
    "\n",
    "gdf = gpd.GeoDataFrame(\n",
    "    df, geometry=gpd.GeoSeries.from_wkb(df[\"geometry\"]), crs=\"EPSG:4326\"\n",
    ")\n",
    "\n",
    "geojson = json.loads(gdf.to_json())\n",
    "data = df.drop(columns=[\"geometry\"]).to_dict(\"records\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\n",
    "    [\n",
    "        \"neighbourhood_name\",\n",
    "        \"affordability_index\",\n",
    "        \"rent_to_income_pct\",\n",
    "        \"avg_rent_2bed\",\n",
    "        \"is_affordable\",\n",
    "    ]\n",
    "].head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Data Visualization\n",
    "\n",
    "### Figure Factory\n",
    "\n",
    "Uses `create_choropleth_figure` from `portfolio_app.figures.toronto.choropleth`.\n",
    "\n",
    "**Key Parameters:**\n",
    "- `color_column`: 'affordability_index'\n",
    "- `color_scale`: 'RdYlGn_r' (reversed: green=affordable, red=expensive)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.insert(0, \"../..\")\n",
    "\n",
    "from portfolio_app.figures.toronto.choropleth import create_choropleth_figure\n",
    "\n",
    "fig = create_choropleth_figure(\n",
    "    geojson=geojson,\n",
    "    data=data,\n",
    "    location_key=\"neighbourhood_id\",\n",
    "    color_column=\"affordability_index\",\n",
    "    hover_data=[\"neighbourhood_name\", \"rent_to_income_pct\", \"avg_rent_2bed\"],\n",
    "    color_scale=\"RdYlGn_r\",  # Reversed: lower index (affordable) = green\n",
    "    title=\"Toronto Housing Affordability Index\",\n",
    "    zoom=10,\n",
    ")\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Index Interpretation\n",
    "\n",
    "| Index | Meaning |\n",
    "|-------|--------|\n",
    "| < 100 | More affordable than city average |\n",
    "| = 100 | City average affordability |\n",
    "| > 100 | Less affordable than city average |\n",
    "\n",
    "Affordability calculated as: `rent_to_income_pct / city_avg_rent_to_income * 100`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
