#!/bin/bash
# CA Grow Ops Manager - Automated Deployment Script
# Run this after creating the Forgejo repository

set -e  # Exit on error

echo "🚀 CA Grow Ops Manager - Automated Deployment"
echo "=============================================="
echo ""

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

# Step 1: Add Git remote
echo -e "${BLUE}Step 1: Adding Forgejo remote...${NC}"
if git remote | grep -q "^origin$"; then
    echo "  Remote 'origin' already exists, removing..."
    git remote remove origin
fi
git remote add origin https://git.runfoo.run/malty/ca-grow-ops-manager.git
echo -e "${GREEN}✓ Remote added${NC}"
echo ""

# Step 2: Push to Forgejo
echo -e "${BLUE}Step 2: Pushing code to Forgejo...${NC}"
echo "  You may be prompted for Forgejo credentials"
git push -u origin main
echo -e "${GREEN}✓ Code pushed${NC}"
echo ""

# Step 3: Display SSH private key for Forgejo secret
echo -e "${BLUE}Step 3: SSH Private Key for Forgejo Secret${NC}"
echo -e "${YELLOW}IMPORTANT: Copy the following private key and add it to Forgejo secrets${NC}"
echo ""
echo "Go to: https://git.runfoo.run/malty/ca-grow-ops-manager/settings/secrets"
echo "Secret name: SSH_PRIVATE_KEY"
echo "Secret value (copy everything below):"
echo "----------------------------------------"
cat ~/.ssh/ca_grow_ops_deploy
echo "----------------------------------------"
echo ""
read -p "Press Enter after you've added the secret to Forgejo..."
echo -e "${GREEN}✓ SSH key noted${NC}"
echo ""

# Step 4: Clone to nexus-vector
echo -e "${BLUE}Step 4: Cloning repository to nexus-vector...${NC}"
ssh admin@nexus-vector "cd /srv/containers && git clone https://git.runfoo.run/malty/ca-grow-ops-manager.git ca-grow-ops-manager || (cd ca-grow-ops-manager && git pull origin main)"
echo -e "${GREEN}✓ Repository cloned${NC}"
echo ""

# Step 5: Generate environment variables
echo -e "${BLUE}Step 5: Generating secure environment variables...${NC}"
DB_PASSWORD=$(openssl rand -base64 32)
JWT_SECRET=$(openssl rand -base64 64)

ssh admin@nexus-vector "cat > /srv/containers/ca-grow-ops-manager/docker-compose.env << 'EOF'
# Database
DB_PASSWORD=${DB_PASSWORD}

# JWT
JWT_SECRET=${JWT_SECRET}

# Email (optional for v1)
EMAIL_SERVICE=sendgrid
EMAIL_API_KEY=your_api_key_here
EMAIL_FROM=noreply@example.com
EOF"

echo -e "${GREEN}✓ Environment variables generated${NC}"
echo ""
echo -e "${YELLOW}IMPORTANT: Save these credentials:${NC}"
echo "DB_PASSWORD=${DB_PASSWORD}"
echo "JWT_SECRET=${JWT_SECRET}"
echo ""
read -p "Press Enter after you've saved these credentials..."
echo ""

# Step 6: Check if we should trigger CI/CD or manual deploy
echo -e "${BLUE}Step 6: Deployment Method${NC}"
echo "Choose deployment method:"
echo "  1) Trigger CI/CD (recommended - requires Forgejo Actions enabled)"
echo "  2) Manual deployment (deploy now without CI/CD)"
read -p "Enter choice (1 or 2): " deploy_choice

if [ "$deploy_choice" = "1" ]; then
    echo ""
    echo -e "${BLUE}Triggering CI/CD deployment...${NC}"
    echo "# Trigger CI/CD" >> README.md
    git add README.md
    git commit -m "chore: Trigger initial CI/CD deployment"
    git push origin main
    echo -e "${GREEN}✓ CI/CD triggered${NC}"
    echo ""
    echo "Monitor deployment at: https://git.runfoo.run/malty/ca-grow-ops-manager/actions"
else
    echo ""
    echo -e "${BLUE}Performing manual deployment...${NC}"
    ssh admin@nexus-vector "cd /srv/containers/ca-grow-ops-manager && docker compose build && docker compose up -d"
    echo -e "${GREEN}✓ Services deployed${NC}"
fi

echo ""

# Step 7: Verify deployment
echo -e "${BLUE}Step 7: Verifying deployment...${NC}"
sleep 10  # Wait for services to start
echo "Checking service health..."
ssh admin@nexus-vector "curl -f http://localhost:8010/api/healthz 2>/dev/null || echo 'Health check will be available once backend is implemented'"
echo ""
echo "Container status:"
ssh admin@nexus-vector "cd /srv/containers/ca-grow-ops-manager && docker compose ps"
echo ""

# Final summary
echo -e "${GREEN}=============================================="
echo "✅ Deployment Complete!"
echo "==============================================

${NC}"
echo ""
echo "📊 Summary:"
echo "  - Repository: https://git.runfoo.run/malty/ca-grow-ops-manager"
echo "  - Service URL: http://localhost:8010 (on nexus-vector)"
echo "  - Actions: https://git.runfoo.run/malty/ca-grow-ops-manager/actions"
echo ""
echo "📝 Next Steps:"
echo "  1. Enable Forgejo Actions in repository settings"
echo "  2. Start implementing Week 1 tasks"
echo "  3. Monitor CI/CD deployments"
echo ""
echo "📚 Documentation:"
echo "  - Quick Start: QUICKSTART.md"
echo "  - Deployment: DEPLOYMENT.md"
echo "  - CI/CD: CI-CD.md"
echo "  - Tasks: plans/tasks-week-1-infrastructure.md"
echo ""
echo -e "${GREEN}Happy coding! 🚀${NC}"
