/**
 * GET  /api/web-sessions  → IdP-consolidated rows + cached statuses
 * POST /api/web-sessions  → re-probe every IdP in parallel
 *
 * Distinct from /api/login-status (per-connector): this endpoint is used
 * by the Enterprise badge "Web sessions" section, which wants one row
 * per IdP block rather than one row per SAML SP.
 */

import { NextResponse } from 'next/server';
import { listIdpSources, listIdpStatus, checkSource } from '@/lib/auth/login-status';

export async function GET() {
  return NextResponse.json({ rows: listIdpStatus() });
}

export async function POST() {
  const sources = listIdpSources();
  const results = await Promise.all(
    sources.map(async (s) => {
      try {
        const r = await checkSource(s);
        return { source: s, result: r };
      } catch (e) {
        return {
          source: s,
          result: {
            ok: false,
            message: `check error: ${(e as Error).message}`,
            checked_at: Date.now(),
            duration_ms: 0,
          },
        };
      }
    }),
  );
  return NextResponse.json({ rows: results });
}
