name: farmers-market-ci

on:
  push:
    branches:
      - main
      - develop
  pull_request:
    branches:
      - main

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

env:
  NODE_VERSION: "22"
  POSTGRES_DB: farmers_market_test
  POSTGRES_USER: marketplace
  POSTGRES_PASSWORD: marketplace_test
  DATABASE_URL: postgresql://marketplace:marketplace_test@localhost:5432/farmers_market_test
  REDIS_URL: redis://localhost:6379/0

jobs:
  test:
    name: Lint, test, and build
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:17-alpine
        env:
          POSTGRES_DB: farmers_market_test
          POSTGRES_USER: marketplace
          POSTGRES_PASSWORD: marketplace_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U marketplace -d farmers_market_test"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Check formatting
        run: npm run format:check

      - name: Run linter
        run: npm run lint

      - name: Apply database migrations
        run: npm run db:migrate

      - name: Seed marketplace fixtures
        run: npm run db:seed:test

      - name: Run unit and integration tests
        run: npm test -- --coverage

      - name: Build marketplace application
        run: npm run build
        env:
          NEXT_PUBLIC_MARKETPLACE_NAME: Green Basket Market
          NEXT_PUBLIC_DEFAULT_CURRENCY: CAD
          NEXT_PUBLIC_PICKUP_TIMEZONE: America/Toronto

      - name: Upload coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/
          retention-days: 14

  container:
    name: Build container image
    needs: test
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Check out repository
        uses: actions/checkout@v4

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

      - name: Generate image metadata
        id: metadata
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}/marketplace
          tags: |
            type=sha,prefix=
            type=raw,value=latest

      - name: Build and publish image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.metadata.outputs.tags }}
          labels: ${{ steps.metadata.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max