name: Release

on:
  workflow_dispatch:

permissions:
  contents: write
  packages: write

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v6
    
    - name: Set up Go
      uses: actions/setup-go@v6
      with:
        go-version: '1.25'
    
    - name: Extract version
      id: version
      run: |
        echo "tag=$(cat VERSION)" >> $GITHUB_OUTPUT
    
    - name: Build binaries
      run: |
        VERSION=$(cat VERSION)
        
        # Linux AMD64
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
          -trimpath \
          -ldflags="-s -w -X main.version=$VERSION" \
          -o prism-linux-amd64 .
        
        # Linux ARM64
        CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build \
          -trimpath \
          -ldflags="-s -w -X main.version=$VERSION" \
          -o prism-linux-arm64 .
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Login to GitHub Container Registry
      uses: docker/login-action@v3
      with:
        registry: ghcr.io
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Build and push Docker image
      uses: docker/build-push-action@v6
      with:
        context: .
        platforms: linux/amd64,linux/arm64
        push: true
        build-args: |
          VERSION=${{ steps.version.outputs.tag }}
        tags: |
          ghcr.io/lone-cloud/prism:${{ steps.version.outputs.tag }}
          ghcr.io/lone-cloud/prism:latest
    
    - name: Create GitHub Release
      uses: softprops/action-gh-release@v2
      with:
        tag_name: v${{ steps.version.outputs.tag }}
        name: Release ${{ steps.version.outputs.tag }}
        draft: false
        prerelease: false
        generate_release_notes: true
        files: |
          prism-linux-amd64
          prism-linux-arm64
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
