#!/bin/bash
set -e

# Sync wiki documentation from docs/wiki to wiki repository

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
WIKI_DIR="$PROJECT_ROOT/wiki"
DOCS_WIKI_DIR="$PROJECT_ROOT/docs/wiki"

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

echo -e "${YELLOW}Syncing wiki documentation...${NC}"

# Check if wiki directory exists
if [ ! -d "$WIKI_DIR" ]; then
    echo -e "${RED}Error: Wiki directory not found at $WIKI_DIR${NC}"
    echo "Run: git clone https://github.com/ndave92/claude-code-status-line.wiki.git wiki"
    exit 1
fi

# Copy all markdown files from docs/wiki to wiki
echo "Copying files from docs/wiki/ to wiki/..."
cp "$DOCS_WIKI_DIR"/*.md "$WIKI_DIR/"

# Check for changes
cd "$WIKI_DIR"
if [ -z "$(git status --porcelain)" ]; then
    echo -e "${GREEN}✓ No changes to sync${NC}"
    exit 0
fi

# Show status
echo -e "\n${YELLOW}Changed files:${NC}"
git status --short

# If commit message provided as argument, commit and push
if [ -n "$1" ]; then
    echo -e "\n${YELLOW}Committing changes...${NC}"
    git add *.md
    git commit -m "$1"

    echo -e "\n${YELLOW}Pushing to wiki repository...${NC}"
    git push origin master

    echo -e "\n${GREEN}✓ Wiki updated successfully!${NC}"
else
    echo -e "\n${YELLOW}Changes staged but not committed.${NC}"
    echo "To commit and push, run:"
    echo "  cd wiki && git add *.md && git commit -m 'Your message' && git push"
    echo ""
    echo "Or run this script with a commit message:"
    echo "  ./scripts/sync-wiki.sh 'Your commit message'"
fi
