/**
 * Form Factors API
 * ─────────────────────────────────────────────────────────────────────────────
 * Routes:
 *   GET /api/form-factors        — All form factors with descriptions
 *   GET /api/form-factors/:name  — Single form factor by name (e.g. QSFP28)
 */

import { Router, Request, Response } from "express";
import { pool } from "../db/client";

export const formFactorsRouter = Router();

// GET /api/form-factors
formFactorsRouter.get("/", async (_req: Request, res: Response) => {
  try {
    const { rows } = await pool.query(`
      SELECT
        ff.*,
        COUNT(t.id)::int AS transceiver_count
      FROM form_factors ff
      LEFT JOIN transceivers t ON t.form_factor = ff.name
      GROUP BY ff.id
      ORDER BY
        CASE ff.status
          WHEN 'current'   THEN 1
          WHEN 'emerging'  THEN 2
          WHEN 'legacy'    THEN 3
          WHEN 'obsolete'  THEN 4
          ELSE 5
        END,
        ff.max_speed_gbps DESC NULLS LAST,
        ff.name
    `);
    res.json({ success: true, data: rows, total: rows.length });
  } catch (err) {
    console.error("GET /api/form-factors error:", err);
    res.status(500).json({ success: false, error: "Internal server error" });
  }
});

// GET /api/form-factors/:name
formFactorsRouter.get("/:name", async (req: Request, res: Response) => {
  try {
    const { rows } = await pool.query(`
      SELECT
        ff.*,
        COUNT(t.id)::int AS transceiver_count,
        COALESCE(
          json_agg(DISTINCT jsonb_build_object(
            'id', t.id,
            'part_number', t.part_number,
            'speed_gbps', t.speed_gbps,
            'fiber_type', t.fiber_type,
            'reach_label', t.reach_label
          )) FILTER (WHERE t.id IS NOT NULL),
          '[]'
        ) AS sample_transceivers
      FROM form_factors ff
      LEFT JOIN transceivers t ON t.form_factor = ff.name
      WHERE LOWER(ff.name) = LOWER($1)
      GROUP BY ff.id
    `, [req.params.name]);

    if (!rows.length) {
      return res.status(404).json({ success: false, error: "Form factor not found" });
    }
    res.json({ success: true, data: rows[0] });
  } catch (err) {
    console.error(`GET /api/form-factors/${req.params.name} error:`, err);
    res.status(500).json({ success: false, error: "Internal server error" });
  }
});
