/**
 * Lightweight, dependency-free content moderation for the kids AI features.
 *
 * This is a defence-in-depth layer on top of the hard-coded safety system
 * prompts — it is intentionally strict (better to over-deflect than to let one
 * unsafe reply through). A cloud classifier can be layered in later.
 *
 * Server-only.
 */

// Unsafe topic markers (Bangla + English + common romanised forms). Kept broad.
const BLOCK_TERMS = [
  // violence / weapons
  'kill', 'murder', 'gun', 'knife', 'blood', 'weapon', 'bomb', 'suicide', 'die', 'death',
  'খুন', 'মার', 'রক্ত', 'অস্ত্র', 'বন্দুক', 'ছুরি', 'বোমা', 'আত্মহত্যা', 'মৃত্যু', 'মরা',
  // adult / romance
  'sex', 'sexy', 'porn', 'nude', 'naked', 'kiss', 'girlfriend', 'boyfriend', 'dating', 'drug', 'alcohol', 'beer', 'cigarette',
  'যৌন', 'নগ্ন', 'চুম্বন', 'প্রেমিক', 'মদ', 'সিগারেট', 'নেশা', 'ড্রাগ',
  // hate / self-harm
  'hate', 'racist', 'abuse', 'hurt myself', 'cut myself',
  'ঘৃণা', 'গালি', 'নিজেকে কষ্ট',
];

// Personal-info elicitation / sharing patterns.
const PHONE_RE = /(?:\+?88)?0?1[3-9]\d{8}/; // BD mobile
const EMAIL_RE = /[\w.+-]+@[\w-]+\.[\w.-]+/;

export interface ModerationResult {
  safe: boolean;
  /** child-facing deflection message when unsafe */
  message?: string;
  reason?: string;
}

const DEFLECT =
  'এটা নিয়ে আমি কথা বলতে পারব না। এই প্রশ্নটা বাবা-মা বা বড় কাউকে জিজ্ঞেস করো। চলো বরং শেখার কিছু জিজ্ঞেস করি! 😊';

function normalise(s: string): string {
  return s.toLowerCase();
}

// Word-start boundary: string start or a whitespace/punctuation char. This
// avoids false positives where a blocked term is a substring INSIDE an innocent
// word (e.g. "মার" inside "তোমার"/"আমার"), while still catching words that
// START with the term (e.g. "মারবে", "খুনি").
const BOUNDARY = '(?:^|[\\s।,;:?!.।\\-–—()\\u0964])';

function containsBlocked(text: string): string | null {
  for (const term of BLOCK_TERMS) {
    const re = new RegExp(BOUNDARY + term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
    if (re.test(text)) return term;
  }
  return null;
}

/** Check a child's message before sending it to the model. */
export function moderateInput(text: string): ModerationResult {
  const t = normalise(text);
  if (text.length > 500) {
    return { safe: false, reason: 'too-long', message: 'একটু ছোট করে বলো তো! 😊' };
  }
  const hit = containsBlocked(t);
  if (hit) return { safe: false, reason: `blocked:${hit}`, message: DEFLECT };
  if (PHONE_RE.test(text) || EMAIL_RE.test(text)) {
    return {
      safe: false,
      reason: 'personal-info',
      message: 'নিজের ফোন নম্বর বা ঠিকানা কখনো কাউকে দিয়ো না, এমনকি আমাকেও না! নিরাপদ থাকো। 🛡️',
    };
  }
  return { safe: true };
}

/** Check a model's reply before showing it to the child. */
export function moderateOutput(text: string): ModerationResult {
  const hit = containsBlocked(normalise(text));
  if (hit) {
    return {
      safe: false,
      reason: `output-blocked:${hit}`,
      message: 'দুঃখিত, এখন এটার উত্তর দিতে পারছি না। চলো অন্য মজার কিছু শিখি! ✨',
    };
  }
  return { safe: true, message: text };
}
