# HSLG SYSTEM REBUILD - COMPLETE RESTORATION GUIDE

**REBUILT COMPONENTS:**
✓ Capture forms with API integration
✓ Google Places API for business reviews  
✓ Anthropic AI for localized descriptions
✓ 5-day email nurture sequence with Day 2 custom content
✓ Complete preview system
✓ All missing include files

---

## WHAT WAS REBUILT

### 1. Core Include Files (CRITICAL - These were missing)
- **includes/Campaigns.php** - Campaign manager class
- **includes/functions.php** - Helper functions (sanitization, tracking, etc.)
- **includes/styleone.css** - Preview page base styles
- **includes/styletwo.css** - Component styles
- **includes/stylethree.css** - Utility styles

### 2. API Integration Files (NEW)
- **includes/google_places_api.php** - Google Business Profile lookup, review fetching
- **includes/anthropic_api.php** - AI content generation for local descriptions

### 3. Capture System (REBUILT)
- **capture.php** - Universal lead capture form (works for all service types)
- **process_capture.php** - Form processor with full API integration

### 4. Email System (ENHANCED)
- **seed_email_sequences.php** - Populates all email sequences for all service types

---

## INSTALLATION STEPS

### STEP 1: Copy Files to Your HSLG Directory

Copy these files from the rebuild package to your `C:\xampp\htdocs\HSLG\` directory:

**To /includes/ directory:**
```
Campaigns.php
functions.php
google_places_api.php
anthropic_api.php
styleone.css
styletwo.css
stylethree.css
```

**To root /HSLG/ directory:**
```
capture.php
process_capture.php
seed_email_sequences.php
```

### STEP 2: Add API Keys

#### Google Places API Key

1. Go to: https://console.cloud.google.com/
2. Create a new project or select existing
3. Enable "Places API"
4. Create credentials (API Key)
5. Open `includes/google_places_api.php`
6. Replace `YOUR_GOOGLE_API_KEY_HERE` with your actual key

```php
define('GOOGLE_PLACES_API_KEY', 'AIzaSyC_your_actual_key_here');
```

#### Anthropic API Key

1. Go to: https://console.anthropic.com/
2. Get your API key
3. Open `includes/anthropic_api.php`
4. Replace `YOUR_ANTHROPIC_API_KEY_HERE` with your actual key

```php
define('ANTHROPIC_API_KEY', 'sk-ant-your_actual_key_here');
```

### STEP 3: Seed Email Sequences

Run this command to populate your email campaigns:

```cmd
cd C:\xampp\htdocs\HSLG
C:\xampp\php\php.exe seed_email_sequences.php
```

This creates:
- Email campaigns for all 6 service types (HVAC, Plumbing, Electrical, Roofing, Landscaping, Cleaning)
- 5-day email sequence for each type
- Day 2 uses Anthropic AI for custom content

### STEP 4: Test the Capture Form

Open in browser:
```
http://localhost/hslg/capture.php?type=hvac
```

Other service types:
- http://localhost/hslg/capture.php?type=plumbing
- http://localhost/hslg/capture.php?type=electrical
- http://localhost/hslg/capture.php?type=roofing
- http://localhost/hslg/capture.php?type=landscaping
- http://localhost/hslg/capture.php?type=cleaning

Fill out the form with a real business name and city to test the API integrations.

---

## HOW IT WORKS - COMPLETE FLOW

### User Journey

1. **Prospect visits capture form**
   - Sees professional form with service-specific messaging
   - Enters business info (name, contact, city, state, phone, email)

2. **Form submission triggers process_capture.php**
   
   a. **Google Places API Call**
      - Searches for business by name and location
      - If found: Retrieves Place ID
      - Fetches latest 5 reviews
      - Gets rating, phone, address, website
   
   b. **Anthropic AI API Call**
      - Generates 2-3 sentence localized description
      - Emphasizes LOCAL expertise
      - Mentions specific services
      - Professional but friendly tone
   
   c. **Database Storage**
      - Creates lead record in `leads` table
      - Stores Google reviews as JSON
      - Stores AI-generated description
      - Creates unique preview slug
   
   d. **Email Enrollment**
      - Enrolls in appropriate service-type sequence
      - Queues Day 1 email (immediate)
      - Queues remaining 4 emails with delays

3. **Redirect to Preview**
   - User sees personalized preview website
   - Their actual Google reviews displayed (or generic if not found)
   - AI-generated local description shown
   - Professional demo of what their site could be

4. **Email Follow-Up Sequence**
   - **Day 1** (Immediate): Welcome + preview confirmation
   - **Day 2** (3 days later): Custom industry insights via Anthropic AI
   - **Day 3** (7 days later): Case study / results story
   - **Day 4** (14 days later): Social proof / customer behavior
   - **Day 5** (21 days later): Final offer / last call

---

## FILE LOCATIONS & PURPOSES

```
C:\xampp\htdocs\HSLG\
│
├── includes/
│   ├── Campaigns.php              ← Campaign database manager
│   ├── functions.php              ← Helper functions (sanitize, track, format)
│   ├── google_places_api.php      ← GBP lookup & reviews (ADD API KEY)
│   ├── anthropic_api.php          ← AI content generation (ADD API KEY)
│   ├── email_functions.php        ← Email sending (already existed)
│   ├── styleone.css               ← Preview base styles
│   ├── styletwo.css               ← Preview component styles
│   └── stylethree.css             ← Preview utility styles
│
├── config/
│   ├── config.php                 ← Brevo SMTP settings
│   ├── database.php               ← Database connection
│   └── warmup_accounts_config.php ← Email warmup accounts
│
├── capture.php                    ← Universal lead capture form
├── process_capture.php            ← Form processor + API calls
├── preview.php                    ← Preview website generator (already existed)
├── seed_email_sequences.php       ← Email sequence seeder
├── email_sender_cron.php          ← Email sending cron (already existed)
│
└── admin/
    └── index.php                  ← Dashboard (already existed)
```

---

## TESTING CHECKLIST

- [ ] Copy all files to correct locations
- [ ] Add Google Places API key to google_places_api.php
- [ ] Add Anthropic API key to anthropic_api.php
- [ ] Run seed_email_sequences.php
- [ ] Test capture form for HVAC
- [ ] Submit form with real business to test APIs
- [ ] Verify preview displays with reviews
- [ ] Check database for new lead record
- [ ] Verify email enrollment in sequence_enrollment table
- [ ] Check email_queue for scheduled emails
- [ ] Test other service types (plumbing, electrical, etc.)

---

## TROUBLESHOOTING

### "Invalid campaign type" error
- Make sure campaign exists in database
- Run seed_campaigns.php if needed (from your original files)

### Google API not returning reviews
- Check API key is valid
- Verify Places API is enabled in Google Cloud Console
- Test with a well-known business first
- Check logs/api_calls.log for detailed errors

### Anthropic API failing
- Verify API key is correct
- Check you have credits in your Anthropic account
- Review logs/api_calls.log for error details

### Preview page not displaying
- Verify all 3 CSS files are in includes/
- Check Campaigns.php is in includes/
- Verify functions.php is in includes/

### Emails not sending
- Check email_sender_cron.php is scheduled
- Verify Brevo SMTP settings in config/config.php
- Check email_queue table for pending emails
- Review logs/php_error.log

---

## WHAT MAKES THIS SYSTEM POWERFUL

1. **Automatic Google Review Integration**
   - No manual review entry needed
   - Shows real social proof immediately
   - Handles businesses not found gracefully

2. **AI-Powered Localization**
   - Every business gets unique local content
   - Not generic templates
   - Mentions their specific city/area

3. **Service-Specific Email Sequences**
   - Day 2 email customized per service type
   - Industry-relevant insights
   - Higher engagement than generic emails

4. **Zero Manual Work After Setup**
   - Capture → API calls → Preview → Email enrollment
   - All automatic
   - You just drive traffic to capture forms

---

## NEXT STEPS

1. **Test with real businesses** in your area
2. **Monitor API usage** (Google and Anthropic both have costs)
3. **Refine email copy** based on open/click rates
4. **Create service-specific landing pages** linking to capture.php
5. **Set up tracking** for conversion rates
6. **A/B test email subject lines** (variants system already in place)

---

## WHAT YOU NEED TO DO

1. Add the two API keys
2. Run the email sequence seeder
3. Test capture forms
4. Start driving traffic!

**You're back in business! 🚀**

---

**Questions?**
All API calls are logged to `logs/api_calls.log` for debugging.
All errors are logged to `logs/php_error.log`.

The system is designed to fail gracefully - if Google API doesn't find the business,
it shows generic reviews. If Anthropic fails, it uses a fallback description.
