name: CI

on:
  pull_request:
    branches:
      - main

  push:
    branches:
      - main

permissions:
  contents: read

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

jobs:
  frontend-build:
    name: Frontend Build
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Check out code
        uses: actions/checkout@v6

      - name: Set up bun
        uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile --cwd ./internal/site
        shell: bash

      - name: Build site
        run: bun run --cwd ./internal/site build
        shell: bash

  go-test:
    name: Go Tests
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Check out code
        uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
          cache-dependency-path: go.sum

      - name: Run tests
        run: go test -tags=testing ./...
        shell: bash

  release-smoke:
    name: Release Smoke Build
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Check out code
        uses: actions/checkout@v6

      - name: Set up bun
        uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile --cwd ./internal/site
        shell: bash

      - name: Build site
        run: bun run --cwd ./internal/site build
        shell: bash

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
          cache-dependency-path: go.sum

      - name: Set up .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: "9.0.x"

      - name: Build Windows sensor helper
        run: dotnet build -c Release ./agent/lhm/beszel_lhm.csproj
        shell: bash

      - name: Fetch Windows smartctl asset
        run: go generate -run fetchsmartctl ./agent
        shell: bash

      - name: Build Linux binaries
        run: go build ./internal/cmd/hub ./internal/cmd/agent
        shell: bash

      - name: Build Windows agent
        run: GOOS=windows GOARCH=amd64 go build ./internal/cmd/agent
        shell: bash

  docker-smoke:
    name: Docker Smoke Build
    runs-on: ubuntu-latest
    timeout-minutes: 45
    strategy:
      fail-fast: false
      matrix:
        include:
          - dockerfile: ./internal/dockerfile_hub
            image: beszel-hub-smoke
          - dockerfile: ./internal/dockerfile_agent
            image: beszel-agent-smoke
    steps:
      - name: Check out code
        uses: actions/checkout@v6

      - name: Set up bun
        uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile --cwd ./internal/site
        shell: bash

      - name: Build site
        run: bun run --cwd ./internal/site build
        shell: bash

      - name: Build Docker image
        run: docker build --file "${{ matrix.dockerfile }}" --tag "${{ matrix.image }}" .
        shell: bash
