import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { WifiScanService } from "../services/wifiScanService";

const wifiScanService = container.resolve(WifiScanService);
const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
  try {
    const wifiScans = await wifiScanService.getAllWifiScans();
    res.status(200).json(wifiScans);
  } catch (error) {
    res.status(500).json({ error: "Error retrieving wifi scans" });
  }
});

router.get("/:id", async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const wifiScan = await wifiScanService.getWifiScanById(id);
    if (!wifiScan) {
      res.status(404).json({ error: "Wifi scan not found" });
      return;
    }
    res.status(200).json(wifiScan);
  } catch (error) {
    res.status(500).json({ error: "Error retrieving wifi scan" });
  }
});

router.post("/", async (req: Request, res: Response) => {
  try {
    const newWifiScan = await wifiScanService.createWifiScan(req.body);
    res.status(201).json(newWifiScan);
  } catch (error) {
    res.status(500).json({ error: "Error creating wifi scan" });
  }
});

router.put("/:id", async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const updatedWifiScan = await wifiScanService.updateWifiScan({
      ...req.body,
      wifi_scan_id: id,
    });
    if (!updatedWifiScan) {
      res.status(404).json({ error: "Wifi scan not found" });
      return;
    }
    res.status(200).json(updatedWifiScan);
  } catch (error) {
    res.status(500).json({ error: "Error updating wifi scan" });
  }
});

router.delete("/:id", async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const deleted = await wifiScanService.deleteWifiScan(id);
    if (!deleted) {
      res.status(404).json({ error: "Wifi scan not found" });
      return;
    }
    res.status(204).send();
  } catch (error) {
    res.status(500).json({ error: "Error deleting wifi scan" });
  }
});

router.delete("/:id", async (req: Request, res: Response) => {});

export default router;