/**
 * Social Engine — Phase 5: Compliance Gate
 * Validates content against brand rules, banned words, duplicates, and length.
 * Auto-approves content that passes all checks.
 */
import pool from '../../db';
import { updateContentStatus } from './data-queries';

const BANNED_WORDS = [
  'bet', 'bets', 'betting', 'bettor', 'bettors',
  'wager', 'wagers', 'wagering',
  'lock', 'locks', 'locked',
  'fade', 'fades', 'fading',
  'gamble', 'gambling', 'gambler',
  'parlay', 'parlays',
  'degen', 'degenerate',
  'tout', 'touts',
  'guaranteed', 'guarantee',
  'sure thing', 'can\'t lose',
  'free money',
];

const BRAND_LEAKAGE = [
  'eventheodds', 'eventheodds.ai',
  'sportsclaw', 'sportsclaw.guru',
  'cashhive', 'cashhive.ai',
  'komodotechinc', 'komodotech',
  'openclaw',
];

const REPETITIVE_PHRASES = [
  'rain man\'s models',
  'full analysis on rainmakersports.app',
  'follow rainmaker sports',
  'what do you think?',
  'our intelligence',
  'our signals indicate',
  'the numbers tell the story',
  'here\'s what this means',
];

const MAX_TWEET_LENGTH = 25000; // X Premium

export async function runComplianceGate(): Promise<number> {
  console.log('[social-engine] Phase 5: Running compliance checks...');

  // Get all draft content that hasn't been compliance-checked
  const result = await pool.query(
    `SELECT id, text, persona_id, content_type, format
     FROM rm_social_content
     WHERE status = 'draft' AND NOT compliance_ok
     AND created_at > NOW() - INTERVAL '4 hours'
     ORDER BY quality_score DESC NULLS LAST`
  );

  let approved = 0;
  let rejected = 0;

  for (const row of result.rows) {
    const issues = validateContent(row.text);

    if (issues.length === 0) {
      // Check for duplicate (first 100 chars match in last 12h, excluding self)
      const dupeCheck = await pool.query(
        `SELECT 1 FROM rm_social_content
         WHERE id != $1 AND LEFT(text, 100) = $2
         AND status IN ('approved', 'posted')
         AND created_at > NOW() - INTERVAL '12 hours'
         LIMIT 1`,
        [row.id, row.text.substring(0, 100)]
      );

      if (dupeCheck.rowCount! > 0) {
        console.log(`[social-engine] Compliance: rejected duplicate for content ${row.id}`);
        await updateContentStatus(row.id, 'rejected', { compliance_ok: false });
        rejected++;
        continue;
      }

      // All gates pass
      await updateContentStatus(row.id, 'approved', { compliance_ok: true });
      approved++;
    } else {
      console.log(`[social-engine] Compliance: rejected content ${row.id} — ${issues.join(', ')}`);
      await updateContentStatus(row.id, 'rejected', { compliance_ok: false });
      rejected++;
    }
  }

  console.log(`[social-engine] Phase 5 complete: ${approved} approved, ${rejected} rejected`);
  return approved;
}

function validateContent(text: string): string[] {
  const issues: string[] = [];
  const textLower = text.toLowerCase();

  // Length check
  if (text.length < 1) {
    issues.push('empty content');
  }
  if (text.length > MAX_TWEET_LENGTH) {
    issues.push(`exceeds max length (${text.length}/${MAX_TWEET_LENGTH})`);
  }

  // Banned words check (whole word match)
  for (const word of BANNED_WORDS) {
    const regex = new RegExp(`\\b${word}\\b`, 'i');
    if (regex.test(textLower)) {
      issues.push(`banned word: "${word}"`);
      break; // One banned word is enough to reject
    }
  }

  // Brand leakage check
  for (const brand of BRAND_LEAKAGE) {
    if (textLower.includes(brand.toLowerCase())) {
      issues.push(`brand leakage: "${brand}"`);
      break;
    }
  }

  // No external URLs (only rainmakersports.app allowed)
  const urlRegex = /https?:\/\/[^\s]+/gi;
  const urls = text.match(urlRegex) || [];
  for (const url of urls) {
    if (!url.includes('rainmakersports.app')) {
      issues.push(`external URL: "${url}"`);
      break;
    }
  }

  // Avoid over-templated phrasing and repeated branded tails.
  const matchedPhrases = REPETITIVE_PHRASES.filter(phrase => textLower.includes(phrase));
  if (matchedPhrases.length >= 2) {
    issues.push('too many templated phrases');
  }

  const doubleLineBreaks = text.split('\n\n').length - 1;
  if (doubleLineBreaks >= 4) {
    issues.push('over-structured thread-style formatting');
  }

  return issues;
}
