#!/bin/bash

################################################################################
# AI-Impress Complete Server Report Generator - Master Script
# Version: 5.0.0 - Modular Architecture
# Generates comprehensive server report from individual modules
################################################################################

set -euo pipefail

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

REPORT_DIR="/opt/infrastructure-docs/reports"
MODULE_DIR="/opt/infrastructure-docs/scripts/modules"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
REPORT_FILE="$REPORT_DIR/complete-server-report-$TIMESTAMP.md"

mkdir -p "$REPORT_DIR"

log() { echo -e "${CYAN}[$(date +%H:%M:%S)]${NC} $1"; }
success() { echo -e "${GREEN}✅ $1${NC}"; }
error() { echo -e "${RED}❌ $1${NC}"; exit 1; }

# Slack notification function
send_slack_summary() {
    local report_file="$1"

    # Get Vault token and Slack webhook
    export VAULT_ADDR="http://127.0.0.1:8200"
    export VAULT_TOKEN=$(cat /opt/00-infrastructure/vault/.vault-token 2>/dev/null || echo "")
    local slack_webhook=$(vault kv get -field=slack_webhook secret/monitoring 2>/dev/null || echo "")

    if [[ -z "$slack_webhook" ]]; then
        log "Slack webhook not configured, skipping notification"
        return 0
    fi

    # Extract key metrics from report
    local containers_running=$(grep "Docker Containers" "$report_file" | grep -oP '\d+(?= running)' | head -1)
    local containers_total=$(grep "Docker Containers" "$report_file" | grep -oP 'running / \d+' | grep -oP '\d+' | head -1)
    local unhealthy=$(grep "Unhealthy Containers" "$report_file" | grep -oP '\d+' | head -1)
    local memory=$(grep "Memory" "$report_file" | grep -oP '\| \*\*Memory\*\* \| \K[^|]+' | xargs)
    local disk=$(grep "Disk (/)" "$report_file" | grep -oP '\| \*\*Disk \(/\)\*\* \| \K[^|]+' | xargs)
    local disk_percent=$(echo "$disk" | grep -oP '\d+(?=\%)')
    local uptime=$(grep "Uptime" "$report_file" | grep -oP '\| \*\*Uptime\*\* \| \K[^|]+' | xargs)

    # Count websites status
    local websites_ok=$(grep -c "✅ OK" "$report_file" 2>/dev/null || echo "0")
    local websites_down=$(grep -c "❌" "$report_file" 2>/dev/null || echo "0")

    # Detect problems and create recommendations
    local problems=""
    local recommendations=""
    local color="good"
    local status_emoji="✅"

    if [[ "$unhealthy" -gt 0 ]]; then
        problems="${problems}• $unhealthy unhealthy container(s) detected\n"
        recommendations="${recommendations}• Check logs: \`docker logs <container>\`\n• Restart if needed: \`docker restart <container>\`\n"
        color="danger"
        status_emoji="🚨"
    fi

    if [[ "$websites_down" -gt 0 ]]; then
        problems="${problems}• $websites_down website(s) are down\n"
        recommendations="${recommendations}• Check Traefik: \`docker logs traefik --tail 50\`\n• Verify DNS: \`nslookup <domain>\`\n• Check SSL certs: \`/opt/05-backups/scripts/admin.sh status websites\`\n"
        if [[ "$color" != "danger" ]]; then
            color="warning"
            status_emoji="⚠️"
        fi
    fi

    if [[ -n "$disk_percent" ]] && [[ "$disk_percent" -gt 80 ]]; then
        problems="${problems}• Disk usage is high: ${disk_percent}%\n"
        recommendations="${recommendations}• Clean up old logs: \`/opt/05-backups/scripts/admin.sh cleanup logs\`\n• Clean up Docker: \`/opt/05-backups/scripts/admin.sh cleanup docker\`\n• Check disk: \`/opt/05-backups/scripts/admin.sh status disk\`\n"
        if [[ "$color" == "good" ]]; then
            color="warning"
            status_emoji="⚠️"
        fi
    fi

    # Create fields array
    local fields='[
        {
            "title": "System Status",
            "value": "🐳 Containers: '"$containers_running/$containers_total"' running\n💾 Memory: '"$memory"'\n💿 Disk: '"$disk"'\n⏱️ Uptime: '"$uptime"'",
            "short": true
        },
        {
            "title": "Health Check",
            "value": "🔴 Unhealthy: '"$unhealthy"' containers\n🌐 Websites: '"$websites_ok"' OK, '"$websites_down"' Down",
            "short": true
        }'

    # Add problems section if any
    if [[ -n "$problems" ]]; then
        fields+=',
        {
            "title": "⚠️ Detected Problems",
            "value": "'"${problems}"'",
            "short": false
        },
        {
            "title": "🔧 Recommended Actions",
            "value": "'"${recommendations}"'SSH: \`ssh ubuntu@51.89.231.46\`\nAdmin tool: \`/opt/05-backups/scripts/admin.sh help\`",
            "short": false
        }'
    fi

    fields+=',
        {
            "title": "Full Report",
            "value": "📄 Generated: \`'"$(basename $report_file)"'\`\n📍 Location: \`/opt/infrastructure-docs/reports/\`\n📤 Upload to Wiki: \`/opt/05-backups/scripts/upload-to-outline.sh latest-report\`",
            "short": false
        }
    ]'

    # Create Slack message
    local payload='{
        "attachments": [
            {
                "color": "'"$color"'",
                "title": "'"$status_emoji"' Daily Server Report - '"$(date '+%Y-%m-%d %H:%M')"'",
                "fields": '"$fields"',
                "footer": "AI-Impress Infrastructure Monitor",
                "footer_icon": "https://wiki.ai-impress.com/favicon.png",
                "ts": '"$(date +%s)"'
            }
        ]
    }'

    # Send to Slack
    local response=$(curl -s -X POST "$slack_webhook" \
        -H 'Content-Type: application/json' \
        -d "$payload")

    if [[ "$response" == "ok" ]]; then
        success "Slack summary sent with $(echo -e "$problems" | grep -c "•" || echo "0") problems detected"
    else
        log "Slack notification may have failed: $response"
    fi
}

log "╔════════════════════════════════════════════════════════════╗"
log "║  AI-Impress Complete Server Report Generator v5.0         ║"
log "║  Modular Architecture - Full System Report                ║"
log "╚════════════════════════════════════════════════════════════╝"
log ""

# Check modules exist
if [[ ! -d "$MODULE_DIR" ]]; then
    error "Module directory not found: $MODULE_DIR"
fi

MODULES=(
    "generate-summary.sh"
    "generate-services.sh"
    "generate-websites.sh"
    "generate-databases.sh"
    "generate-storage.sh"
    "generate-networks.sh"
    "generate-guides.sh"
)

log "Checking modules..."
for module in "${MODULES[@]}"; do
    if [[ ! -f "$MODULE_DIR/$module" ]]; then
        error "Module missing: $module"
    fi
    chmod +x "$MODULE_DIR/$module"
done
success "All modules found"

log ""
log "Generating report: $REPORT_FILE"
log ""

################################################################################
# HEADER
################################################################################

cat > "$REPORT_FILE" << EOFHEADER
# 📋 AI-Impress Complete Server Administration Report

> **Generated:** $(date '+%Y-%m-%d %H:%M:%S')  
> **Server:** ai-impress-prod  
> **IP:** 51.89.231.46  
> **Purpose:** Full system overview for administrators

---

## 📑 TABLE OF CONTENTS

1. [Quick Summary](#quick-summary) - System overview at a glance
2. [System Health](#system-health) - Critical services & websites status
3. [All Services](#all-services) - Docker containers inventory
4. [Websites & Access](#websites--access) - URLs and authentication
5. [Databases](#databases) - Database management & backups
6. [Storage & Backups](#storage--backups) - Disk usage and backup status
7. [Networks](#networks) - Docker networking configuration
8. [Secrets & Passwords](#secrets--passwords) - Vault access & structure
9. [How To Guides](#how-to-guides) - Common administrative tasks
10. [Troubleshooting](#troubleshooting) - Fix common problems
11. [System Architecture](#system-architecture) - System design & dependencies

---

EOFHEADER

################################################################################
# GENERATE EACH MODULE
################################################################################

log "Module 1/7: Quick Summary + System Health..."
bash "$MODULE_DIR/generate-summary.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate summary module"
}
success "Summary + Health complete"

log "Module 2/7: All Services..."
bash "$MODULE_DIR/generate-services.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate services module"
}
success "Services complete"

log "Module 3/7: Websites & Access..."
bash "$MODULE_DIR/generate-websites.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate websites module"
}
success "Websites complete"

log "Module 4/7: Databases..."
bash "$MODULE_DIR/generate-databases.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate databases module"
}
success "Databases complete"

log "Module 5/7: Storage & Backups..."
bash "$MODULE_DIR/generate-storage.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate storage module"
}
success "Storage complete"

log "Module 6/7: Networks..."
bash "$MODULE_DIR/generate-networks.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate networks module"
}
success "Networks complete"

log "Module 7/7: Guides & Architecture..."
bash "$MODULE_DIR/generate-guides.sh" >> "$REPORT_FILE" 2>/dev/null || {
    error "Failed to generate guides module"
}
sed -i "s/TIMESTAMP_PLACEHOLDER/$(date '+%Y-%m-%d %H:%M:%S')/g" "$REPORT_FILE"
success "Guides complete"

################################################################################
# FINALIZE
################################################################################

log ""
log "Report generation complete!"
log ""

# Get report stats
REPORT_SIZE=$(du -h "$REPORT_FILE" | cut -f1)
LINE_COUNT=$(wc -l < "$REPORT_FILE")
WORD_COUNT=$(wc -w < "$REPORT_FILE")

cat << EOFSTATS

╔════════════════════════════════════════════════════════════╗
║                   📊 REPORT STATISTICS                     ║
╠════════════════════════════════════════════════════════════╣
║  File: $(basename "$REPORT_FILE")
║  Size: $REPORT_SIZE
║  Lines: $LINE_COUNT
║  Words: $WORD_COUNT
║  Location: $REPORT_FILE
╚════════════════════════════════════════════════════════════╝

✅ Full server report generated successfully!

📖 View report:
   cat $REPORT_FILE | less

📤 Upload to Outline:
   /opt/05-backups/scripts/upload-to-outline.sh latest-report

EOFSTATS

# Send Slack summary
log ""
log "Sending Slack summary..."
send_slack_summary "$REPORT_FILE"

exit 0
