name: Tinyauth CI
on:
  push:
    branches:
      - main
  pull_request:

permissions:
  contents: read

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Setup pnpm
        uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10
        with:
          package_json_file: ./frontend/package.json

      - name: Setup go
        uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
        with:
          go-version: "^1.26.4"

      - name: Go dependencies
        run: go mod download

      - name: Setup sqlc
        uses: sqlc-dev/setup-sqlc@v5
        with:
          sqlc-version: "1.31.1"

      - name: Check codegen is up to date
        run: |
          sqlc generate
          go generate ./...
          git diff --exit-code
          git status --porcelain | grep -q . && echo "untracked files code gen files" && exit 1 || true

      - name: Install frontend dependencies
        working-directory: ./frontend
        run: pnpm ci

      - name: Set version
        run: echo testing > internal/assets/version

      - name: Lint frontend
        working-directory: ./frontend
        run: pnpm run lint

      - name: Build frontend
        working-directory: ./frontend
        run: pnpm run build

      - name: Copy frontend
        run: cp -r frontend/dist internal/assets/dist

      - name: Run tests
        run: go test -coverprofile=coverage.txt -v ./...

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
