# Deploying Southwest Automotive Website to Bluehost

This guide walks through deploying the Next.js app to Bluehost with Node.js support.

## Prerequisites

- Bluehost account with SSH access enabled
- PostgreSQL database already set up (inatetec_southwest_auto)
- Domain pointing to Bluehost nameservers
- SMTP credentials for email (Gmail, SendGrid, etc.)

## Step 1: Access Bluehost via SSH

1. Go to Bluehost cPanel → Advanced → Terminal
2. Or SSH from your local machine:
   ```bash
   ssh your-bluehost-username@your-domain.com
   ```

## Step 2: Install Node.js

Bluehost supports Node.js through Node Managers. In cPanel:

1. Go to **Software → Node.js Manager** (or similar)
2. Create a new Node.js application:
   - **Node.js Version:** 18.x or higher
   - **Application Root:** `/home/yourusername/southwest-auto` (or similar)
   - **Application URL:** `southwestautomotiveaustin.com` (or subdomain)
   - **Application Startup File:** `server.js` (we'll create this)

## Step 3: Upload Your Code

Option A - Via Git:
```bash
cd ~/southwest-auto
git clone <your-repo-url> .
```

Option B - Via SFTP:
1. Use an SFTP client (Cyberduck, WinSCP, etc.)
2. Upload the entire `sw-auto-website` folder to `/home/yourusername/southwest-auto`

## Step 4: Install Dependencies

```bash
cd ~/southwest-auto
npm install --production
```

## Step 5: Run Database Migrations

```bash
npm run db:migrate
```

(This creates the `appointments` and `reviews` tables)

## Step 6: Create Startup File

Create `server.js` in your app root:

```javascript
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url, true);
    handle(req, res, parsedUrl);
  }).listen(3000, (err) => {
    if (err) throw err;
    console.log('> Ready on http://localhost:3000');
  });
});
```

## Step 7: Configure Environment Variables

In cPanel → Node.js Manager → Select your app → Environment Variables:

Add all variables from `.env.local`:

```
DB_HOST=127.0.0.200
DB_PORT=5432
DB_NAME=inatetec_southwest_auto
DB_USER=inatetec_sw_auto_user
DB_PASSWORD=hypke4-totpir-vyCgab
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-app-password
SMTP_FROM=noreply@southwestautomotiveaustin.com
SHOP_MANAGER_EMAIL=manager@southwestautomotiveaustin.com
SITE_URL=https://southwestautomotiveaustin.com
NODE_ENV=production
```

## Step 8: Build the App

```bash
cd ~/southwest-auto
npm run build
```

## Step 9: Start the App

In cPanel → Node.js Manager → Select your app → Click **Start**

Or via SSH:
```bash
npm start
```

## Step 10: Set Up SSL/HTTPS

In cPanel → AutoSSL or Let's Encrypt to enable HTTPS on your domain.

## Step 11: Test the Site

Visit https://southwestautomotiveaustin.com and verify:

- [ ] Home page loads
- [ ] All navigation links work
- [ ] Appointment form submits successfully
- [ ] Manager receives notification email
- [ ] Shop manager dashboard works (`/admin/dashboard`)

## Step 12: Set Up Automatic Restarts

If the Node.js process crashes, Bluehost should restart it automatically. To ensure this, check in Node.js Manager that "Auto Restart" is enabled.

Alternatively, use PM2 for more control:

```bash
npm install -g pm2
pm2 start npm --name "sw-auto" -- start
pm2 startup
pm2 save
```

## Troubleshooting

### Site Won't Load
- Check Node.js app status in cPanel → Node.js Manager
- View logs: `~/.pm2/logs/` or check cPanel error logs

### Emails Not Sending
- Verify SMTP credentials are correct
- Check email provider allows "Less Secure Apps" or has an app-specific password
- Look for errors in error logs

### Database Connection Issues
- Verify PostgreSQL credentials in `.env.local`
- Test connection: `psql -h 127.0.0.200 -U inatetec_sw_auto_user -d inatetec_southwest_auto`
- Ensure database tables exist: Run `npm run db:migrate` again

### Performance
- Monitor Bluehost CPU/memory usage
- Restart Node.js app periodically if needed
- Consider upgrading Bluehost plan if traffic is high

## Updating the Site

To deploy changes:

1. Pull latest code:
   ```bash
   cd ~/southwest-auto && git pull origin main
   ```

2. Install dependencies:
   ```bash
   npm install
   ```

3. Rebuild:
   ```bash
   npm run build
   ```

4. Restart the app in Node.js Manager

## Next Steps

1. Set up Google Business Profile integration (Task #3)
2. Test reviews scraping from Google
3. Monitor appointments and reviews on the shop manager dashboard

Questions? Check Bluehost documentation or contact support.
