import { NextResponse } from 'next/server';

import { withAuth, withPermission } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { enableTenanciesModule } from '@/lib/services/tenancies-module.service';

/**
 * POST /api/v1/admin/tenancies-module/enable — admin-driven enable.
 * Idempotent; flipping an already-enabled module is a no-op.
 */
export const POST = withAuth(
  withPermission('admin', 'manage_settings', async (_req, ctx) => {
    try {
      await enableTenanciesModule(ctx.portId);
      return new NextResponse(null, { status: 204 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
