// MCP Hero — mock data
// All sample data for the prototype. Exposed on window.MH_DATA.

const MH_CATEGORIES = [
  { id: 'all', label: 'All' },
  { id: 'business-data', label: 'Business Data', hue: 222 },
  { id: 'legal', label: 'Legal', hue: 270 },
  { id: 'finance', label: 'Finance', hue: 152 },
  { id: 'productivity', label: 'Productivity', hue: 38 },
];

const MH_MCPS = [
  {
    slug: 'ssm',
    name: 'SSM Company Profile',
    tagline: 'Official Malaysian company profiles, straight from SSM.',
    description: 'Fetch official company profile reports from Suruhanjaya Syarikat Malaysia (SSM) — registration details, financial statements, officers, shareholders, and charges — returned as structured JSON your AI agent can reason over.',
    category: 'business-data',
    icon: 'building',
    iconHue: 222,
    fromCredits: 2,
    free: false,
    installs: 1284,
    featured: true,
    status: 'active',
    revenue: 18430,
    callsToday: 312,
    useCases: [
      'Due diligence: "Analyze Agmo Studio 974707-M before we sign the contract."',
      'KYB onboarding: verify a supplier\u2019s registration, directors and paid-up capital.',
      'Credit checks: pull financial statements and charges for a loan assessment.',
    ],
    tools: [
      { name: 'request_ssm_profile', desc: 'Full company profile report — registration, financials, officers, shareholders, charges.', credits: 10 },
      { name: 'search_entity', desc: 'Search companies by name or registration number.', credits: 2 },
      { name: 'list_companies', desc: 'List previously retrieved companies in your account.', credits: 0 },
    ],
  },
  {
    slug: 'court-search',
    name: 'Court Case Search',
    tagline: 'Malaysian civil & commercial court case lookups.',
    description: 'Search Malaysian court records by party name or case number. Returns case summaries, hearing dates, and judgment status for civil and commercial matters.',
    category: 'legal',
    icon: 'scale',
    iconHue: 270,
    fromCredits: 4,
    free: false,
    installs: 521,
    status: 'active',
    revenue: 6210,
    callsToday: 88,
    useCases: [
      'Litigation checks during vendor due diligence.',
      'Monitor ongoing cases involving a counterparty.',
    ],
    tools: [
      { name: 'search_cases', desc: 'Search cases by party name or company BRN.', credits: 4 },
      { name: 'get_case_detail', desc: 'Full case detail with hearing history.', credits: 8 },
    ],
  },
  {
    slug: 'einvoice',
    name: 'e-Invoice Validator',
    tagline: 'Validate LHDN MyInvois e-invoices before submission.',
    description: 'Validate e-invoice payloads against LHDN MyInvois rules — TIN checks, field validation, and submission-readiness reports. Catch rejections before they happen.',
    category: 'finance',
    icon: 'receipt',
    iconHue: 152,
    fromCredits: 1,
    free: false,
    installs: 947,
    status: 'active',
    revenue: 4120,
    callsToday: 203,
    useCases: [
      'Pre-flight check invoices generated by your billing system.',
      'Verify a supplier TIN before issuing a self-billed invoice.',
    ],
    tools: [
      { name: 'validate_invoice', desc: 'Validate a UBL invoice payload against MyInvois rules.', credits: 1 },
      { name: 'check_tin', desc: 'Verify a taxpayer identification number.', credits: 2 },
      { name: 'list_rules', desc: 'List current validation rule set.', credits: 0 },
    ],
  },
  {
    slug: 'tax-helper',
    name: 'LHDN Tax Helper',
    tagline: 'Malaysian tax rates, deadlines and computations.',
    description: 'Query Malaysian corporate and personal tax rates, filing deadlines, and run estimated tax computations. Kept current with LHDN gazettes.',
    category: 'finance',
    icon: 'calculator',
    iconHue: 152,
    fromCredits: 0,
    free: true,
    installs: 2103,
    status: 'active',
    revenue: 1830,
    callsToday: 451,
    useCases: [
      'Estimate CP204 instalments for the coming year of assessment.',
      'Check SME tax rate eligibility for a client.',
    ],
    tools: [
      { name: 'get_tax_rates', desc: 'Current corporate & personal tax rate tables.', credits: 0 },
      { name: 'compute_tax', desc: 'Estimated tax computation from P&L figures.', credits: 3 },
      { name: 'filing_deadlines', desc: 'Filing deadlines for a given entity type.', credits: 0 },
    ],
  },
  {
    slug: 'watchlist',
    name: 'Company Watchlist',
    tagline: 'Monitor companies for status & officer changes.',
    description: 'Subscribe to Malaysian companies and get notified of registration status changes, new charges, and officer appointments or resignations.',
    category: 'business-data',
    icon: 'eye',
    iconHue: 222,
    fromCredits: 5,
    free: false,
    installs: 318,
    status: 'active',
    revenue: 3905,
    callsToday: 42,
    useCases: [
      'Watch key suppliers for winding-up petitions or new charges.',
      'Track director changes across a portfolio of investees.',
    ],
    tools: [
      { name: 'add_to_watchlist', desc: 'Start monitoring a company by BRN.', credits: 5 },
      { name: 'get_watchlist_events', desc: 'Recent events for watched companies.', credits: 1 },
      { name: 'list_watchlist', desc: 'List companies you are watching.', credits: 0 },
    ],
  },
  {
    slug: 'legal-docs',
    name: 'MyLegal Templates',
    tagline: 'Malaysian-law agreement templates, agent-fillable.',
    description: 'Generate Malaysian-law contract drafts — NDAs, employment contracts, tenancy and service agreements — with clause-level controls your agent can fill from conversation context.',
    category: 'legal',
    icon: 'document',
    iconHue: 270,
    fromCredits: 6,
    free: false,
    installs: 689,
    status: 'active',
    revenue: 7740,
    callsToday: 117,
    useCases: [
      'Draft an NDA from a chat: parties, term, governing state.',
      'Generate a tenancy agreement with stamped-ready formatting.',
    ],
    tools: [
      { name: 'generate_agreement', desc: 'Generate a contract draft from a template + parameters.', credits: 6 },
      { name: 'list_templates', desc: 'Browse available agreement templates.', credits: 0 },
    ],
  },
  {
    slug: 'bm-translate',
    name: 'BM Translate',
    tagline: 'Formal Bahasa Malaysia translation for business docs.',
    description: 'Translate business and legal documents between English and Bahasa Malaysia with formal register, consistent terminology, and gazette-aligned legal vocabulary.',
    category: 'productivity',
    icon: 'language',
    iconHue: 38,
    fromCredits: 0,
    free: true,
    installs: 1576,
    status: 'active',
    revenue: 0,
    callsToday: 388,
    useCases: [
      'Translate a contract to BM for stamping.',
      'Localize product copy with consistent terminology.',
    ],
    tools: [
      { name: 'translate', desc: 'Translate text EN ↔ BM with formal register.', credits: 0 },
      { name: 'glossary_lookup', desc: 'Look up official BM legal/business terms.', credits: 0 },
    ],
  },
  {
    slug: 'property-data',
    name: 'Property Transactions',
    tagline: 'Malaysian property transaction records & valuations.',
    description: 'Query JPPH property transaction records — past transacted prices by area, property type, and date range — plus indicative valuations for comparables analysis.',
    category: 'business-data',
    icon: 'home',
    iconHue: 222,
    fromCredits: 3,
    free: false,
    installs: 264,
    status: 'draft',
    revenue: 0,
    callsToday: 0,
    useCases: [
      'Pull comparable transactions for a valuation memo.',
      'Track price trends in a township over 5 years.',
    ],
    tools: [
      { name: 'search_transactions', desc: 'Transaction records by area and property type.', credits: 3 },
      { name: 'get_valuation', desc: 'Indicative valuation from comparables.', credits: 12 },
    ],
  },
];

const MH_PACKAGES = [
  { id: 'starter', name: 'Starter', price: 10, credits: 100, bonus: null, note: '≈ 10 SSM reports' },
  { id: 'standard', name: 'Standard', price: 50, credits: 550, bonus: '+10% bonus', note: 'Most popular', popular: true },
  { id: 'pro', name: 'Pro', price: 100, credits: 1200, bonus: '+20% bonus', note: 'Best value per credit' },
  { id: 'custom', name: 'Custom', price: null, credits: null, bonus: null, note: 'Enterprise & bulk — talk to us' },
];

// ----- Account fixtures (per role) -----
const MH_ACCOUNTS = {
  personal: {
    name: 'Amir Hakim',
    email: 'amir@hakimventures.my',
    initials: 'AH',
    type: 'Personal',
    balance: 342,
    installedSlugs: ['ssm', 'einvoice', 'tax-helper'],
    callsThisMonth: 87,
    lastTopUp: { date: '2 Jun 2026', package: 'Standard', credits: 550 },
  },
  'org-admin': {
    name: 'Sarah Lim',
    email: 'sarah@kapitalpartners.my',
    initials: 'SL',
    type: 'Organization',
    orgName: 'Kapital Partners',
    role: 'Org Admin',
    balance: 1180,
    installedSlugs: ['ssm', 'court-search', 'legal-docs', 'watchlist'],
    callsThisMonth: 643,
    lastTopUp: { date: '28 May 2026', package: 'Pro', credits: 1200 },
    members: 5,
  },
  'org-editor': {
    name: 'Daniel Wong',
    email: 'daniel@kapitalpartners.my',
    initials: 'DW',
    type: 'Organization',
    orgName: 'Kapital Partners',
    role: 'Org Editor',
    balance: 1180,
    installedSlugs: ['ssm', 'court-search'],
    callsThisMonth: 214,
  },
};

// ----- Organization profile (Kapital Partners) -----
const MH_ORG_PROFILE = {
  legalName: 'Kapital Partners Sdn Bhd',
  displayName: 'Kapital Partners',
  regNo: '202101034567',
  emailDomain: 'kapitalpartners.my',
  domainVerified: true,
  website: 'https://www.kapitalpartners.my',
  industry: 'Financial Services',
  size: '11–50',
  phone: '+60 3-2710 8800',
  address: 'Level 18, Menara Kapital, Jalan Sultan Ismail, 50250 Kuala Lumpur',
  initials: 'KP',
  hue: 222,
};

const MH_INDUSTRIES = ['Financial Services', 'Legal', 'Accounting & Audit', 'Real Estate', 'Technology', 'Consulting', 'Retail & Commerce', 'Other'];
const MH_ORG_SIZES = ['1–10', '11–50', '51–200', '201–500', '500+'];

const MH_INSTALLED = [
  { slug: 'ssm', installed: '14 Mar 2026', lastCall: 'Today, 11:42', spent: 480, keyPrefix: 'mh_live_k8Xw', status: 'active' },
  { slug: 'einvoice', installed: '2 Apr 2026', lastCall: 'Yesterday, 16:05', spent: 63, keyPrefix: 'mh_live_p2Rn', status: 'active' },
  { slug: 'tax-helper', installed: '19 Apr 2026', lastCall: '6 Jun 2026', spent: 12, keyPrefix: 'mh_live_v5Tq', status: 'active' },
  { slug: 'court-search', installed: '8 May 2026', lastCall: 'Today, 09:18', spent: 196, keyPrefix: 'mh_live_z3Jd', status: 'active' },
  { slug: 'legal-docs', installed: '12 May 2026', lastCall: '4 Jun 2026', spent: 84, keyPrefix: 'mh_live_m7Cb', status: 'active' },
  { slug: 'watchlist', installed: '20 May 2026', lastCall: 'Today, 08:02', spent: 51, keyPrefix: 'mh_live_q9Fs', status: 'active' },
];

const MH_USAGE = [
  { ts: 'Today, 11:42', mcp: 'ssm', tool: 'request_ssm_profile', credits: 10, status: 'ok', member: 'Amir Hakim' },
  { ts: 'Today, 11:41', mcp: 'ssm', tool: 'search_entity', credits: 2, status: 'ok', member: 'Amir Hakim' },
  { ts: 'Today, 09:18', mcp: 'court-search', tool: 'search_cases', credits: 4, status: 'ok', member: 'Daniel Wong' },
  { ts: 'Today, 08:02', mcp: 'watchlist', tool: 'get_watchlist_events', credits: 1, status: 'ok', member: 'Sarah Lim' },
  { ts: 'Yesterday, 16:05', mcp: 'einvoice', tool: 'validate_invoice', credits: 1, status: 'ok', member: 'Amir Hakim' },
  { ts: 'Yesterday, 14:30', mcp: 'ssm', tool: 'request_ssm_profile', credits: 0, status: 'insufficient', member: 'Daniel Wong' },
  { ts: 'Yesterday, 10:12', mcp: 'legal-docs', tool: 'generate_agreement', credits: 6, status: 'ok', member: 'Mei Chen' },
  { ts: '8 Jun, 17:44', mcp: 'tax-helper', tool: 'get_tax_rates', credits: 0, status: 'ok', member: 'Amir Hakim' },
  { ts: '8 Jun, 15:21', mcp: 'ssm', tool: 'search_entity', credits: 2, status: 'ok', member: 'Priya Nair' },
  { ts: '8 Jun, 11:09', mcp: 'court-search', tool: 'get_case_detail', credits: 8, status: 'ok', member: 'Daniel Wong' },
  { ts: '7 Jun, 16:50', mcp: 'einvoice', tool: 'check_tin', credits: 2, status: 'ok', member: 'Amir Hakim' },
  { ts: '7 Jun, 09:35', mcp: 'ssm', tool: 'request_ssm_profile', credits: 10, status: 'ok', member: 'Sarah Lim' },
];

const MH_TRANSACTIONS = [
  { date: '2 Jun 2026', pkg: 'Standard', amount: 50, credits: 550, status: 'paid', gateway: 'Stripe' },
  { date: '28 May 2026', pkg: 'Pro', amount: 100, credits: 1200, status: 'paid', gateway: 'iPay88' },
  { date: '3 May 2026', pkg: 'Starter', amount: 10, credits: 100, status: 'paid', gateway: 'Stripe' },
  { date: '3 May 2026', pkg: 'Starter', amount: 10, credits: 0, status: 'abandoned', gateway: 'Stripe' },
  { date: '14 Apr 2026', pkg: 'Standard', amount: 50, credits: 550, status: 'paid', gateway: 'Razorpay' },
];

const MH_KEYS = [
  { mcp: 'ssm', prefix: 'mh_live_k8Xw', created: '14 Mar 2026', lastUsed: 'Today, 11:42', owner: 'Amir Hakim', status: 'active' },
  { mcp: 'einvoice', prefix: 'mh_live_p2Rn', created: '2 Apr 2026', lastUsed: 'Yesterday, 16:05', owner: 'Amir Hakim', status: 'active' },
  { mcp: 'tax-helper', prefix: 'mh_live_v5Tq', created: '19 Apr 2026', lastUsed: '6 Jun 2026', owner: 'Amir Hakim', status: 'active' },
  { mcp: 'ssm', prefix: 'mh_live_a1Pz', created: '2 Feb 2026', lastUsed: '13 Mar 2026', owner: 'Amir Hakim', status: 'revoked', revokedAt: '14 Mar 2026' },
];

const MH_TEAM = [
  { name: 'Sarah Lim', email: 'sarah@kapitalpartners.my', initials: 'SL', role: 'Admin', joined: '12 Jan 2026', lastActive: 'Today', creditsUsed: 218 },
  { name: 'Daniel Wong', email: 'daniel@kapitalpartners.my', initials: 'DW', role: 'Editor', joined: '15 Jan 2026', lastActive: 'Today', creditsUsed: 305 },
  { name: 'Mei Chen', email: 'mei@kapitalpartners.my', initials: 'MC', role: 'Editor', joined: '3 Feb 2026', lastActive: 'Yesterday', creditsUsed: 96 },
  { name: 'Priya Nair', email: 'priya@kapitalpartners.my', initials: 'PN', role: 'Admin', joined: '20 Feb 2026', lastActive: '5 Jun 2026', creditsUsed: 24 },
  { name: 'Hafiz Rahman', email: 'hafiz@kapitalpartners.my', initials: 'HR', role: 'Editor', joined: '1 Apr 2026', lastActive: '2 Jun 2026', creditsUsed: 0 },
];

const MH_PENDING_INVITES = [
  { email: 'aisyah@kapitalpartners.my', role: 'Editor', invited: '8 Jun 2026', expires: '10 Jun 2026' },
];

// ----- Admin portal fixtures -----
const MH_ADMIN_USERS = [
  { name: 'Kapital Partners', email: 'sarah@kapitalpartners.my', type: 'org', members: 5, registered: '12 Jan 2026', balance: 1180, status: 'active' },
  { name: 'Amir Hakim', email: 'amir@hakimventures.my', type: 'personal', registered: '8 Feb 2026', balance: 342, status: 'active' },
  { name: 'Lexora Law', email: 'ops@lexora.my', type: 'org', members: 12, registered: '22 Feb 2026', balance: 4210, status: 'active' },
  { name: 'Jason Teh', email: 'jason.teh@gmail.com', type: 'personal', registered: '3 Mar 2026', balance: 18, status: 'active' },
  { name: 'Nurul Izzah', email: 'nurul@bizcheck.my', type: 'personal', registered: '15 Mar 2026', balance: 0, status: 'active' },
  { name: 'Vantage Audit', email: 'admin@vantageaudit.my', type: 'org', members: 8, registered: '2 Apr 2026', balance: 760, status: 'suspended' },
  { name: 'Chong Wei Liang', email: 'cwl@outlook.com', type: 'personal', registered: '18 Apr 2026', balance: 95, status: 'active' },
  { name: 'Meridian Capital', email: 'it@meridiancap.my', type: 'org', members: 3, registered: '5 May 2026', balance: 2400, status: 'active' },
];

const MH_ADMIN_STATS = {
  mrr: 12840, mrrDelta: '+18%',
  creditsSold: 148200, creditsSoldDelta: '+12%',
  activeUsers: 1841, activeUsersDelta: '+9%',
  callsToday: 1601, callsTodayDelta: '+24%',
  revenueByWeek: [820, 940, 1180, 1050, 1390, 1620, 1480, 1840, 2010, 1930, 2240, 2480],
  callsByDay: [310, 420, 380, 510, 470, 290, 240, 530, 610, 580, 660, 720, 410, 380, 690, 740, 810, 770, 850, 920, 480, 440, 880, 940, 1010, 980, 1090, 1160, 1601, 1240],
  signupsByWeek: [12, 18, 15, 24, 31, 28, 36, 42, 39, 51, 58, 64],
  packageSplit: [ { name: 'Starter', pct: 22 }, { name: 'Standard', pct: 47 }, { name: 'Pro', pct: 28 }, { name: 'Custom', pct: 3 } ],
  gatewaySplit: [ { name: 'Stripe', pct: 61 }, { name: 'iPay88', pct: 27 }, { name: 'Razorpay', pct: 12 } ],
  funnel: [ { stage: 'Registered', n: 2412 }, { stage: 'Verified', n: 2068 }, { stage: 'First install', n: 1377 }, { stage: 'First top-up', n: 591 } ],
};

// ----- Admin team / access control -----
const MH_ADMIN_SCOPES = [
  { id: 'mcps', label: 'MCPs & pricing', desc: 'Create, edit, publish and disable marketplace listings.' },
  { id: 'users', label: 'User accounts', desc: 'View accounts, adjust credits, suspend or reactivate.' },
  { id: 'analytics', label: 'Analytics & reports', desc: 'Revenue, usage and growth dashboards. Export CSV.' },
  { id: 'billing', label: 'Billing & payouts', desc: 'Payment gateways, invoices and platform payouts.' },
  { id: 'team', label: 'Admin team', desc: 'Invite, edit and revoke other admin accounts.' },
];

// perm levels: 'full' (edit & update) · 'view' (read-only) · 'none'
const MH_ADMIN_ROLES = [
  { id: 'owner', label: 'Owner', tone: 'accent', desc: 'Unrestricted control of the platform, including billing and admin management.', system: true,
    perms: { mcps: 'full', users: 'full', analytics: 'full', billing: 'full', team: 'full' } },
  { id: 'admin', label: 'Admin', tone: 'accent', desc: 'Manage MCPs, users and analytics. Cannot manage billing or other admins.',
    perms: { mcps: 'full', users: 'full', analytics: 'full', billing: 'view', team: 'none' } },
  { id: 'editor', label: 'Editor', tone: 'ok', desc: 'Create and update MCP listings, pricing and content. No access to user accounts or billing.',
    perms: { mcps: 'full', users: 'none', analytics: 'view', billing: 'none', team: 'none' } },
  { id: 'analyst', label: 'Analyst', tone: 'neutral', desc: 'Read-only access to analytics and listings. Cannot make changes.',
    perms: { mcps: 'view', users: 'view', analytics: 'full', billing: 'none', team: 'none' } },
];

const MH_ADMIN_STAFF = [
  { name: 'Agmo Staff', email: 'you@agmogroup.com', initials: 'AG', role: 'owner', status: 'active', lastActive: 'Active now', joined: '1 Jan 2026', you: true },
  { name: 'Faizal Aziz', email: 'faizal@agmogroup.com', initials: 'FA', role: 'admin', status: 'active', lastActive: '12 min ago', joined: '4 Jan 2026' },
  { name: 'Lina Tan', email: 'lina@agmogroup.com', initials: 'LT', role: 'editor', status: 'active', lastActive: '2 hours ago', joined: '18 Feb 2026' },
  { name: 'Rajesh Kumar', email: 'rajesh@agmogroup.com', initials: 'RK', role: 'editor', status: 'active', lastActive: 'Yesterday', joined: '3 Mar 2026' },
  { name: 'Wong Mei Ling', email: 'meiling@agmogroup.com', initials: 'WM', role: 'analyst', status: 'active', lastActive: '4 Jun 2026', joined: '20 Apr 2026' },
  { name: 'Imran Shah', email: 'imran@agmogroup.com', initials: 'IS', role: 'admin', status: 'suspended', lastActive: '28 May 2026', joined: '15 Jan 2026' },
];

const MH_ADMIN_INVITES = [
  { email: 'sophia@agmogroup.com', role: 'editor', invited: '13 Jun 2026', expires: '15 Jun 2026' },
];

function mhAdminRole(id) { return MH_ADMIN_ROLES.find((r) => r.id === id) || MH_ADMIN_ROLES[1]; }

const MH_GATEWAY_URL = 'https://mcp-hero.agmogroup.com/mcp/';

function mhConfigSnippet(slug, key) {
  return JSON.stringify({
    mcpServers: {
      [slug]: {
        url: MH_GATEWAY_URL + slug,
        headers: { Authorization: 'Bearer ' + (key || 'mh_live_xxxxxxxxxxxxxxxx') },
      },
    },
  }, null, 2);
}

// ----- Credit scenarios (for demoing low / out-of-credits states) -----
const MH_CREDIT_SCENARIOS = ['Normal', 'Low', 'Empty'];
function mhAcct(role, scenario) {
  const base = MH_ACCOUNTS[role];
  if (!base) return base;
  if (scenario === 'Empty') return { ...base, balance: 0 };
  if (scenario === 'Low') return { ...base, balance: 8 };
  return base;
}

function mhGetMcp(slug) { return MH_MCPS.find((m) => m.slug === slug); }
function mhCatLabel(id) { const c = MH_CATEGORIES.find((c) => c.id === id); return c ? c.label : id; }
function mhCatHue(id) { const c = MH_CATEGORIES.find((c) => c.id === id); return c && c.hue != null ? c.hue : 222; }

window.MH_DATA = {
  MH_CATEGORIES, MH_MCPS, MH_PACKAGES, MH_ACCOUNTS, MH_INSTALLED, MH_USAGE,
  MH_TRANSACTIONS, MH_KEYS, MH_TEAM, MH_PENDING_INVITES, MH_ADMIN_USERS,
  MH_ADMIN_STATS, MH_GATEWAY_URL, mhConfigSnippet, mhGetMcp, mhCatLabel, mhCatHue,
  MH_ORG_PROFILE, MH_INDUSTRIES, MH_ORG_SIZES, MH_CREDIT_SCENARIOS, mhAcct,
  MH_ADMIN_SCOPES, MH_ADMIN_ROLES, MH_ADMIN_STAFF, MH_ADMIN_INVITES, mhAdminRole,
};
Object.assign(window, window.MH_DATA);
