#!/bin/bash
# Step 14: Install screen brightness daemon (ambient light + buzzer-triggered alert)
# Regulatory: IMO MSC.191(79), MSC.302(87), IHO S-52 / IEC 62288

step_14_screen_brightness() {
  local script_dst="/usr/local/bin/screen-brightness.py"
  local service_dst="/etc/systemd/system/screen-brightness.service"
  local conf_dst="/etc/screen-brightness.conf"

  log "Downloading screen-brightness.py ..."
  curl -fsSL "${FILE_SERVER}/screen-brightness.py" -o "$script_dst" \
    || { log "ERROR: could not download screen-brightness.py"; return 1; }
  chmod 755 "$script_dst"
  log "Installed $script_dst"

  log "Downloading screen-brightness.service ..."
  curl -fsSL "${FILE_SERVER}/screen-brightness.service" -o "$service_dst" \
    || { log "ERROR: could not download screen-brightness.service"; return 1; }
  chmod 644 "$service_dst"
  log "Installed $service_dst"

  # Deploy default config only if one doesn't already exist on the device
  # (preserves any vessel-specific tuning done post-provisioning)
  if [[ ! -f "$conf_dst" ]]; then
    log "Downloading screen-brightness.conf (default) ..."
    curl -fsSL "${FILE_SERVER}/screen-brightness.conf" -o "$conf_dst" \
      || log "WARNING: could not download screen-brightness.conf (daemon uses built-in defaults)"
    [[ -f "$conf_dst" ]] && chmod 644 "$conf_dst" && log "Installed $conf_dst"
  else
    log "Config $conf_dst already exists — keeping vessel settings"
  fi

  # Create the runtime dir; tmpfiles.d rule ensures it re-appears after reboot
  mkdir -p /run/screen-brightness
  cat > /etc/tmpfiles.d/screen-brightness.conf <<'TMPFILES'
d /run/screen-brightness 0755 root root -
TMPFILES

  # Brightness API (localhost only) so overlay can set override without root
  log "Downloading brightness-api.py ..."
  curl -fsSL "${FILE_SERVER}/brightness-api.py" -o /usr/local/bin/brightness-api.py \
    && chmod 755 /usr/local/bin/brightness-api.py \
    && log "Installed /usr/local/bin/brightness-api.py" || true
  if curl -fsSL "${FILE_SERVER}/brightness-api.service" -o /etc/systemd/system/brightness-api.service 2>/dev/null; then
    chmod 644 /etc/systemd/system/brightness-api.service
    systemctl enable brightness-api.service
    log "brightness-api.service enabled"
  fi

  # Brightness overlay (crew manual control on top of kiosk)
  if curl -fsSL "${FILE_SERVER}/brightness-overlay.py" -o "$PI_HOME/brightness-overlay.py" 2>/dev/null; then
    chmod 755 "$PI_HOME/brightness-overlay.py"
    chown "$PI_USER:$PI_USER" "$PI_HOME/brightness-overlay.py"
    if curl -fsSL "${FILE_SERVER}/brightness-overlay-launch.sh" -o "$PI_HOME/brightness-overlay-launch.sh" 2>/dev/null; then
      chmod 755 "$PI_HOME/brightness-overlay-launch.sh"
      chown "$PI_USER:$PI_USER" "$PI_HOME/brightness-overlay-launch.sh"
    fi
    if curl -fsSL "${FILE_SERVER}/brightness-overlay.desktop" -o /tmp/br-overlay.desktop 2>/dev/null; then
      sed "s|/home/pi|$PI_HOME|g" /tmp/br-overlay.desktop > "$AUTOSTART/brightness-overlay.desktop"
      chown "$PI_USER:$PI_USER" "$AUTOSTART/brightness-overlay.desktop"
      chmod 644 "$AUTOSTART/brightness-overlay.desktop"
      rm -f /tmp/br-overlay.desktop
      log "brightness-overlay installed (autostart)"
    fi
  fi

  systemctl daemon-reload
  systemctl enable screen-brightness.service
  log "screen-brightness.service enabled (starts on next boot)"
}
