export type QAEntry = {
  question: string;
  answer: string;
  keywords: string[];
};

export const qaEntries: QAEntry[] = [
  {
    question: "What does QuantFortune do?",
    answer:
      "QuantFortune helps you invest long-term using disciplined, automated strategies. No noise. Just structured wealth building.",
    keywords: ["what does quantfortune do", "what is quantfortune", "quantfortune do"],
  },
  {
    question: "How do I connect my broker?",
    answer:
      "Click ‘Connect Broker’ on the top right. You’ll be redirected to your broker’s secure login to authorize access.",
    keywords: ["connect broker", "broker connect", "link broker", "connect my broker"],
  },
  {
    question: "How does SIP investing work?",
    answer:
      "SIP invests a fixed amount at regular intervals, reducing timing risk and building wealth steadily over time.",
    keywords: ["sip investing", "sip work", "sip works", "systematic investment"],
  },
  {
    question: "Is my money safe?",
    answer: "Your funds always stay with your broker. QuantFortune never holds your money.",
    keywords: ["money safe", "funds safe", "is my money safe", "safe"],
  },
  {
    question: "Do I need to trade daily?",
    answer: "No. Strategies are automated and designed for long-term investing.",
    keywords: ["trade daily", "daily trade", "do i need to trade daily"],
  },
  {
    question: "What strategies are available?",
    answer: "We offer simple, rules-based strategies focused on disciplined long-term growth.",
    keywords: ["strategies available", "what strategies", "available strategies"],
  },
  {
    question: "Can I stop anytime?",
    answer: "Yes. You have full control. You can stop or pause strategies anytime.",
    keywords: ["stop anytime", "pause", "stop strategies", "can i stop"],
  },
  {
    question: "What brokers are supported?",
    answer: "Currently supported brokers are shown in the ‘Connect Broker’ section.",
    keywords: ["brokers supported", "supported brokers", "which brokers"],
  },
  {
    question: "Is this fully automated?",
    answer: "Yes. Once started, strategies run automatically based on predefined rules.",
    keywords: ["fully automated", "automated", "auto"],
  },
  {
    question: "How do I contact support?",
    answer: "You can reach us via the Support section or email support@quantfortune.in",
    keywords: ["contact support", "support email", "help", "support"],
  },
];

export const suggestedQuestions = qaEntries.map((entry) => entry.question);

export const outOfScopeAnswer =
  "I can help with platform usage and onboarding. For other queries, please contact support.";

export function normalizeText(value: string) {
  return value
    .toLowerCase()
    .replace(/[^a-z0-9\s]/g, " ")
    .replace(/\s+/g, " ")
    .trim();
}

export function matchAnswer(message: string) {
  const normalized = normalizeText(message);
  if (!normalized) {
    return null;
  }
  const exact = qaEntries.find(
    (entry) => normalizeText(entry.question) === normalized,
  );
  if (exact) {
    return exact.answer;
  }
  const keywordMatch = qaEntries.find((entry) =>
    entry.keywords.some((keyword) => normalized.includes(keyword)),
  );
  return keywordMatch?.answer ?? null;
}
