import { Router } from 'express';
import { raspiService } from '../services/raspi.service.js';

const router = Router();

router.post('/', async (req, res) => {
  const { task } = req.body;
  if (!task) {
    return res.status(400).json({ error: 'Task name is required' });
  }
  try {
    const result = await raspiService.runTask({ task });
    res.json(result);
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : 'Unknown error';
    res.status(500).json({ error: `Run task failed: ${message}` });
  }
});

export default router;
