# Enhanced Stabilization Plan: God Objects → Domain Architecture

**Date:** 2026-01-23
**Context:** Major refactoring - Strangler Fig pattern to domain architecture
**Critical Requirement:** Constant regression testing against production patterns

---

## Phase 0: Production Baseline + Regression Strategy (NEW)

### 0.1: Production Data Analysis (10 min)

**Purpose:** Understand real data patterns before testing.

```bash
# SSH to production
ssh globalgala_prod
cd ~/public_html/2026_backend_prod

# Get critical metrics
php artisan tinker
>>> DB::table('orders')->count()
>>> DB::table('orders')->select('status', DB::raw('count(*) as cnt'))->groupBy('status')->get()
>>> DB::table('seat_reservations')->count()
>>> DB::table('gala_events')->count()
```

**Document:**
- Order status distribution (for regression baselines)
- Common order flows (confirmed → paid → issued)
- Domain cardinality (Orders:Payments:Notifications ratios)

**Output:** `docs/PRODUCTION_DATA_PATTERNS.md`

---

### 0.2: Create Anonymized Test Dataset (20 min)

**Purpose:** Test against production-like data without PII.

```bash
# On production
php artisan db:seed --class=AnonymizedTestDataSeeder --env=testing

# Export anonymized subset
mysqldump --user=root --password \
  --where="created_at >= '2025-12-01'" \
  showprima_prod orders seat_reservations gala_events payments \
  > /tmp/anonymized_test_data.sql

# Download locally
scp globalgala_prod:/tmp/anonymized_test_data.sql \
  /Users/charlie/code/showprima-notif-decomp/tests/fixtures/
```

**Anonymization rules:**
- Hash email addresses
- Mask phone numbers
- Keep order flows intact
- Keep status distributions intact

---

### 0.3: Domain Boundary Contract Tests (15 min)

**Purpose:** Verify domain interactions don't break during refactoring.

**Create test suite:**
```php
// tests/Integration/DomainBoundaries/OrderGalaIntegrationTest.php
// tests/Integration/DomainBoundaries/OrderPaymentFlowTest.php
// tests/Integration/DomainBoundaries/NotificationTriggerTest.php
```

**Test scenarios:**
- Order creation → Gala seat assignment
- Payment completion → Order status update → Notification sent
- Order refund → Payment reversal → Notification sent

---

### 0.4: Regression Test Baseline (10 min)

**Purpose:** Establish "known good" state before domain refactoring.

```bash
# Run full test suite against PRODUCTION schema (before domain tables)
php artisan test --env=testing --coverage --log-junit=baseline_results.xml

# Capture metrics
echo "Baseline: $(date)" > docs/REGRESSION_BASELINE.md
cat baseline_results.xml | grep -oP 'tests="\K[^"]+' >> docs/REGRESSION_BASELINE.md
```

**Baseline metrics:**
- Total tests passing
- Domain coverage %
- Critical flows (order creation, payment, notification)

---

## Original Phases 1-6 (From STABILIZATION_PLAN.md)

Continue with:
- Phase 1: Replace Schema Baseline
- Phase 2: Archive Conflicting Migrations
- Phase 3: Create Domain-Specific Migrations
- Phase 4: Verify Database Works
- Phase 5: Run Test Suite
- Phase 6: Document Deployment State

**Enhancement:** After Phase 5, compare results to regression baseline.

---

## Phase 7: Continuous Regression Testing Setup (NEW)

### 7.1: Pre-commit Hook

```bash
# .git/hooks/pre-commit
#!/bin/bash
php artisan test tests/Integration/DomainBoundaries --stop-on-failure
```

### 7.2: CI/CD Integration

```yaml
# .github/workflows/domain-regression.yml
name: Domain Regression Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_DATABASE: showprima_test
    steps:
      - uses: actions/checkout@v2
      - name: Install dependencies
        run: composer install
      - name: Run migrations
        run: php artisan migrate --env=testing
      - name: Run domain tests
        run: php artisan test tests/Unit/Domains tests/Feature/Domains
      - name: Check regression baseline
        run: ./scripts/check_regression_baseline.sh
```

---

## Success Criteria (Enhanced)

✅ **Ready for DEV when:**
1. Production schema loaded successfully
2. Domain migrations run without conflicts
3. Test suite passes at >80%
4. **NEW:** Domain boundary tests pass 100%
5. **NEW:** No regressions vs baseline metrics
6. **NEW:** Anonymized production data tests pass

✅ **Ready for MAIN when:**
1. DEV testing complete (separate phase)
2. Domain functionality validated
3. Performance benchmarks met
4. **NEW:** Production data validation complete
5. **NEW:** Regression tests integrated in CI/CD
6. Rollback plan tested

---

## Timeline (Updated)

| Phase | Duration | Focus |
|-------|----------|-------|
| **0.1** Production Analysis | 10 min | Data patterns |
| **0.2** Anonymized Dataset | 20 min | Test data |
| **0.3** Boundary Tests | 15 min | Domain contracts |
| **0.4** Regression Baseline | 10 min | Known good state |
| 1. Replace Schema | 15 min | Production baseline |
| 2. Archive Migrations | 10 min | Clean slate |
| 3. Domain Migrations | 30 min | New tables |
| 4. Verify Database | 15 min | Smoke test |
| 5. Run Test Suite | 30 min | Full validation |
| 6. Document State | 15 min | Deployment docs |
| **7.** CI/CD Setup | 20 min | Continuous testing |
| **TOTAL** | **~3 hours** | **Comprehensive** |

---

## Methodology Alignment

**Your Requirements:**
- ✅ Test against live DB structure → Phase 1 (production schema)
- ✅ Constant regression testing → Phase 0.4 baseline + Phase 7 CI/CD
- ✅ Test with production data → Phase 0.2 (anonymized dataset)
- ✅ Domain architecture preserved → Phase 3 (domain migrations)
- ✅ Incremental validation → Phase 0.3 (boundary tests) + Phase 7 (hooks)

**Strangler Fig Pattern Support:**
- Old code (god objects) still works with production schema
- New code (domain architecture) adds tables incrementally
- Tests verify BOTH work correctly
- Can deploy domain-by-domain (GALA first, then ORDER, etc.)

---

## Ready to Execute?

**Option A:** Full enhanced plan (3 hours, comprehensive)
**Option B:** Original plan only (2 hours, faster but gaps)
**Option C:** Phases 0 + 1-6 only (2.5 hours, skip CI/CD for now)

Your call, Charlie.
