# Maintainer: NixOrb Contributors
# Repo: https://github.com/minerofthesoal/nixorb
pkgname=nixorb
pkgver=0.01.0.01
pkgrel=1
pkgdesc="Floating AI assistant orb for Arch Linux — KDE Plasma 6 Wayland"
arch=('x86_64')
url="https://github.com/minerofthesoal/nixorb"
license=('MIT')
install=nixorb.install
depends=(
    'python>=3.12'
    'python-pyside6'
    'qt6-base'
    'qt6-declarative'
    'qt6-wayland'
    'qt6-multimedia'
    'portaudio'
    'wl-clipboard'
    'grim'
    'ffmpeg'
)
optdepends=(
    'cuda: NVIDIA GPU acceleration'
    'cudnn: cuDNN for faster-whisper'
    'kglobalacceld: Native Wayland hotkeys (AUR)'
    'bubblewrap: Sandboxed command execution'
    'piper-tts: Offline TTS (pip install piper-tts)'
    'openwakeword: Wake-word detection (pip install openwakeword)'
)
makedepends=(
    'python-build'
    'python-installer'
    'python-wheel'
    'python-hatchling'
    'qt6-tools'
    'python-pip'
)
source=("nixorb-${pkgver}.tar.gz::https://github.com/minerofthesoal/nixorb/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('SKIP')

build() {
    cd "nixorb-${pkgver}"

    # Compile QML shaders
    local QSB=/usr/lib/qt6/bin/qsb
    if [[ -x "$QSB" ]]; then
        echo "==> Compiling shaders..."
        "$QSB" --glsl "100es,120,150" --hlsl 50 --msl 12             assets/shaders/orb_glow.vert -o assets/shaders/orb_glow.vert.qsb
        "$QSB" --glsl "100es,120,150" --hlsl 50 --msl 12             assets/shaders/orb_glow.frag -o assets/shaders/orb_glow.frag.qsb
    else
        echo "WARNING: qsb not found at $QSB. Install qt6-tools."
        touch assets/shaders/orb_glow.vert.qsb assets/shaders/orb_glow.frag.qsb
    fi

    # Build Python wheel using hatchling
    python -m build --wheel --no-isolation

    # Pre-bundle pip-only dependencies
    pip install         --quiet         --no-warn-script-location         --target="${srcdir}/pip_bundle"         faster-whisper         openai         chromadb         aiohttp         qasync         "pydantic>=2.7"         pydantic-settings         tomli-w         "typer>=0.12"         rich         pygments         cryptography         aiofiles         pynput         hypernix         "huggingface-hub>=0.23"         sounddevice         soundfile         numpy
}

package() {
    cd "nixorb-${pkgver}"

    # Install wheel
    python -m installer --destdir="${pkgdir}" --prefix=/usr dist/*.whl

    # Install bundled pip deps into site-packages
    local site="${pkgdir}/usr/lib/python3.12/site-packages"
    mkdir -p "${site}"
    cp -r "${srcdir}/pip_bundle/." "${site}/"
    rm -rf "${site}/bin" 2>/dev/null || true

    # Assets
    install -dm755 "${pkgdir}/usr/share/nixorb"
    cp -r assets "${pkgdir}/usr/share/nixorb/"
    cp -r config "${pkgdir}/usr/share/nixorb/"

    # Icons
    install -Dm644 assets/icons/nixorb_256.png         "${pkgdir}/usr/share/icons/hicolor/256x256/apps/nixorb.png"
    install -Dm644 assets/icons/nixorb_48.png         "${pkgdir}/usr/share/icons/hicolor/48x48/apps/nixorb.png"
    install -Dm644 assets/nixorb.svg         "${pkgdir}/usr/share/icons/hicolor/scalable/apps/nixorb.svg"

    # Desktop entry
    install -Dm644 packaging/nixorb.desktop         "${pkgdir}/usr/share/applications/nixorb.desktop"

    # License
    install -Dm644 LICENSE         "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

    # Default config
    install -Dm644 config/default.toml         "${pkgdir}/usr/share/nixorb/default.toml"
}
