import { NextResponse } from 'next/server';

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

/**
 * GET /api/v1/admin/tenancies-module/status — surface whether the module
 * is currently enabled (via setting OR lazy "any row exists" sentinel)
 * so the admin Operations page can render the toggle in the correct state.
 */
export const GET = withAuth(
  withPermission('admin', 'manage_settings', async (_req, ctx) => {
    try {
      const enabled = await isTenanciesModuleEnabled(ctx.portId);
      return NextResponse.json({ data: { enabled } });
    } catch (error) {
      return errorResponse(error);
    }
  }),
);
