#!/usr/bin/env bun

import { $ } from "bun"

const PI_HOST = "yellow-phone.local"
const PI_DIR = "/home/corey/yellow-phone"

// Parse command line arguments
const shouldBootstrap = process.argv.includes("--bootstrap")

console.log(`🧪 Run basic tests...\n`)

await $`bun run typecheck`

console.log(`📦 Deploying to ${PI_HOST}...\n`)

// Create directory on Pi
console.log("Creating directory on Pi...")
await $`ssh ${PI_HOST} "mkdir -p ${PI_DIR}"`

// Sync files from . directory to Pi (only transfers changed files)
console.log("Syncing files from . directory...")
await $`rsync -avz --delete  --exclude-from='../.gitignore' --exclude='.git' . ${PI_HOST}:${PI_DIR}/`

// Make all TypeScript files executable
console.log("Making scripts executable...")
await $`ssh ${PI_HOST} "chmod +x ${PI_DIR}/scripts/*"`

console.log("\n✓ Files deployed!\n")

// Run bootstrap if requested
if (shouldBootstrap) {
  console.log("Running bootstrap on Pi...\n")
  await $`ssh ${PI_HOST} "cd ${PI_DIR} && sudo bun bootstrap.ts ${PI_DIR}"`
}

// Always check if services exist and restart them (whether we bootstrapped or not)
console.log("Checking for existing services...")
const apServiceExists = await $`ssh ${PI_HOST} "systemctl is-enabled phone-ap.service"`
  .nothrow()
  .quiet()
const webServiceExists = await $`ssh ${PI_HOST} "systemctl is-enabled phone-web.service"`
  .nothrow()
  .quiet()

if (apServiceExists.exitCode === 0 && webServiceExists.exitCode === 0) {
  console.log("Restarting services...")
  await $`ssh ${PI_HOST} "sudo systemctl restart phone-ap.service phone-web.service"`
  console.log("✓ Services restarted\n")
} else if (!shouldBootstrap) {
  console.log("Services not installed. Run with --bootstrap to install.\n")
}

console.log(`
✓ Deploy complete!

Access via WiFi at http://yellow-phone.local
The Pi is discoverable as "yellow-phone-setup"
`)
