name: Check
on: push
jobs:
  cargo:
    name: Cargo
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          components: clippy, rustfmt
      - name: Install Lean toolchain
        run: |
          curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y --default-toolchain "$(cat lean-toolchain)"
          echo "$HOME/.elan/bin" >> "$GITHUB_PATH"
      - name: Verify Lean installation
        run: |
          lake --version
          lean --version
      - name: Provision pinned Lean dependencies
        run: |
          sudo mkdir -p /Users/hxrts/projects
          sudo chown -R "$USER":"$USER" /Users/hxrts
          ./scripts/bootstrap/dependency-checkouts.sh
      - name: Check formatting
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check
      - name: Build sources
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --workspace --all-targets --all-features
      - name: Install native tooling
        run: sudo apt-get update && sudo apt-get install -y ripgrep gnuplot
      - name: Run executable VM placeholder baseline checks
        run: ./scripts/check/vm-placeholders.sh
      - name: Run parity checks
        run: ./scripts/check/cross-runtime-parity.sh --all
      - name: Check docs drift
        run: ./scripts/check/docs-drift.sh
      - name: Check docs links referenced from code
        run: ./scripts/check/doc-links-in-code.sh
      - name: Run linter
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --workspace --all-targets --all-features -- -D warnings
      - name: Run tests
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --workspace --all-targets --all-features

  wasm:
    name: WASM Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2
      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          target: wasm32-unknown-unknown
      - name: Install wasm-pack
        run: cargo install wasm-pack --version 0.14.0 --locked
      - name: Install just
        run: cargo install just --locked
      - name: Check WASM compilation (choreography)
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --package telltale-choreography --target wasm32-unknown-unknown --features wasm
      - name: Check WASM compilation (core)
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --package telltale --target wasm32-unknown-unknown --features wasm
      - name: Build WASM example
        run: |
          cd examples/wasm-ping-pong
          wasm-pack build --target web
      - name: Test WASM suites
        run: |
          just wasm-test-all

  benchmarks:
    name: Benchmarks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2
      - name: Install toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
      - name: Install gnuplot
        run: sudo apt-get update && sudo apt-get install -y gnuplot
      - name: Build benchmark targets
        uses: actions-rs/cargo@v1
        with:
          command: bench
          args: --workspace --no-run

  docs:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2
      - name: Install Nix
        uses: cachix/install-nix-action@v24
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - name: Build documentation
        run: nix develop --command just book
      - name: Check documentation built
        run: |
          if [ -d docs/book ]; then
            echo "Documentation built successfully"
          else
            echo "Documentation build failed"
            exit 1
          fi

  doc-links:
    name: Doc Links
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: gaurav-nelson/github-action-markdown-link-check@v1
        with:
          use-quiet-mode: "yes"
          use-verbose-mode: "yes"
          config-file: ".github/config/markdown-link-check.json"
          base-branch: "main"
          folder-path: "docs"

  # Lean verification disabled - telltale_runner target removed in V2 refactor
  # TODO: Re-enable once V2 runner is implemented
  # lean:
  #   name: Lean Verification
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout sources
  #       uses: actions/checkout@v2
  #     - name: Install Nix
  #       uses: cachix/install-nix-action@v24
  #       with:
  #         nix_path: nixpkgs=channel:nixos-unstable
  #     - name: Build Lean binary
  #       run: nix develop --command bash -c "cd lean && lake build"
  #     - name: Run Lean verification (sample)
  #       run: nix develop --command just telltale-lean-check
  #     - name: Run Lean verification (extended)
  #       run: nix develop --command just telltale-lean-check-extended
  #     - name: Run Lean verification (negative test)
  #       run: nix develop --command just telltale-lean-check-failing
  #     - name: Run Lean-validated Rust tests
  #       run: |
  #         nix develop --command cargo test -p telltale-lean-bridge --all-targets
  #         echo "✓ All Lean-validated Rust tests passed"
