#!/bin/bash

# Development Setup Script
# This script sets up the development environment for the Rust web application

set -e

echo "🚀 Setting up development environment..."

# Check if we're in the correct directory
if [ ! -f "Cargo.toml" ]; then
    echo "❌ Error: Please run this script from the project root directory"
    exit 1
fi

# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
    echo "📝 Creating .env file from template..."
    cp .env.example .env
    echo "✅ .env file created"
else
    echo "✅ .env file already exists"
fi

# Install Rust dependencies
echo "📦 Installing Rust dependencies..."
cargo fetch

# Install Node.js dependencies
echo "📦 Installing Node.js dependencies..."
if command -v pnpm &> /dev/null; then
    pnpm install
elif command -v npm &> /dev/null; then
    npm install
else
    echo "❌ Error: pnpm or npm is required but not installed"
    exit 1
fi

# Build CSS
echo "🎨 Building CSS..."
if command -v pnpm &> /dev/null; then
    pnpm run build:css
else
    npm run build:css
fi

# Create certs directory
echo "📁 Creating certificates directory..."
mkdir -p certs

# Ask user if they want to generate TLS certificates
echo ""
read -p "🔐 Do you want to generate self-signed TLS certificates for HTTPS development? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🔐 Generating TLS certificates..."

    # Check if OpenSSL is available
    if ! command -v openssl &> /dev/null; then
        echo "❌ Error: OpenSSL is required to generate certificates but is not installed"
        echo "Please install OpenSSL or generate certificates manually"
        exit 1
    fi

    # Run the certificate generation script
    cd scripts
    ./generate_certs.sh
    cd ..

    echo "📝 To use HTTPS, set SERVER_PROTOCOL=https in your .env file"
else
    echo "⏭️  Skipping TLS certificate generation"
fi

# Check if cargo-leptos is installed
echo "🔧 Checking for cargo-leptos..."
if ! command -v cargo-leptos &> /dev/null; then
    echo "📦 Installing cargo-leptos..."
    cargo install cargo-leptos
else
    echo "✅ cargo-leptos is already installed"
fi

# Build the project
echo "🔨 Building the project..."
cargo build

echo ""
echo "🎉 Development environment setup complete!"
echo ""
echo "📋 Next steps:"
echo "   1. Review and customize your .env file"
echo "   2. Start the development server:"
echo "      cargo leptos watch"
echo "   3. Open your browser to:"
echo "      HTTP:  http://127.0.0.1:3030"
echo "      HTTPS: https://127.0.0.1:3030 (if TLS is enabled)"
echo ""
echo "📚 Available commands:"
echo "   cargo leptos watch    - Start development server with hot reload"
echo "   cargo leptos build    - Build for production"
echo "   cargo build           - Build Rust code only"
echo "   pnpm run build:css    - Build CSS only"
echo "   pnpm run dev          - Watch CSS changes"
echo ""
echo "🔧 Configuration:"
echo "   Environment variables are loaded from .env file"
echo "   Modify .env to change server settings"
echo "   Example DaisyUI components available at /daisyui"
echo ""
echo "🆘 Need help?"
echo "   Check the README.md file for more information"
echo "   Review DAISYUI_INTEGRATION.md for UI component usage"
