/**
 * GET  /api/login-status              → list all sources + their cached
 *                                       last-check result
 * POST /api/login-status              → run all checks in parallel,
 *                                       return fresh results
 */

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

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

export async function POST() {
  const sources = listSources();
  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 });
}
