import { NextRequest, NextResponse } from 'next/server';
import { getSentinelOneSyncService } from '@/lib/services/sentinelone-sync-service';
import { postgresClient } from '@/lib/services/postgres-client';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json().catch(() => ({}));
    const triggeredBy = body.triggeredBy || 'manual';

    const svc = getSentinelOneSyncService();
    if (svc.isSyncInProgress()) {
      return NextResponse.json({ error: 'Sync already in progress' }, { status: 409 });
    }

    svc.fullSync(triggeredBy).catch(err => console.error('[S1Sync] Background sync error:', err));

    return NextResponse.json({ success: true, message: 'SentinelOne sync started' });
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}

export async function GET() {
  try {
    const [historyRes, countsRes] = await Promise.all([
      postgresClient.query(
        `SELECT id, sync_type, status, triggered_by, started_at, completed_at,
                duration_ms, total_upserted, error_message, entity_results
         FROM s1_sync_history ORDER BY started_at DESC LIMIT 10`
      ),
      postgresClient.query(
        `SELECT
          (SELECT COUNT(*) FROM s1_sites)  AS sites,
          (SELECT COUNT(*) FROM s1_agents) AS agents,
          (SELECT COUNT(*) FROM s1_agents WHERE infected = true) AS infected,
          (SELECT COUNT(*) FROM s1_agents WHERE is_active = true) AS active_agents,
          (SELECT COUNT(*) FROM s1_threats) AS threats`
      ),
    ]);

    const svc = getSentinelOneSyncService();

    return NextResponse.json({
      inProgress: svc.isSyncInProgress(),
      history: historyRes.rows,
      counts: countsRes.rows[0],
    });
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}
