name: GBCI

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

jobs:
  build:
    runs-on: gbo

    steps:
      - name: Disable SSL verification (temporary)
        run: git config --global http.sslVerify false

      - uses: actions/checkout@v4

      - name: Clone botlib dependency
        run: git clone --depth 1 https://github.com/GeneralBots/botlib.git ../botlib

      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-botui-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-botui-

      - name: Install Rust
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          echo "/root/.cargo/bin" >> $GITHUB_PATH

      - name: Setup environment
        run: |
          sudo cp /opt/gbo/bin/system/botui.env .env

      - name: Build Linux x86_64
        run: /root/.cargo/bin/cargo build --locked --release

      - name: Prepare release artifacts
        run: |
          sudo mkdir -p /opt/gbo/releases/botui/linux-x86_64
          sudo cp ./target/release/botui /opt/gbo/releases/botui/linux-x86_64/ || true
          sudo chmod -R 755 /opt/gbo/releases/botui/

      - name: Deploy and restart local service
        run: |
          lxc exec bot:pragmatismo-system -- systemctl stop botui

          sudo cp ./target/release/botui /opt/gbo/bin/botui
          sudo chmod +x /opt/gbo/bin/botui

          lxc exec bot:pragmatismo-system -- systemctl start botui
