# MailChimp Test Setup for Debugging

This guide explains how to set up MailChimp test endpoints for debugging purposes.

## Overview

The system now supports a test mode for MailChimp API calls that allows you to:

- Use test API keys and audiences
- Debug API calls without affecting production data
- Test batch operations safely
- Get detailed error logging for troubleshooting

## Setup Instructions

### 1. Environment Variables

Add the following variables to your `.env.local` file:

```bash
# Enable MailChimp test mode
MAILCHIMP_TEST_MODE=true

# Test API Key (get this from MailChimp developer account)
MAILCHIMP_TEST_API_KEY=your-test-api-key-us13

# Test Audience IDs (create test audiences in MailChimp)
MAILCHIMP_TEST_MARKETING_AUDIENCE=test-marketing-audience-id
MAILCHIMP_TEST_SCHOOL_AUDIENCE=test-school-audience-id
MAILCHIMP_TEST_COMBINED_AUDIENCE=test-combined-audience-id
MAILCHIMP_TEST_MAIN_AUDIENCE=test-main-audience-id
MAILCHIMP_TEST_MEMBER_AUDIENCE=test-member-audience-id
```

### 2. Get Test API Key

1. Log into your MailChimp account
2. Go to Account → Extras → API Keys
3. Create a new API key for testing
4. Use the test API key in `MAILCHIMP_TEST_API_KEY`

### 3. Create Test Audiences

1. In MailChimp, create separate test audiences
2. Use these audience IDs in the environment variables
3. This ensures test operations don't affect production data

### 4. Test Endpoints

The system will now use these test endpoints when `MAILCHIMP_TEST_MODE=true`:

- **Batches API**: `/batches` with test mode flag
- **Members API**: `/lists/{audience_id}/members` with test audiences
- **Batch Operations**: All batch operations use test credentials

### 5. Debugging Features

When in test mode:

- All API calls are marked with `test_mode=true` parameter
- Error logs include "(Test Mode)" indicator
- Batch operations use test audiences only
- No production data is affected

### 6. Switch Between Test and Production

**Enable Test Mode:**

```bash
MAILCHIMP_TEST_MODE=true
```

**Disable Test Mode (Production):**

```bash
MAILCHIMP_TEST_MODE=false
# or remove the variable entirely
```

### 7. Clear Cache

After making changes:

```bash
php bin/console cache:clear
```

## API Reference

The test setup uses the [MailChimp Marketing API](https://mailchimp.com/developer/marketing/api/list-members/add-member-to-list/) with test endpoints.

Key endpoints used:

- `GET /batches` - List batch operations
- `GET /batches/{batch_id}` - Get batch status
- `POST /lists/{audience_id}/members` - Add members to list
- `POST /batches` - Start batch operations

## Troubleshooting

1. **API Key Issues**: Ensure your test API key is valid and has proper permissions
2. **Audience ID Issues**: Verify test audience IDs exist in your MailChimp account
3. **Test Mode Not Working**: Check that `MAILCHIMP_TEST_MODE=true` is set in `.env.local`
4. **Cache Issues**: Clear Symfony cache after changing environment variables

## Security Notes

- Test API keys should have limited permissions
- Test audiences should be separate from production data
- Never commit test credentials to version control
- Use `.env.local` for local test configuration

## MailChimp Batch Response Example

```json
array(9) {
  ["id"]=>
  string(10) "p3gyu4dk3b"
  ["status"]=>
  string(7) "pending"
  ["total_operations"]=>
  int(0)
  ["finished_operations"]=>
  int(0)
  ["errored_operations"]=>
  int(0)
  ["submitted_at"]=>
  string(25) "2025-07-28T09:35:41+00:00"
  ["completed_at"]=>
  string(0) ""
  ["response_body_url"]=>
  string(0) ""
  ["_links"]=>
  array(3) {
    [0]=>
    array(5) {
      ["rel"]=>
      string(6) "parent"
      ["href"]=>
      string(42) "https://us13.api.mailchimp.com/3.0/batches"
      ["method"]=>
      string(3) "GET"
      ["targetSchema"]=>
      string(85) "https://us13.api.mailchimp.com/schema/3.0/Definitions/Batches/CollectionResponse.json"
      ["schema"]=>
      string(71) "https://us13.api.mailchimp.com/schema/3.0/Paths/Batches/Collection.json"
    }
    [1]=>
    array(4) {
      ["rel"]=>
      string(4) "self"
      ["href"]=>
      string(53) "https://us13.api.mailchimp.com/3.0/batches/p3gyu4dk3b"
      ["method"]=>
      string(3) "GET"
      ["targetSchema"]=>
      string(75) "https://us13.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"
    }
    [2]=>
    array(3) {
      ["rel"]=>
      string(6) "delete"
      ["href"]=>
      string(53) "https://us13.api.mailchimp.com/3.0/batches/p3gyu4dk3b"
      ["method"]=>
      string(6) "DELETE"
    }
  }
}
```
