name: Build and Deploy ASP.NET API

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: localACDC
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: '8.0.x' # ou sua versão
        
    - name: Restore dependencies
      run: dotnet restore 
      
    - name: Build application
      run: dotnet build --configuration Release --no-restore
      
    - name: Run tests (opcional)
      run: dotnet test --no-build --verbosity normal
      
    - name: Publish application
      run: dotnet publish --configuration Release --output ./publish

    # - name: Build Docker image for ARM64
    #   run: |
    #     # Criar builder se não existir
    #     docker buildx create --name arm-builder --use --platform linux/arm64 || docker buildx use arm-builder
        
    #     # Build específico para ARM64
    #     docker buildx build \
    #       --platform linux/arm64 \
    #       --tag registry.redecarneir.us/ytextractor:latest \
    #       --tag registry.redecarneir.us/ytextractor:${{ github.sha }} \
    #       -f YTExtractor/Dockerfile \
    #       --push .

    # - name: Build Docker image 1
    #   run: |
    #     docker build -t ytextractor:${{ github.sha }} -f YTExtractor/Dockerfile .
    #     docker tag ytextractor:${{ github.sha }} registry.redecarneir.us/ytextractor:latest
    #     docker tag ytextractor:${{ github.sha }} registry.redecarneir.us/ytextractor:${{ github.sha }}
        
    # - name: Push to registry
    #   run: |
    #     COMMIT_SHA=$(git rev-parse --short HEAD)
    #     docker push registry.redecarneir.us/ytextractor:latest
    #     docker push registry.redecarneir.us/ytextractor:${{ github.sha }}
        
    # - name: Deploy to remote VPS
    #   run: |
    #     ssh -o StrictHostKeyChecking=no ubuntu@137.131.63.61 << 'EOF'
    #       # Pull da nova imagem
    #       docker pull registry.redecarneir.us/ytextractor:latest
          
    #       # Parar container atual se existir
    #       docker stop ytextractor-api || true
    #       docker rm ytextractor-api || true
          
    #       # Rodar novo container em produção
    #       # Porta 80 interna (do container) mapeada para 5000 externa
    #       docker run -d \
    #         --name ytextractor-api \
    #         --restart unless-stopped \
    #         -p 5000:80 \
    #         -e ASPNETCORE_ENVIRONMENT=Production \
    #         -v /tmp/ytextractor:/app/temp \
    #         registry.redecarneir.us/ytextractor:latest
            
    #       # Verificar se está rodando
    #       sleep 5
    #       docker ps | grep ytextractor-api
          
    #       # Limpeza de imagens antigas (opcional)
    #       docker image prune -f
    #     EOF

    - name: Deploy and build on VPS
      run: |
        # Enviar código para VPS
        rsync -avz --delete \
          --exclude '.git' \
          --exclude 'bin' \
          --exclude 'obj' \
          ./ ubuntu@137.131.63.61:~/ytextractor/
          
        # Build no VPS (que já é ARM64)
        ssh ubuntu@137.131.63.61 << 'EOF'
          cd ~/ytextractor
          docker build -t registry.redecarneir.us/ytextractor:latest -f YTExtractor/Dockerfile .
          docker stop ytextractor-api || true
          docker rm ytextractor-api || true
          docker run -d \
            --name ytextractor-api \
            --restart unless-stopped \
            -p 8082:8080 \
            -e ASPNETCORE_ENVIRONMENT=Production \
            -v /tmp/ytextractor:/app/temp \
            registry.redecarneir.us/ytextractor:latest
        EOF
        
    - name: Verify deployment
      run: |
        ssh ubuntu@137.131.63.61 'docker ps | grep ytextractor-api'
        echo "✅ Deploy completed successfully!"