#!/bin/bash
#
# Gurobi Optimization Agent
# =========================
#
# Automatically speed up any Gurobi optimization model.
#
# USAGE:
#   ./optimize <github-url>              # Optimize and create PR
#   ./optimize <github-url> --no-pr      # Just show results
#
# EXAMPLES:
#   ./optimize https://github.com/user/my-optimization
#   ./optimize https://github.com/user/my-optimization --no-pr
#   ./optimize https://github.com/user/my-optimization --timeout 600
#
# OPTIONS:
#   --no-pr           Don't create a pull request
#   --timeout N       Max seconds per model run (default: 300)
#   --entry-point F   Path to solver script (auto-detected by default)
#   --quiet           Reduce output verbosity
#
# REQUIREMENTS:
#   - Target repo must have solve.py with model = gp.Model() inline
#   - Target repo must have requirements.txt with gurobipy
#   - GitHub CLI (gh) must be authenticated for PR creation
#
# DOCUMENTATION:
#   See docs/REPO_GUIDE.md for detailed compatibility guide
#

set -e

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

# Get the directory where this script lives
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Show help if no arguments
if [ $# -eq 0 ]; then
    echo -e "${BLUE}Gurobi Optimization Agent${NC}"
    echo ""
    echo "Usage: ./optimize <github-url> [options]"
    echo ""
    echo "Examples:"
    echo "  ./optimize https://github.com/user/repo"
    echo "  ./optimize https://github.com/user/repo --no-pr"
    echo "  ./optimize https://github.com/user/repo --timeout 600"
    echo ""
    echo "Options:"
    echo "  --no-pr           Don't create a pull request"
    echo "  --timeout N       Max seconds per model run (default: 300)"
    echo "  --entry-point F   Path to solver script"
    echo "  --quiet           Reduce output"
    echo ""
    echo "See docs/REPO_GUIDE.md for repository requirements."
    exit 0
fi

# Check for GitHub URL
if [[ ! "$1" =~ ^https://github.com/ ]]; then
    echo -e "${RED}Error: First argument must be a GitHub URL${NC}"
    echo "Example: ./optimize https://github.com/user/repo"
    exit 1
fi

# Clean up old temp folders to avoid conflicts
rm -rf /var/folders/96/l4rh5ft119v81z9mklc7n0s80000gn/T/gurobi_agent_* 2>/dev/null || true

# Check if gh is authenticated (for PR creation)
if ! echo "$@" | grep -q "\-\-no-pr"; then
    if ! gh auth status &>/dev/null; then
        echo -e "${YELLOW}Warning: GitHub CLI not authenticated. PRs won't be created.${NC}"
        echo "Run 'gh auth login' to enable PR creation."
        echo ""
    fi
fi

# Build args with better defaults
ARGS="$@"

# Add default timeout of 300s if not specified
if ! echo "$ARGS" | grep -q "\-\-timeout"; then
    ARGS="$ARGS --timeout 300"
fi

# Run the agent with unbuffered output for real-time logs
echo -e "${GREEN}Starting Gurobi Optimization Agent...${NC}"
echo ""
cd "$SCRIPT_DIR"
PYTHONUNBUFFERED=1 python3 run_repo_agent.py $ARGS

# Check exit code
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
    echo ""
    echo -e "${GREEN}✓ Optimization complete!${NC}"
else
    echo ""
    echo -e "${RED}✗ Optimization failed${NC}"
    echo ""
    echo "Troubleshooting:"
    echo "  1. Check that the repo has requirements.txt with gurobipy"
    echo "  2. Check that solve.py has model = gp.Model() inline"
    echo "  3. Try: ./optimize <url> --entry-point solve.py"
    echo "  4. See docs/REPO_GUIDE.md for full guide"
fi

exit $EXIT_CODE
