export const HERMES_BUSINESS_CONTACT_WORKFLOW = [
  {
    id: "seed_discovery",
    label: "Seed Discovery",
    detail: "Collect business seeds from Google Maps, websites, directories, social pages, booking platforms, and public company pages"
  },
  {
    id: "business_validation",
    label: "Business Validation",
    detail: "Verify the business is active, niche-relevant, and inside the requested location scope"
  },
  {
    id: "website_social_scrape",
    label: "Website/Social Scrape",
    detail: "Scrape official websites and public pages for emails, phones, contact forms, social links, and source evidence"
  },
  {
    id: "contact_extraction",
    label: "Contact Extraction",
    detail: "Normalize public business emails, phones, contact-form URLs, social URLs, website URLs, and evidence URLs"
  },
  {
    id: "validation",
    label: "Validation",
    detail: "Remove duplicates, malformed emails, bad domains, weak rows, wrong-location rows, and unusable contacts"
  },
  {
    id: "quality_scoring",
    label: "Quality Scoring",
    detail: "Score lead quality by contact confidence, website status, niche fit, source evidence, and location fit"
  },
  {
    id: "campaign_readiness",
    label: "Campaign Readiness",
    detail: "Mark leads ready when they have valid public contact evidence and usable campaign fields"
  },
  {
    id: "csv_export",
    label: "CSV Export",
    detail: "Export clean business contacts, validation state, evidence URLs, scores, notes, and campaign-ready fields"
  }
];

export const HERMES_AGENT_WORKFLOW = HERMES_BUSINESS_CONTACT_WORKFLOW;
export const HERMES_LEAD_SCRAPER_WORKFLOW = HERMES_BUSINESS_CONTACT_WORKFLOW;

export const HERMES_WORKFLOW_METADATA = {
  name: process.env.HERMES_WORKFLOW_NAME || "Business Contact Lead Machine",
  planId: process.env.HERMES_PLAN_ID || "a57a3bb4-f75c-4ca5-8e35-c01687e25985"
};

export const HERMES_SELF_EVOLVING_LOOP = [
  { id: "detect", label: "Detect" },
  { id: "classify", label: "Classify" },
  { id: "retry", label: "Retry" },
  { id: "evolve", label: "Evolve" },
  { id: "self_repair", label: "Self-Repair" },
  { id: "resume", label: "Resume" }
];

const PHASE_LABELS = new Map([
  ...HERMES_BUSINESS_CONTACT_WORKFLOW.map((step) => [step.id, step.label]),
  ...HERMES_SELF_EVOLVING_LOOP.map((step) => [step.id, step.label]),
  ["client_input", "Seed Discovery"],
  ["maps_discovery", "Seed Discovery"],
  ["website_enrichment", "Website/Social Scrape"],
  ["lead_extraction", "Contact Extraction"],
  ["quality_gate", "Quality Scoring"],
  ["deliver", "CSV Export"],
  ["delivery", "CSV Export"],
  ["status", "Status"],
  ["plan", "MiniMax Planning"],
  ["cancel", "Cancel"],
  ["kill", "Kill"]
]);

const PHASE_TO_WORKFLOW_STAGE = new Map([
  ["client_input", "seed_discovery"],
  ["plan", "seed_discovery"],
  ["maps_discovery", "seed_discovery"],
  ["business_validation", "business_validation"],
  ["tool_safety_gate", "business_validation"],
  ["website_enrichment", "website_social_scrape"],
  ["owner_evidence_discovery", "website_social_scrape"],
  ["lead_extraction", "contact_extraction"],
  ["owner_identity_proof", "contact_extraction"],
  ["owner_profile_rabbit_hole", "validation"],
  ["owner_contact_extraction", "campaign_readiness"],
  ["quality_gate", "quality_scoring"],
  ["deliver", "csv_export"],
  ["delivery", "csv_export"]
]);

export function createRunWorkflow(input = {}) {
  return {
    agent: "Hermes Agent",
    name: "Business Contact Lead Machine",
    plan_id: HERMES_WORKFLOW_METADATA.planId,
    source: input.source || "lead-scraper",
    workflow: HERMES_BUSINESS_CONTACT_WORKFLOW,
    loop: HERMES_SELF_EVOLVING_LOOP
  };
}

export function workflowPhaseLabel(phase) {
  return PHASE_LABELS.get(phase) || titleCase(phase);
}

export function workflowStageForPhase(phase) {
  return PHASE_TO_WORKFLOW_STAGE.get(phase) || "";
}

function titleCase(value) {
  return String(value || "")
    .replace(/[_-]+/g, " ")
    .replace(/\b\w/g, (char) => char.toUpperCase());
}
