#!/bin/bash

# Comprehensive Gitea Wiki Upload Script
# Uploads ALL homelab documentation to Gitea wiki via API

set -e

# Configuration
GITEA_TOKEN=REDACTED_TOKEN
GITEA_URL="https://git.vish.gg"
REPO_OWNER="Vish"
REPO_NAME="homelab"
BASE_URL="$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/wiki"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color

echo -e "${BLUE}🚀 Starting COMPREHENSIVE Gitea Wiki documentation upload...${NC}"
echo -e "${PURPLE}📊 Scanning for all documentation files...${NC}"

# Find all markdown files
total_files=$(find docs/ -name "*.md" -type f | wc -l)
echo -e "${BLUE}📚 Found $total_files markdown files to upload${NC}"
echo ""

# Function to create or update wiki page
create_wiki_page() {
    local title="$1"
    local file_path="$2"
    local message="$3"

    if [[ ! -f "$file_path" ]]; then
        echo -e "${RED}❌ File not found: $file_path${NC}"
        return 1
    fi

    echo -e "${YELLOW}📄 Processing: $file_path → $title${NC}"

    # Read file content and escape for JSON
    local content
    content=$(cat "$file_path" | jq -Rs .)

    # Create JSON payload
    local json_payload
    json_payload=$(jq -n \
        --arg title "$title" \
        --argjson content "$content" \
        --arg message "$message" \
        '{
            title: $title,
            content_base64: ($content | @base64),
            message: $message
        }')

    # Try to create new page first
    local response
    response=$(curl -s -w "%{http_code}" -o /tmp/wiki_response.json \
        -X POST \
        -H "Authorization: token $GITEA_TOKEN" \
        -H "Content-Type: application/json" \
        -d "$json_payload" \
        "$BASE_URL/new")

    local http_code="${response: -3}"

    if [[ "$http_code" == "201" ]]; then
        echo -e "${GREEN}✅ Created: $title${NC}"
        return 0
    elif [[ "$http_code" == "409" ]] || [[ "$http_code" == "400" ]]; then
        # Page exists, try to update it
        echo -e "${YELLOW}📝 Page exists, updating: $title${NC}"

        response=$(curl -s -w "%{http_code}" -o /tmp/wiki_response.json \
            -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            -d "$json_payload" \
            "$BASE_URL/$title")

        http_code="${response: -3}"

        if [[ "$http_code" == "200" ]]; then
            echo -e "${GREEN}✅ Updated: $title${NC}"
            return 0
        else
            echo -e "${RED}❌ Failed to update $title (HTTP $http_code)${NC}"
            return 1
        fi
    else
        echo -e "${RED}❌ Failed to create $title (HTTP $http_code)${NC}"
        return 1
    fi
}

# Function to convert file path to wiki title
path_to_wiki_title() {
    local file_path="$1"

    # Remove docs/ prefix and .md suffix
    local title="${file_path#docs/}"
    title="${title%.md}"

    # Replace directory separators with dashes and sanitize
    title=$(echo "$title" | sed 's|/|-|g' | sed 's/[^a-zA-Z0-9_-]/_/g' | sed 's/__*/_/g' | sed 's/^_\|_$//g')

    # Capitalize first letter of each word separated by dash
    title=$(echo "$title" | sed 's/-/ /g' | sed 's/\b\w/\U&/g' | sed 's/ /-/g')

    echo "$title"
}

# Success and failure counters
success_count=0
total_count=0
failed_files=()

echo -e "${BLUE}📋 Creating comprehensive homelab wiki index...${NC}"

# Create main wiki index page with complete navigation
cat > /tmp/comprehensive_wiki_index.md << 'EOF'
# Homelab Documentation Wiki - Complete Index

*This wiki contains ALL documentation from the homelab Git repository*
*Last Updated: $(date)*

## 🎯 Quick Navigation

### 📖 Core Documentation
- [Repository README](README) - Complete repository overview
- [Documentation Index](INDEX) - Master navigation guide
- [Operational Status](Operational-Status) - Current system status

### 🔧 Administration & Operations
- [GitOps Comprehensive Guide](Admin-GITOPS-COMPREHENSIVE-GUIDE) - Complete deployment procedures ⭐
- [DokuWiki Integration](Admin-DOKUWIKI-INTEGRATION) - Documentation mirroring setup
- [Gitea Wiki Integration](Admin-GITEA-WIKI-INTEGRATION) - Native wiki integration
- [Deployment Workflow](Admin-DEPLOYMENT-WORKFLOW) - Deployment procedures
- [Operational Notes](Admin-OPERATIONAL-NOTES) - Administrative notes
- [Monitoring Setup](Admin-Monitoring-Setup) - Monitoring configuration
- [Backup Strategies](Admin-Backup-Strategies) - Backup procedures
- [Security](Admin-Security) - Security configuration
- [Maintenance](Admin-Maintenance) - Maintenance procedures

### 🏗️ Infrastructure
- [Infrastructure Health Report](Infrastructure-INFRASTRUCTURE-HEALTH-REPORT) - System health status
- [Infrastructure Overview](Infrastructure-INFRASTRUCTURE-OVERVIEW) - Complete infrastructure guide
- [Networking](Infrastructure-Networking) - Network configuration
- [Storage](Infrastructure-Storage) - Storage configuration
- [SSH Access Guide](Infrastructure-SSH-ACCESS-GUIDE) - SSH access procedures
- [User Access Guide](Infrastructure-USER-ACCESS-GUIDE) - User access management
- [Tailscale Setup](Infrastructure-Tailscale-Setup-Guide) - VPN configuration
- [Cloudflare Tunnels](Infrastructure-Cloudflare-Tunnels) - Tunnel configuration

### 🚀 Getting Started
- [Beginner Quickstart](Getting-Started-BEGINNER-QUICKSTART) - Quick start guide
- [What Is Homelab](Getting-Started-What-Is-Homelab) - Introduction to homelabs
- [Prerequisites](Getting-Started-Prerequisites) - Requirements and setup
- [Architecture](Getting-Started-Architecture) - System architecture overview
- [Shopping Guide](Getting-Started-Shopping-Guide) - Hardware recommendations

### 🔧 Services
- [Service Index](Services-Index) - All available services
- [Dashboard Setup](Services-DASHBOARD-SETUP) - Dashboard configuration
- [Homarr Setup](Services-HOMARR-SETUP) - Homarr dashboard setup
- [Verified Service Inventory](Services-VERIFIED-SERVICE-INVENTORY) - Service catalog
- [ARR Suite Enhancements](Services-ARR-SUITE-ENHANCEMENTS-FEB2025) - Media stack improvements
- [Authentik SSO](Services-Authentik-Sso) - Single sign-on setup

### 📚 Runbooks & Procedures
- [Add New Service](Runbooks-Add-New-Service) - Service deployment runbook
- [Add New User](Runbooks-Add-New-User) - User management procedures
- [Certificate Renewal](Runbooks-Certificate-Renewal) - SSL certificate management
- [Service Migration](Runbooks-Service-Migration) - Service migration procedures
- [Disk Full Procedure](Runbooks-Disk-Full-Procedure) - Storage management

### 🛠️ Troubleshooting
- [Common Issues](Troubleshooting-Common-Issues) - Frequently encountered problems
- [Emergency Access Guide](Troubleshooting-EMERGENCY-ACCESS-GUIDE) - Emergency procedures
- [Disaster Recovery](Troubleshooting-Disaster-Recovery) - Recovery procedures
- [Recovery Guide](Troubleshooting-RECOVERY-GUIDE) - System recovery
- [Container Diagnosis](Troubleshooting-CONTAINER-DIAGNOSIS-REPORT) - Container troubleshooting
- [Watchtower Emergency Procedures](Troubleshooting-WATCHTOWER-EMERGENCY-PROCEDURES) - Watchtower issues

### 🔒 Security
- [Server Hardening](Security-SERVER-HARDENING) - Security hardening guide

### 🏗️ Advanced Topics
- [Homelab Maturity Roadmap](Advanced-HOMELAB-MATURITY-ROADMAP) - Growth planning
- [Repository Optimization](Advanced-REPOSITORY-OPTIMIZATION-GUIDE) - Optimization guide
- [Terraform Implementation](Advanced-TERRAFORM-IMPLEMENTATION-GUIDE) - Infrastructure as code
- [Stack Comparison Report](Advanced-STACK-COMPARISON-REPORT) - Technology comparisons

### 📊 Diagrams & Architecture
- [Network Topology](Diagrams-Network-Topology) - Network diagrams
- [Service Architecture](Diagrams-Service-Architecture) - Service architecture
- [Storage Topology](Diagrams-Storage-Topology) - Storage layout
- [10GbE Backbone](Diagrams-10gbe-Backbone) - High-speed networking

### 🖥️ Hardware
- [Hardware README](Hardware-README) - Hardware documentation
- [Network Equipment](Hardware-Network-Equipment) - Network hardware
- [Atlantis Storage](Hardware-Atlantis-Storage) - Storage hardware

## 🌐 Access Points

- **Git Repository**: https://git.vish.gg/Vish/homelab
- **Gitea Wiki**: https://git.vish.gg/Vish/homelab/wiki
- **DokuWiki Mirror**: http://atlantis.vish.local:8399/doku.php?id=homelab:start

## 📊 Repository Status

- **GitOps Status**: ✅ 18 active stacks, 50+ containers
- **Servers**: 5 active (Atlantis, Calypso, Gaming VPS, Homelab VM, Concord NUC)
- **Services**: 100+ containerized services
- **Documentation Files**: 291+ markdown files
- **Wiki Pages**: Complete documentation mirror

---

**Source Repository**: https://git.vish.gg/Vish/homelab
**Maintainer**: Homelab Administrator
**Documentation Coverage**: Complete (all docs/ files mirrored)
EOF

total_count=$((total_count + 1))
if create_wiki_page "Home" "/tmp/comprehensive_wiki_index.md" "Updated comprehensive homelab wiki index with complete navigation"; then
    success_count=$((success_count + 1))
fi

echo ""
echo -e "${BLUE}📚 Uploading ALL documentation files...${NC}"
echo -e "${PURPLE}This may take a while - processing $total_files files...${NC}"
echo ""

# Process all markdown files in docs/
while IFS= read -r -d '' file; do
    # Skip hidden files and directories
    if [[ "$file" == *"/."* ]]; then
        continue
    fi

    # Convert file path to wiki title
    wiki_title=$(path_to_wiki_title "$file")

    # Skip if title is empty
    if [[ -z "$wiki_title" ]]; then
        echo -e "${RED}⚠️  Skipping file with empty title: $file${NC}"
        continue
    fi

    echo ""
    echo -e "${PURPLE}📄 [$((total_count + 1))/$((total_files + 1))] Processing: $file${NC}"
    echo -e "${YELLOW}   → Wiki Title: $wiki_title${NC}"

    total_count=$((total_count + 1))

    if create_wiki_page "$wiki_title" "$file" "Updated $wiki_title from repository ($file)"; then
        success_count=$((success_count + 1))
    else
        failed_files+=("$file")
    fi

    # Add small delay to avoid overwhelming the API
    sleep 0.1

done < <(find docs/ -name "*.md" -type f -print0 | sort -z)

# Also upload root-level documentation files
echo ""
echo -e "${BLUE}📚 Uploading root-level documentation files...${NC}"

root_docs=(
    "README.md"
    "OPERATIONAL_STATUS.md"
    "MONITORING_ARCHITECTURE.md"
    "GITOPS_DEPLOYMENT_GUIDE.md"
    "DOCUMENTATION_AUDIT_REPORT.md"
    "CHANGELOG.md"
    "DEVELOPMENT.md"
    "DEPLOYMENT_DOCUMENTATION.md"
    "SECURITY_HARDENING_SUMMARY.md"
)

for file in "${root_docs[@]}"; do
    if [[ -f "$file" ]]; then
        wiki_title=$(basename "$file" .md | sed 's/[^a-zA-Z0-9_-]/_/g' | sed 's/__*/_/g' | sed 's/^_\|_$//g')
        wiki_title=$(echo "$wiki_title" | sed 's/_/ /g' | sed 's/\b\w/\U&/g' | sed 's/ /-/g')

        echo ""
        echo -e "${PURPLE}📄 [$((total_count + 1))/$((total_files + ${#root_docs[@]} + 1))] Processing root file: $file${NC}"
        echo -e "${YELLOW}   → Wiki Title: $wiki_title${NC}"

        total_count=$((total_count + 1))

        if create_wiki_page "$wiki_title" "$file" "Updated $wiki_title from repository root"; then
            success_count=$((success_count + 1))
        else
            failed_files+=("$file")
        fi

        sleep 0.1
    fi
done

echo ""
echo -e "${BLUE}🎯 COMPREHENSIVE Upload Summary:${NC}"
echo -e "${GREEN}✅ Successful: $success_count/$total_count${NC}"
echo -e "${RED}❌ Failed: $((total_count - success_count))/$total_count${NC}"

if [[ ${#failed_files[@]} -gt 0 ]]; then
    echo ""
    echo -e "${RED}❌ Failed files:${NC}"
    for file in "${failed_files[@]}"; do
        echo -e "${RED}   - $file${NC}"
    done
fi

echo ""
echo -e "${BLUE}🌐 Complete Gitea Wiki available at:${NC}"
echo -e "   ${BLUE}https://git.vish.gg/$REPO_OWNER/$REPO_NAME/wiki${NC}"
echo -e "   ${BLUE}https://git.vish.gg/$REPO_OWNER/$REPO_NAME/wiki/Home${NC}"

# Get final page count
final_page_count=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$BASE_URL/pages" | jq '. | length' 2>/dev/null || echo "unknown")
echo ""
echo -e "${GREEN}📊 Final Wiki Statistics:${NC}"
echo -e "${GREEN}   Total Wiki Pages: $final_page_count${NC}"
echo -e "${GREEN}   Documentation Files Processed: $total_files${NC}"
echo -e "${GREEN}   Success Rate: $(( success_count * 100 / total_count ))%${NC}"

if [[ $success_count -eq $total_count ]]; then
    echo ""
    echo -e "${GREEN}✅ COMPREHENSIVE Gitea Wiki upload completed successfully!${NC}"
    echo -e "${GREEN}🎉 ALL homelab documentation is now available in the wiki!${NC}"
    exit 0
else
    echo ""
    echo -e "${YELLOW}⚠️  Gitea Wiki upload completed with some failures.${NC}"
    echo -e "${YELLOW}📊 $success_count out of $total_count files uploaded successfully.${NC}"
    exit 1
fi
