name: Build and Release

on:
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch:
    inputs:
      build_only:
        description: 'Build only (no upload to PyPI)'
        required: false
        default: 'true'
        type: boolean

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: "3.11"

    - name: Install build dependencies
      run: |
        python -m pip install --upgrade pip
        pip install build twine

    - name: Build package
      run: python -m build

    - name: Check distribution
      run: twine check dist/*

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: dist-artifacts
        path: dist/

  test-install:
    needs: build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.9", "3.10", "3.11", "3.12"]
    steps:
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - name: Download build artifacts
      uses: actions/download-artifact@v4
      with:
        name: dist-artifacts
        path: dist/

    - name: Test wheel installation
      run: |
        pip install dist/*.whl
        python -c "import smdpfier; print('SMDPfier installed successfully')"

  # This job would publish to PyPI in a real release
  # For now, it just validates the process without uploading
  validate-release:
    needs: [build, test-install]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    steps:
    - name: Download build artifacts
      uses: actions/download-artifact@v4
      with:
        name: dist-artifacts
        path: dist/

    - name: Validate release artifacts
      run: |
        echo "Release validation complete for tag: ${{ github.ref_name }}"
        echo "Built artifacts:"
        ls -la dist/
        echo "These artifacts would be published to PyPI in a production release"
