-- ═══════════════════════════════════════════════════════
-- DreamStack Mission Control — Source
-- ═══════════════════════════════════════════════════════
-- Simulates a spacecraft telemetry source that streams
-- signals to remote Mission Control dashboards.
--
-- Run:
--   Tab 1: cargo run -p ds-stream              (relay)
--   Tab 2: dreamstack stream examples/mission-source.ds --port 3000
--   Tab 3: dreamstack dev examples/mission-control.ds   (dashboard)

-- ── Telemetry Signals ──
let altitude  = 408
let velocity  = 7660
let fuel      = 87
let heartRate = 72
let oxygen    = 98
let temp      = 21
let status    = "nominal"
let crew      = ["Amir", "Nova", "Atlas"]
let logCount  = 0
let timestamp = 0

-- Computed signals
let orbitalPeriod = 90
let fuelStatus = "normal"

-- Stream telemetry via relay
stream telemetry on "ws://localhost:9100/peer/mission" { mode: signal, output: altitude, velocity, fuel, heartRate, oxygen, temp, status, crew, logCount, timestamp, orbitalPeriod, fuelStatus }

view source = column [
  text "🛰️ Mission Source — ISS Telemetry" { variant: "title" }
  text "Broadcasting to ws://localhost:9100/peer/mission" { variant: "subtitle" }

  row [
    column [
      text "── Orbital ──"
      text "Altitude: {altitude} km"
      text "Velocity: {velocity} m/s"
      row [
        button "Boost" { click: velocity += 100, variant: "primary" }
        button "Brake" { click: velocity -= 100, variant: "secondary" }
        button "Raise" { click: altitude += 10, variant: "primary" }
        button "Lower" { click: altitude -= 10, variant: "ghost" }
      ]
    ]
    column [
      text "── Life Support ──"
      text "❤️ {heartRate} bpm"
      text "🫁 {oxygen}%"
      text "🌡️ {temp}°C"
      row [
        button "Exercise" { click: heartRate += 15, variant: "primary" }
        button "Rest" { click: heartRate -= 10, variant: "ghost" }
      ]
    ]
  ]

  row [
    column [
      text "── Fuel ──"
      text "⛽ {fuel}%"
      row [
        button "Burn" { click: fuel -= 5, variant: "destructive" }
        button "Refuel" { click: fuel = 100, variant: "primary" }
      ]
    ]
    column [
      text "── Status ──"
      row [
        button "Nominal" { click: status = "nominal", variant: "primary" }
        button "Warning" { click: status = "warning", variant: "secondary" }
        button "Critical" { click: status = "critical", variant: "destructive" }
      ]
    ]
  ]

  text "── Crew EVA Log ──"
  text "Log entries: {logCount}"
  button "Log EVA" { click: logCount += 1, variant: "primary" }
]
