name: Build and Deploy Hugo to AWS Amplify

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'

      - name: Build Hugo site
        run: hugo build --minify

      - name: Create deployment zip
        run: |
          cd public
          zip -r ../deployment.zip .
          cd ..

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

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: eu-central-1

      - name: Upload zip to S3 and deploy to Amplify
        run: |
          ZIP_NAME="deployment-$(git rev-parse --short HEAD)-$(date +%s).zip"
          aws s3 cp deployment.zip s3://${{ secrets.S3_BUCKET_NAME }}/$ZIP_NAME
          aws amplify start-deployment \
            --app-id dkm4p8nhpq96d \
            --branch-name prod \
            --source-url s3://${{ secrets.S3_BUCKET_NAME }}/$ZIP_NAME

