#!/bin/bash
#
# Archive Old Status Files
# Moves old status and completion files to archive directories.
#
# Usage: ./scripts/cleanup-archive-old-status.sh [options]
# Options:
#   --dry-run    Show what would be moved without actually moving
#   --help       Show this help message
#

set -euo pipefail

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

DRY_RUN=false

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        --help)
            echo "Usage: $0 [options]"
            echo "Options:"
            echo "  --dry-run    Show what would be moved without actually moving"
            echo "  --help       Show this help message"
            exit 0
            ;;
        *)
            echo -e "${RED}Unknown option: $1${NC}"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

FILES_MOVED=0

# Function to move file to archive
move_to_archive() {
    local file="$1"
    local archive_dir="$2"
    local reason="$3"
    
    if [[ ! -f "$file" ]]; then
        echo -e "${YELLOW}  ⚠ Skipping (not found): $file${NC}"
        return
    fi
    
    local filename=$(basename "$file")
    local dest="$archive_dir/$filename"
    
    if [[ "$DRY_RUN" == true ]]; then
        echo -e "${YELLOW}  [DRY RUN] Would move: $file${NC}"
        echo -e "    To: $dest"
        echo -e "    Reason: $reason"
        ((FILES_MOVED++))
        return
    fi
    
    mkdir -p "$archive_dir"
    
    # Check if destination already exists
    if [[ -f "$dest" ]]; then
        echo -e "${YELLOW}  ⚠ Destination exists, skipping: $file${NC}"
        return
    fi
    
    mv "$file" "$dest"
    echo -e "${GREEN}  ✓ Moved: $file${NC}"
    echo -e "    To: $dest"
    ((FILES_MOVED++))
}

echo "=========================================="
echo "Archive Old Status Files"
echo "=========================================="
echo ""

if [[ "$DRY_RUN" == true ]]; then
    echo -e "${YELLOW}DRY RUN MODE - No files will be moved${NC}"
    echo ""
fi

echo "Archiving old status files..."
echo ""

# Files in docs/proxmox/status/ to archive
proxmox_status_files=(
    "docs/proxmox/status/COMPLETE_STATUS.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/COMPLETE_STATUS_FINAL.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/COMPLETE_STATUS_REPORT.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/COMPLETE_SUMMARY.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/COMPLETION_SUMMARY.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/FINAL_STATUS.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/FINAL_STATUS_UPDATE.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/NEXT_STEPS_COMPLETED.md:docs/proxmox/archive:Old status file"
    "docs/proxmox/status/TASK_COMPLETION_SUMMARY.md:docs/proxmox/archive:Old status file"
)

for entry in "${proxmox_status_files[@]}"; do
    IFS=':' read -r file archive_dir reason <<< "$entry"
    move_to_archive "$file" "$archive_dir" "$reason"
done

# Files in docs/status/implementation/ to archive
status_impl_files=(
    "docs/status/implementation/ALL_TASKS_COMPLETE.md:docs/archive/status:Old status file"
    "docs/status/implementation/IMPLEMENTATION_COMPLETE.md:docs/archive/status:Old status file"
    "docs/status/implementation/NEXT_STEPS_COMPLETE.md:docs/archive/status:Old status file"
    "docs/status/implementation/NEXT_STEPS_FINAL_STATUS.md:docs/archive/status:Old status file"
)

for entry in "${status_impl_files[@]}"; do
    IFS=':' read -r file archive_dir reason <<< "$entry"
    move_to_archive "$file" "$archive_dir" "$reason"
done

# Files in docs/status/ to archive
status_files=(
    "docs/status/NEXT_STEPS_COMPLETION.md:docs/archive/status:Old status file"
)

for entry in "${status_files[@]}"; do
    IFS=':' read -r file archive_dir reason <<< "$entry"
    move_to_archive "$file" "$archive_dir" "$reason"
done

echo ""
echo "=========================================="
echo "Summary"
echo "=========================================="
echo -e "${GREEN}Files moved: $FILES_MOVED${NC}"
echo ""

if [[ "$DRY_RUN" == true ]]; then
    echo -e "${YELLOW}This was a dry run. Run without --dry-run to actually move files.${NC}"
fi

echo "Done!"

