name: Build the Jar

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

jobs:
  build:
    runs-on: ubuntu-24.04

    steps:
    - uses: actions/checkout@v4

    - name: Set up JDK 20
      uses: actions/setup-java@v5
      with:
        java-version: '20'
        distribution: 'temurin'

    - name: Create output directories
      run: |
        mkdir -p out/production/Calc
        mkdir -p dist

    - name: Compile Java files
      run: |
        javac -encoding UTF-8 -cp "lib/flatlaf-3.7.jar" -d out/production/Calc \
          $(find src/main/java -name "*.java")

    - name: Copy resources
      run: |
        if [ -d "src/main/resources" ]; then
          cp -r src/main/resources/* out/production/Calc/ 2>/dev/null || true
        fi

    - name: Extract FlatLaf library
      run: |
        cd out/production/Calc
        jar xf ../../../lib/flatlaf-3.7.jar
        # Remove META-INF to avoid conflicts
        rm -rf META-INF
        cd ../../..

    - name: Create manifest
      run: |
        echo "Manifest-Version: 1.0" > manifest.txt
        echo "Main-Class: dev.sillyangel.calc.Calculator" >> manifest.txt

    - name: Create fat JAR
      run: |
        cd out/production/Calc
        jar cfm ../../../dist/CalcShortforCalculator.jar ../../../manifest.txt .
        cd ../../..

    - name: Upload JAR artifact
      uses: actions/upload-artifact@v3
      with:
        name: CalcShortforCalculator
        path: dist/CalcShortforCalculator.jar
