import { NextResponse } from 'next/server';

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

/**
 * POST /api/v1/admin/tenancies-module/disable — admin-driven disable.
 * Data is preserved; only the rendering surfaces hide. Frontend warns
 * the operator about the row count before calling this.
 */
export const POST = withAuth(
  withPermission('admin', 'manage_settings', async (_req, ctx) => {
    try {
      await disableTenanciesModule(ctx.portId);
      return new NextResponse(null, { status: 204 });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
