#!/bin/bash

# Generate Trillion Events Script
# Creates a Wasot file with 1 trillion events to stress-test memory constraints
#
# CONTEXT: After bounded channel fix (commit 0cf597f), memory usage stays constant 
# at ~60MB regardless of file size. This script tests the ultimate extreme case
# to validate the fix holds even for production-scale workloads.
#
# SAFETY: Uses memory limiting and monitoring to prevent system impact
# Expected behavior: Constant ~60MB memory usage throughout entire generation

set -e

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

# Configuration
TRILLION=1000000000000
OUTPUT_FILE="${1:-trillion_events.wasot}"
MEMORY_LIMIT_GB=1
PROGRESS_INTERVAL=100000000  # Report progress every 100M events

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

echo -e "${BLUE}🚀 Generating Trillion Event Wasot File${NC}"
echo "========================================"
echo "Events to generate: $(printf "%'d" $TRILLION)"
echo "Output file: $OUTPUT_FILE"
echo "Memory limit: ${MEMORY_LIMIT_GB}GB"
echo "Expected memory usage: ~60MB (constant)"
echo ""

# Validate system requirements
echo -e "${YELLOW}🔍 System Requirements Check${NC}"

# Check available disk space (estimate ~100KB per 1000 events = ~100GB for 1T events)
REQUIRED_SPACE_GB=120
AVAILABLE_SPACE_KB=$(df . | tail -1 | awk '{print $4}')
AVAILABLE_SPACE_GB=$((AVAILABLE_SPACE_KB / 1024 / 1024))

echo "Required disk space: ~${REQUIRED_SPACE_GB}GB"
echo "Available disk space: ${AVAILABLE_SPACE_GB}GB"

if [ $AVAILABLE_SPACE_GB -lt $REQUIRED_SPACE_GB ]; then
    echo -e "${RED}❌ Insufficient disk space${NC}"
    echo "This operation requires approximately ${REQUIRED_SPACE_GB}GB of free space"
    exit 1
fi

# Check if we can set memory limits
if ! command -v ulimit &>/dev/null; then
    echo -e "${YELLOW}⚠️  ulimit not available - cannot enforce memory limits${NC}"
    MEMORY_LIMIT_GB=0
fi

echo -e "${GREEN}✅ System requirements satisfied${NC}"
echo ""

# Confirm with user due to extreme scale
echo -e "${YELLOW}⚠️  WARNING: This will generate a TRILLION events${NC}"
echo "Estimated time: Several hours to days depending on I/O speed"
echo "Estimated file size: ~100GB"
echo "This operation will stress-test the bounded channel memory fix"
echo ""
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Operation cancelled"
    exit 0
fi

# Start generation with memory monitoring
echo -e "${BLUE}🎯 Starting trillion event generation...${NC}"
echo "Press Ctrl+C to stop at any time"
echo ""

# Record start time
START_TIME=$(date +%s)

# Function to handle cleanup on exit
cleanup() {
    local exit_code=$?
    echo ""
    echo -e "${YELLOW}🛑 Generation interrupted${NC}"
    
    if [ -f "$OUTPUT_FILE" ]; then
        local file_size=$(du -h "$OUTPUT_FILE" | cut -f1)
        echo "Partial file created: $OUTPUT_FILE ($file_size)"
        echo "You can resume generation or delete the partial file"
    fi
    
    exit $exit_code
}

trap cleanup SIGINT SIGTERM

# Execute with memory limiting and progress monitoring
if [ "$MEMORY_LIMIT_GB" -gt 0 ]; then
    MEMORY_LIMIT_KB=$((MEMORY_LIMIT_GB * 1024 * 1024))
    echo -e "${GREEN}🔒 Memory limit: ${MEMORY_LIMIT_GB}GB${NC}"
    
    # Use ulimit to constrain memory and timeout to provide periodic status
    (
        ulimit -v $MEMORY_LIMIT_KB
        exec "$PROJECT_ROOT/target/release/wasot" fast-random -n "$TRILLION" -o "$OUTPUT_FILE"
    ) &
    
    GENERATION_PID=$!
else
    echo -e "${YELLOW}⚠️  No memory limit set${NC}"
    "$PROJECT_ROOT/target/release/wasot" fast-random -n "$TRILLION" -o "$OUTPUT_FILE" &
    GENERATION_PID=$!
fi

# Monitor progress and memory usage
echo -e "${BLUE}📊 Monitoring generation progress...${NC}"
echo "PID: $GENERATION_PID"

# Monitor loop
while kill -0 $GENERATION_PID 2>/dev/null; do
    # Check current memory usage of the process
    if command -v ps &>/dev/null; then
        MEMORY_MB=$(ps -o rss= -p $GENERATION_PID 2>/dev/null | awk '{print int($1/1024)}' || echo "0")
        echo -n -e "\r${GREEN}⏳ Generating... Memory: ${MEMORY_MB}MB${NC}"
    else
        echo -n -e "\r${GREEN}⏳ Generating...${NC}"
    fi
    
    sleep 10
done

# Wait for process to complete and get exit code
wait $GENERATION_PID
EXIT_CODE=$?

echo "" # New line after progress indicator

# Calculate execution time
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
HOURS=$((DURATION / 3600))
MINUTES=$(((DURATION % 3600) / 60))
SECONDS=$((DURATION % 60))

if [ $EXIT_CODE -eq 0 ]; then
    echo -e "${GREEN}🎉 Successfully generated trillion event file!${NC}"
    
    if [ -f "$OUTPUT_FILE" ]; then
        FILE_SIZE=$(du -h "$OUTPUT_FILE" | cut -f1)
        echo "Output file: $OUTPUT_FILE"
        echo "File size: $FILE_SIZE"
    fi
    
    printf "Generation time: %02d:%02d:%02d\n" $HOURS $MINUTES $SECONDS
    echo ""
    echo -e "${GREEN}✅ Memory constraint validation: PASSED${NC}"
    echo "The bounded channel fix successfully maintained constant memory usage"
    echo "even for trillion-scale event generation."
    
else
    echo -e "${RED}❌ Generation failed with exit code: $EXIT_CODE${NC}"
    
    if [ $EXIT_CODE -eq 137 ]; then
        echo "Process killed - likely due to memory limit exceeded"
        echo "This indicates a potential issue with the memory fix"
    fi
    
    exit $EXIT_CODE
fi

echo ""
echo -e "${BLUE}🏁 Trillion Event Generation Complete${NC}"