/**
 * Return the static Atlas hardware inventory used as fallback data.
 *
 * WHY: the home page needs stable content when live cluster data cannot be
 * fetched during startup or testing.
 */
export function fallbackHardware() {
  return {
    clusters: [
      {
        name: "atlas",
        role: "Flux-managed Kubernetes for bstein.dev services",
        nodes: [
          { name: "titan-0a", role: "control-plane (leader)", hardware: "rpi5", status: "ready" },
          { name: "titan-0b", role: "control-plane", hardware: "rpi5", status: "ready" },
          { name: "titan-0c", role: "control-plane", hardware: "rpi5", status: "ready" },
          { name: "titan-04", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-05", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-06", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-07", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-08", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-09", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-10", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-11", role: "worker", hardware: "rpi5", status: "ready" },
          { name: "titan-12", role: "worker (HDD astreae)", hardware: "rpi4", status: "ready" },
          { name: "titan-13", role: "worker (HDD astreae)", hardware: "rpi4", status: "ready" },
          { name: "titan-14", role: "worker (HDD astreae)", hardware: "rpi4", status: "ready" },
          { name: "titan-15", role: "worker (HDD astreae)", hardware: "rpi4", status: "ready" },
          { name: "titan-16", role: "worker", hardware: "rpi4", status: "offline" },
          { name: "titan-17", role: "worker (HDD asteria)", hardware: "rpi4", status: "ready" },
          { name: "titan-18", role: "worker (HDD asteria)", hardware: "rpi4", status: "ready" },
          { name: "titan-19", role: "worker (HDD asteria)", hardware: "rpi4", status: "ready" },
          { name: "titan-20", role: "jetson ai workload", hardware: "jetson xavier", status: "ready" },
          { name: "titan-21", role: "jetson ai workload", hardware: "jetson xavier", status: "ready" },
          { name: "titan-22", role: "gpu mini-pc (jellyfin)", hardware: "mini pc", status: "ready" },
          { name: "titan-23", alias: "oceanus", role: "worker (upcoming AI workload staging)", hardware: "epyc-24c", status: "ready" },
          { name: "titan-24", alias: "tethys", role: "worker (utility + metrics)", hardware: "ryzen-3900x", status: "ready" },
        ],
      },
    ],
    specialty: [
      { name: "titan-db", alias: "atlas-db", role: "control-plane database (postgres)", hardware: "rpi5", status: "active" },
      { name: "titan-jh", alias: "theia", role: "bastion / KVM landing / lesavka", status: "active", hardware: "rpi5" },
    ],
  };
}

/**
 * Return the curated service catalog shown on the home page when live data is absent.
 *
 * WHY: the service grid must stay useful without a live backend response.
 */
export function fallbackServices() {
  return {
    services: [
      {
        name: "Keycloak",
        icon: "🔐",
        category: "identity",
        summary: "Unified accounts for Single Sign-On. OIDC & LDAP",
        link: "https://sso.bstein.dev",
      },
      {
        name: "Nextcloud",
        icon: "☁️",
        category: "productivity",
        summary: "Storage, office, and mail apps for bstein.dev users.",
        link: "https://cloud.bstein.dev",
      },
      {
        name: "Outline",
        icon: "📝",
        category: "productivity",
        summary: "Team docs and knowledge base for Atlas users.",
        link: "https://notes.bstein.dev",
      },
      {
        name: "Planka",
        icon: "🧭",
        category: "productivity",
        summary: "Kanban planning boards for tasks and projects.",
        link: "https://tasks.bstein.dev",
      },
      {
        name: "VaultWarden",
        icon: "🔑",
        category: "security",
        summary: "Open Source & private password manager.",
        link: "https://vault.bstein.dev",
      },
      {
        name: "Vault",
        icon: "🗝️",
        category: "security",
        summary: "Secrets for secure environment injection.",
        link: "https://secret.bstein.dev",
      },
      {
        name: "Element",
        icon: "💬",
        category: "comms",
        summary: "Discord style communication system for users.",
        link: "https://live.bstein.dev",
        host: "live.bstein.dev",
        status: "live",
      },
      {
        name: "Matrix (Synapse)",
        icon: "🧩",
        category: "comms",
        summary: "Encrypted chat backend with bot integrations.",
        link: "https://live.bstein.dev",
        host: "matrix.live.bstein.dev",
        status: "live",
      },
      {
        name: "LiveKit",
        icon: "🎥",
        category: "comms",
        summary: "Video and Voice SFU calls integrated into Element.",
        link: "https://live.bstein.dev",
        host: "kit.live.bstein.dev",
        status: "live",
      },
      {
        name: "Coturn",
        icon: "📡",
        category: "comms",
        summary: "Integrated VoIP server for reliable calls.",
        link: "https://live.bstein.dev",
        host: "turn.live.bstein.dev",
        status: "live",
      },
      {
        name: "Mailu",
        icon: "✉️",
        category: "mail",
        summary: "Self-hosted mailserver with accepted outgoing mail.",
        link: "https://mail.bstein.dev",
        host: "mail.bstein.dev",
      },
      {
        name: "Jellyfin",
        icon: "🎬",
        category: "streaming",
        summary: "Family movies streaming server with GPU acceleration.",
        link: "https://stream.bstein.dev",
      },
      {
        name: "Pegasus",
        icon: "📤",
        category: "streaming",
        summary: "Upload pipeline feeding the Jellyfin library.",
        link: "https://pegasus.bstein.dev",
      },
      {
        name: "Wger",
        icon: "🏋️",
        category: "health",
        summary: "Workout + nutrition tracking with the wger mobile app.",
        link: "https://health.bstein.dev",
      },
      {
        name: "Actual Budget",
        icon: "💸",
        category: "finance",
        summary: "Local-first budgeting with Keycloak SSO.",
        link: "https://budget.bstein.dev",
      },
      {
        name: "Firefly III",
        icon: "💵",
        category: "finance",
        summary: "Personal finance manager with Abacus mobile sync.",
        link: "https://money.bstein.dev",
      },
      {
        name: "Grafana",
        icon: "📈",
        category: "observability",
        summary: "Atlas metrics and dashboards for the lab.",
        link: "https://metrics.bstein.dev",
      },
      {
        name: "OpenSearch",
        icon: "🔎",
        category: "observability",
        summary: "Centralized Atlas services logs. Powered by Fluent Bit.",
        link: "https://logs.bstein.dev",
      },
      {
        name: "Gitea",
        icon: "🍵",
        category: "dev",
        summary: "Source control for dev projects.",
        link: "https://scm.bstein.dev",
      },
      {
        name: "Jenkins",
        icon: "🏗️",
        category: "dev",
        summary: "Continuous integration build pipelines.",
        link: "https://ci.bstein.dev",
      },
      {
        name: "Harbor",
        icon: "⚓",
        category: "dev",
        summary: "Artifact Registry for build artifacts.",
        link: "https://registry.bstein.dev",
      },
      {
        name: "Flux",
        icon: "🧲",
        category: "dev",
        summary: "GitOps UI for flux continuous deployment drift prevention.",
        link: "https://cd.bstein.dev",
      },
      {
        name: "Monero",
        icon: "🪙",
        category: "crypto",
        summary: "Private monero node for monero wallets.",
        link: "https://bstein.dev/monero",
        host: "monerod.crypto.svc.cluster.local:18081",
      },
      {
        name: "AI Chat",
        icon: "🤖",
        category: "ai",
        summary: "Customized LLM for the titan home lab.",
        link: "https://bstein.dev/ai/chat",
        host: "chat.ai.bstein.dev",
        status: "live",
      },
      {
        name: "Cassandra",
        icon: "🃏",
        category: "ai",
        summary: "Magic: The Gathering AI playtester for deck construction.",
        link: "https://cassandra.bstein.dev/",
        host: "cassandra.bstein.dev",
        status: "live",
      },
      {
        name: "AI Vision",
        icon: "👁️",
        category: "ai",
        summary: "Visualization tool - Planned",
        link: "/ai/roadmap",
        host: "vision.ai.bstein.dev",
        status: "planned",
      },
      {
        name: "AI Speech",
        icon: "🎙️",
        category: "ai",
        summary: "Live Translation - Planned",
        link: "/ai/roadmap",
        host: "talk.ai.bstein.dev",
        status: "planned",
      },
      {
        name: "AI Translation",
        icon: "🌐",
        category: "ai",
        summary: "Inline translation (roadmap) for comms.",
        link: "/ai/roadmap",
        host: "translate.ai.bstein.dev",
        status: "planned",
      },
    ],
  };
}

/**
 * Return the static ingress and egress relationships that power the network diagram.
 *
 * WHY: the topology diagram needs deterministic fallback data for offline runs.
 */
export function fallbackNetwork() {
  return {
    ingress: [
      {
        name: "dns_and_tls",
        path: "DNS -> Traefik -> oauth2-proxy -> service",
        notes: "TLS via cert-manager; Keycloak on the edge.",
      },
      {
        name: "media",
        path: "media.bstein.dev -> Traefik -> Pegasus -> Longhorn PVC -> Jellyfin",
        notes: "Uploads and playback with storage on Longhorn.",
      },
      {
        name: "registry",
        path: "registry.bstein.dev -> Traefik -> Harbor -> FluxCD pull",
        notes: "Harbor issues signed images for GitOps.",
      },
    ],
    egress: [
      {
        name: "ci_to_registry",
        path: "Gitea webhook -> Jenkins -> Harbor push",
        notes: "Builds signed before promotion.",
      },
      {
        name: "metrics",
        path: "Atlas scraping -> Prometheus -> Grafana -> metrics.bstein.dev",
        notes: "Cluster and service telemetry feed public dashboards where safe.",
      },
    ],
    ingress_gateway: "Traefik with oauth2-proxy and Keycloak; Longhorn backs stateful ingress targets.",
  };
}

/**
 * Return the Atlas metrics summary card content used on the overview page.
 *
 * WHY: the metrics cards should still render a coherent overview if live
 * dashboard links are unavailable.
 */
export function fallbackMetrics() {
  return {
    dashboard: "https://metrics.bstein.dev",
    description: "Atlas platform metrics.",
  };
}

/**
 * Render the hardware topology diagram used on the home page.
 *
 * WHY: the landing page needs a deterministic Mermaid diagram even before
 * live cluster state is available.
 *
 * @param {object} _data - Live hardware state, accepted for future shaping.
 * @returns {string} Mermaid flowchart text for the Atlas hardware overview.
 */
export function buildHardwareDiagram(_data) {
  return `
flowchart LR
  Users["Members + public visitors"] --> Edge["DNS / TLS / Traefik"]

  subgraph TitanLab["Titan Lab"]
    subgraph Atlas["Atlas k3s cluster"]
      Control["HA control plane"]
      Workers["service workers"]
      Storage["Longhorn storage pool"]
      Heavy["AI / media / utility nodes<br/>Oceanus + Tethys included"]
      Services["member services<br/>identity / cloud / media / CI / registry / AI"]
    end

    subgraph Dedicated["Dedicated non-cluster hosts"]
      DB["database node"]
      Jump["jumphost / KVM landing"]
    end
  end

  Edge --> Services
  Control --> Workers
  Workers --> Services
  Services --> Storage
  Heavy --> Services
  DB --> Control
  Jump --> Control

  classDef cluster fill:#0d1a2b,stroke:#4fb3ff,color:#eef6ff;
  classDef edge fill:#082421,stroke:#00e5c5,color:#effffc;
  classDef dedicated fill:#21182b,stroke:#b58cff,color:#f8f2ff;
  class TitanLab,Atlas,Control,Workers,Storage,Heavy,Services cluster;
  class Users,Edge edge;
  class Dedicated,DB,Jump dedicated;
`;
}

/**
 * Render the ingress and auth sequence for the portal network flow.
 *
 * WHY: the home page should explain request routing without depending on live
 * cluster state.
 */
export function buildNetworkDiagram() {
  return `
flowchart LR
  Browser["browser"] --> DNS["DNS + TLS"]
  DNS --> Traefik["Traefik ingress"]

  Traefik --> Public["public routes<br/>home / metrics / selected apps"]
  Traefik --> Auth["auth gate<br/>oauth2-proxy"]
  Auth --> Keycloak["Keycloak SSO"]
  Keycloak --> Auth
  Auth --> Member["member services"]

  Member --> Storage["Longhorn PVCs"]
  Member --> Logs["logs / metrics"]
  Logs --> Grafana["Grafana dashboards"]

  classDef public fill:#082421,stroke:#00e5c5,color:#effffc;
  classDef protected fill:#22172b,stroke:#b58cff,color:#f8f2ff;
  classDef data fill:#1e2430,stroke:#7aa2ff,color:#eef6ff;
  class Browser,DNS,Traefik,Public public;
  class Auth,Keycloak,Member protected;
  class Storage,Logs,Grafana data;
`;
}

/**
 * Render the delivery pipeline from developer push to Flux reconciliation.
 *
 * WHY: the overview page needs a compact visual of the release path even when
 * the CI backend is not reachable.
 */
export function buildPipelineDiagram() {
  return `
flowchart LR
  Source["scm.bstein.dev<br/>source of truth"] --> CI["Jenkins<br/>build + test"]
  CI --> Image["Harbor<br/>image registry"]
  Image --> GitOps["Flux<br/>reconcile desired state"]
  GitOps --> Atlas["Atlas<br/>running workloads"]

  CI --> Gates["quality gates<br/>tests / lint / reports"]
  Atlas --> Observe["Prometheus + Grafana<br/>health and evidence"]
  Observe --> Feedback["fix / tune / automate"]
  Feedback --> Source

  Keycloak["Keycloak SSO"] --> Source
  Keycloak --> CI
  Keycloak --> Image
  Keycloak --> GitOps

  classDef source fill:#082421,stroke:#00e5c5,color:#effffc;
  classDef delivery fill:#0d1a2b,stroke:#4fb3ff,color:#eef6ff;
  classDef guard fill:#22172b,stroke:#b58cff,color:#f8f2ff;
  class Source,Feedback source;
  class CI,Image,GitOps,Atlas,Observe delivery;
  class Gates,Keycloak guard;
`;
}
