name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: nixbuild/nix-quick-install-action@v30
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore and save Nix store
        uses: nix-community/cache-nix-action@v6
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          gc-max-store-size-linux: 1G
          purge: true
          purge-prefixes: nix-${{ runner.os }}-
          purge-primary-key: never

      - name: Test
        run: nix develop --command xc test-cover

      - name: Upload coverage artifact
        if: github.event_name == 'push'
        uses: actions/upload-artifact@v4
        with:
          name: coverage
          path: coverage.out
      
      - name: Build
        run: nix build

  update-coverage:
    if: github.event_name == 'push'
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Download coverage
        uses: actions/download-artifact@v4
        with:
          name: coverage

      - name: Update coverage report
        uses: ncruces/go-coverage-report@57ac6f0f19874f7afbab596105154f08004f482e
        with:
          coverage-file: coverage.out
          report: 'true'
          chart: 'true'
          reuse-go: 'true'

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: nixbuild/nix-quick-install-action@v30
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore and save Nix store
        uses: nix-community/cache-nix-action@v6
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          gc-max-store-size-linux: 1G
          purge: true
          purge-prefixes: nix-${{ runner.os }}-
          purge-primary-key: never

      - name: Lint
        run: nix develop --command xc lint

  ensure-generated:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: nixbuild/nix-quick-install-action@v30
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore and save Nix store
        uses: nix-community/cache-nix-action@v6
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          gc-max-store-size-linux: 1G
          purge: true
          purge-prefixes: nix-${{ runner.os }}-
          purge-primary-key: never

      - name: Generate
        run: nix develop --command xc ensure-generated

  ensure-fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: nixbuild/nix-quick-install-action@v30
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore and save Nix store
        uses: nix-community/cache-nix-action@v6
        with:
          primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          gc-max-store-size-linux: 1G
          purge: true
          purge-prefixes: nix-${{ runner.os }}-
          purge-primary-key: never

      - name: Fmt
        run: nix develop --command xc fmt

      - name: Ensure clean
        run: git diff --exit-code
