import type { Request, Response, NextFunction } from 'express';
import { prisma } from '../../config/database';

/** Middleware that checks if the social feature is enabled in SiteSettings */
export async function checkSocialEnabled(req: Request, res: Response, next: NextFunction) {
  try {
    const settings = await prisma.siteSettings.findFirst({ select: { enableSocial: true } });
    if (!settings?.enableSocial) {
      res.status(404).json({ error: { message: 'Social features are not enabled', code: 'SOCIAL_DISABLED' } });
      return;
    }
    next();
  } catch {
    // Fail closed — if we can't check the feature flag, deny access
    res.status(503).json({ error: { message: 'Service temporarily unavailable', code: 'SERVICE_UNAVAILABLE' } });
  }
}
