#!/bin/bash

set -e
set -u
set -o pipefail

# --- Configuration ---
VENV_DIR=".venv_dev_tools"
PYTHON_INTERPRETER="python3"

# --- Script Start ---
echo "INFO: Starting MyPy Type Checking script..."
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# 1. Setup virtual environment
echo "INFO: Ensuring development virtual environment in '$VENV_DIR'..."
uv venv "$VENV_DIR" --python "$PYTHON_INTERPRETER"
PYTHON_IN_VENV="$VENV_DIR/bin/python"
if [ ! -f "$PYTHON_IN_VENV" ]; then PYTHON_IN_VENV="$VENV_DIR/Scripts/python.exe"; fi

# 2. Install project dependencies and MyPy with type stubs
echo "INFO: Installing project dependencies and type stubs..."
uv pip install -r requirements.txt --python "$PYTHON_IN_VENV"
uv pip install mypy types-python-dateutil types-setuptools types-urllib3 --python "$PYTHON_IN_VENV"

# 3. Run MyPy
# MyPy will automatically pick up configuration from pyproject.toml if run from the project root
echo "INFO: Running MyPy..."
"$PYTHON_IN_VENV" -m mypy goodmem_client samples # Skip auto-generated test directory

echo "INFO: MyPy Type Checking script finished successfully."
