'use strict';

import type { Db } from '../types/db';

const { getDb } = require('../db/database.cts');

// Lazy-loaded in-memory cache — loaded once on first use
let _patterns: any[] | null = null;
let _overrideTerms: string[] | null = null;

function normalize(text: unknown): string {
  if (!text) return '';
  return String(text).toLowerCase().replace(/&/g, 'and').replace(/\s+/g, ' ').trim();
}

function loadCache(): void {
  if (_patterns !== null) return;
  const db: Db = getDb();
  _patterns = db
    .prepare('SELECT pattern, confidence, category, rationale FROM advisory_non_bill_filters')
    .all();
  _overrideTerms = db
    .prepare('SELECT term FROM advisory_bill_like_overrides')
    .all()
    .map((r: any) => r.term);
}

/**
 * Check a transaction title against advisory filter patterns.
 * Returns null if Create Bill should be shown normally, or
 * { confidence: 'high'|'medium', category, rationale } if it should be suppressed.
 */
function checkTransaction(
  title: unknown,
): { confidence: string; category: any; rationale: any } | null {
  if (!title) return null;
  try {
    loadCache();
  } catch {
    return null;
  }

  const normalized = normalize(title);
  if (!normalized) return null;

  // Bill-like override terms take priority — always show Create Bill
  for (const term of _overrideTerms!) {
    if (normalized.includes(term)) return null;
  }

  // Find the highest-confidence matching pattern
  let highMatch: any = null;
  let mediumMatch: any = null;

  for (const row of _patterns!) {
    if (normalized.includes(row.pattern)) {
      if (row.confidence === 'high') {
        highMatch = row;
        break; // high confidence — no need to keep looking
      } else if (!mediumMatch) {
        mediumMatch = row;
      }
    }
  }

  const match = highMatch || mediumMatch;
  if (!match) return null;

  return {
    confidence: match.confidence,
    category: match.category,
    rationale: match.rationale || null,
  };
}

/** Clear the in-memory cache (used after re-seeding in tests). */
function clearCache(): void {
  _patterns = null;
  _overrideTerms = null;
}

module.exports = { checkTransaction, clearCache };
