# Phase 4/5/6 Audit Report

## SITUATION
Agents delivered Phase 4/5/6 code without verifying schema compatibility. Compilation fails with 122 errors.

## ROOT CAUSE ANALYSIS

### 1. SCHEMA MISMATCHES

#### `licenses` table
- **Missing:** `active` BOOLEAN column
- **Mismatch:** Code uses `owner_id`, actual column is `owner_user_id`
- **Impact:** B2B host provisioning queries fail

#### `server_connections` table
- **Missing:** `auth_token` VARCHAR column
- **Actual:** Column is `companion_agent_token`
- **Impact:** Host provisioning can't store companion tokens

#### `notifications_config` vs `public_site_config`
- Code queries `public_site_config.discord_webhook_url`
- **Actual:** `discord_webhook_url` is in `notifications_config` table
- **Impact:** db/notifications.rs and db/public.rs queries fail

### 2. CODE ERRORS

#### Import Errors
**File:** `backend/src/api/webstore.rs`
- Uses `AppError` — should be `ApiError`
- Uses `middleware::jwt::Claims` — should verify path exists

**File:** `backend/src/api/public_store.rs`
- Uses `AppError` — should be `ApiError`

**File:** `backend/src/api/host.rs`
- Uses `AppError` — should be `ApiError`
- Missing import: `use axum::extract::Path;`

**File:** `backend/src/services/module_installer.rs`
- Missing trait import: `use crate::services::panel_adapter::PanelAdapter;`
- Causes `put_file()` and `send_command()` method not found errors

#### Type Mismatches
**File:** `backend/src/api/webstore.rs` (line 287)
- `StoreConfig` struct expects non-nullable fields
- Database `store_config` columns are nullable
- **Fix:** Use `Option<String>` and `Option<bool>` in struct

### 3. MIGRATION GAPS

#### Existing Migrations (Applied)
✅ 001_initial_schema.sql — Core tables
✅ 002_early_access_signups.sql
✅ 003_super_admin.sql
✅ 005_map_analytics.sql
✅ 006_player_sessions.sql
✅ 007_status_page_description.sql
✅ 008_alert_system.sql — Creates alert_config, alert_history
✅ 009_module_licensing.sql — Creates modules, module_purchases, module_installations
✅ 010_payment_orders.sql
✅ 011_webstore_tables.sql — Creates webstore_subscriptions, store_config, store_categories, store_items, store_transactions
✅ 012_b2b_hosts.sql — Creates hosts, host_licenses, host_billing_records

#### Required New Migration
📝 **013_schema_fixes.sql** — Add missing columns:
- `licenses.active` BOOLEAN DEFAULT true
- `licenses.owner_id` as alias/migrate from `owner_user_id`
- `server_connections.auth_token` as alias/migrate from `companion_agent_token`

## BLAST RADIUS

### Broken Files (Won't Compile)
1. backend/src/api/webstore.rs (7 errors)
2. backend/src/api/public_store.rs (1 error)
3. backend/src/api/host.rs (4 errors)
4. backend/src/services/module_installer.rs (6 errors)
5. backend/src/services/host_provisioning.rs (3 errors)
6. backend/src/services/subscription_processor.rs (1 warning)
7. backend/src/db/notifications.rs (1 error)
8. backend/src/db/public.rs (3 errors)
9. backend/src/db/alerts.rs (9 errors - these are false positives, tables exist)

### Impact Assessment
- **Critical:** Cannot build Docker image
- **Moderate:** 122 compilation errors blocking deployment
- **Low:** Once fixed, runtime should work (migrations already applied to DB)

## FIXES REQUIRED

### Category A: Schema Fixes (Migration 013)
1. Add `licenses.active` column
2. Add `licenses.owner_id` column OR update all queries to use `owner_user_id`
3. Add `server_connections.auth_token` column OR update all queries to use `companion_agent_token`

### Category B: Code Fixes
1. Replace `AppError` with `ApiError` (3 files)
2. Add missing imports (2 files)
3. Fix nullable type mismatches (1 file)
4. Fix query column references (4 files)

### Category C: Verification
1. Rebuild with `cargo check`
2. Generate sqlx cache
3. Docker build test
4. Runtime smoke test

## RECOMMENDATION
**Fix forward, not rollback.** All migrations are applied. Code fixes + one new migration will resolve.

**ETA:** 45 minutes
- 15 min: Write migration 013
- 20 min: Fix code errors
- 10 min: Test compilation + generate sqlx cache
