name: CI

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

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

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  ci:
    name: CI Checks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Cache cargo registry
        uses: Swatinem/rust-cache@v2

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Install oxfmt
        run: npm install -g oxfmt

      - name: Check code formatting
        run: make fmt-check

      - name: Run linter
        run: make lint

      - name: Run tests
        run: make test

  build-check:
    name: Build Check
    runs-on: ubuntu-latest
    needs: ci
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry
        uses: Swatinem/rust-cache@v2

      - name: Build release
        run: make release

      - name: Verify binary
        run: ./dist/scafgen --version
