# TrueNute Plant Doctor — Accounts & History Setup (Bluehost)

This adds "sign in, see your past diagnoses, get them emailed to you
automatically" to the Plant Doctor page. It needs three things Bluehost's
static file hosting alone doesn't have: a database, a bit of server code,
and an email-sending service. Here's the full setup, start to finish.

Budget about 30–45 minutes the first time. None of it requires touching
code — just filling in one config file with values you copy from two
dashboards (Bluehost and SendGrid).

## What you're setting up

- A MySQL database on Bluehost (free — included with your hosting) that
  stores customer emails, sign-in links, and diagnosis history.
- A small set of PHP files on Bluehost (also free/included) that the page
  talks to — sign in, save a diagnosis, load history.
- A free SendGrid account (100 emails/day free forever) that actually
  sends the sign-in links and the "here's your diagnosis" emails. Bluehost
  can technically send email itself, but it's rate-limited and often gets
  flagged as spam — SendGrid is the reliable way to do this.

## Folder layout on your host

Put things in this shape (whatever public folder you're using, e.g.
`public_html/plant-doctor/`):

```
plant-doctor/
  index.html              <- rename plant-doctor.html to this (or leave it named
                              plant-doctor.html and link to it directly — your call)
  backend/
    api/
      request-login.php
      verify-login.php
      me.php
      logout.php
      save-diagnosis.php
      get-history.php
    config.php             <- you create this in step 3 (copy from config.example.php)
    config.example.php
    db.php
    helpers.php
    schema.sql              <- only needed for step 2, doesn't need to stay on the server
```

The important part: `backend/api/` sits right next to your HTML file, at
the relative path `backend/api`. If you place the HTML file somewhere
else, update the `API_BASE` line near the top of the `<script>` block in
the HTML file to match.

## Step 1 — Create the database

1. Log into Bluehost, go to **cPanel → MySQL Databases**.
2. Under "Create New Database," name it something like `plantdoc` (Bluehost
   will prefix it automatically, e.g. `yourcpaneluser_plantdoc`) → Create.
3. Under "MySQL Users → Add New User," create a user (e.g. `plantdocuser`)
   with a strong password → Create. **Save this password somewhere safe.**
4. Under "Add User to Database," add that user to the database you just
   made, and grant **All Privileges**.
5. Write down the full database name (`yourcpaneluser_plantdoc`), full
   username (`yourcpaneluser_plantdocuser`), and the password. You'll need
   all three in Step 3.

## Step 2 — Create the tables

1. Still in cPanel, open **phpMyAdmin**.
2. Click your new database in the left sidebar, then the **SQL** tab.
3. Open `schema.sql` (included in this folder), copy its entire contents,
   paste into the SQL box, and click **Go**.
4. You should see 4 new tables appear on the left: `users`, `login_tokens`,
   `sessions`, `diagnoses`. That's the whole database side done.

## Step 3 — Set up SendGrid (the email service)

1. Go to sendgrid.com and create a free account.
2. During or after signup, verify a **Single Sender** — this is the email
   address diagnoses will be sent *from*. Use something like
   `trueaquaponicsllc@gmail.com` or an address at your own domain if you
   have one set up for email. SendGrid will email that address a
   confirmation link — click it.
3. Go to **Settings → API Keys → Create API Key**. Name it
   "Plant Doctor," give it **Restricted Access** with **Mail Send: Full
   Access** only, and create it. **Copy the key immediately** — SendGrid
   only shows it once.

## Step 4 — Upload the backend files

1. In cPanel, open **File Manager**, navigate to wherever `plant-doctor.html`
   (or `index.html`) lives.
2. Upload the entire `backend/` folder there (drag-and-drop the folder, or
   zip it first and use File Manager's "Extract" after uploading the zip).
3. Inside `backend/`, duplicate `config.example.php` and rename the copy to
   `config.php`.
4. Edit `config.php` (File Manager has a built-in code editor — right-click
   → Edit) and fill in:
   - `DB_HOST` → leave as `localhost`
   - `DB_NAME` → the full database name from Step 1
   - `DB_USER` → the full username from Step 1
   - `DB_PASS` → the password from Step 1
   - `SITE_URL` → the exact URL where the page lives, e.g.
     `https://trueaquaponics.com/plant-doctor` (no trailing slash, no
     `index.html` on the end)
   - `API_URL` → same idea, with `/backend/api` on the end
   - `SENDGRID_API_KEY` → the key from Step 3
   - `FROM_EMAIL` → the address you verified as a Single Sender
   - `APP_SECRET` → any long random string (30+ random characters — mash
     the keyboard, or ask any password generator for one)
5. Save.

## Step 5 — Test it

1. Visit your live Plant Doctor page.
2. Click **Sign in** in the header, enter your own email, submit.
3. Check your inbox for "Your TrueNute Plant Doctor sign-in link" (check
   spam the first time). Click it — you should land back on the page,
   signed in.
4. Describe a plant symptom and get a diagnosis. Within a second or two, a
   small "✓ Saved to your history and emailed to you" line should appear
   under the answer, and a diagnosis email should land in your inbox.
5. Click **My diagnoses** in the header — the diagnosis you just got
   should be listed there.

If something doesn't work, the most common causes are: a typo in
`config.php` (double-check DB name/user/password character-for-character),
the SendGrid sender not yet verified (check your email for that
confirmation link), or `SITE_URL`/`API_URL` not matching where the files
actually live.

## Notes

- **HTTPS required.** The sign-in cookie is set with `secure: true`, so
  this only works over `https://`. Bluehost gives you a free SSL
  certificate — make sure it's active for this domain/subfolder (cPanel →
  Security → SSL/TLS Status).
- **Costs:** the Bluehost side is included in hosting you already pay for.
  SendGrid's free tier (100 emails/day) covers roughly 3,000 diagnoses +
  sign-ins a month combined — plenty of headroom unless the tool takes off
  faster than expected, at which point SendGrid's paid tiers start around
  $20/month for much higher volume.
- **The embedded Shopify widget is unaffected.** This accounts/history
  feature was built for the standalone page only. The floating chat bubble
  widget on your Shopify storefront still works exactly as before, with no
  sign-in.
- Deleting a customer's data on request: run
  `DELETE FROM users WHERE email = 'their@email.com';` in phpMyAdmin —
  the `diagnoses` and `sessions` tables are set up to delete their rows
  automatically along with the user.
