/* Job-centric data model for Greydigi AI Recruiting.
   Jobs mirror the live Supabase `jobs` table (project hr-recruitment) — sbId holds the
   real row UUID; requirements.required_skills → mustHave, nice_to_have → preferred.
   Each job owns its candidates + interviews. Deleting a job cascades (handled in app state).
   decision: "shortlisted" | "borderline" | "rejected"  (Proceed / Caution / Reject) */

const STAGE_FLOW = ['Sourcing', 'Screening', 'Interviewing', 'Offer', 'Hired'];
const clampN = (n, lo, hi) => Math.max(lo, Math.min(hi, Math.round(n)));

/* ---------------- Jobs (from Supabase) ---------------- */
const JOBS = [
  {
    id: 'j1', sbId: 'a88f7828-2289-4e0f-af21-e5069f536378',
    title: 'Forward Deployed Engineer', roleNoun: 'Forward Deployed Engineer',
    dept: 'Engineering', loc: 'Remote (Southeast Asia)', type: 'Full-time', minYears: 5,
    description: 'You will design, build, and own the agents and workflows that run inside real client environments — multi-agent pipelines, LLM orchestration, automated workflows, guardrails, evals. You ship production code. You sit close to the problem. Not research. Not slides. Deployed.',
    mustHave: ['LLM orchestration frameworks', 'Multi-agent system design', 'Python or TypeScript', 'Retrieval systems and vector databases', 'Tool-use protocols and function calling', 'Cloud deployment', 'Evaluation and observability for LLM systems', 'Enterprise system integration'],
    preferred: ['MCP-style integration patterns', 'Containerised workloads', 'ERP integration', 'Guardrails for LLM systems'],
    niceToHave: [],
    stage: 'Interviewing', status: 'open', opened: '2026-06-26',
  },
  {
    id: 'j2', sbId: 'ca8d34bc-acf2-4849-a58e-f33ad85a1d2b',
    title: 'Delivery Manager', roleNoun: 'Delivery Manager',
    dept: 'Delivery', loc: 'Remote (Southeast Asia)', type: 'Full-time', minYears: 3,
    description: 'You will own end-to-end delivery of agentic workflow deployments — from scoping through handover and into scale. You track what changes after the workflow goes live, not just that it shipped. No handoffs without accountability.',
    mustHave: ['Technology or AI project delivery', 'AI workflows and automation logic', 'Enterprise solution delivery', 'Stakeholder management C-suite to operational', 'Fluent English written and spoken', 'AI tools daily usage'],
    preferred: ['Agentic AI platforms', 'LLM orchestration', 'Multi-agent system design', 'Prompt engineering', 'RPA'],
    niceToHave: [],
    stage: 'Screening', status: 'open', opened: '2026-06-26',
  },
  {
    id: 'j3', sbId: 'cf4b2a39-af21-409e-a005-71a1801ae8dc',
    title: 'Business Operations Associate', roleNoun: 'Operations Associate',
    dept: 'Operations', loc: 'Remote (Southeast Asia)', type: 'Full-time', minYears: 2,
    description: "You will keep greydigi running — internal processes, sales and CRM hygiene, marketing execution, vendor and contractor coordination, finance and HR admin, founder support. You don't wait for the perfect system before you improve the current one.",
    mustHave: ['Fluent English written and spoken', 'Google Workspace', 'AI tools daily usage', 'CRM hygiene and sales operations', 'Marketing execution and content scheduling', 'HR administration', 'Finance coordination', 'Process ownership and improvement'],
    preferred: ['Agent-driven process automation', 'Legal and compliance coordination', 'Founder support and scheduling'],
    niceToHave: [],
    stage: 'Screening', status: 'open', opened: '2026-06-26',
  },
];

/* ---------------- j1 — Forward Deployed Engineer candidates ---------------- */
const CANDS_J1 = [
  {
    id: 'c1', jobId: 'j1', name: 'Nguyen Minh Tri', initials: 'NT',
    headline: 'Senior AI Engineer — agent platforms', company: 'Grab', location: 'Ho Chi Minh City, VN',
    years: 7, applied: 'Jun 28', appliedDate: '2026-06-28',
    ats: 93, skills_pct: 91, decision: 'shortlisted', risk: 'Low',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 94, variant: 'matched' },
      { name: 'Multi-agent system design', percentage: 90, variant: 'matched' },
      { name: 'Python or TypeScript', percentage: 96, variant: 'matched' },
      { name: 'Retrieval systems and vector databases', percentage: 88, variant: 'matched' },
      { name: 'Tool-use protocols and function calling', percentage: 92, variant: 'matched' },
      { name: 'Cloud deployment', percentage: 86, variant: 'matched' },
      { name: 'Evaluation and observability for LLM systems', percentage: 81, variant: 'matched' },
      { name: 'Enterprise system integration', percentage: 68, variant: 'partial' },
      { name: 'MCP-style integration patterns', percentage: 84, variant: 'matched' },
      { name: 'Guardrails for LLM systems', percentage: 72, variant: 'partial' },
    ],
    strengths: ['Production agent pipelines', 'RAG at scale', 'Function calling', 'AWS/GCP'],
    gaps: ['Enterprise system integration'],
  },
  {
    id: 'c2', jobId: 'j1', name: 'Arjun Mehta', initials: 'AM',
    headline: 'ML Platform Engineer', company: 'Gojek', location: 'Jakarta, ID',
    years: 8, applied: 'Jun 27', appliedDate: '2026-06-27',
    ats: 88, skills_pct: 84, decision: 'shortlisted', risk: 'Low',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 82, variant: 'matched' },
      { name: 'Multi-agent system design', percentage: 66, variant: 'partial' },
      { name: 'Python or TypeScript', percentage: 94, variant: 'matched' },
      { name: 'Retrieval systems and vector databases', percentage: 90, variant: 'matched' },
      { name: 'Tool-use protocols and function calling', percentage: 80, variant: 'matched' },
      { name: 'Cloud deployment', percentage: 92, variant: 'matched' },
      { name: 'Evaluation and observability for LLM systems', percentage: 88, variant: 'matched' },
      { name: 'Enterprise system integration', percentage: 74, variant: 'partial' },
      { name: 'Containerised workloads', percentage: 93, variant: 'matched' },
      { name: 'MCP-style integration patterns', percentage: 55, variant: 'partial' },
    ],
    strengths: ['LLM infra & observability', 'Vector search', 'Kubernetes', 'Python'],
    gaps: ['Multi-agent system design', 'ERP integration'],
  },
  {
    id: 'c3', jobId: 'j1', name: 'Sarah Lim', initials: 'SL',
    headline: 'Applied AI Engineer', company: 'GovTech Singapore', location: 'Singapore, SG',
    years: 6, applied: 'Jun 30', appliedDate: '2026-06-30',
    ats: 85, skills_pct: 80, decision: 'shortlisted', risk: 'Low',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 85, variant: 'matched' },
      { name: 'Multi-agent system design', percentage: 74, variant: 'partial' },
      { name: 'Python or TypeScript', percentage: 90, variant: 'matched' },
      { name: 'Retrieval systems and vector databases', percentage: 84, variant: 'matched' },
      { name: 'Tool-use protocols and function calling', percentage: 82, variant: 'matched' },
      { name: 'Cloud deployment', percentage: 80, variant: 'matched' },
      { name: 'Evaluation and observability for LLM systems', percentage: 70, variant: 'partial' },
      { name: 'Enterprise system integration', percentage: 86, variant: 'matched' },
      { name: 'Guardrails for LLM systems', percentage: 88, variant: 'matched' },
      { name: 'MCP-style integration patterns', percentage: 48, variant: 'partial' },
    ],
    strengths: ['Gov-scale integrations', 'RAG services', 'Guardrails & safety', 'TypeScript'],
    gaps: ['Evaluation and observability for LLM systems'],
  },
  {
    id: 'c4', jobId: 'j1', name: 'Bao Tran', initials: 'BT',
    headline: 'Full-stack Engineer building LLM apps', company: 'FPT Software', location: 'Hanoi, VN',
    years: 5, applied: 'Jul 1', appliedDate: '2026-07-01',
    ats: 76, skills_pct: 66, decision: 'borderline', risk: 'Medium',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 70, variant: 'partial' },
      { name: 'Multi-agent system design', percentage: 42, variant: 'missing' },
      { name: 'Python or TypeScript', percentage: 92, variant: 'matched' },
      { name: 'Retrieval systems and vector databases', percentage: 68, variant: 'partial' },
      { name: 'Tool-use protocols and function calling', percentage: 74, variant: 'partial' },
      { name: 'Cloud deployment', percentage: 84, variant: 'matched' },
      { name: 'Evaluation and observability for LLM systems', percentage: 35, variant: 'missing' },
      { name: 'Enterprise system integration', percentage: 60, variant: 'partial' },
      { name: 'Containerised workloads', percentage: 78, variant: 'matched' },
      { name: 'MCP-style integration patterns', percentage: 30, variant: 'missing' },
    ],
    strengths: ['TypeScript/React', 'Cloud deployment', 'LLM app prototypes'],
    gaps: ['Multi-agent system design', 'Evals & observability'],
  },
  {
    id: 'c5', jobId: 'j1', name: 'Kevin Santos', initials: 'KS',
    headline: 'Senior Data Scientist', company: 'Globe Telecom', location: 'Manila, PH',
    years: 6, applied: 'Jun 29', appliedDate: '2026-06-29',
    ats: 71, skills_pct: 58, decision: 'borderline', risk: 'Medium',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 62, variant: 'partial' },
      { name: 'Multi-agent system design', percentage: 28, variant: 'missing' },
      { name: 'Python or TypeScript', percentage: 88, variant: 'matched' },
      { name: 'Retrieval systems and vector databases', percentage: 72, variant: 'partial' },
      { name: 'Tool-use protocols and function calling', percentage: 55, variant: 'partial' },
      { name: 'Cloud deployment', percentage: 58, variant: 'partial' },
      { name: 'Evaluation and observability for LLM systems', percentage: 66, variant: 'partial' },
      { name: 'Enterprise system integration', percentage: 40, variant: 'missing' },
      { name: 'Guardrails for LLM systems', percentage: 35, variant: 'missing' },
      { name: 'ERP integration', percentage: 20, variant: 'missing' },
    ],
    strengths: ['Python & ML modelling', 'Experimentation'],
    gaps: ['Multi-agent system design', 'Enterprise system integration', 'Production deployment'],
  },
  {
    id: 'c6', jobId: 'j1', name: 'Rahul Iyer', initials: 'RI',
    headline: 'Chatbot Developer', company: 'Independent', location: 'Chennai, IN',
    years: 4, applied: 'Jun 27', appliedDate: '2026-06-27',
    ats: 55, skills_pct: 36, decision: 'rejected', risk: 'High',
    skillbars: [
      { name: 'LLM orchestration frameworks', percentage: 48, variant: 'partial' },
      { name: 'Multi-agent system design', percentage: 15, variant: 'missing' },
      { name: 'Python or TypeScript', percentage: 70, variant: 'partial' },
      { name: 'Retrieval systems and vector databases', percentage: 30, variant: 'missing' },
      { name: 'Tool-use protocols and function calling', percentage: 44, variant: 'missing' },
      { name: 'Cloud deployment', percentage: 32, variant: 'missing' },
      { name: 'Evaluation and observability for LLM systems', percentage: 12, variant: 'missing' },
      { name: 'Enterprise system integration', percentage: 18, variant: 'missing' },
      { name: 'MCP-style integration patterns', percentage: 10, variant: 'missing' },
      { name: 'Guardrails for LLM systems', percentage: 14, variant: 'missing' },
    ],
    strengths: ['Prompt-based chatbots', 'Rapid prototyping'],
    gaps: ['Below 5 yrs experience', 'Retrieval systems', 'Cloud deployment'],
  },
];

/* ---------------- j2 — Delivery Manager candidates ---------------- */
const CANDS_J2 = [
  {
    id: 'c7', jobId: 'j2', name: 'Melissa Tan', initials: 'MT',
    headline: 'Senior Delivery Manager — AI programmes', company: 'Accenture', location: 'Singapore, SG',
    years: 9, applied: 'Jun 28', appliedDate: '2026-06-28',
    ats: 91, skills_pct: 89, decision: 'shortlisted', risk: 'Low',
    skillbars: [
      { name: 'Technology or AI project delivery', percentage: 95, variant: 'matched' },
      { name: 'AI workflows and automation logic', percentage: 84, variant: 'matched' },
      { name: 'Enterprise solution delivery', percentage: 92, variant: 'matched' },
      { name: 'Stakeholder management C-suite to operational', percentage: 94, variant: 'matched' },
      { name: 'Fluent English written and spoken', percentage: 98, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 82, variant: 'matched' },
      { name: 'Agentic AI platforms', percentage: 68, variant: 'partial' },
      { name: 'RPA', percentage: 74, variant: 'partial' },
    ],
    strengths: ['Enterprise AI rollouts', 'C-suite stakeholder management', 'Delivery governance', 'Daily AI tooling'],
    gaps: ['Hands-on LLM orchestration'],
  },
  {
    id: 'c8', jobId: 'j2', name: 'Quang Pham', initials: 'QP',
    headline: 'Technical Project Manager', company: 'NashTech', location: 'Ho Chi Minh City, VN',
    years: 6, applied: 'Jul 2', appliedDate: '2026-07-02',
    ats: 83, skills_pct: 78, decision: 'shortlisted', risk: 'Medium',
    skillbars: [
      { name: 'Technology or AI project delivery', percentage: 86, variant: 'matched' },
      { name: 'AI workflows and automation logic', percentage: 80, variant: 'matched' },
      { name: 'Enterprise solution delivery', percentage: 74, variant: 'partial' },
      { name: 'Stakeholder management C-suite to operational', percentage: 72, variant: 'partial' },
      { name: 'Fluent English written and spoken', percentage: 90, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 88, variant: 'matched' },
      { name: 'Prompt engineering', percentage: 82, variant: 'matched' },
      { name: 'LLM orchestration', percentage: 58, variant: 'partial' },
    ],
    strengths: ['Agile tech delivery', 'Automation projects', 'Fluent English', 'Prompt engineering'],
    gaps: ['C-suite exposure'],
  },
  {
    id: 'c9', jobId: 'j2', name: 'Dian Prameswari', initials: 'DP',
    headline: 'Digital Transformation Consultant', company: 'Deloitte', location: 'Jakarta, ID',
    years: 5, applied: 'Jun 30', appliedDate: '2026-06-30',
    ats: 75, skills_pct: 64, decision: 'borderline', risk: 'Medium',
    skillbars: [
      { name: 'Technology or AI project delivery', percentage: 72, variant: 'partial' },
      { name: 'AI workflows and automation logic', percentage: 65, variant: 'partial' },
      { name: 'Enterprise solution delivery', percentage: 80, variant: 'matched' },
      { name: 'Stakeholder management C-suite to operational', percentage: 84, variant: 'matched' },
      { name: 'Fluent English written and spoken', percentage: 88, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 60, variant: 'partial' },
      { name: 'RPA', percentage: 85, variant: 'matched' },
      { name: 'Agentic AI platforms', percentage: 25, variant: 'missing' },
    ],
    strengths: ['Enterprise consulting', 'RPA programmes', 'Stakeholder workshops'],
    gaps: ['Hands-on AI workflow delivery', 'Agentic AI platforms'],
  },
  {
    id: 'c10', jobId: 'j2', name: 'James Ocampo', initials: 'JO',
    headline: 'Scrum Master / Project Manager', company: 'Concentrix', location: 'Manila, PH',
    years: 7, applied: 'Jun 29', appliedDate: '2026-06-29',
    ats: 70, skills_pct: 55, decision: 'borderline', risk: 'Medium',
    skillbars: [
      { name: 'Technology or AI project delivery', percentage: 68, variant: 'partial' },
      { name: 'AI workflows and automation logic', percentage: 40, variant: 'missing' },
      { name: 'Enterprise solution delivery', percentage: 72, variant: 'partial' },
      { name: 'Stakeholder management C-suite to operational', percentage: 74, variant: 'partial' },
      { name: 'Fluent English written and spoken', percentage: 92, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 66, variant: 'partial' },
      { name: 'Prompt engineering', percentage: 45, variant: 'missing' },
      { name: 'RPA', percentage: 50, variant: 'partial' },
    ],
    strengths: ['Agile ceremonies', 'Fluent English', 'BPO-scale coordination'],
    gaps: ['AI workflows and automation logic', 'Agentic delivery track record'],
  },
  {
    id: 'c11', jobId: 'j2', name: 'Li Wen', initials: 'LW',
    headline: 'IT Program Manager', company: 'Huawei', location: 'Shenzhen, CN',
    years: 10, applied: 'Jun 27', appliedDate: '2026-06-27',
    ats: 60, skills_pct: 42, decision: 'rejected', risk: 'High',
    skillbars: [
      { name: 'Technology or AI project delivery', percentage: 74, variant: 'partial' },
      { name: 'AI workflows and automation logic', percentage: 25, variant: 'missing' },
      { name: 'Enterprise solution delivery', percentage: 82, variant: 'matched' },
      { name: 'Stakeholder management C-suite to operational', percentage: 70, variant: 'partial' },
      { name: 'Fluent English written and spoken', percentage: 45, variant: 'missing' },
      { name: 'AI tools daily usage', percentage: 30, variant: 'missing' },
      { name: 'RPA', percentage: 40, variant: 'missing' },
      { name: 'LLM orchestration', percentage: 12, variant: 'missing' },
    ],
    strengths: ['Large-programme governance', 'Enterprise delivery'],
    gaps: ['Fluent English', 'AI tools daily usage', 'AI workflow experience'],
  },
];

/* ---------------- j3 — Business Operations Associate candidates ---------------- */
const CANDS_J3 = [
  {
    id: 'c12', jobId: 'j3', name: 'Thao Nguyen', initials: 'TN',
    headline: 'Operations Executive — SaaS', company: 'Katalon', location: 'Ho Chi Minh City, VN',
    years: 4, applied: 'Jun 28', appliedDate: '2026-06-28',
    ats: 90, skills_pct: 88, decision: 'shortlisted', risk: 'Low',
    skillbars: [
      { name: 'Fluent English written and spoken', percentage: 92, variant: 'matched' },
      { name: 'Google Workspace', percentage: 95, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 90, variant: 'matched' },
      { name: 'CRM hygiene and sales operations', percentage: 86, variant: 'matched' },
      { name: 'Marketing execution and content scheduling', percentage: 80, variant: 'matched' },
      { name: 'HR administration', percentage: 76, variant: 'partial' },
      { name: 'Finance coordination', percentage: 82, variant: 'matched' },
      { name: 'Process ownership and improvement', percentage: 91, variant: 'matched' },
      { name: 'Founder support and scheduling', percentage: 88, variant: 'matched' },
      { name: 'Agent-driven process automation', percentage: 64, variant: 'partial' },
    ],
    strengths: ['SaaS ops background', 'HubSpot CRM hygiene', 'Process improvement', 'Daily AI tooling'],
    gaps: ['HR administration depth'],
  },
  {
    id: 'c13', jobId: 'j3', name: 'Amanda Cruz', initials: 'AC',
    headline: 'Executive Assistant & Ops Coordinator', company: 'Remote (US startup)', location: 'Manila, PH',
    years: 5, applied: 'Jul 1', appliedDate: '2026-07-01',
    ats: 84, skills_pct: 79, decision: 'shortlisted', risk: 'Medium',
    skillbars: [
      { name: 'Fluent English written and spoken', percentage: 96, variant: 'matched' },
      { name: 'Google Workspace', percentage: 92, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 74, variant: 'partial' },
      { name: 'CRM hygiene and sales operations', percentage: 70, variant: 'partial' },
      { name: 'Marketing execution and content scheduling', percentage: 82, variant: 'matched' },
      { name: 'HR administration', percentage: 84, variant: 'matched' },
      { name: 'Finance coordination', percentage: 72, variant: 'partial' },
      { name: 'Process ownership and improvement', percentage: 80, variant: 'matched' },
      { name: 'Founder support and scheduling', percentage: 94, variant: 'matched' },
      { name: 'Legal and compliance coordination', percentage: 66, variant: 'partial' },
    ],
    strengths: ['Founder support', 'Fluent English', 'HR & vendor admin', 'Content scheduling'],
    gaps: ['CRM/sales ops depth', 'AI tools fluency'],
  },
  {
    id: 'c14', jobId: 'j3', name: 'Farah Abdullah', initials: 'FA',
    headline: 'Marketing & Admin Coordinator', company: 'Mediabrands', location: 'Kuala Lumpur, MY',
    years: 3, applied: 'Jun 30', appliedDate: '2026-06-30',
    ats: 74, skills_pct: 62, decision: 'borderline', risk: 'Medium',
    skillbars: [
      { name: 'Fluent English written and spoken', percentage: 90, variant: 'matched' },
      { name: 'Google Workspace', percentage: 84, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 68, variant: 'partial' },
      { name: 'CRM hygiene and sales operations', percentage: 52, variant: 'partial' },
      { name: 'Marketing execution and content scheduling', percentage: 88, variant: 'matched' },
      { name: 'HR administration', percentage: 45, variant: 'missing' },
      { name: 'Finance coordination', percentage: 38, variant: 'missing' },
      { name: 'Process ownership and improvement', percentage: 64, variant: 'partial' },
      { name: 'Agent-driven process automation', percentage: 20, variant: 'missing' },
      { name: 'Legal and compliance coordination', percentage: 30, variant: 'missing' },
    ],
    strengths: ['Marketing execution', 'Content calendars', 'Fluent English'],
    gaps: ['Finance coordination', 'HR administration'],
  },
  {
    id: 'c15', jobId: 'j3', name: 'Duc Le', initials: 'DL',
    headline: 'Sales Operations Admin', company: 'MISA', location: 'Da Nang, VN',
    years: 2, applied: 'Jul 2', appliedDate: '2026-07-02',
    ats: 68, skills_pct: 52, decision: 'borderline', risk: 'High',
    skillbars: [
      { name: 'Fluent English written and spoken', percentage: 62, variant: 'partial' },
      { name: 'Google Workspace', percentage: 78, variant: 'matched' },
      { name: 'AI tools daily usage', percentage: 72, variant: 'partial' },
      { name: 'CRM hygiene and sales operations', percentage: 84, variant: 'matched' },
      { name: 'Marketing execution and content scheduling', percentage: 40, variant: 'missing' },
      { name: 'HR administration', percentage: 35, variant: 'missing' },
      { name: 'Finance coordination', percentage: 58, variant: 'partial' },
      { name: 'Process ownership and improvement', percentage: 60, variant: 'partial' },
      { name: 'Agent-driven process automation', percentage: 44, variant: 'missing' },
      { name: 'Founder support and scheduling', percentage: 25, variant: 'missing' },
    ],
    strengths: ['CRM data hygiene', 'Spreadsheet automation'],
    gaps: ['Written English polish', 'Marketing execution', 'HR administration'],
  },
  {
    id: 'c16', jobId: 'j3', name: 'Jessica Wong', initials: 'JW',
    headline: 'Business Administration graduate', company: 'Internships', location: 'Singapore, SG',
    years: 1, applied: 'Jun 27', appliedDate: '2026-06-27',
    ats: 54, skills_pct: 34, decision: 'rejected', risk: 'High',
    skillbars: [
      { name: 'Fluent English written and spoken', percentage: 94, variant: 'matched' },
      { name: 'Google Workspace', percentage: 70, variant: 'partial' },
      { name: 'AI tools daily usage', percentage: 66, variant: 'partial' },
      { name: 'CRM hygiene and sales operations', percentage: 22, variant: 'missing' },
      { name: 'Marketing execution and content scheduling', percentage: 35, variant: 'missing' },
      { name: 'HR administration', percentage: 20, variant: 'missing' },
      { name: 'Finance coordination', percentage: 15, variant: 'missing' },
      { name: 'Process ownership and improvement', percentage: 28, variant: 'missing' },
      { name: 'Founder support and scheduling', percentage: 30, variant: 'missing' },
      { name: 'Legal and compliance coordination', percentage: 12, variant: 'missing' },
    ],
    strengths: ['Fluent English', 'Fast learner'],
    gaps: ['Below 2 yrs experience', 'No ops ownership', 'Finance coordination'],
  },
];

/* ---------------- generator for new applicants (apply form) ---------------- */
const FIRST = ['Liam', 'Noah', 'Olivia', 'Emma', 'Aarav', 'Mia', 'Lucas', 'Sophia', 'Ethan', 'Isabella', 'Raj', 'Ananya', 'Wei', 'Yuki', 'Diego', 'Lucia', 'Omar', 'Layla', 'Nina', 'Pavel', 'Sara', 'Ivan', 'Maya', 'Chen'];
const LAST = ['Smith', 'Patel', 'Kim', 'Wang', 'Garcia', 'Rossi', 'Mueller', 'Silva', 'Khan', 'Dubois', 'Nguyen', 'Tanaka', 'Costa', 'Haddad', 'Novak', 'Singh', 'Park', 'Lopez', 'Ferrari', 'Schmidt'];
const COMPANIES = ['Grab', 'Gojek', 'Sea Group', 'Shopee', 'FPT Software', 'VNG', 'NashTech', 'GovTech Singapore', 'Accenture', 'Deloitte', 'Globe Telecom', 'Katalon', 'Independent', 'Contract'];
const LOCS = ['Ho Chi Minh City, VN', 'Hanoi, VN', 'Singapore, SG', 'Jakarta, ID', 'Manila, PH', 'Kuala Lumpur, MY', 'Bangkok, TH', 'Da Nang, VN', 'Bengaluru, IN', 'Remote'];

function seeded(seed) { let s = (seed * 2654435761) % 2147483647; if (s <= 0) s += 2147483646; return () => (s = s * 16807 % 2147483647) / 2147483647; }
function pickR(r, arr) { return arr[Math.floor(r() * arr.length)]; }
function initialsOf(name) { const p = name.split(' '); return (p[0][0] + p[p.length - 1][0]).toUpperCase(); }

function genCandidate(job, i, seedExtra) {
  const r = seeded(job.id.charCodeAt(1) * 977 + i * 131 + 7 + (seedExtra || 0));
  const name = pickR(r, FIRST) + ' ' + pickR(r, LAST);
  const years = 4 + Math.floor(r() * 11);
  const strength = r();
  function vary(weight) {
    const x = r() * 0.5 + weight * 0.5;
    if (x > 0.64) return ['matched', 80 + Math.floor(r() * 18)];
    if (x > 0.4) return ['partial', 48 + Math.floor(r() * 22)];
    return ['missing', 12 + Math.floor(r() * 24)];
  }
  const skillbars = [];
  job.mustHave.forEach((n) => { const [v, p] = vary(strength); skillbars.push({ name: n, percentage: p, variant: v }); });
  job.preferred.forEach((n) => { const [v, p] = vary(strength * 0.8); skillbars.push({ name: n, percentage: p, variant: v }); });
  const mustMatched = skillbars.slice(0, job.mustHave.length).filter((s) => s.variant === 'matched').length;
  const avg = Math.round(skillbars.reduce((s, x) => s + x.percentage, 0) / skillbars.length);
  const ats = clampN(38 + avg * 0.5 + mustMatched * 6 + strength * 8, 42, 97);
  const skills_pct = clampN((mustMatched / job.mustHave.length) * 60 + avg * 0.35, 20, 98);
  const decision = ats >= 80 ? 'shortlisted' : ats >= 66 ? 'borderline' : 'rejected';
  const risk = ats >= 85 ? 'Low' : ats >= 70 ? 'Medium' : 'High';
  const matchedNames = skillbars.filter((s) => s.variant === 'matched').map((s) => s.name);
  const missNames = skillbars.filter((s) => s.variant === 'missing').map((s) => s.name);
  const day = 6 + Math.floor(r() * 18);
  return {
    id: job.id + '_c' + i, jobId: job.id, name, initials: initialsOf(name),
    headline: job.roleNoun, company: pickR(r, COMPANIES), location: pickR(r, LOCS),
    years, applied: 'Jun ' + day, appliedDate: `2026-06-${String(day).padStart(2, '0')}`,
    ats, skills_pct, decision, risk, skillbars,
    strengths: (matchedNames.length ? matchedNames : skillbars.slice(0, 2).map((s) => s.name)).slice(0, 4),
    gaps: (missNames.length ? missNames : [job.preferred[0] || 'Seniority']).slice(0, 3),
  };
}

const CANDIDATES = [...CANDS_J1, ...CANDS_J2, ...CANDS_J3];

/* applicant count is derived from candidates */
function applicantsFor(jobId, pool) { return (pool || CANDIDATES).filter((c) => c.jobId === jobId).length; }

/* ---------------- Interviews ---------------- */
const INTERVIEWS = [
  { id: 'iv1', candidateId: 'c1', jobId: 'j1', date: '2026-07-07', start: '14:00', end: '15:00', kind: 'Technical · agent architecture', mode: 'Video', panel: ['MA', 'TL'] },
  { id: 'iv2', candidateId: 'c3', jobId: 'j1', date: '2026-07-07', start: '16:30', end: '17:15', kind: 'System design · RAG & tool use', mode: 'Video', panel: ['MA'] },
  { id: 'iv3', candidateId: 'c2', jobId: 'j1', date: '2026-07-08', start: '10:00', end: '11:00', kind: 'Platform deep-dive', mode: 'Video', panel: ['TL'] },
  { id: 'iv4', candidateId: 'c7', jobId: 'j2', date: '2026-07-08', start: '13:00', end: '13:45', kind: 'Delivery case study', mode: 'Video', panel: ['MA'] },
  { id: 'iv5', candidateId: 'c8', jobId: 'j2', date: '2026-07-09', start: '11:00', end: '11:30', kind: 'Screening call', mode: 'Phone', panel: ['MA'] },
  { id: 'iv6', candidateId: 'c12', jobId: 'j3', date: '2026-07-09', start: '15:00', end: '15:30', kind: 'Screening call', mode: 'Phone', panel: ['MA'] },
  { id: 'iv7', candidateId: 'c1', jobId: 'j1', date: '2026-07-10', start: '09:30', end: '10:30', kind: 'Final · founder round', mode: 'Video', panel: ['MA', 'TL'] },
  { id: 'iv8', candidateId: 'c13', jobId: 'j3', date: '2026-07-10', start: '14:00', end: '14:30', kind: 'Ops scenario walkthrough', mode: 'Video', panel: ['MA'] },
];

const DECISION_META = {
  shortlisted: { label: 'Shortlisted', verdict: 'AI recommends proceeding to interview.' },
  borderline: { label: 'Borderline', verdict: 'Requires manual review before a decision.' },
  rejected: { label: 'Rejected', verdict: 'Below threshold for this role.' },
};

/* must-have / preferred coverage for a candidate against a job */
function matchCounts(c, job) {
  if (!job) return { must: 0, mustTotal: 0, pref: 0, prefTotal: 0 };
  const byName = {}; c.skillbars.forEach((s) => { byName[s.name] = s; });
  const must = job.mustHave.filter((n) => byName[n] && byName[n].variant === 'matched').length;
  const pref = job.preferred.filter((n) => byName[n] && byName[n].variant !== 'missing').length;
  return { must, mustTotal: job.mustHave.length, pref, prefTotal: job.preferred.length };
}

const JOB = JOBS[0]; // legacy default

Object.assign(window, {
  JOBS, CANDIDATES, INTERVIEWS, JOB, DECISION_META, STAGE_FLOW,
  matchCounts, genCandidate, applicantsFor, initialsOf,
});
