#!/bin/bash

# 🚀 SkyArtShop Quick Start Script
# This script sets up your development environment

set -e

echo "🎨 SkyArtShop - Production Structure Setup"
echo "=========================================="
echo ""

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

# Function to print colored output
print_step() {
    echo -e "${BLUE}▶${NC} $1"
}

print_success() {
    echo -e "${GREEN}✓${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}⚠${NC} $1"
}

# Check if node is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js first."
    exit 1
fi

print_success "Node.js version: $(node --version)"
echo ""

# Frontend Setup
print_step "Setting up Frontend..."
cd frontend

if [ ! -f ".env" ]; then
    print_warning "Creating .env file from .env.example"
    cp .env.example .env 2>/dev/null || echo "VITE_API_URL=http://localhost:3000/api" > .env
fi

print_step "Installing frontend dependencies..."
npm install

print_success "Frontend setup complete!"
echo ""

# Backend Setup
cd ../backend
print_step "Setting up Backend..."

if [ ! -f ".env" ]; then
    print_warning "Creating .env file from .env.example"
    cp .env.example .env 2>/dev/null || cat > .env << EOF
PORT=3000
NODE_ENV=development
DATABASE_URL="postgresql://user:password@localhost:5432/skyartshop?schema=public"
JWT_SECRET=$(openssl rand -base64 32 2>/dev/null || echo "change-this-secret-key-in-production")
JWT_EXPIRES_IN=7d
CORS_ORIGIN=http://localhost:5173
MAX_FILE_SIZE=5242880
EOF
    print_warning "⚠️  Please update the DATABASE_URL in backend/.env with your database credentials"
fi

print_step "Installing backend dependencies..."
npm install

print_step "Generating Prisma client..."
npx prisma generate

print_success "Backend setup complete!"
echo ""

# Summary
echo "=========================================="
echo "✅ Setup Complete!"
echo "=========================================="
echo ""
echo "📝 Next Steps:"
echo ""
echo "1. Update backend/.env with your database credentials"
echo "   Edit: backend/.env"
echo ""
echo "2. Run database migrations:"
echo "   cd backend && npx prisma migrate dev"
echo ""
echo "3. Start development servers:"
echo ""
echo "   Terminal 1 (Backend):"
echo "   $ cd backend && npm run dev"
echo ""
echo "   Terminal 2 (Frontend):"
echo "   $ cd frontend && npm run dev"
echo ""
echo "4. Open your browser:"
echo "   Frontend: http://localhost:5173"
echo "   Backend:  http://localhost:3000"
echo ""
echo "📚 Documentation:"
echo "   - docs/ARCHITECTURE.md - Full architecture guide"
echo "   - docs/STRUCTURE_COMPLETE.md - Structure overview"
echo "   - frontend/readme.md - Frontend guide"
echo "   - backend/readme.md - Backend guide"
echo ""
print_success "Happy coding! 🎉"
