#!/bin/bash

# Agent Context Protocol (ACP) Update Script
# This script updates AGENT.md, template files, and utility scripts from the repository

set -e

# Colors for output using tput (more reliable than ANSI codes)
if command -v tput >/dev/null 2>&1 && [ -t 1 ]; then
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    YELLOW=$(tput setaf 3)
    BLUE=$(tput setaf 4)
    BOLD=$(tput bold)
    NC=$(tput sgr0)
else
    RED=''
    GREEN=''
    YELLOW=''
    BLUE=''
    BOLD=''
    NC=''
fi

# Repository details
REPO_URL="https://github.com/prmichaelsen/agent-context-protocol.git"
BRANCH="mainline"

echo "${BLUE}Agent Context Protocol (ACP) Updater${NC}"
echo "======================================"
echo ""

# Check if AGENT.md exists
if [ ! -f "AGENT.md" ]; then
    echo "${RED}Error: AGENT.md not found in current directory${NC}"
    echo "This script should be run from your project root where AGENT.md is located."
    exit 1
fi

# Create temporary directory
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT

echo "Fetching latest ACP files..."
if ! git clone --depth 1 --branch "$BRANCH" "$REPO_URL" "$TEMP_DIR" &>/dev/null; then
    echo "${RED}Error: Failed to fetch repository${NC}"
    echo "Please check your internet connection and try again."
    exit 1
fi

echo "${GREEN}✓${NC} Latest files fetched"
echo ""

# Ensure reports directory exists and is ignored
mkdir -p "agent/reports"
if [ ! -f "agent/.gitignore" ]; then
    echo "Creating agent/.gitignore..."
    cat > "agent/.gitignore" << 'EOF'
# Agent Context Protocol - Local Files
# These files are generated locally and should not be committed

# Reports directory - generated by @acp.report command
reports/
EOF
    echo "${GREEN}✓${NC} Created agent/.gitignore"
fi

echo "Updating ACP files..."

# Update template files (only .template.md files from these directories)
find "$TEMP_DIR/agent/design" -maxdepth 1 -name "*.template.md" -exec cp {} "agent/design/" \;
find "$TEMP_DIR/agent/milestones" -maxdepth 1 -name "*.template.md" -exec cp {} "agent/milestones/" \;
find "$TEMP_DIR/agent/patterns" -maxdepth 1 -name "*.template.md" -exec cp {} "agent/patterns/" \;
find "$TEMP_DIR/agent/tasks" -maxdepth 1 -name "*.template.md" -exec cp {} "agent/tasks/" \;

# Update command template
mkdir -p "agent/commands"
cp "$TEMP_DIR/agent/commands/command.template.md" "agent/commands/"

# Update all command files (flat structure with dot notation)
# Copies files like acp.init.md, acp.status.md, deploy.production.md, etc.
if [ -d "$TEMP_DIR/agent/commands" ]; then
    find "$TEMP_DIR/agent/commands" -maxdepth 1 -name "*.*.md" -exec cp {} "agent/commands/" \;
fi

# Update progress template
cp "$TEMP_DIR/agent/progress.template.yaml" "agent/"

# Update AGENT.md
cp "$TEMP_DIR/AGENT.md" "."

# Update scripts
cp "$TEMP_DIR/agent/scripts/update.sh" "agent/scripts/"
cp "$TEMP_DIR/agent/scripts/check-for-updates.sh" "agent/scripts/"
cp "$TEMP_DIR/agent/scripts/uninstall.sh" "agent/scripts/"
cp "$TEMP_DIR/agent/scripts/version.sh" "agent/scripts/"
cp "$TEMP_DIR/agent/scripts/install.sh" "agent/scripts/"
chmod +x agent/scripts/*.sh

echo "${GREEN}✓${NC} All files updated"
echo ""
echo "${GREEN}Update complete!${NC}"
echo ""
echo "${BLUE}What was updated:${NC}"
echo "  ✓ AGENT.md (methodology documentation)"
echo "  ✓ Template files (design, milestone, task, pattern)"
echo "  ✓ Command files (all ACP commands)"
echo "  ✓ Utility scripts (update, check-for-updates, version, etc.)"
echo ""
echo "${BLUE}Next steps:${NC}"
echo "1. Review changes: git diff"
echo "2. See what changed: git status"
echo "3. Revert if needed: git checkout <file>"
echo ""
echo "For detailed changelog:"
echo "  https://github.com/prmichaelsen/agent-context-protocol/blob/mainline/CHANGELOG.md"
echo ""
echo "${BLUE}ACP Commands Available:${NC}"
echo ""
echo "  ${GREEN}@acp.init${NC}                    - Initialize agent context (run after update!)"
echo "  ${GREEN}@acp.proceed${NC}                 - Continue with next task"
echo "  ${GREEN}@acp.status${NC}                  - Display project status"
echo "  ${GREEN}@acp.update${NC}                  - Update progress tracking"
echo "  ${GREEN}@acp.sync${NC}                    - Sync documentation with code"
echo "  ${GREEN}@acp.validate${NC}                - Validate ACP documents"
echo "  ${GREEN}@acp.report${NC}                  - Generate project report"
echo "  ${GREEN}@acp.version-check${NC}           - Show current ACP version"
echo "  ${GREEN}@acp.version-check-for-updates${NC} - Check for ACP updates"
echo "  ${GREEN}@acp.version-update${NC}          - Update ACP to latest version"
echo "  ${GREEN}@acp.package-install${NC}         - Install third-party command packages"
echo ""
echo "${BLUE}Git Commands Available:${NC}"
echo ""
echo "  ${GREEN}@git.init${NC}                    - Initialize git repository with smart .gitignore"
echo "  ${GREEN}@git.commit${NC}                  - Intelligent version-aware git commit"
echo ""
echo "${BLUE}For AI agents:${NC}"
echo "Type '${GREEN}@acp.init${NC}' to reload context with updated files."
echo ""
