#!/bin/bash
# DSS MVP1 Setup Script
# Configures DSS for dss.overbits.luz.uy

set -e  # Exit on error

echo "🚀 DSS MVP1 Setup for dss.overbits.luz.uy"
echo "=========================================="
echo ""

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

# Check if running as overbits user
if [ "$USER" != "overbits" ]; then
    echo -e "${YELLOW}⚠️  Warning: This script is designed for user 'overbits'${NC}"
    echo -e "   Current user: $USER"
    read -p "Continue anyway? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# 1. Check dependencies
echo "📦 Checking dependencies..."
python3 -m dss.settings check-deps || {
    echo -e "${RED}❌ Dependency check failed${NC}"
    echo "   Installing Python dependencies..."
    pip install -r requirements.txt
    echo "   Installing Node dependencies..."
    npm install
}

# 2. Create necessary directories
echo ""
echo "📁 Creating directories..."
mkdir -p ~/.dss/{cache,logs,backups}
mkdir -p dist/tokens
mkdir -p components
echo -e "${GREEN}✅ Directories created${NC}"

# 3. Check for API keys
echo ""
echo "🔑 Checking API keys..."

if [ ! -f .env ]; then
    echo -e "${YELLOW}⚠️  .env file not found - already created${NC}"
fi

# Check if keys are configured
if grep -q "^ANTHROPIC_API_KEY=$" .env 2>/dev/null; then
    echo -e "${YELLOW}⚠️  ANTHROPIC_API_KEY not set in .env${NC}"
    echo "   Get your key from: https://console.anthropic.com/settings/keys"
fi

if grep -q "^FIGMA_TOKEN=$" .env 2>/dev/null; then
    echo -e "${YELLOW}⚠️  FIGMA_TOKEN not set in .env${NC}"
    echo "   Get your token from: https://www.figma.com/developers/api#access-tokens"
fi

if grep -q "^JWT_SECRET=$" .env 2>/dev/null; then
    echo -e "${YELLOW}⚠️  JWT_SECRET not set in .env${NC}"
    echo "   Generate with: openssl rand -hex 32"
    read -p "Generate JWT_SECRET now? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        JWT_SECRET=$(openssl rand -hex 32)
        sed -i "s/^JWT_SECRET=$/JWT_SECRET=$JWT_SECRET/" .env
        echo -e "${GREEN}✅ JWT_SECRET generated and saved to .env${NC}"
    fi
fi

# 4. Run tests
echo ""
read -p "Run tests to verify setup? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🧪 Running tests..."
    python3 -m dss.settings test || {
        echo -e "${RED}❌ Tests failed${NC}"
        echo "   Check the output above for errors"
        exit 1
    }
    echo -e "${GREEN}✅ All tests passed!${NC}"
fi

# 5. System info
echo ""
echo "📊 System Information:"
python3 -m dss.settings info

# 6. Final instructions
echo ""
echo "=========================================="
echo -e "${GREEN}✅ Setup Complete!${NC}"
echo ""
echo "Next steps:"
echo "  1. Add your API keys to .env:"
echo "     - ANTHROPIC_API_KEY (https://console.anthropic.com/settings/keys)"
echo "     - FIGMA_TOKEN (https://www.figma.com/developers/api#access-tokens)"
echo ""
echo "  2. Configure your Figma file:"
echo "     - Add FIGMA_FILE_KEY to .env"
echo "     - Format: figma.com/file/{FILE_KEY}/..."
echo ""
echo "  3. Start the server:"
echo "     python3 -m uvicorn dss.api.server:app --host 0.0.0.0 --port 3456"
echo ""
echo "  4. Visit: https://dss.overbits.luz.uy"
echo ""
echo "Commands:"
echo "  python3 -m dss.settings test          # Run tests"
echo "  python3 -m dss.settings reset         # Reset to fresh state"
echo "  python3 -m dss.settings info          # Show system info"
echo ""
echo "See SETTINGS.md for full documentation"
echo "=========================================="
