import { Router } from 'express';
import { getConfig, setRaspberryHost, setRaspberryPort, setToken } from '../config.js';

const router = Router();

router.get('/', (_req, res) => {
  const settings = getConfig();
  res.json(settings);
});

router.put('/', (req, res) => {
  const { raspberryPi, token } = req.body;
  if (raspberryPi?.host !== undefined) {
    setRaspberryHost(raspberryPi.host);
  }
  if (raspberryPi?.port !== undefined) {
    setRaspberryPort(Number(raspberryPi.port));
  }
  if (token !== undefined) {
    setToken(token);
  }
  res.json(getConfig());
});

export default router;
