name: Nightly

on:
  schedule:
    # Run nightly at 2:00 AM UTC
    - cron: "0 2 * * *"
  # Allow manual trigger for testing
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  # Generate matrix of all crypto combinations for each platform
  windows-crypto-test:
    name: Windows - L:${{ matrix.l_crypto }} R:${{ matrix.r_crypto }}
    runs-on: windows-latest
    timeout-minutes: 15

    strategy:
      fail-fast: false
      matrix:
        l_crypto: ["rust-crypto", "aws", "wincrypto"]
        r_crypto: ["rust-crypto", "aws", "wincrypto"]

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@stable
        id: toolchain

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.l_crypto }}-${{ matrix.r_crypto }}

      - name: Test L:${{ matrix.l_crypto }} <-> R:${{ matrix.r_crypto }}
        env:
          L_CRYPTO: ${{ matrix.l_crypto }}
          R_CRYPTO: ${{ matrix.r_crypto }}
        run: cargo +${{steps.toolchain.outputs.name}} test --release --no-default-features --features rust-crypto,aws-lc-rs,wincrypto --lib --bins --tests

  linux-crypto-test:
    name: Linux - L:${{ matrix.l_crypto }} R:${{ matrix.r_crypto }}
    runs-on: ubuntu-latest
    timeout-minutes: 15

    strategy:
      fail-fast: false
      matrix:
        l_crypto: ["rust-crypto", "aws", "openssl"]
        r_crypto: ["rust-crypto", "aws", "openssl"]

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@stable
        id: toolchain

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.l_crypto }}-${{ matrix.r_crypto }}

      - name: Test L:${{ matrix.l_crypto }} <-> R:${{ matrix.r_crypto }}
        env:
          L_CRYPTO: ${{ matrix.l_crypto }}
          R_CRYPTO: ${{ matrix.r_crypto }}
        run: cargo +${{steps.toolchain.outputs.name}} test --release --no-default-features --features rust-crypto,aws-lc-rs,openssl --lib --bins --tests

  macos-crypto-test:
    name: macOS - L:${{ matrix.l_crypto }} R:${{ matrix.r_crypto }}
    runs-on: macos-latest
    timeout-minutes: 15

    strategy:
      fail-fast: false
      matrix:
        l_crypto: ["rust-crypto", "aws", "apple-crypto", "openssl"]
        r_crypto: ["rust-crypto", "aws", "apple-crypto", "openssl"]

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@stable
        id: toolchain

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.l_crypto }}-${{ matrix.r_crypto }}

      - name: Test L:${{ matrix.l_crypto }} <-> R:${{ matrix.r_crypto }}
        env:
          L_CRYPTO: ${{ matrix.l_crypto }}
          R_CRYPTO: ${{ matrix.r_crypto }}
        run: cargo +${{steps.toolchain.outputs.name}} test --release --no-default-features --features rust-crypto,aws-lc-rs,apple-crypto,openssl --lib --bins --tests

  # Additional platform testing
  platform-build-test:
    name: ${{ matrix.platform.name }} - Build & Test
    runs-on: ${{ matrix.platform.os }}
    timeout-minutes: 30

    strategy:
      fail-fast: false
      matrix:
        platform:
          - name: Linux ARM64
            os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            features: rust-crypto,aws-lc-rs,openssl
          - name: Windows ARM64
            os: windows-11-arm
            target: aarch64-pc-windows-msvc
            features: rust-crypto,aws-lc-rs,wincrypto
          - name: macOS Intel
            os: macos-15-intel
            target: x86_64-apple-darwin
            features: rust-crypto,aws-lc-rs,apple-crypto,openssl

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@stable
        id: toolchain
        with:
          targets: ${{ matrix.platform.target }}

      - name: Install build dependencies on Linux ARM64
        if: matrix.platform.os == 'ubuntu-24.04-arm'
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.platform.target }}

      - name: Build ${{ matrix.platform.name }}
        run: cargo +${{steps.toolchain.outputs.name}} build --target ${{ matrix.platform.target }} --no-default-features --features ${{ matrix.platform.features }}

      - name: Set Swift library path for macOS Intel
        if: matrix.platform.os == 'macos-15-intel'
        run: echo "DYLD_LIBRARY_PATH=/usr/lib/swift${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}" >> $GITHUB_ENV

      - name: Test ${{ matrix.platform.name }}
        run: cargo +${{steps.toolchain.outputs.name}} test --release --target ${{ matrix.platform.target }} --no-default-features --features ${{ matrix.platform.features }} --lib --bins --tests

  # Build-only for mobile platforms
  mobile-build:
    name: ${{ matrix.platform.name }} - Build Only
    runs-on: ${{ matrix.platform.os }}
    timeout-minutes: 30

    strategy:
      fail-fast: false
      matrix:
        platform:
          # iOS targets - ARM64 only
          - name: iOS ARM64
            os: macos-latest
            target: aarch64-apple-ios
            features: apple-crypto
          # Android targets - ARM64 only
          - name: Android ARM64
            os: ubuntu-latest
            target: aarch64-linux-android
            features: rust-crypto,aws-lc-rs

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@stable
        id: toolchain
        with:
          targets: ${{ matrix.platform.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.platform.target }}

      - name: Install Android NDK
        if: contains(matrix.platform.target, 'android')
        run: |
          sudo apt-get update
          sudo apt-get install -y unzip wget
          wget https://dl.google.com/android/repository/android-ndk-r27-linux.zip
          unzip -q android-ndk-r27-linux.zip
          echo "ANDROID_NDK_HOME=$PWD/android-ndk-r27" >> $GITHUB_ENV
          echo "CC_aarch64_linux_android=$PWD/android-ndk-r27/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV
          echo "AR_aarch64_linux_android=$PWD/android-ndk-r27/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar" >> $GITHUB_ENV
          echo "CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$PWD/android-ndk-r27/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang" >> $GITHUB_ENV

      - name: Configure Android linker
        if: contains(matrix.platform.target, 'android')
        run: |
          # Linker is now configured via environment variables above
          echo "Android linker configured via environment variables"

      - name: Build ${{ matrix.platform.name }}
        run: cargo +${{steps.toolchain.outputs.name}} build --target ${{ matrix.platform.target }} --no-default-features --features ${{ matrix.platform.features }}
