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 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: Build Docker image 2
      run: |
        echo "Current directory: $(pwd)"
        echo "Files in current directory:"
        ls -la
        
        COMMIT_SHA=$(git rev-parse --short HEAD)
        
        # O Dockerfile está em YTExtractor/YTExtractor/ baseado na estrutura mostrada
        if [ -f "YTExtractor/YTExtractor/Dockerfile" ]; then
          echo "✅ Found Dockerfile in YTExtractor/YTExtractor/ directory"
          cd YTExtractor/YTExtractor
          echo "Building Docker image from project directory..."
          echo "Files in this directory:"
          ls -la
          docker build -t ytextractor:$COMMIT_SHA .
        elif [ -f "YTExtractor/Dockerfile" ]; then
          echo "✅ Found Dockerfile in YTExtractor/ directory"
          cd YTExtractor
          docker build -t ytextractor:$COMMIT_SHA .
        else
          echo "❌ Dockerfile not found!"
          echo "Searching for Dockerfile..."
          find . -name "Dockerfile" -type f
          exit 1
        fi
        
        # Tag das imagens (voltar para raiz se necessário)
        cd /workspace/ricardo/YTExtractor
        docker tag ytextractor:$COMMIT_SHA registry.redecarneir.us/ytextractor:latest
        docker tag ytextractor:$COMMIT_SHA registry.redecarneir.us/ytextractor:$COMMIT_SHA
        
        echo "✅ Docker images built successfully"
        docker images | grep ytextractor
        
    - 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:$COMMIT_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: Verify deployment
      run: |
        ssh ubuntu@137.131.63.61 'docker ps | grep ytextractor-api'
        echo "✅ Deploy completed successfully!"