stages:
  - build-web
  - build-desktop

variables:
  npm_config_cache: "$CI_PROJECT_DIR/.npm"
  ELECTRON_CACHE: "$CI_PROJECT_DIR/.cache/electron"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - pwa/node_modules/
    - pwa/.npm/
    - pwa/.cache/

# Build the web application first
build:web:
  stage: build-web
  image: node:20-alpine
  script:
    - cd pwa
    - npm ci
    - npm run build
  artifacts:
    paths:
      - pwa/dist/
    expire_in: 1 hour
  rules:
    - if: '$CI_COMMIT_TAG'
  tags:
    - docker

# # Build Linux AppImage and .deb
# build:linux:
#   stage: build-desktop
#   image: electronuserland/builder:wine
#   dependencies:
#     - build:web
#   script:
#     - cd pwa
#     - npm ci
#     - npm run electron:build:linux
#     - echo "=== AppImage size ==="
#     - ls -lh dist-electron/*.AppImage
#     - du -h dist-electron/*.AppImage
#   artifacts:
#     paths:
#       - pwa/dist-electron/*.AppImage
#     expire_in: 1 week
#     name: "linux-appimage-$CI_COMMIT_TAG"
#   rules:
#     - if: '$CI_COMMIT_TAG'
#   tags:
#     - docker

# # Build Linux .deb (separate job to avoid size limits)
# build:linux-deb:
#   stage: build-desktop
#   image: electronuserland/builder:wine
#   dependencies:
#     - build:web
#   script:
#     - cd pwa
#     - npm ci
#     - npm run electron:build:linux
#   artifacts:
#     paths:
#       - pwa/dist-electron/*.deb
#     expire_in: 1 week
#     name: "linux-deb-$CI_COMMIT_TAG"
#   rules:
#     - if: '$CI_COMMIT_TAG'
#   tags:
#     - docker

# Build Windows executable
build:windows:
  stage: build-desktop
  image: electronuserland/builder:wine
  dependencies:
    - build:web
  script:
    - cd pwa
    - npm ci
    - npm run electron:build:win
    # Show file sizes
    - echo "=== Build artifacts ===" 
    - ls -lh dist-electron/*.exe || echo "No .exe files found"
    - echo "=== Total size ===" 
    - du -sh dist-electron/
  artifacts:
    paths:
      - pwa/dist-electron/*.exe
    expire_in: 1 week
    name: "windows-installer-$CI_COMMIT_TAG"
  rules:
    - if: '$CI_COMMIT_TAG'
  tags:
    - docker
