on: push
name: build-lint-test
permissions:
  contents: write

jobs:
  clippy:
    name: Run Clippy Lints
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Install Clippy
        run: rustup component add clippy

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v3
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

      - name: Run Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  tests:
    name: Run Tests
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v3
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

      - name: Run Tests
        run: cargo test --all-targets --all-features

  build:
    name: Build ${{ matrix.platform.name }} (${{ matrix.platform.arch }})
    runs-on: ${{ matrix.platform.runner }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          # Linux x64
          - name: Linux
            runner: ubuntu-latest
            arch: x86_64
            target: x86_64-unknown-linux-gnu
            artifact_name: tdoc-linux-x86_64

          # Linux ARM64
          - name: Linux
            runner: ubuntu-24.04-arm
            arch: aarch64
            target: aarch64-unknown-linux-gnu
            artifact_name: tdoc-linux-aarch64

          # macOS x64 (Intel)
          - name: macOS
            runner: macos-13
            arch: x86_64
            target: x86_64-apple-darwin
            deps: echo "No additional dependencies needed for macOS"
            artifact_name: tdoc-macos-x86_64

          # macOS ARM64 (Apple Silicon)
          - name: macOS
            runner: macos-latest
            arch: aarch64
            target: aarch64-apple-darwin
            deps: echo "No additional dependencies needed for macOS"
            artifact_name: tdoc-macos-aarch64

          # Windows x64
          - name: Windows
            runner: windows-latest
            arch: x86_64
            target: x86_64-pc-windows-msvc
            deps: echo "No additional dependencies needed for Windows"
            artifact_name: tdoc-windows-x86_64.exe

          # Windows ARM64
          - name: Windows
            runner: windows-latest
            arch: aarch64
            target: aarch64-pc-windows-msvc
            deps: echo "No additional dependencies needed for Windows"
            artifact_name: tdoc-windows-aarch64.exe

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install platform dependencies
        run: ${{ matrix.platform.deps }}

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          target: ${{ matrix.platform.target }}

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v3
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo build
        uses: actions/cache@v3
        with:
          path: target
          key: ${{ runner.os }}-${{ matrix.platform.target }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.platform.target }} --bin tdoc

      - name: Prepare artifact (Unix)
        if: runner.os != 'Windows'
        run: |
          mkdir -p artifacts
          cp target/${{ matrix.platform.target }}/release/tdoc artifacts/${{ matrix.platform.artifact_name }}

      - name: Prepare artifact (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir artifacts
          cp target/${{ matrix.platform.target }}/release/tdoc.exe artifacts/${{ matrix.platform.artifact_name }}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.platform.artifact_name }}
          path: artifacts/${{ matrix.platform.artifact_name }}
          if-no-files-found: error

  release:
    name: Publish Release
    runs-on: ubuntu-latest
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: tdoc-*
          merge-multiple: true
          path: release

      - name: Create GitHub release
        uses: softprops/action-gh-release@v1
        with:
          files: release/**
          name: ${{ github.ref_name }}
          fail_on_unmatched_files: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
