// devex-data.jsx — adapted for real backend data injected via window.DEVEX_INIT

const SERVICE_ICON_MAP = {
  kuma:       'Shield',
  monitoring: 'Globe',
  n8n:        'Route',
  portainer:  'Container',
  vault:      'Lock',
  openclaw:   'Terminal',
  code:       'Code',
  postgres:   'Database',
  listmonk:   'Mail',
  authentik:  'Eye',
};

const TEMPLATE_ICON_MAP = {
  'github-repo-template':   'GitBranch',
  'terraform-repo-template': 'Cloud',
  'eks-iac-template':        'Stack',
};

const _init = window.DEVEX_INIT || {};

const SERVICES = Object.entries(_init.service_types || {}).map(([id, svc]) => ({
  id,
  name: svc.label,
  desc: svc.description,
  tag: '',
  icon: SERVICE_ICON_MAP[id] || 'Cube',
  schema: [],
}));

const TEMPLATES = Object.entries(_init.repo_templates || {}).map(([id, tmpl]) => ({
  id,
  name: tmpl.label,
  desc: tmpl.description,
  stack: [],
}));

const ADJ = ['silent','rapid','clever','dusty','frosty','gilded','humble','iron','lucid','molten','nimble','obsidian','plucky','quiet','rusted','swift','tidy','umbral','velvet','wired','zesty','crimson','amber','onyx','ember','prairie','arctic','rogue','copper','ivory'];
const NOUN = ['compiler','beacon','foundry','lantern','mailbox','orbit','pivot','quasar','runway','sentinel','telegram','utility','vector','workshop','xenon','yeoman','zenith','anvil','bastion','cipher','daemon','engine','forge','glyph','harbor'];

function randName() {
  const a = ADJ[Math.floor(Math.random() * ADJ.length)];
  const n = NOUN[Math.floor(Math.random() * NOUN.length)];
  return `${a}-${n}`;
}

const _userLogin = window.DEVEX_INIT?.user?.login || 'org';

const ACTIVITY = [
  { t: '2m',  icon: 'ok',     text: React.createElement(React.Fragment, null, React.createElement('span', {className:'repo'}, `${_userLogin}/silent-foundry`), ' created ', React.createElement('span', {className:'by'}, '· by mara')), kind: 'Check' },
  { t: '14m', icon: '',       text: React.createElement(React.Fragment, null, React.createElement('span', {className:'repo'}, `${_userLogin}/molten-runway`), ' deployed to staging ', React.createElement('span', {className:'by'}, '· auto')), kind: 'GitBranch' },
  { t: '38m', icon: '',       text: React.createElement(React.Fragment, null, React.createElement('span', {className:'repo'}, `${_userLogin}/nimble-orbit`), ' README updated ', React.createElement('span', {className:'by'}, '· by lin')), kind: 'Doc' },
  { t: '1h',  icon: 'accent', text: React.createElement(React.Fragment, null, React.createElement('span', {className:'repo'}, `${_userLogin}/iron-bastion`), ' scaffolded from ', React.createElement('span', {className:'by'}, 'vps-stack')), kind: 'Cube' },
  { t: '3h',  icon: '',       text: React.createElement(React.Fragment, null, React.createElement('span', {className:'repo'}, `${_userLogin}/copper-glyph`), ' archived ', React.createElement('span', {className:'by'}, '· by ravi')), kind: 'Folder' },
];

const STATS = [
  { label: 'Repos this week', value: '47', delta: '+12 vs last' },
  { label: 'Avg. scaffold',   value: '6.2s', delta: '-0.4s' },
  { label: 'Active templates', value: String(TEMPLATES.length || 3).padStart(2,'0'), delta: '' },
  { label: 'Services', value: String(SERVICES.length || 10).padStart(2,'0'), delta: '' },
];

const REPOS = [
  { id: 1, name: 'silent-foundry',  owner: 'mara.k',  template: 'vps-stack',    services: ['traefik','postgres','next'], created: '2m ago',  status: 'live',      endpoint: 'silent-foundry.apps.example.com', ci: 'success', updateAvail: false, version: 'v3.2.1' },
  { id: 2, name: 'molten-runway',   owner: 'lin.h',   template: 'vps-stack',    services: ['traefik','postgres','redis','fastapi'], created: '14m ago', status: 'deploying', endpoint: 'molten-runway.apps.example.com',  ci: 'running', updateAvail: false, version: 'v3.2.1' },
  { id: 3, name: 'nimble-orbit',    owner: 'mara.k',  template: 'service-go',   services: ['fastapi'], created: '2h ago',  status: 'live',      endpoint: 'api.nimble-orbit.dev',              ci: 'success', updateAvail: true,  version: 'v3.1.4' },
  { id: 4, name: 'iron-bastion',    owner: 'ravi.p',  template: 'vps-stack',    services: ['traefik','postgres','authelia','minio'], created: '1d ago',  status: 'live',      endpoint: 'iron-bastion.apps.example.com',   ci: 'success', updateAvail: true,  version: 'v3.0.9' },
  { id: 5, name: 'copper-glyph',    owner: 'lin.h',   template: 'lib-ts',       services: [], created: '3d ago',  status: 'archived',  endpoint: '—',                                 ci: 'skipped', updateAvail: false, version: 'v3.2.1' },
  { id: 6, name: 'frosty-anvil',    owner: 'jules.t', template: 'iac-aws',      services: [], created: '4d ago',  status: 'live',      endpoint: '—',                                 ci: 'failed',  updateAvail: true,  version: 'v3.1.0' },
  { id: 7, name: 'amber-cipher',    owner: 'mara.k',  template: 'vps-stack',    services: ['traefik','postgres'], created: '1w ago',  status: 'live',      endpoint: 'amber-cipher.apps.example.com',   ci: 'success', updateAvail: false, version: 'v3.2.1' },
  { id: 8, name: 'velvet-quasar',   owner: 'ravi.p',  template: 'service-go',   services: ['fastapi','redis'], created: '2w ago',  status: 'live',      endpoint: 'velvet-quasar.dev',               ci: 'success', updateAvail: true,  version: 'v3.1.7' },
];

const UPDATES = [
  { id: 'pr-401', repo: 'iron-bastion', title: 'bump traefik 3.1 → 3.2 (security)', kind: 'security', from: 'v3.0.9', to: 'v3.2.1', files: 3, additions: 18, deletions: 12, opened: '2h ago', status: 'open',
    diff: [
      { f: 'docker-compose.yml', d: '- image: traefik:v3.1\n+ image: traefik:v3.2' },
      { f: 'traefik/dynamic.yml', d: '- # legacy header\n+ headers:\n+   contentSecurityPolicy: ...' },
    ]},
  { id: 'pr-402', repo: 'nimble-orbit', title: 'bump postgres 16.1 → 16.2', kind: 'patch', from: 'v3.1.4', to: 'v3.2.1', files: 1, additions: 1, deletions: 1, opened: '5h ago', status: 'open',
    diff: [{ f: 'docker-compose.yml', d: '- image: postgres:16.1-alpine\n+ image: postgres:16.2-alpine' }]},
  { id: 'pr-403', repo: 'frosty-anvil', title: 'rotate OIDC roles to v2 schema', kind: 'breaking', from: 'v3.1.0', to: 'v3.2.1', files: 6, additions: 88, deletions: 42, opened: '1d ago', status: 'review',
    diff: [{ f: 'terraform/modules/oidc/main.tf', d: '- resource "aws_iam_role" ...\n+ resource "aws_iam_role" ... (v2)' }]},
  { id: 'pr-404', repo: 'velvet-quasar', title: 'add OTel SDK auto-instrumentation', kind: 'feature', from: 'v3.1.7', to: 'v3.2.1', files: 2, additions: 24, deletions: 0, opened: '2d ago', status: 'open',
    diff: [{ f: 'cmd/server/main.go', d: '+ import "go.opentelemetry.io/otel"' }]},
];

const TEMPLATES_EXPANDED = [
  ...TEMPLATES,
  { id: 'minio',    name: 'MinIO',    desc: 'S3-compatible object storage with built-in console, lifecycle rules, and Traefik TLS.', stack: ['MinIO', 'Traefik', 'Cron Backup'], category: 'Storage', author: 'devex-core', usage: 142 },
  { id: 'metabase', name: 'Metabase', desc: 'Self-hosted BI dashboard. Auto-wired to Postgres if present; SSO via Authentik optional.', stack: ['Metabase', 'Postgres', 'SSO'], category: 'Analytics', author: 'devex-core', usage: 38 },
  { id: 'gitea',    name: 'Gitea',    desc: 'Self-hosted Git hosting with Actions runner, mirroring, and webhooks.', stack: ['Gitea', 'Postgres', 'Runner'], category: 'DevOps', author: 'devex-core', usage: 24 },
  { id: 'drone',    name: 'Drone CI', desc: 'Container-native pipelines. Pairs with Gitea or GitHub. Pluggable runners.', stack: ['Drone', 'Runner', 'Postgres'], category: 'DevOps', author: 'devex-core', usage: 19 },
];

const DEPLOY_SPARK = [3, 5, 4, 7, 6, 9, 8, 11, 9, 12, 10, 14, 13, 17];

window.DEVEX_DATA = { SERVICES, TEMPLATES, TEMPLATES_EXPANDED, ADJ, NOUN, randName, ACTIVITY, STATS, REPOS, UPDATES, DEPLOY_SPARK };
