{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CrumblPy: Examples and Tutorials"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Table of Contents\n",
    "\n",
    "- [Installation](#Installation)\n",
    "- [Import](#Import-the-package)\n",
    "- [SnowflakeToolKit](#SnowflakeToolKit)\n",
    "   - [Snowflake Connection](#Snowflake-Connection)\n",
    "   - [Fetch Data](#Fetch-Data)\n",
    "   - [Write Data](#Write-Data)\n",
    "   - [Execute Query (DML)](#Execute-DML-Query)\n",
    "- [SlackToolKit](#SlackToolKit)\n",
    "   - [Slack Connection](#Slack-Connection)\n",
    "   - [Post Message](#Post-Message)\n",
    "   - [Post Reply](#Post-Reply)\n",
    "   - [Post File](#Post-File)\n",
    "   - [Push Notification](#Push-Notification)\n",
    "- [Gmail](#Gmail)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install crumblpy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import the package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from CrumblPy import SnowflakeToolKit, SlackToolKit, send_gmail"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SnowflakeToolKit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Snowflake Connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure the Snowflake connection\n",
    "\n",
    "# Option 1: Use environment variables (os.environ)\n",
    "sf = SnowflakeToolKit()\n",
    "\n",
    "# Option 2: Hard-code the credentials (not recommended)\n",
    "# sf = SnowflakeToolKit(user='your_username', password='your_password', role='your_role')\n",
    "\n",
    "# # Option 3: Prefect Secrets (deployment only)\n",
    "# sf = SnowflakeToolKit(prefect=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fetch Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "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>STORE_ID</th>\n",
       "      <th>BRAND_ID</th>\n",
       "      <th>WHEN_I_WORK_LOCATION_ID</th>\n",
       "      <th>PUSH_OPERATIONS_ID</th>\n",
       "      <th>SQUARE_LOCATION_ID</th>\n",
       "      <th>UBER_EATS_STORE_ID</th>\n",
       "      <th>GRUB_HUB_MERCHANT_ID</th>\n",
       "      <th>DOORDASH_NAME</th>\n",
       "      <th>NAME</th>\n",
       "      <th>STATE</th>\n",
       "      <th>...</th>\n",
       "      <th>WEEKLY_MINI_PRICE</th>\n",
       "      <th>MINI_PRICE</th>\n",
       "      <th>MAXIMUM_COOKIES_PER_DAY</th>\n",
       "      <th>MINIMUM_LEAD_TIME_IN_HOURS</th>\n",
       "      <th>CATERING_COOKIE_IDS</th>\n",
       "      <th>SQUARE_ID</th>\n",
       "      <th>EVENT_NAME</th>\n",
       "      <th>PROCESSED_AT</th>\n",
       "      <th>UPDATED_AT</th>\n",
       "      <th>CREATED_AT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e0628b10-74f8-11ee-8d9f-f38f32e60e86:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25639</td>\n",
       "      <td>e0628b10-74f8-11ee-8d9f-f38f32e60e86:Store</td>\n",
       "      <td>52f62611-38f2-5fcf-8518-a672693c1602</td>\n",
       "      <td>None</td>\n",
       "      <td>Crumbl Canada</td>\n",
       "      <td>Nepean</td>\n",
       "      <td>Ontario</td>\n",
       "      <td>...</td>\n",
       "      <td>299.0</td>\n",
       "      <td>319.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:05:53.570</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2023-10-27 18:44:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>d6621dc0-e696-11ec-b408-e37cce0087a4:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Garden City</td>\n",
       "      <td>New York</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:05:59.799</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2022-06-07 19:20:05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2b963950-a8d1-11ed-86d0-f7fefe7d0de0:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Wheat Ridge</td>\n",
       "      <td>Colorado</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-23 17:00:59.028</td>\n",
       "      <td>2024-10-23 17:00:56</td>\n",
       "      <td>2023-02-09 23:26:25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6a7e55e0-7fd8-11eb-84bd-ab163f932224:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>5112727.0</td>\n",
       "      <td>None</td>\n",
       "      <td>6a7e55e0-7fd8-11eb-84bd-ab163f932224:Store</td>\n",
       "      <td>a01f9a90-83e8-5b1e-b479-4c700fc20877</td>\n",
       "      <td>None</td>\n",
       "      <td>CRUMBL (HILLSBORO)</td>\n",
       "      <td>Hillsboro</td>\n",
       "      <td>Oregon</td>\n",
       "      <td>...</td>\n",
       "      <td>249.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>200.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-06 06:01:52.087</td>\n",
       "      <td>2024-10-06 06:00:51</td>\n",
       "      <td>2021-03-08 06:35:04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3ff382f0-4c1d-11ee-9d49-6d7f8c129d9e:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Edgewater</td>\n",
       "      <td>New Jersey</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:06:00.012</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2023-09-05 18:51:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>625b8e70-0c39-11ed-98e1-291a683e694a:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>5446711.0</td>\n",
       "      <td>None</td>\n",
       "      <td>625b8e70-0c39-11ed-98e1-291a683e694a:Store</td>\n",
       "      <td>ff077d6f-e754-5c00-9db7-1275d60a4c42</td>\n",
       "      <td>None</td>\n",
       "      <td>Crumbl (Norwalk)</td>\n",
       "      <td>Norwalk</td>\n",
       "      <td>Connecticut</td>\n",
       "      <td>...</td>\n",
       "      <td>249.0</td>\n",
       "      <td>299.0</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:05:57.452</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2022-07-25 16:46:52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>113ac8a0-50e6-11ef-897e-39d4e6071072:Store</td>\n",
       "      <td>be2c5570-74e6-11ed-a722-fb8c9888066e:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>113ac8a0-50e6-11ef-897e-39d4e6071072:Store</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Preston</td>\n",
       "      <td>Idaho</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:06:00.781</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2024-08-02 15:44:13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>71d48e70-ae22-11ed-97e0-b3effe7779ef:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Grove St. Station</td>\n",
       "      <td>New Jersey</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-16 16:12:48.937</td>\n",
       "      <td>2024-10-16 16:12:45</td>\n",
       "      <td>2023-02-16 17:50:48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>16f5f4d0-513f-11ec-aaed-ab35c35632a6:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>Northgate</td>\n",
       "      <td>Washington</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:05:57.836</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2021-11-29 18:06:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>6291c400-b219-11ed-9811-e5c137e4de84:Store</td>\n",
       "      <td>b92e9010-74e6-11ed-9518-ab265dbfb691:Brand</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>West Orange</td>\n",
       "      <td>New Jersey</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>MODIFY</td>\n",
       "      <td>2024-10-04 19:05:52.839</td>\n",
       "      <td>2024-10-04 19:05:49</td>\n",
       "      <td>2023-02-21 18:56:01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 66 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     STORE_ID  \\\n",
       "0  e0628b10-74f8-11ee-8d9f-f38f32e60e86:Store   \n",
       "1  d6621dc0-e696-11ec-b408-e37cce0087a4:Store   \n",
       "2  2b963950-a8d1-11ed-86d0-f7fefe7d0de0:Store   \n",
       "3  6a7e55e0-7fd8-11eb-84bd-ab163f932224:Store   \n",
       "4  3ff382f0-4c1d-11ee-9d49-6d7f8c129d9e:Store   \n",
       "5  625b8e70-0c39-11ed-98e1-291a683e694a:Store   \n",
       "6  113ac8a0-50e6-11ef-897e-39d4e6071072:Store   \n",
       "7  71d48e70-ae22-11ed-97e0-b3effe7779ef:Store   \n",
       "8  16f5f4d0-513f-11ec-aaed-ab35c35632a6:Store   \n",
       "9  6291c400-b219-11ed-9811-e5c137e4de84:Store   \n",
       "\n",
       "                                     BRAND_ID  WHEN_I_WORK_LOCATION_ID  \\\n",
       "0  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "1  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "2  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "3  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                5112727.0   \n",
       "4  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "5  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                5446711.0   \n",
       "6  be2c5570-74e6-11ed-a722-fb8c9888066e:Brand                      NaN   \n",
       "7  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "8  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "9  b92e9010-74e6-11ed-9518-ab265dbfb691:Brand                      NaN   \n",
       "\n",
       "  PUSH_OPERATIONS_ID                          SQUARE_LOCATION_ID  \\\n",
       "0              25639  e0628b10-74f8-11ee-8d9f-f38f32e60e86:Store   \n",
       "1               None                                        None   \n",
       "2               None                                        None   \n",
       "3               None  6a7e55e0-7fd8-11eb-84bd-ab163f932224:Store   \n",
       "4               None                                        None   \n",
       "5               None  625b8e70-0c39-11ed-98e1-291a683e694a:Store   \n",
       "6               None  113ac8a0-50e6-11ef-897e-39d4e6071072:Store   \n",
       "7               None                                        None   \n",
       "8               None                                        None   \n",
       "9               None                                        None   \n",
       "\n",
       "                     UBER_EATS_STORE_ID GRUB_HUB_MERCHANT_ID  \\\n",
       "0  52f62611-38f2-5fcf-8518-a672693c1602                 None   \n",
       "1                                  None                 None   \n",
       "2                                  None                 None   \n",
       "3  a01f9a90-83e8-5b1e-b479-4c700fc20877                 None   \n",
       "4                                  None                 None   \n",
       "5  ff077d6f-e754-5c00-9db7-1275d60a4c42                 None   \n",
       "6                                  None                 None   \n",
       "7                                  None                 None   \n",
       "8                                  None                 None   \n",
       "9                                  None                 None   \n",
       "\n",
       "        DOORDASH_NAME               NAME        STATE  ... WEEKLY_MINI_PRICE  \\\n",
       "0       Crumbl Canada             Nepean      Ontario  ...             299.0   \n",
       "1                None        Garden City     New York  ...               NaN   \n",
       "2                None        Wheat Ridge     Colorado  ...               NaN   \n",
       "3  CRUMBL (HILLSBORO)          Hillsboro       Oregon  ...             249.0   \n",
       "4                None          Edgewater   New Jersey  ...               NaN   \n",
       "5    Crumbl (Norwalk)            Norwalk  Connecticut  ...             249.0   \n",
       "6                None            Preston        Idaho  ...               NaN   \n",
       "7                None  Grove St. Station   New Jersey  ...               NaN   \n",
       "8                None          Northgate   Washington  ...               NaN   \n",
       "9                None        West Orange   New Jersey  ...               NaN   \n",
       "\n",
       "  MINI_PRICE MAXIMUM_COOKIES_PER_DAY MINIMUM_LEAD_TIME_IN_HOURS  \\\n",
       "0      319.0                     NaN                        NaN   \n",
       "1        NaN                     NaN                        NaN   \n",
       "2        NaN                     NaN                        NaN   \n",
       "3      299.0                   200.0                       48.0   \n",
       "4        NaN                     NaN                        NaN   \n",
       "5      299.0                  1000.0                       36.0   \n",
       "6        NaN                     NaN                        NaN   \n",
       "7        NaN                     NaN                        NaN   \n",
       "8        NaN                     NaN                        NaN   \n",
       "9        NaN                     NaN                        NaN   \n",
       "\n",
       "  CATERING_COOKIE_IDS SQUARE_ID EVENT_NAME            PROCESSED_AT  \\\n",
       "0                None      None     MODIFY 2024-10-04 19:05:53.570   \n",
       "1                None      None     MODIFY 2024-10-04 19:05:59.799   \n",
       "2                None      None     MODIFY 2024-10-23 17:00:59.028   \n",
       "3                None      None     MODIFY 2024-10-06 06:01:52.087   \n",
       "4                None      None     MODIFY 2024-10-04 19:06:00.012   \n",
       "5                None      None     MODIFY 2024-10-04 19:05:57.452   \n",
       "6                None      None     MODIFY 2024-10-04 19:06:00.781   \n",
       "7                None      None     MODIFY 2024-10-16 16:12:48.937   \n",
       "8                None      None     MODIFY 2024-10-04 19:05:57.836   \n",
       "9                None      None     MODIFY 2024-10-04 19:05:52.839   \n",
       "\n",
       "           UPDATED_AT          CREATED_AT  \n",
       "0 2024-10-04 19:05:49 2023-10-27 18:44:36  \n",
       "1 2024-10-04 19:05:49 2022-06-07 19:20:05  \n",
       "2 2024-10-23 17:00:56 2023-02-09 23:26:25  \n",
       "3 2024-10-06 06:00:51 2021-03-08 06:35:04  \n",
       "4 2024-10-04 19:05:49 2023-09-05 18:51:40  \n",
       "5 2024-10-04 19:05:49 2022-07-25 16:46:52  \n",
       "6 2024-10-04 19:05:49 2024-08-02 15:44:13  \n",
       "7 2024-10-16 16:12:45 2023-02-16 17:50:48  \n",
       "8 2024-10-04 19:05:49 2021-11-29 18:06:35  \n",
       "9 2024-10-04 19:05:49 2023-02-21 18:56:01  \n",
       "\n",
       "[10 rows x 66 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Fetch data from Snowflake\n",
    "sf.fetch_data(\n",
    "    \"\"\"\n",
    "    SELECT * FROM STORES limit 10;\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Write Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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>COOKIE_ID</th>\n",
       "      <th>NAME</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9b2ba8a0-9875-11ef-a16e-abc1c2cf6cf5:Cookies</td>\n",
       "      <td>Chocolate Peanut Butter Sandwich</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>e72f42b0-c4f1-11ed-a893-2158bb08e2f1:Cookies</td>\n",
       "      <td>Apple Pie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>dfbe4a70-e2ed-11ed-b65e-4bac356c9d69:Cookies</td>\n",
       "      <td>Chocolate Chip Test - V1-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>37b52280-8fbe-11ee-961e-e1a804ad307a:Cookies</td>\n",
       "      <td>Apple Sweet Pie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>8e90dd00-a0a1-11ec-adcf-a5314440ec47:Cookie</td>\n",
       "      <td>Dirt Cake ft. OTTO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>77eab600-84c1-11ef-b69d-eb08e677efd8:Cookies</td>\n",
       "      <td>Holiday Spiced Butter Cake</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>ed449980-72bf-11ee-a2b7-319903869c9c:Cookies</td>\n",
       "      <td>Buckeye Brownie*</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>c93898e0-63fd-11ef-acc7-8b24d42a682d:Cookies</td>\n",
       "      <td>Cookie Dough Bits</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>854a3ee0-7e6b-11ee-bd83-59656f65a976:Cookies</td>\n",
       "      <td>Chocolate Sprinkle Blondie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d3c9b120-27e8-11ed-a43a-21c73e66cda7:Cookies</td>\n",
       "      <td>R&amp;D Recipe Builder</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      COOKIE_ID  \\\n",
       "0  9b2ba8a0-9875-11ef-a16e-abc1c2cf6cf5:Cookies   \n",
       "1  e72f42b0-c4f1-11ed-a893-2158bb08e2f1:Cookies   \n",
       "2  dfbe4a70-e2ed-11ed-b65e-4bac356c9d69:Cookies   \n",
       "3  37b52280-8fbe-11ee-961e-e1a804ad307a:Cookies   \n",
       "4   8e90dd00-a0a1-11ec-adcf-a5314440ec47:Cookie   \n",
       "5  77eab600-84c1-11ef-b69d-eb08e677efd8:Cookies   \n",
       "6  ed449980-72bf-11ee-a2b7-319903869c9c:Cookies   \n",
       "7  c93898e0-63fd-11ef-acc7-8b24d42a682d:Cookies   \n",
       "8  854a3ee0-7e6b-11ee-bd83-59656f65a976:Cookies   \n",
       "9  d3c9b120-27e8-11ed-a43a-21c73e66cda7:Cookies   \n",
       "\n",
       "                               NAME  \n",
       "0  Chocolate Peanut Butter Sandwich  \n",
       "1                         Apple Pie  \n",
       "2        Chocolate Chip Test - V1-3  \n",
       "3                   Apple Sweet Pie  \n",
       "4                Dirt Cake ft. OTTO  \n",
       "5        Holiday Spiced Butter Cake  \n",
       "6                  Buckeye Brownie*  \n",
       "7                 Cookie Dough Bits  \n",
       "8        Chocolate Sprinkle Blondie  \n",
       "9                R&D Recipe Builder  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table_to_write = sf.fetch_data(\n",
    "    \"\"\"\n",
    "    SELECT COOKIE_ID, NAME FROM COOKIES limit 10;\n",
    "    \"\"\"\n",
    ")\n",
    "table_to_write"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write data to Snowflake\n",
    "sf.insert_data(\n",
    "    df = table_to_write,\n",
    "    table_name = 'CRUMBLPY_DEV',\n",
    "    auto_create_table=True # May not the best practice in production. The table better be created manually. \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>COOKIE_ID</th>\n",
       "      <th>NAME</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e72f42b0-c4f1-11ed-a893-2158bb08e2f1:Cookies</td>\n",
       "      <td>Apple Pie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>dfbe4a70-e2ed-11ed-b65e-4bac356c9d69:Cookies</td>\n",
       "      <td>Chocolate Chip Test - V1-3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37b52280-8fbe-11ee-961e-e1a804ad307a:Cookies</td>\n",
       "      <td>Apple Sweet Pie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8e90dd00-a0a1-11ec-adcf-a5314440ec47:Cookie</td>\n",
       "      <td>Dirt Cake ft. OTTO</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>77eab600-84c1-11ef-b69d-eb08e677efd8:Cookies</td>\n",
       "      <td>Holiday Spiced Butter Cake</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>ed449980-72bf-11ee-a2b7-319903869c9c:Cookies</td>\n",
       "      <td>Buckeye Brownie*</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c93898e0-63fd-11ef-acc7-8b24d42a682d:Cookies</td>\n",
       "      <td>Cookie Dough Bits</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>854a3ee0-7e6b-11ee-bd83-59656f65a976:Cookies</td>\n",
       "      <td>Chocolate Sprinkle Blondie</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d3c9b120-27e8-11ed-a43a-21c73e66cda7:Cookies</td>\n",
       "      <td>R&amp;D Recipe Builder</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>ea84d370-8d22-11eb-b017-13424779f04a:Cookie</td>\n",
       "      <td>Chocolate Wafer Bar</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      COOKIE_ID                        NAME\n",
       "0  e72f42b0-c4f1-11ed-a893-2158bb08e2f1:Cookies                   Apple Pie\n",
       "1  dfbe4a70-e2ed-11ed-b65e-4bac356c9d69:Cookies  Chocolate Chip Test - V1-3\n",
       "2  37b52280-8fbe-11ee-961e-e1a804ad307a:Cookies             Apple Sweet Pie\n",
       "3   8e90dd00-a0a1-11ec-adcf-a5314440ec47:Cookie          Dirt Cake ft. OTTO\n",
       "4  77eab600-84c1-11ef-b69d-eb08e677efd8:Cookies  Holiday Spiced Butter Cake\n",
       "5  ed449980-72bf-11ee-a2b7-319903869c9c:Cookies            Buckeye Brownie*\n",
       "6  c93898e0-63fd-11ef-acc7-8b24d42a682d:Cookies           Cookie Dough Bits\n",
       "7  854a3ee0-7e6b-11ee-bd83-59656f65a976:Cookies  Chocolate Sprinkle Blondie\n",
       "8  d3c9b120-27e8-11ed-a43a-21c73e66cda7:Cookies          R&D Recipe Builder\n",
       "9   ea84d370-8d22-11eb-b017-13424779f04a:Cookie        Chocolate Wafer Bar "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mnotebook controller is DISPOSED. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "# Check the data we just inserted\n",
    "sf.fetch_data(\n",
    "    \"\"\"\n",
    "    SELECT * FROM DATA_SCIENCE.CRUMBLPY_DEV;\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Execute DML Query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Drop the table\n",
    "sf.execute_query(\n",
    "    \"\"\"\n",
    "    DROP TABLE IF EXISTS ANALYTICS.DATA_SCIENCE.CRUMBLPY_DEV;\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Check it again. This will return an error because the table no longer exists. \n",
    "# sf.fetch_data(\n",
    "#     \"\"\"\n",
    "#     SELECT * FROM DATA_SCIENCE.CRUMBLPY_DEV;\n",
    "#     \"\"\"\n",
    "# )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SlackToolKit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Slack Connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure the Slack connection\n",
    "\n",
    "# Channel ID\n",
    "channel = 'C07ULKY3VD2' # CrumblPy Dev channel\n",
    "\n",
    "# Option 1: Use environment variables (os.environ)\n",
    "slack = SlackToolKit(default_channel=channel)\n",
    "\n",
    "# Option 2: Hard-code the credentials (not recommended)\n",
    "# slack = SlackToolKit(token='data_bot_token', default_channel=channel)\n",
    "\n",
    "# # Option 3: Prefect Secrets (deployment only)\n",
    "# sf = SnowflakeToolKit(prefect=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Post Message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "slack.post_message(\"Hello from CrumblPy!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./slack_message.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Post Reply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "latest_thread_id = slack.get_thread_id(channel=channel)\n",
    "slack.post_message(\"Reply from CrumblPy!\", thread_id=latest_thread_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./slack_reply.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Post File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create an example file\n",
    "sf.fetch_data(\n",
    "    \"\"\"\n",
    "    SELECT NAME, OPENED_AT FROM STORES limit 10;\n",
    "    \"\"\"\n",
    ").to_csv('example.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "slack.post_file('example.csv', 'Here is an example file from CrumblPy!', thread_id=latest_thread_id)\n",
    "# The file will be deleted locally after being uploaded to Slack."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./slack_file.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Push Notification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Success message. Usually at the end of the script\n",
    "slack.push_notification(project=\"CrumblPy\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./slack_success.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Error message\n",
    "try:\n",
    "    1/0 # This will raise an error because you can't divide by zero\n",
    "    slack.push_notification(project=\"CrumblPy\")\n",
    "except Exception as e:\n",
    "    slack.push_notification(project=\"CrumblPy\", e=e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./slack_error.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gmail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create an example csv\n",
    "sf.fetch_data(\n",
    "    \"\"\"\n",
    "    SELECT NAME, OPENED_AT FROM STORES limit 10;\n",
    "    \"\"\"\n",
    ").to_csv('example.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "send_gmail(\n",
    "    sender = 'donation-reporting@crumbl.com',\n",
    "    recipient = 'steven.wang@crumbl.com',\n",
    "    subject = 'Subject from CrumblPy',\n",
    "    body = 'Body from CrumblPy',\n",
    "    token_path = 'token.json', # This token is not in Doppler or Prefect yet.\n",
    "    image_paths = ['slack_message.png','slack_reply.png'],\n",
    "    attachment_paths = ['example.csv']\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Alt text](./email.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "crumblpy",
   "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
