# You need a .env file in the root of the project.

set dotenv-path := "../../.env"
set dotenv-required := false
set dotenv-load := true

help:
    just -l

# Run all gates: test with coverage + type-check + lint + build
check: test-cov typecheck lint build

# Run unit tests
test *args:
    SG_ENV=test PYTHONUNBUFFERED=1 uv run pytest tests {{ args }}

# Run unit tests with coverage (fail_under configured in pyproject.toml)
test-cov *args:
    SG_ENV=test PYTHONUNBUFFERED=1 uv run pytest --cov --cov-report=term-missing tests {{ args }}

# Type-check with ty
typecheck *args:
    uv run ty check sec_gemini/ {{ args }}

# Lint with ruff
lint *args:
    uv run ruff check sec_gemini/ tests/ {{ args }}

alias cs := check-source
check-source:
    uv run ruff format --check sec_gemini/ tests/ && uv run ruff check sec_gemini/ tests/ && uv run ty check --error-on-warning sec_gemini/ tests/

alias fs := fix-source
alias fix := fix-source
fix-source:
    uv run ruff format sec_gemini/ tests/
    uv run ruff check --fix sec_gemini/ tests/
    just check-source

# Format code
format:
    uv run ruff format sec_gemini/ tests/

# Build sdist + wheel from clean state and verify critical files
build:
    rm -rf dist/ build/ *.egg-info
    uv build
    @echo "--- Verifying wheel contents ---"
    @uv run python -c "\
    import zipfile, sys, glob; \
    whl = glob.glob('dist/sec_gemini-*.whl')[0]; \
    names = zipfile.ZipFile(whl).namelist(); \
    required = [ \
        'sec_gemini/api_pb2.py', \
        'sec_gemini/api_pb2_grpc.py', \
        'sec_gemini/byot/__init__.py', \
        'sec_gemini/byot/byot_api_pb2.py', \
        'sec_gemini/byot/byot_api_pb2_grpc.py', \
        'sec_gemini/byot/client.py', \
        'sec_gemini/byot/cli.py', \
        'sec_gemini/byot/service.py', \
        'sec_gemini/tools/__init__.py', \
        'sec_gemini/tools/file_ops.py', \
        'sec_gemini/tools/shell.py', \
        'sec_gemini/tools/network.py', \
        'sec_gemini/tools/safety.py', \
    ]; \
    missing = [f for f in required if f not in names]; \
    print(f'Checked {len(required)} required files in {whl}'); \
    [print(f'  MISSING: {f}') for f in missing]; \
    sys.exit(1) if missing else print('All required files present.') \
    "

# Clean build artifacts
clean:
    rm -rf dist/ build/ *.egg-info .coverage .pytest_cache
    find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

# Regenerate proto files
proto:
    ./bin/update_proto.sh

# Run the TUI
tui:
    uv run -m sec_gemini.tui2

# Run the TUI v2
tui2:
    uv run -m sec_gemini.tui2

byot *args:
  uv run sec-gemini-byot {{ args }}

# Run production tests
test-production *args:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_simple.py

# Run production logging suppression tests
test-logging-suppression *args:
    PYTHONUNBUFFERED=1 uv run code_examples/verify_logging_suppression.py {{ args }}

# --- Demos ---

demo-complete:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_complete.py

demo-files:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_files.py

demo-simple:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_simple.py

demo-mcps:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_mcps.py

demo-skills:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_skills.py

demo-cybergym:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_cybergym.py

demo-cpg:
    PYTHONUNBUFFERED=1 uv run code_examples/demo_code_property_graph.py
