name: Build and Deploy

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Setup .NET 8 SDK
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: '8.0.x'

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --configuration Release --no-restore

    - name: Test (optional)
      run: dotnet test --no-build --verbosity normal

    - name: Publish
      run: dotnet publish -c Release -o out

    - name: Build Docker Image
      run: docker build -t watcher:latest .

    - name: Docker Login
      run: echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin

    - name: Push Docker Image
      run: docker push watcher:latest

    - name: Deploy (Beispiel - via SSH)
      run: |
        ssh daniel-hbn@git.triggermeelmo.com "docker pull deine-app:latest && docker stop watcher || true && docker rm watcher || true && docker run -d --name watcher -p 5000:5000 deine-app:latest"
      env:
        SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
