name: CI

on:
  release:
    types: [ published ]
  push:
    branches:
      - master
  pull_request:
    branches: [ "master" ]

jobs:
  backend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./backend
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.12'
    - name: Install uv
      run: curl -LsSf https://astral.sh/uv/install.sh | sh
    - name: Install dependencies
      run: |
        source ~/.cargo/env
        uv sync --group test
    - name: Lint with ruff
      run: |
        uv run ruff check .
    - name: Test with pytest
      run: |
        uv run pytest

  frontend:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    steps:
    - uses: actions/checkout@v4
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: 20
        cache: 'npm'
        cache-dependency-path: frontend/package-lock.json
    - name: Install dependencies
      run: npm install
    - name: Lint with ESLint
      run: npm run lint
    - name: Test with Jest
      run: npm run test

  docker_publish_backend:
    runs-on: ubuntu-latest
    needs: backend
    if: github.event_name == 'release' || github.ref == 'refs/heads/master'

    steps:
      - uses: actions/checkout@v4

      - name: Build and publish a Docker image for backend
        uses: macbre/push-to-ghcr@master
        with:
          image_name: LeonMusCoden/letterfeed-backend
          github_token: ${{ secrets.GITHUB_TOKEN }}
          dockerfile: backend/Dockerfile
          context: backend/

  docker_publish_frontend:
    runs-on: ubuntu-latest
    needs: frontend
    if: github.event_name == 'release' || github.ref == 'refs/heads/master'

    steps:
      - uses: actions/checkout@v4

      - name: Build and publish a Docker image for frontend
        uses: macbre/push-to-ghcr@master
        with:
          image_name: LeonMusCoden/letterfeed-frontend
          github_token: ${{ secrets.GITHUB_TOKEN }}
          dockerfile: frontend/Dockerfile
          context: frontend/
