name: CI

permissions:
  contents: write

env:
  NODE_VERSION: '25.2.1'

on:
  push:
    branches: [main]
    tags:
      - '*'

  pull_request:

  workflow_dispatch:

jobs:
  # 1. Lint and Prettier
  lint:
    runs-on: ubuntu-latest
    # Run on pushes/dispatch always; on PRs only if not draft
    if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run ESLint
        run: npm run lint

      - name: Check Prettier formatting
        run: npm run format:check

  # 2. Build TypeScript
  build:
    runs-on: ubuntu-latest
    needs: lint
    if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Compile extension
        run: npm run compile

  # 3. Tests (Vitest + E2E)
  test:
    runs-on: ubuntu-latest
    needs: [lint, build]
    if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run unit, integration, and e2e tests
        run: xvfb-run -a npm test

      - name: Upload coverage artifacts
        uses: actions/upload-artifact@v4
        with:
          name: test-artifacts
          path: |
            coverage/coverage-summary.json
            docs/source/_generated/coverage-report.md

  # 4. Package VSIX + Draft GitHub Release (tag pushes only)
  release:
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Install vsce
        run: npm install -g @vscode/vsce@3.9.1

      - name: Build VSIX
        run: |
          npm run compile
          vsce package --out embedded-device-logger-${{ github.ref_name }}.vsix

      # Optional: keep workflow run artifact too (visible under Actions run)
      - name: Upload VSIX artifact (Actions)
        uses: actions/upload-artifact@v4
        with:
          name: embedded-device-logger-${{ github.ref_name }}
          path: embedded-device-logger-${{ github.ref_name }}.vsix

      # Create a release for this tag and upload the VSIX as a release asset
      - name: Create GitHub Release + upload VSIX
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: VSCode-Logger ${{ github.ref_name }}
          draft: false
          make_latest: true
          generate_release_notes: true
          files: embedded-device-logger-${{ github.ref_name }}.vsix

      # Publish the release to VSCode Market Place
      - name: Publish extension to VSCode Market Place
        run: vsce publish -p ${{ secrets.VSCE_TOKEN }}

  # 5. Documentation (build on PRs/pushes/manual; deploy on push-to-main OR manual; never on PRs)
  docs:
    runs-on: ubuntu-latest
    needs: [build, test]

    # Build on pushes + workflow_dispatch always; on PRs only if not draft
    if: github.event_name != 'pull_request' || github.event.pull_request.draft == false

    permissions:
      contents: read
      pages: write
      id-token: write

    steps:
      - name: Check out repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Clean documentation outputs
        run: make docs-clean

      - name: Download test artifacts
        uses: actions/download-artifact@v4
        with:
          name: test-artifacts
          path: .

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'

      - name: Install documentation dependencies
        run: pip install -r docs/requirements.txt

      - name: Build docs via Makefile
        run: |
          make docs

      # Deploy condition: push to main OR workflow_dispatch, but NEVER pull_request
      - name: Configure GitHub Pages
        if: github.event_name != 'pull_request' && (github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/main')
        uses: actions/configure-pages@v5

      - name: Upload GitHub Pages artifact
        if: github.event_name != 'pull_request' && (github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/main')
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/build/html

      - name: Deploy to GitHub Pages
        if: github.event_name != 'pull_request' && (github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/main')
        id: deployment
        uses: actions/deploy-pages@v4
