import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { getAdminTemplateVersions } from '@/lib/services/document-templates.service';

/**
 * GET /api/v1/admin/templates/[templateId]/versions
 * Returns version history for a template, sourced from audit_logs.
 */
export const GET = withAuth(
  withPermission('document_templates', 'manage', async (req, ctx, params) => {
    try {
      const versions = await getAdminTemplateVersions(ctx.portId, params.templateId!);
      return NextResponse.json({ data: versions });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
