name: Build Go/Fyne App for macOS ARM64

on:
  push:
    branches: [ main, master ]
  pull_request:
    branches: [ main, master ]

jobs:
  build:
    runs-on: macos-latest
    
    steps:
    - uses: actions/checkout@v4
    
    - name: Set up Go
      uses: actions/setup-go@v5
      with:
        go-version: '1.21'
    
    - name: Install Fyne dependencies
      run: |
        # Install required system dependencies for Fyne
        brew install pkg-config
    
    - name: Install Fyne CLI
      run: go install fyne.io/fyne/v2/cmd/fyne@latest
    
    - name: Set environment variables
      run: |
        echo "CGO_ENABLED=1" >> $GITHUB_ENV
        echo "GOOS=darwin" >> $GITHUB_ENV
        echo "GOARCH=arm64" >> $GITHUB_ENV
    
    - name: Cache Go modules
      uses: actions/cache@v4
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    
    - name: Download dependencies
      run: go mod download
    
    - name: Verify dependencies
      run: go mod verify
    
    - name: Check code formatting
      run: |
        if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
          echo "Code is not properly formatted. Please run 'gofmt -s -w .'"
          gofmt -s -l .
          exit 1
        fi
    
    - name: Run go vet
      run: go vet ./...
    
    - name: Run tests
      run: go test -v ./...
    
    - name: Build application
      run: make build
    
    - name: Verify build output
      run: |
        ls -la TurtleSilicon.app/
        ls -la TurtleSilicon.app/Contents/Resources/
    
    - name: Upload build artifact
      uses: actions/upload-artifact@v4
      with:
        name: TurtleSilicon-macos
        path: TurtleSilicon.app/
        retention-days: 30
