import { NextResponse } from 'next/server';
import { z } from 'zod';

import { withAuth } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { errorResponse } from '@/lib/errors';
import { ALERT_RULES } from '@/lib/db/schema/insights';
import { dismissAllForPort } from '@/lib/services/alerts.service';

const bodySchema = z.object({
  ruleId: z.enum(ALERT_RULES).optional(),
  severity: z.enum(['info', 'warning', 'critical']).optional(),
});

export const POST = withAuth(async (req, ctx) => {
  try {
    const { ruleId, severity } = await parseBody(req, bodySchema);
    const dismissed = await dismissAllForPort(ctx.portId, ctx.userId, { ruleId, severity });
    return NextResponse.json({ data: { dismissed } });
  } catch (error) {
    return errorResponse(error);
  }
});
