name: Build and Push Docker Image

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  IMAGE_NAME: karaokepedia

jobs:
  build:
    runs-on: Agent
    steps:
      - name: Build Docker image
        run: |
          echo "Building Docker image..."
          docker build -t karaokepedia:latest .
          docker tag karaokepedia:latest karaokepedia:${{ github.sha }}
          echo "✅ Image built successfully"
      
      - name: Log in to Docker Hub
        run: |
          echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
      
      - name: Push Docker image
        run: |
          # Push with registry prefix if secrets are configured
          if [ -n "${{ secrets.DOCKER_USERNAME }}" ]; then
            docker tag karaokepedia:latest ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:latest
            docker tag karaokepedia:latest ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:main-${{ github.sha }}
            docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:latest
            docker push ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}:main-${{ github.sha }}
            echo "✅ Pushed to Docker Hub: ${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME }}"
          else
            echo "⚠️ Docker Hub credentials not configured, skipping push"
          fi
