{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Promotion Forecast Lab - Notebook Solution\n",
    "\n",
    "This notebook demonstrates the synthetic promotion forecast lab using Chronos-2.\n",
    "\n",
    "**Attribution:**\n",
    "- Source: [amazon-science/chronos-forecasting](https://github.com/amazon-science/chronos-forecasting)\n",
    "- Commit: `10afa9ebe016e514f9d7dc1aa873f66af57e116b`\n",
    "- Model: `autogluon/chronos-2-small` at revision `ddec01313e50b6bc58ebaa92ede81bc24a3d9f9a`\n",
    "\n",
    "**Synthetic Adaptation:**\n",
    "This notebook uses synthetic data generated by `forecast_core.make_fixture`. It is not a reproduction of the Rossmann dataset. All data is synthetic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import json\n",
    "import numpy as np\n",
    "\n",
    "# Add project root to path\n",
    "PROJECT_ROOT = os.environ.get('PROJECT_ROOT', '.')\n",
    "sys.path.insert(0, PROJECT_ROOT)\n",
    "\n",
    "from forecast_core import make_fixture, predict, run_analysis\n",
    "\n",
    "print('Setup complete.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stage 1: Prepare Synthetic History and Known Future Covariates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate fixture\n",
    "seed = 42\n",
    "horizon = 28\n",
    "promotion_start = 7\n",
    "promotion_days = 7\n",
    "\n",
    "fixture = make_fixture(seed=seed, horizon=horizon, promotion_start=promotion_start, promotion_days=promotion_days)\n",
    "\n",
    "print(f'Context shape: {fixture[\"context\"].shape}')\n",
    "print(f'Actual shape: {fixture[\"actual\"].shape}')\n",
    "print(f'Future promotion sum: {fixture[\"future_promotion\"].sum()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stage 2: Real Chronos Inference and Seasonal Baseline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run full analysis which includes prediction and baseline\n",
    "results = run_analysis(seed=seed, horizon=horizon, promotion_start=promotion_start, promotion_days=promotion_days)\n",
    "\n",
    "print(f'Forecast shape: {len(results[\"predictions\"][\"forecast\"])}')\n",
    "print(f'MAE: {results[\"metrics\"][\"mae\"]:.4f}')\n",
    "print(f'Baseline MAE: {results[\"metrics\"][\"baseline_mae\"]:.4f}')\n",
    "print(f'Coverage: {results[\"metrics\"][\"coverage\"]:.4f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stage 3: Evaluate and Persist Results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Persist results to results.json in the current working directory\n",
    "output_path = 'results.json'\n",
    "\n",
    "# The verifier expects a top-level 'results' key containing the metrics/results object\n",
    "# We wrap the run_analysis output in a 'results' key to satisfy the acceptance contract\n",
    "output_data = {\n",
    "    \"results\": results[\"metrics\"],\n",
    "    \"fixture\": results[\"fixture\"],\n",
    "    \"predictions\": results[\"predictions\"],\n",
    "    \"baseline\": results[\"baseline\"]\n",
    "}\n",
    "\n",
    "with open(output_path, 'w') as f:\n",
    "    json.dump(output_data, f, indent=2)\n",
    "\n",
    "print(f'Results saved to {output_path}')\n",
    "\n",
    "# Verify file exists and has content\n",
    "if os.path.exists(output_path):\n",
    "    with open(output_path, 'r') as f:\n",
    "        loaded = json.load(f)\n",
    "    print('Verification: results.json loaded successfully.')\n",
    "    print(f'Keys: {list(loaded.keys())}')\n",
    "    print(f'Results keys: {list(loaded[\"results\"].keys())}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
