name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build and Release
    runs-on: ubuntu-latest

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

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.23'

      - name: Get version
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Build for all platforms
        run: |
          VERSION=${{ steps.version.outputs.VERSION }}
          COMMIT=${{ github.sha }}
          COMMIT_SHORT=${COMMIT:0:10}
          BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
          LDFLAGS="-s -w -X main.Version=${VERSION} -X main.GitCommit=${COMMIT_SHORT} -X main.BuildTime=${BUILD_TIME}"

          # Linux amd64
          GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-linux-amd64 ./cmd/drip

          # Linux arm64
          GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-linux-arm64 ./cmd/drip

          # macOS amd64
          GOOS=darwin GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-darwin-amd64 ./cmd/drip

          # macOS arm64
          GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-darwin-arm64 ./cmd/drip

          # Windows amd64
          GOOS=windows GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-windows-amd64.exe ./cmd/drip

          # Windows arm64
          GOOS=windows GOARCH=arm64 go build -ldflags="${LDFLAGS}" -o drip-${VERSION}-windows-arm64.exe ./cmd/drip

      - name: Generate checksums
        run: |
          sha256sum drip-${{ steps.version.outputs.VERSION }}-* > checksums.txt

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            drip-${{ steps.version.outputs.VERSION }}-linux-amd64
            drip-${{ steps.version.outputs.VERSION }}-linux-arm64
            drip-${{ steps.version.outputs.VERSION }}-darwin-amd64
            drip-${{ steps.version.outputs.VERSION }}-darwin-arm64
            drip-${{ steps.version.outputs.VERSION }}-windows-amd64.exe
            drip-${{ steps.version.outputs.VERSION }}-windows-arm64.exe
            checksums.txt
          draft: false
          prerelease: false
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
