const PUBLISH_KEYWORDS = [
  "\u53d1\u5e03",
  "\u53d1\u5e16",
  "\u53d1\u4e00\u4e2a",
  "\u53d1\u4e00\u7bc7",
  "\u53d1\u4e00\u6761",
  "\u53d1\u4e2a",
  "\u53d1\u6761",
  "\u53d1\u9001",
  "\u53d1\u9001\u4e00\u4e2a",
  "\u53d1\u9001\u4e00\u7bc7",
  "\u81ea\u52a8\u53d1",
  "\u81ea\u52a8\u53d1\u5e03",
  "\u63d0\u4ea4",
  "publish",
  "post"
];
const DRAFT_KEYWORDS = ["\u8349\u7a3f", "markdown", "draft", "md"];
const WRITING_KEYWORDS = [
  "\u5199",
  "\u751f\u6210",
  "\u6587\u6848",
  "\u5e16\u5b50",
  "\u7b14\u8bb0",
  "\u6807\u9898",
  "\u7f16\u8f91",
  "\u6da6\u8272"
];
const STRATEGY_KEYWORDS = ["\u9009\u9898", "\u7b56\u7565", "\u89c4\u5212", "plan", "topic"];
const REVIEW_KEYWORDS = ["\u5ba1\u6838", "\u6821\u5bf9", "review", "\u68c0\u67e5", "\u5408\u89c4"];
const OPERATIONS_KEYWORDS = ["\u8fd0\u8425", "\u52a9\u624b", "\u5b89\u6392", "\u8def\u7531", "\u534f\u540c", "workflow", "orchestrator"];

export interface ProjectPromptSignals {
  publishCount: number;
  draftCount: number;
  writingCount: number;
  strategyCount: number;
  reviewCount: number;
  operationsCount: number;
}

export function normalizeProjectPrompt(value: string): string {
  return value
    .normalize("NFKC")
    .toLowerCase()
    .replace(/[^\p{L}\p{N}\n]+/gu, " ")
    .replace(/\s+/g, " ")
    .trim();
}

function keywordCount(prompt: string, keywords: string[]): number {
  return keywords.reduce((count, keyword) => count + (prompt.includes(keyword) ? 1 : 0), 0);
}

export function detectProjectPromptSignals(prompt: string): ProjectPromptSignals {
  const normalizedPrompt = normalizeProjectPrompt(prompt);
  return {
    publishCount: keywordCount(normalizedPrompt, PUBLISH_KEYWORDS),
    draftCount: keywordCount(normalizedPrompt, DRAFT_KEYWORDS),
    writingCount: keywordCount(normalizedPrompt, WRITING_KEYWORDS),
    strategyCount: keywordCount(normalizedPrompt, STRATEGY_KEYWORDS),
    reviewCount: keywordCount(normalizedPrompt, REVIEW_KEYWORDS),
    operationsCount: keywordCount(normalizedPrompt, OPERATIONS_KEYWORDS)
  };
}

export function isPublishIntentPrompt(prompt: string): boolean {
  return detectProjectPromptSignals(prompt).publishCount > 0;
}
