name: CI/CD Pipeline

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_stiftung
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      
      redis:
        image: redis:7-alpine
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.10'
    
    - name: Cache pip dependencies
      uses: actions/cache@v4
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('app/requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r app/requirements.txt
    
    - name: Set up environment
      run: |
        cp env-template.txt .env
        echo "DJANGO_DEBUG=1" >> .env
        echo "DJANGO_SECRET_KEY=test-secret-key-for-ci" >> .env
        echo "POSTGRES_DB=test_stiftung" >> .env
        echo "POSTGRES_USER=postgres" >> .env
        echo "POSTGRES_PASSWORD=postgres" >> .env
        echo "DB_HOST=localhost" >> .env
        echo "DB_PORT=5432" >> .env
        echo "REDIS_URL=redis://localhost:6379/0" >> .env
    
    - name: Run migrations
      working-directory: ./app
      env:
        DJANGO_DEBUG: "1"
        DJANGO_SECRET_KEY: "test-secret-key-for-ci"
        POSTGRES_DB: "test_stiftung"
        POSTGRES_USER: "postgres"
        POSTGRES_PASSWORD: "postgres"
        DB_HOST: "localhost"
        DB_PORT: "5432"
      run: |
        python manage.py migrate
    
    - name: Run tests
      working-directory: ./app
      env:
        DJANGO_DEBUG: "1"
        DJANGO_SECRET_KEY: "test-secret-key-for-ci"
        POSTGRES_DB: "test_stiftung"
        POSTGRES_USER: "postgres"
        POSTGRES_PASSWORD: "postgres"
        DB_HOST: "localhost"
        DB_PORT: "5432"
      run: |
        python manage.py test
    
    - name: Check Django configuration
      working-directory: ./app
      env:
        DJANGO_DEBUG: "1"
        DJANGO_SECRET_KEY: "test-secret-key-for-ci"
        POSTGRES_DB: "test_stiftung"
        POSTGRES_USER: "postgres"
        POSTGRES_PASSWORD: "postgres"
        DB_HOST: "localhost"
        DB_PORT: "5432"
      run: |
        python manage.py check --deploy
    
    - name: Collect static files
      working-directory: ./app
      env:
        DJANGO_DEBUG: "1"
        DJANGO_SECRET_KEY: "test-secret-key-for-ci"
        POSTGRES_DB: "test_stiftung"
        POSTGRES_USER: "postgres"
        POSTGRES_PASSWORD: "postgres"
        DB_HOST: "localhost"
        DB_PORT: "5432"
      run: |
        python manage.py collectstatic --noinput

  build:
    needs: test
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    
    permissions:
      contents: read
      packages: write

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

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

    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=ref,event=pr
          type=sha,prefix={{branch}}-

    - name: Build and push Docker image
      uses: docker/build-push-action@v5
      with:
        context: ./app
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}

  deploy:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    environment: production
    
    steps:
    - name: Deploy to production
      uses: appleboy/ssh-action@v1.0.3
      with:
        host: ${{ secrets.PROD_HOST }}
        username: ${{ secrets.PROD_USERNAME }}
        key: ${{ secrets.PROD_SSH_KEY }}
        script: |
          cd /opt/stiftung
          git pull origin main
          docker compose -f docker-compose.prod.yml pull
          docker compose -f docker-compose.prod.yml up -d
          docker compose -f docker-compose.prod.yml exec web python manage.py migrate
          docker compose -f docker-compose.prod.yml exec web python manage.py collectstatic --noinput
