name: Build and Push Backend Docker Image

on:
  push:
    branches:
      - master
    tags:
      - 'v*.*.*'
    paths:
      - 'media_manager/**'
      - 'alembic/**'
      - 'alembic.ini'
      - 'Dockerfile'
      - 'pyproject.toml'
      - 'uv.lock'
      - '.github/workflows/build-push-backend.yml'
      - 'tests/**'
      - 'web/**'

  pull_request:
    paths:
      - 'media_manager/**'
      - 'alembic/**'
      - 'alembic.ini'
      - 'Dockerfile'
      - 'pyproject.toml'
      - 'uv.lock'
      - '.github/workflows/build-push-backend.yml'
      - 'tests/**'
      - 'web/**'

  workflow_dispatch:

jobs:
  lint-backend:
    name: Lint Python Code
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/ruff-action@v3
        with:
          src: "./media_manager"

  lint-frontend:
    name: Lint Frontend
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: './web/package-lock.json'
      - name: Install dependencies
        run: npm ci
        working-directory: ./web
      - name: Lint code
        run: npm run lint
        working-directory: ./web

  run-tests:
    needs: lint-backend
    name: Run Python Tests
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          python-version: '3.13'

      - name: Install the project
        run: uv sync --locked --all-extras --dev

      - name: Setup test config
        run: cp config.example.toml config.toml

      - name: Run tests
        run: uv run pytest tests

  build-and-push:
    needs: [run-tests, lint-frontend]
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set repository name to lowercase
        id: repo_name
        run: echo "name=$(echo '${{ github.event.repository.name }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager
          tags: |
            type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') && github.event_name != 'pull_request' }}
            type=ref,event=tag
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha

      - name: Extract version
        id: version
        run: |
          if [[ "${{ github.ref }}" == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          else
            VERSION="dev-${GITHUB_SHA::7}"
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            VERSION=${{ steps.version.outputs.version }}
            BASE_PATH=
          cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache
          cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/${{ steps.repo_name.outputs.name }}/mediamanager:buildcache,mode=max

