# ---------------------------------------------------------------------------
# GPS-Denied Onboard — ArduPilot SITL Integration Harness
# ---------------------------------------------------------------------------
# Runs ArduPlane SITL alongside the gps-denied service on a shared network.
# GPS_INPUT messages are sent by gps-denied to the SITL FC via MAVLink.
#
# Usage:
#   docker compose -f docker-compose.sitl.yml up --build
#   docker compose -f docker-compose.sitl.yml run integration-tests
#   docker compose -f docker-compose.sitl.yml down -v
#
# Integration tests (skipped unless ARDUPILOT_SITL_HOST is set):
#   ARDUPILOT_SITL_HOST=ardupilot-sitl \
#   docker compose -f docker-compose.sitl.yml run integration-tests
# ---------------------------------------------------------------------------

version: "3.9"

services:

  # --------------------------------------------------------------------------
  # ArduPilot SITL — ArduPlane (fixed-wing) simulator
  # Exposes:
  #   5762/tcp — MAVLink connection for gps-denied (GPS_INPUT output)
  #   5763/tcp — MAVLink connection for ground-station / MAVProxy
  # --------------------------------------------------------------------------
  ardupilot-sitl:
    image: ardupilot/ardupilot-dev:latest
    container_name: ardupilot-sitl
    command: >
      bash -c "
        cd /ardupilot &&
        Tools/autotest/sim_vehicle.py
          -v ArduPlane
          -f plane-elevon
          --no-rebuild
          --no-mavproxy
          --out=tcp:0.0.0.0:5762
          --out=tcp:0.0.0.0:5763
          --speedup=1
          --simin=none
      "
    ports:
      - "5762:5762"
      - "5763:5763"
    networks:
      - sitl_net
    # SITL needs a few seconds to boot before gps-denied connects
    healthcheck:
      test: ["CMD", "nc", "-z", "localhost", "5762"]
      interval: 5s
      timeout: 3s
      retries: 12
      start_period: 30s

  # --------------------------------------------------------------------------
  # GPS-Denied Onboard service — connects to SITL via TCP MAVLink
  # --------------------------------------------------------------------------
  gps-denied:
    build:
      context: .
      dockerfile: Dockerfile
    image: gps-denied-onboard:sitl
    container_name: gps-denied-sitl
    ports:
      - "8000:8000"
    environment:
      GPS_DENIED_DB_PATH: /data/flights.db
      GPS_DENIED_TILE_DIR: /data/satellite_tiles
      GPS_DENIED_LOG_LEVEL: DEBUG
      # MAVLink: connect to SITL FC on TCP
      ARDUPILOT_SITL_HOST: ardupilot-sitl
      ARDUPILOT_SITL_PORT: "5762"
      MAVLINK_CONNECTION: "tcp:ardupilot-sitl:5762"
    volumes:
      - sitl_data:/data
    depends_on:
      ardupilot-sitl:
        condition: service_healthy
    networks:
      - sitl_net
    restart: on-failure

  # --------------------------------------------------------------------------
  # Integration test runner — runs test_sitl_integration.py against live SITL
  # --------------------------------------------------------------------------
  integration-tests:
    build:
      context: .
      dockerfile: Dockerfile
      target: builder
    container_name: sitl-integration-tests
    entrypoint: >
      python -m pytest tests/test_sitl_integration.py -v
        --timeout=60
        --tb=short
    environment:
      ARDUPILOT_SITL_HOST: ardupilot-sitl
      ARDUPILOT_SITL_PORT: "5762"
      PYTHONPATH: /app/src
    volumes:
      - ./src:/app/src:ro
      - ./tests:/app/tests:ro
    depends_on:
      ardupilot-sitl:
        condition: service_healthy
      gps-denied:
        condition: service_healthy
    networks:
      - sitl_net
    profiles:
      - test

volumes:
  sitl_data:
    driver: local

networks:
  sitl_net:
    driver: bridge
