name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  check:
    name: Type Check & Lint
    runs-on: ubuntu-latest
    container:
      image: code.foss.global/host.today/ht-docker-node:latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Deno
        uses: denoland/setup-deno@v1
        with:
          deno-version: v2.x

      - name: Install dependencies
        run: deno install --entrypoint mod.ts

      - name: Check TypeScript types
        run: deno check mod.ts

      - name: Lint code
        run: deno lint
        continue-on-error: true

      - name: Format check
        run: deno fmt --check
        continue-on-error: true

  build:
    name: Build Test (Current Platform)
    runs-on: ubuntu-latest
    container:
      image: code.foss.global/host.today/ht-docker-node:latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Deno
        uses: denoland/setup-deno@v1
        with:
          deno-version: v2.x

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Enable corepack
        run: corepack enable

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Compile for current platform
        run: |
          echo "Testing compilation for Linux x86_64..."
          npx tsdeno compile --allow-all --no-check \
            --output onebox-test \
            --target x86_64-unknown-linux-gnu mod.ts

      - name: Test binary execution
        run: |
          chmod +x onebox-test
          ./onebox-test --version
          ./onebox-test --help

  build-all:
    name: Build All Platforms
    runs-on: ubuntu-latest
    container:
      image: code.foss.global/host.today/ht-docker-node:latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Deno
        uses: denoland/setup-deno@v1
        with:
          deno-version: v2.x

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Enable corepack
        run: corepack enable

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Compile all platform binaries
        run: mkdir -p dist/binaries && npx tsdeno compile

      - name: Upload all binaries as artifact
        uses: actions/upload-artifact@v3
        with:
          name: onebox-binaries.zip
          path: dist/binaries/*
          retention-days: 30
