name: Test and Build

on:
  push:
    branches: [master]
  pull_request:
  workflow_dispatch:
  workflow_call:

jobs:
  nix-flake-test:
    name: Flake check ❄️
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Flake check
        run: |
          nix flake show
          nix flake check --print-build-logs

  nix-build:
    name: Nix build ❄️
    runs-on: ubuntu-latest
    outputs:
      artifact_path: ${{ steps.upload-artifact.outputs.artifact_path }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Build package
        run: |
          nix build .#static --print-build-logs
          cp ./result/bin/mdfried .

      - name: Test binary portability for Ubuntu 24.04
        run: |
          output=$(docker run --rm -v $(pwd):/work ubuntu:24.04 /work/mdfried --version)
          echo "Output: $output"
          if [[ "$output" =~ ^mdfried\ [0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then
            echo "✅ Binary works on Ubuntu 24.04 - Version: $output"
          else
            echo "❌ Binary test failed! Expected format: 'mdfried x.y.z'"
            exit 1
          fi

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: mdfried-static
          path: ./mdfried

  cargo-build:
    name: Cargo build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - container: ubuntu:24.04 # chafa 1.14.0, glibc 2.39
            os: ubuntu-latest
            artifact_name: mdfried-ubuntu-24.04
            deb: null
            publish_dry_run: true
          - container: ubuntu:22.04 # chafa 1.18.0, glibc 2.35
            os: ubuntu-latest
            artifact_name: mdfried-ubuntu-22.04
            deb: true
            publish_dry_run: null
          - os: macos-15-intel
            container: null
            artifact_name: mdfried_macos_x86_64
            deb: null
            publish_dry_run: null
          - os: macos-latest
            container: null
            artifact_name: mdfried_macos_aarch64
            deb: null
            publish_dry_run: null
    container: ${{ matrix.container }}
    outputs:
      artifact_path: ${{ steps.upload-artifact.outputs.artifact_path }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install dependencies (Ubuntu)
        if: runner.os == 'Linux'
        run: |
          sed -i 's|http://archive.ubuntu.com|http://azure.archive.ubuntu.com|g' /etc/apt/sources.list
          sed -i 's|http://security.ubuntu.com|http://azure.archive.ubuntu.com|g' /etc/apt/sources.list
          apt-get update
          DEBIAN_FRONTEND=noninteractive apt-get install -y \
            build-essential \
            curl \
            git \
            ca-certificates \
            libchafa-dev \
            libglib2.0-dev

      - name: Install dependencies (macOS)
        if: runner.os == 'macOS'
        run: |
          brew install chafa pkg-config
          echo "PKG_CONFIG_PATH=$(brew --prefix)/lib/pkgconfig:$(brew --prefix)/share/pkgconfig" >> $GITHUB_ENV

      - name: Set up Rust
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly-2025-08-04
          override: true

      - name: Cache Rust dependencies
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ matrix.artifact_name }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ matrix.artifact_name }}-cargo-

      - name: Build locked release
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --locked

      - name: Test binary works (macOS)
        if: runner.os == 'macOS'
        run: |
          output=$(./target/release/mdfried --version)
          echo "Output: $output"
          if [[ "$output" =~ ^mdfried\ [0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then
            echo "✅ Binary works on macOS - Version: $output"
          else
            echo "❌ Binary test failed! Expected format: 'mdfried x.y.z'"
            exit 1
          fi

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ./target/release/mdfried

      - name: Install cargo-deb
        if: matrix.deb
        uses: actions-rs/cargo@v1
        with:
          command: install
          args: cargo-deb

      - name: Build .deb package
        if: matrix.deb
        uses: actions-rs/cargo@v1
        with:
          command: deb
          args: --no-build

      - name: Upload .deb artifact
        if: matrix.deb
        uses: actions/upload-artifact@v4
        with:
          name: mdfried-deb
          path: ./target/debian/*.deb

      - name: Check mdfrier is publishable
        if: matrix.publish_dry_run && github.ref == 'refs/heads/master'
        run: cargo publish -p mdfrier --dry-run

      - name: Check mdfried is publishable
        if: matrix.publish_dry_run && github.ref == 'refs/heads/master'
        run: cargo publish -p mdfried --dry-run

  nix-build-windows:
    name: Nix build (windows) ❄️
    runs-on: ubuntu-latest
    outputs:
      artifact_path: ${{ steps.upload-artifact.outputs.artifact_path }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Build package
        run: |
          nix build .#windows --print-build-logs

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: mdfried-windows
          path: ./result/bin/mdfried.exe

  nix-build-macos:
    name: Nix build (macos) ❄️
    runs-on: macos-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Build package
        run: |
          nix flake check --print-build-logs

  nix-flake-screenshots:
    name: Screenshot tests ❄️
    runs-on: ubuntu-latest
    needs: [nix-build]
    permissions:
      contents: read
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
          skipPush: true # never cache the screenshot stuff

      - name: Build screenshotDiffs
        run: nix build .#screenshotDiffs.x86_64-linux --impure

      - name: Save PR number
        if: github.event_name == 'pull_request'
        run: echo ${{ github.event.pull_request.number }} > pr_number.txt

      - name: Upload combined screenshots artifact
        uses: actions/upload-artifact@v4
        with:
          name: screenshots
          path: |
            result/
            pr_number.txt

  nix-flake-screen-recording:
    name: Screen recording ❄️
    runs-on: ubuntu-latest
    needs: [nix-build]
    permissions:
      contents: read
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Nix
        uses: cachix/install-nix-action@v31

      - name: Cachix
        uses: cachix/cachix-action@v12
        with:
          name: mdfried-ci
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
          skipPush: true # never cache the screenshot stuff

      - name: Run screen-recording
        run: nix run .#screen-recording

      - name: Save PR number
        if: github.event_name == 'pull_request'
        run: echo ${{ github.event.pull_request.number }} > pr_number.txt

      - name: Upload recording
        uses: actions/upload-artifact@v4
        with:
          name: recording
          path: |
            recording/
            pr_number.txt

