name: Build MIPS Binary

on:
  push:
    branches: [ "main" ]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Debug - List Files (Pre-Build)
      run: ls -R

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'

    - name: Install Rust
      uses: dtolnay/rust-toolchain@nightly
      with:
        targets: wasm32-unknown-unknown
        components: rust-src

    - name: Rust Cache
      uses: Swatinem/rust-cache@v2

    - name: Install Trunk
      uses: jetli/trunk-action@v0.5.0
      with:
        version: 'latest'

    - name: Build Frontend
      run: |
        cd frontend
        npm install
        trunk build --release

    - name: Install Cross
      run: cargo install cross

    - name: Build Backend (MIPS)
      env:
        RUSTUP_TOOLCHAIN: nightly
        CROSS_NO_WARNINGS: 0
      run: |
        cd backend
        cross build --target mips-unknown-linux-musl --release -Z build-std=std,panic_abort

    - name: Debug - List Files
      run: |
        echo "Listing target directory..."
        find target -maxdepth 5 || true
        ls -R target/mips-unknown-linux-musl/release || true

    - name: Upload Binary
      uses: actions/upload-artifact@v4
      with:
        name: vibetorrent-mips
        path: target/mips-unknown-linux-musl/release/backend
