# QuitSure Refund Form - Deployment Guide

## Overview

This is a standalone Python (FastAPI) app that serves a refund eligibility form for QuitSure users. It runs alongside the existing Laravel backend on the same server, proxied via Nginx at `web.quitsure.app/refund`.

**Architecture:**
```
User opens web.quitsure.app/refund
    -> Nginx proxies to 127.0.0.1:8000
    -> FastAPI (Python) serves the form
    -> On form submit: queries MySQL DB (read-only) + writes to Google Sheet
```

---

## Prerequisites

- Access to the web.quitsure.app server (SSH)
- A read-only MySQL user for QuitSure_Production (Kriti to create)
- A Git repo to host the code (Kriti to create or assign)
- Python 3.8+ on the server

---

## Step 1: Create Git Repo

Kriti or BE team creates a new repo (e.g., `quitsure-refund-form` on GitHub/Bitbucket).

Neel pushes the existing code:
```bash
cd ~/quitsure-refund-form
git init
git add -A
git commit -m "Initial commit: refund eligibility form"
git remote add origin <repo-url>
git push -u origin main
```

**Important:** The `.env` file is gitignored and should NEVER be committed. It contains database credentials and will be created manually on the server.

---

## Step 2: Create Read-Only DB Credentials

Kriti creates a new MySQL user with read-only access.

On the RDS instance (or via MySQL client):
```sql
CREATE USER 'refund_form'@'%' IDENTIFIED BY '<strong-password>';
GRANT SELECT ON QuitSure_Production.* TO 'refund_form'@'%';
FLUSH PRIVILEGES;
```

This user only needs SELECT access. It will never write to the database.

Note the username and password for Step 4.

---

## Step 3: Clone Repo on Server

SSH into the web.quitsure.app server:

```bash
# Install Python if not already installed
sudo apt update
sudo apt install python3 python3-pip -y

# Verify
python3 --version  # Should be 3.8+

# Clone the repo
cd /var/www
git clone <repo-url> quitsure-refund-form
cd quitsure-refund-form

# Install Python dependencies
pip3 install -r requirements.txt
```

---

## Step 4: Create .env File on Server

```bash
nano /var/www/quitsure-refund-form/.env
```

Paste the following (replace placeholders with actual values):

```
# Database (read-only)
DB_HOST=qs-prod-2.cromn2cmgjti.ap-south-1.rds.amazonaws.com
DB_USER=refund_form
DB_PASSWORD=<password-from-step-2>
DB_NAME=QuitSure_Production

# Google Sheets webhook
GOOGLE_SHEET_WEBHOOK_URL=https://script.google.com/macros/s/AKfycbypgaqYZW5FAzNJ4shizH6vlqJKC32Zyl68wjoxT9HOOe6PzrFSgOI2s3AlbX3_-81wbg/exec
GOOGLE_SHEET_SECRET=qs-refund-2026-x7k9m2p56

# App config
ALLOWED_ORIGIN=https://web.quitsure.app
ENV=production
PORT=8000
```

Save and exit (Ctrl+X, Y, Enter).

---

## Step 5: Create systemd Service

This ensures the app starts automatically on boot and restarts if it crashes.

```bash
sudo nano /etc/systemd/system/refund-form.service
```

Paste:

```ini
[Unit]
Description=QuitSure Refund Form
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/quitsure-refund-form
EnvironmentFile=/var/www/quitsure-refund-form/.env
ExecStart=/usr/bin/python3 -m uvicorn app:app --host 127.0.0.1 --port 8000
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
```

Save and exit. Then enable and start:

```bash
sudo systemctl daemon-reload
sudo systemctl enable refund-form
sudo systemctl start refund-form
```

Verify it is running:
```bash
sudo systemctl status refund-form
# Should show "active (running)"

# Test locally on the server
curl -s http://127.0.0.1:8000/health
# Should return: {"status":"healthy","db":true}
```

---

## Step 6: Configure Nginx

Add the refund form route to the existing web.quitsure.app Nginx server block.

```bash
sudo nano /etc/nginx/sites-available/web.quitsure.app
```

Add these lines inside the existing `server { ... }` block:

```nginx
# QuitSure Refund Form
location /refund {
    rewrite ^/refund$ /refund/ permanent;
}

location /refund/ {
    proxy_pass http://127.0.0.1:8000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}
```

Test and reload Nginx:

```bash
sudo nginx -t
# Should say "syntax is ok" and "test is successful"

sudo systemctl reload nginx
```

---

## Step 7: Test the Live Form

Open in browser:
```
https://web.quitsure.app/refund
```

Test checklist:
1. Form loads with QuitSure branding
2. Fill out all fields and submit
3. Check the Google Sheet ("For Coaches" tab) for the new row
4. Check the "Raw Data" tab for the full data dump
5. Test the health endpoint: https://web.quitsure.app/refund/health

---

## Troubleshooting

**Form doesn't load:**
```bash
# Check if the Python app is running
sudo systemctl status refund-form

# Check logs
sudo journalctl -u refund-form -n 50

# Restart if needed
sudo systemctl restart refund-form
```

**DB connection fails:**
```bash
# Test DB connectivity from the server
python3 -c "import pymysql; pymysql.connect(host='qs-prod-2.cromn2cmgjti.ap-south-1.rds.amazonaws.com', user='refund_form', password='<password>', database='QuitSure_Production'); print('OK')"
```
If it fails, check:
- RDS security group allows inbound from this server's IP
- Credentials are correct in .env

**Google Sheet not updating:**
- Check the webhook URL in .env is correct
- Check the shared secret matches between .env and the Apps Script
- Check server logs: `sudo journalctl -u refund-form -n 20`

---

## Updating the App

When code changes are pushed to the repo:

```bash
cd /var/www/quitsure-refund-form
git pull
pip3 install -r requirements.txt  # only if dependencies changed
sudo systemctl restart refund-form
```

---

## Summary of What Each Person Does

| Person | Task |
|--------|------|
| **Kriti** | Create git repo, create read-only DB user, share credentials |
| **BE Team** | Steps 3-6 on the server (clone, .env, systemd, nginx) |
| **Neel** | Push code to repo, test live form, share URL + Google Sheet with coaches |

---

## Key URLs

| What | URL |
|------|-----|
| Live form | https://web.quitsure.app/refund |
| Health check | https://web.quitsure.app/refund/health |
| Google Sheet | (shared with Tiasa) |
| Apps Script | Managed by Neel in Google Apps Script editor |
