#!/bin/bash
# Backup System Setup Script
# One-time setup for the automated backup system

set -e

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

echo -e "${BLUE}=== WorkRoot Backup System Setup ===${NC}\n"

# Step 1: Create directory structure
echo -e "${YELLOW}Step 1: Creating backup directories...${NC}"
mkdir -p "$PROJECT_ROOT/backups"/{daily,weekly,monthly,pre-deploy,incremental,safety}
echo -e "${GREEN}✓ Directories created${NC}\n"

# Step 2: Make scripts executable
echo -e "${YELLOW}Step 2: Making scripts executable...${NC}"
chmod +x "$PROJECT_ROOT/scripts/backup"/*.sh
echo -e "${GREEN}✓ Scripts are now executable${NC}\n"

# Step 3: Test backup
echo -e "${YELLOW}Step 3: Running test backup...${NC}"
cd "$PROJECT_ROOT"
bash scripts/backup/backup-config.sh
echo -e "${GREEN}✓ Test backup completed${NC}\n"

# Step 4: Verify backup
echo -e "${YELLOW}Step 4: Verifying backup system...${NC}"
bash scripts/backup/verify.sh
echo ""

# Step 5: Instructions
echo -e "${BLUE}=== Setup Complete! ===${NC}\n"

echo -e "${GREEN}Next Steps:${NC}"
echo "1. Test full backup:"
echo -e "   ${YELLOW}npm run backup:full${NC}\n"

echo "2. Set up automation (choose one):"
echo -e "   ${YELLOW}Linux/macOS:${NC} Edit cron jobs"
echo "   crontab -e"
echo "   (See scripts/backup/cron.example for template)"
echo ""
echo -e "   ${YELLOW}Windows:${NC} Run PowerShell as Administrator"
echo "   .\\scripts\\backup\\windows-tasks.ps1"
echo ""

echo "3. Verify automated backups work:"
echo -e "   ${YELLOW}npm run backup:verify${NC}\n"

echo -e "${GREEN}Available Commands:${NC}"
echo "  npm run backup:full      - Full backup"
echo "  npm run backup:content   - Content only"
echo "  npm run backup:config    - Config files only"
echo "  npm run backup:verify    - Check health"
echo "  npm run backup:list      - List backups"
echo "  npm run backup:restore   - Restore from backup"
echo "  npm run backup:cleanup   - Remove old backups"
echo ""

echo -e "${BLUE}Documentation:${NC}"
echo "  scripts/backup/README.md"
echo "  .agents/devops-engineer/BACKUP_STRATEGY.md"
echo ""

echo -e "${GREEN}Setup completed successfully!${NC}"
