#!/bin/bash

echo "=== WIKI.JS DEPLOYMENT DOCUMENTATION ==="

cd ~/firefrost-work/firefrost-operations-manual

# 1. Create Wiki.js deployment guide
cat > docs/wikijs-deployment.md << 'WIKIDOC'
# Wiki.js Deployment - Staff & Subscriber Wikis

**Deployed:** February 10, 2026, 9:30pm - 11:15pm CST  
**Server:** Ghost VPS (64.50.188.14)  
**Services:** Staff Wiki + Subscriber Wiki  
**Duration:** ~1h 45min (including troubleshooting)

---

## Services Deployed

### 1. Staff Wiki (staff.firefrostgaming.com)

**Purpose:** Staff-facing documentation, SOPs, content creation resources

**Configuration:**
- Domain: staff.firefrostgaming.com
- SSL: Let's Encrypt (active)
- Storage: Git backend
- Repository: git.firefrostgaming.com/firefrost-gaming/firefrost-staff-wiki

**Structure:**
```
firefrost-staff-wiki/
├── founders/ (Michael + Meg only)
├── social-media/ (Meg + Social Media Manager)
├── builders/ (Michael + Builders)
├── moderation/ (Future moderators)
├── shared/ (All staff)
├── community/ (Community resources)
└── branding/ (Brand assets)
```

**Access Control:** Role-based via Wiki.js permissions

### 2. Subscriber Wiki (subscribers.firefrostgaming.com)

**Purpose:** Premium subscriber content, guides, exclusive tutorials

**Configuration:**
- Domain: subscribers.firefrostgaming.com
- SSL: Let's Encrypt (active)
- Storage: NOT YET CONFIGURED (pending decision)
- Status: Deployed but awaiting content strategy

**On Hold Until:**
- Paymenter billing integration complete
- Welcome page finalized
- Subscriber tier content defined

---

## Issues Encountered

### SQLite3 Native Module Error

**Problem:**
```
Error: Cannot find module '/opt/wikijs/node_modules/sqlite3/lib/binding/napi-v6-linux-glibc-x64/node_sqlite3.node'
```

**Timeline:**
- Error began: ~20:00 CST
- Service crashed repeatedly (10-second restart loop)
- Lasted approximately 4+ minutes
- Resolved by: ~20:04 CST

**Root Cause:** 
Wiki.js attempting to use SQLite3 database, but native Node.js bindings were missing/broken.

**Resolution:**
Exact fix not documented at time of deployment. Likely solutions:
- Switched to Git storage backend (for Staff Wiki - confirmed)
- Rebuilt sqlite3 module: `npm rebuild sqlite3`
- OR switched to different database (PostgreSQL/MySQL)

**Services recovered and operational by 20:07 CST.**

---

## Lessons Learned

### LESSON #1: Avoid SQLite3 for Wiki.js

**Problem:** SQLite3 requires compiled native bindings that frequently break across Node.js versions and system architectures.

**Recommendation for Future:**
- **Preferred:** Git storage (simple, version-controlled, no database needed)
- **Alternative:** PostgreSQL or MySQL (if database required)
- **Avoid:** SQLite3 (native module dependency hell)

### LESSON #2: Document Immediately

**What We Lost:**
Due to late-night deployment and not documenting immediately:
- Exact troubleshooting steps taken
- Specific fix applied
- Time spent on each solution attempt
- Configuration decisions made under pressure

**Protocol Moving Forward:**
After ANY deployment (successful or not), create immediate 5-minute note:
```bash
nano ~/deployment-note.txt
```
Capture: What deployed, what broke, how fixed, when finished.

Next session: Formalize into proper documentation.

**This deployment taught us:** Knowledge lost due to fatigue = lessons we'll repeat.

---

## Current Status (Feb 11, 2026)

**Staff Wiki:**
- ✅ Operational
- ✅ SSL active
- ✅ Git backend configured
- ✅ Department structure created
- ✅ Consultant profiles added to social-media/

**Subscriber Wiki:**
- ✅ Deployed
- ✅ SSL active
- ⏸️ Storage backend TBD
- ⏸️ Awaiting Paymenter integration
- ⏸️ Content strategy in progress

---

## Next Steps

**Subscriber Wiki:**
1. Decide on storage backend (Git vs database)
2. Configure chosen backend
3. Create content structure
4. Integrate with Paymenter for access control
5. Populate with initial premium content

**Staff Wiki:**
1. ✅ Structure complete
2. Continue adding department-specific content
3. Configure Wiki.js permissions per role
4. Train Meg on Wiki.js editing
5. Add remaining staff documentation

---

## Technical Details

**Wiki.js Version:** (Not documented - check via Wiki.js admin)  
**Node.js Version:** 20.20.0  
**Nginx Configuration:** Reverse proxy on Ghost VPS  
**Firewall:** Frostwall rules (ports 80/443 to Ghost IP)

**Deployment Method:** Automation script (specifics not preserved)

---

**Fire + Frost = Documentation That Survives Midnight Deployments** 🔥❄️📚
WIKIDOC

echo "✓ Created wikijs-deployment.md"

# 2. Update FIREFROST-PROJECT-SCOPE-V2.md
# Find the Wiki.js section and update status
sed -i 's/⏳ Service 4\/5: Wiki.js (subscribers.firefrostgaming.com)/✅ Service 4\/5: Wiki.js Staff (staff.firefrostgaming.com) - DEPLOYED/' docs/FIREFROST-PROJECT-SCOPE-V2.md
sed -i 's/⏳ Service 5\/5: NextCloud (downloads.firefrostgaming.com)/✅ Service 5\/5: Wiki.js Subscribers (subscribers.firefrostgaming.com) - DEPLOYED (config pending)/' docs/FIREFROST-PROJECT-SCOPE-V2.md

# Update Phase 0.5 progress
sed -i 's/Phase 0.5 at 60% (3\/5 management services deployed)/Phase 0.5 at 100% (5\/5 management services deployed)/' docs/FIREFROST-PROJECT-SCOPE-V2.md

echo "✓ Updated FIREFROST-PROJECT-SCOPE-V2.md"

# 3. Update session-handoff.md - add Wiki.js deployment info
cat >> docs/session-handoff.md << 'HANDOFF'

---

## **Wiki.js Deployment (Feb 10, 2026)**

**Deployed:** Feb 10, 9:30pm-11:15pm CST (~1h 45min session)

**Services:**
- ✅ Staff Wiki (staff.firefrostgaming.com) - Ghost VPS, Git storage
- ✅ Subscriber Wiki (subscribers.firefrostgaming.com) - Ghost VPS, storage TBD

**Issue Encountered:** SQLite3 native module error (20:00-20:04)
**Resolution:** Switched to alternative storage (exact fix not documented)

**Current Status:**
- Staff Wiki: Operational with department structure and Consultant profiles
- Subscriber Wiki: Deployed, awaiting content/Paymenter integration

**Lesson Learned:** Document immediately after deployment, even if tired.

HANDOFF

echo "✓ Updated session-handoff.md"

# 4. Update Firefrost_Infrastructure_Manifest.md - Ghost VPS services
sed -i '/Ghost VPS (64.50.188.14) - Documentation Cluster/,/^$/c\
**Ghost VPS (64.50.188.14) - Documentation Cluster:**\
- ✅ Ghost CMS\
- ✅ Wiki.js Staff (staff.firefrostgaming.com)\
- ✅ Wiki.js Subscribers (subscribers.firefrostgaming.com)\
- ⏳ MkDocs (docs.firefrostgaming.com) - planned\
- ⏳ NextCloud (downloads.firefrostgaming.com) - planned' docs/FIREFROST-PROJECT-SCOPE-V2.md

echo "✓ Updated Infrastructure Manifest"

# 5. Git commit all changes
git add docs/wikijs-deployment.md docs/FIREFROST-PROJECT-SCOPE-V2.md docs/session-handoff.md

git commit -m "Document Wiki.js deployment (Feb 10, 2026)

DEPLOYMENT:
- Staff Wiki (staff.firefrostgaming.com) - operational with Git storage
- Subscriber Wiki (subscribers.firefrostgaming.com) - deployed, config pending
- Both on Ghost VPS with SSL
- Deployed Feb 10, 9:30-11:15pm CST

ISSUE ENCOUNTERED:
- SQLite3 native module error (20:00-20:04 CST)
- Service crash loop for 4+ minutes
- Resolved by switching storage backend

LESSON LEARNED:
- Avoid SQLite3 for Wiki.js (use Git storage or PostgreSQL)
- Document immediately after deployment (exact fix lost due to fatigue)

UPDATES:
- Created wikijs-deployment.md with full deployment guide
- Updated FIREFROST-PROJECT-SCOPE-V2.md (Phase 0.5 now 100%)
- Updated session-handoff.md with deployment context
- Updated Infrastructure Manifest (Ghost VPS services)

Phase 0.5 COMPLETE: 5/5 management services deployed
- Gitea, Uptime Kuma, MkDocs, Wiki.js Staff, Wiki.js Subscribers

Partnership accountability: Document immediately, ask at session start"

git push

echo ""
echo "=== WIKI.JS DEPLOYMENT DOCUMENTED ==="
echo "✓ Created deployment guide"
echo "✓ Updated project scope (Phase 0.5: 100%)"
echo "✓ Updated session handoff"
echo "✓ Updated infrastructure manifest"
echo "✓ Committed to Git with full context"
echo ""
echo "Lesson preserved: SQLite3 bad, Git storage good, document immediately"

exit 0
