name: Continuous Integration / Continuous Deployment

on:
  pull_request:
    branches:
      - main
    paths:
      - "**.py"
  push:
    branches:
      - main
    paths:
      - "**.py"

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: [3.13]

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r server/src/requirements.txt

      - name: Configure AWS Credentials for Tests
        run: |
          echo "Setting AWS region and mock credentials..."
          export AWS_REGION=us-east-1
          export AWS_ACCESS_KEY_ID=fake_access_key
          export AWS_SECRET_ACCESS_KEY=fake_secret_key
          export AWS_DEFAULT_REGION=us-east-1

      - name: Run tests with coverage
        run: |
          export PYTHONPATH=$(pwd)/server/src
          pytest --cov=src/functions --cov=server/src --cov-report=term-missing --cov-report=xml --cov-report=html

      - name: Upload coverage report (HTML)
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report-html
          path: htmlcov/

      - name: Upload coverage report (XML)
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report-xml
          path: coverage.xml

      - name: Show coverage summary
        run: cat coverage.xml


  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'

    strategy:
      matrix:
        function_name: [fetch_permanent_data, fetch_transient_data, return_all_coordinates, return_historical_data, return_luas_data, return_permanent_data, return_station_data, return_transient_data, update_average_punctuality, return_punctuality_by_objectID, return_punctuality_by_timestamp]

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

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.13'

      - name: Check AWS CLI version
        run: aws --version || echo "AWS CLI not found"

      - name: Update AWS CLI
        run: |
          curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
          unzip -o awscliv2.zip
          sudo ./aws/install --update

      - name: Verify AWS CLI installation
        run: aws --version

      - name: Configure AWS CLI
        run: |
          aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws configure set region ${{ secrets.AWS_REGION }}

      - name: Install dependencies for Lambda function
        run: |
          mkdir -p package/
          pip install -r server/src/requirements.txt -t package/
          cp -r package/* server/src/functions/${{ matrix.function_name }}/

      - name: Zip Lambda function
        run: |
          cd server/src/functions/${{ matrix.function_name }}/
          zip -r ../../../../${{ matrix.function_name }}.zip . -x "*.git*" "*tests*" "*.github*" "*README.md*" "requirements.txt"

      - name: Deploy to AWS Lambda
        run: |
          aws lambda update-function-code --function-name ${{ matrix.function_name }} --zip-file fileb://${{ matrix.function_name }}.zip
