name: Build Tamigo CLI

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

jobs:
  build-linux:
    name: Build Linux Binary
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Build
        run: pyinstaller --onefile --name tamigo-cli tamigo.py
      - name: Upload Linux Artifact
        uses: actions/upload-artifact@v3
        with:
          name: binary-linux
          path: dist/tamigo-cli

  build-windows:
    name: Build Windows Binary
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Build Windows executable via Docker (Manual Extraction)
        run: |
          CONTAINER_NAME="win-builder-${{ github.run_id }}"
          # 1. Create a container from a reliable Wine+Python 3.10 image
          docker create --name $CONTAINER_NAME cdrx/pyinstaller-windows
          
          # 2. Copy source code into the container
          docker cp . $CONTAINER_NAME:/src
          
          # 3. Run the build process manually inside the container
          # We upgrade pyinstaller and run the build
          docker start -a $CONTAINER_NAME || echo "Container finished"
          
          # 4. Extract the result
          # PyInstaller in this image puts output in /src/dist/windows/ by default
          mkdir -p final_dist
          docker cp $CONTAINER_NAME:/src/dist/windows/tamigo-cli.exe ./final_dist/tamigo-cli.exe || \
          docker cp $CONTAINER_NAME:/src/dist/tamigo-cli.exe ./final_dist/tamigo-cli.exe || \
          echo "Could not find EXE in standard locations"
          
          # 5. Cleanup
          docker rm $CONTAINER_NAME
          
      - name: Debug - Verify final file
        run: |
          ls -R final_dist/
          find . -name "*.exe"
          
      - name: Upload Windows Artifact
        uses: actions/upload-artifact@v3
        with:
          name: binary-windows
          path: final_dist/*.exe
