import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { parseBody } from '@/lib/api/route-helpers';
import { createAuditLog } from '@/lib/audit';
import { errorResponse, NotFoundError } from '@/lib/errors';
import { emitToRoom } from '@/lib/socket/server';
import { createNoteSchema } from '@/lib/validators/notes';
import * as notesService from '@/lib/services/notes.service';

export const GET = withAuth(
  withPermission('interests', 'view', async (_req, ctx, params) => {
    try {
      const interestId = params.id;
      if (!interestId) throw new NotFoundError('Interest');
      const notes = await notesService.listForEntity(ctx.portId, 'interests', interestId);
      return NextResponse.json({ data: notes });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);

export const POST = withAuth(
  withPermission('interests', 'edit', async (req, ctx, params) => {
    try {
      const interestId = params.id;
      if (!interestId) throw new NotFoundError('Interest');
      const body = await parseBody(req, createNoteSchema);
      const note = await notesService.create(ctx.portId, 'interests', interestId, ctx.userId, body);

      void createAuditLog({
        userId: ctx.userId,
        portId: ctx.portId,
        action: 'create',
        entityType: 'interest_note',
        entityId: note.id,
        metadata: { interestId },
        ipAddress: ctx.ipAddress,
        userAgent: ctx.userAgent,
      });

      emitToRoom(`interest:${interestId}`, 'interest:noteAdded', {
        interestId,
        noteId: note.id,
        authorName: note.authorName ?? ctx.user.name,
        preview: note.content.slice(0, 100),
      });

      return NextResponse.json({ data: note }, { status: 201 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
