#!/usr/bin/env bash
set -e

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
export PATH="$PATH:$(go env GOPATH)/bin"

# ── 1. Build backend-go ────────────────────────────────────────────────────────
echo "[1/4] Building backend-go..."
cd "$ROOT/backend-go"
go build ./...
echo "      ✓ backend-go"

# ── 2. Build desktop (Go) ──────────────────────────────────────────────────────
echo "[2/4] Building desktop..."
cd "$ROOT/desktop"
go build ./...
echo "      ✓ desktop"

# ── 3. Regenerate Wails JS bindings ───────────────────────────────────────────
echo "[3/4] Regenerating Wails bindings..."
cd "$ROOT/desktop"
wails generate module
echo "      ✓ wailsjs"

# ── 4. Install & type-check frontend ──────────────────────────────────────────
echo "[4/4] Checking frontend..."
cd "$ROOT/frontend-react"
npm install --silent
npx tsc -b --noEmit
echo "      ✓ frontend"

# ── Start dev ─────────────────────────────────────────────────────────────────
echo ""
echo "Starting Wails dev server..."
cd "$ROOT/desktop"
wails dev
