/**
 * Support FAQs API
 * GET - List published FAQs (public endpoint)
 *
 * Convex-first. Falls back to empty array if Convex unavailable.
 */

import { NextRequest, NextResponse } from "next/server";
import { logger } from "@/lib/logger";
import { convexQuery } from "@/lib/convex-client";

// GET - List published FAQs (public endpoint)
export async function GET(request: NextRequest) {
  try {
    // Convex-first
    try {
      const { searchParams: sp } = new URL(request.url);
      const category = sp.get("category") ?? undefined;
      const faqs = await convexQuery("support/queries:getFAQs", {
        category,
      });
      if (faqs !== null) {
        return NextResponse.json({ faqs, success: true });
      }
    } catch (convexErr) {
      logger.warn("Convex FAQs query failed", convexErr);
    }

    // Fallback — return empty
    return NextResponse.json({ faqs: [], success: true });
  } catch (error) {
    logger.error("Failed to fetch FAQs", error);
    return NextResponse.json(
      { error: "Failed to fetch FAQs" },
      { status: 500 },
    );
  }
}
