/**
 * POST /api/phishing/campaigns/[id]/classify
 *
 * On-demand (re-)trigger for campaign classification (D-02). Enforces the
 * same phishing/analyze permission gate as
 * `/api/phishing/tickets/[ticket_id]/analyze` (Phase 18 D-06 convention —
 * NOT a new permission), validates the campaign id as a UUID (V5), then
 * delegates to `classifyCampaign` (Plan 01) and returns the flat camelCase
 * verdict payload.
 */

import { NextRequest, NextResponse } from 'next/server';
import { requirePermission } from '@/lib/auth-utils';
import postgresClient from '@/lib/services/postgres-client';
import { classifyCampaign } from '@/lib/services/campaign-classifier';
import { writeAuditEvent } from '@/lib/services/phishing-audit';

const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const { session, error } = await requirePermission('phishing', 'analyze');
  if (error) return error;

  const { id } = await params;
  // V5: validate UUID shape before querying — a malformed id would otherwise
  // surface as an unhandled Postgres error -> uncaught 500.
  if (!UUID_RE.test(id)) {
    return NextResponse.json({ error: 'Invalid campaign id' }, { status: 400 });
  }

  try {
    const campaignRes = await postgresClient.query<{ id: string }>(
      `SELECT id FROM campaigns WHERE id = $1`,
      [id]
    );
    if (!campaignRes.rows[0]) {
      return NextResponse.json({ error: 'Campaign not found' }, { status: 404 });
    }

    const result = await classifyCampaign(id);

    // REMED-06: classify is the fourth state-changing action requiring audit
    // coverage (alongside approve/remediate/mark-false-positive). The
    // classification row has already been persisted above (append-only) —
    // an audit-write failure surfaces as this route's 500, but the
    // classification itself is not rolled back.
    const actor = (session?.user as { email?: string } | undefined)?.email ?? null;
    await writeAuditEvent({
      campaignId: id,
      actor,
      eventType: 'campaign_classified',
      payload: { verdict: result.verdict, requiresApproval: result.requiresApproval },
    });

    return NextResponse.json(result);
  } catch (err) {
    console.error('[PHISHING-CLASSIFY] Failed to classify campaign', id, err);
    return NextResponse.json(
      { error: 'Failed to classify campaign', message: err instanceof Error ? err.message : 'Unknown error' },
      { status: 500 }
    );
  }
}
