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" },
        ],
      },
    ],
    specialty: [
      { name: "titan-db", alias: "atlas-db", role: "control-plane database (postgres)", hardware: "rpi5", status: "active" },
      { name: "titan-23", alias: "oceanus", role: "SUI validator (baremetal)", status: "active", hardware: "epyc-24c" },
      { name: "titan-24", alias: "tethys", role: "bridge node + scraper for oceanus metrics", status: "active", hardware: "ryzen-3900x" },
      { name: "titan-jh", alias: "theia", role: "bastion / KVM landing / lesavka", status: "active", hardware: "rpi5" },
    ],
  };
}

export function fallbackServices() {
  return {
    services: [
      {
        name: "Nextcloud Hub",
        category: "productivity",
        summary: "Core user hub: storage, office, bstein.dev mail, & more apps.",
        link: "https://cloud.bstein.dev",
      },
      {
        name: "Jellyfin",
        category: "media",
        summary: "Family Movies hosted on titan-22 for GPU acceleration.",
        link: "https://stream.bstein.dev",
      },
      {
        name: "VaultWarden",
        category: "security",
        summary: "Open Source & private password manager.",
        link: "https://vault.bstein.dev",
      },
      {
        name: "Keycloak",
        category: "identity",
        summary: "Unified accounts for Single Sign-On.",
        link: "https://sso.bstein.dev",
      },
      {
        name: "Gitea",
        category: "dev",
        summary: "Source control for dev projects.",
        link: "https://scm.bstein.dev",
      },
      {
        name: "Jenkins",
        category: "dev",
        summary: "Continuous integration build pipelines.",
        link: "https://ci.bstein.dev",
      },
      {
        name: "Harbor",
        category: "dev",
        summary: "Artifact Registry for build artifacts.",
        link: "https://registry.bstein.dev",
      },
      {
        name: "Flux",
        category: "dev",
        summary: "GitOps UI for flux continuous deployment drift prevention.",
        link: "https://cd.bstein.dev",
      },
      {
        name: "Vault",
        category: "dev",
        summary: "Secrets for secure environment injection.",
        link: "https://secret.bstein.dev",
      },
      {
        name: "Grafana",
        category: "observability",
        summary: "Health metrics for atlas and eventually oceanus.",
        link: "https://metrics.bstein.dev",
      },
      {
        name: "Pegasus",
        category: "media ingest",
        summary: "Uploading service to inject jellyfin media.",
        link: "https://pegasus.bstein.dev",
      },
      {
        name: "Monero",
        category: "crypto",
        summary: "Private monero node for monero wallets",
        link: "/monero",
        host: "monerod.crypto.svc.cluster.local:18081",
      },
      {
        name: "Jitsi",
        category: "conferencing",
        summary: "Video Conferencing - Planned",
        link: "https://meet.bstein.dev",
        status: "degraded",
      },
      {
        name: "AI Chat",
        category: "ai",
        summary: "LLM Chat - Planned",
        link: "/ai",
        host: "chat.ai.bstein.dev",
        status: "planned",
      },
      {
        name: "AI Image",
        category: "ai",
        summary: "Visualization tool - Planned",
        link: "/ai",
        host: "draw.ai.bstein.dev",
        status: "planned",
      },
      {
        name: "AI Speech",
        category: "ai",
        summary: "Live Translation - Planned",
        link: "/ai",
        host: "talk.ai.bstein.dev",
        status: "planned",
      },
    ],
  };
}

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: "titan-24 scrapes oceanus (titan-23).",
      },
    ],
    ingress_gateway: "Traefik with oauth2-proxy and Keycloak; Longhorn backs stateful ingress targets.",
  };
}

export function fallbackMetrics() {
  return {
    dashboard: "https://metrics.bstein.dev",
    description: "Atlas + Oceanus metrics.",
  };
}

export function buildHardwareDiagram(data) {
  return `
flowchart TB
  subgraph TitanLab["Titan Lab (25 nodes)"]
    subgraph Atlas["Atlas (k3s cluster)"]
      subgraph CP["Control plane (rpi5)"]
        titan0a["titan-0a<br/>rpi5 4c/8g"]
        titan0b["titan-0b<br/>rpi5 4c/8g"]
        titan0c["titan-0c<br/>rpi5 4c/8g"]
      end

      subgraph Pi5["Workers (rpi5)"]
        titan04["titan-04"]
        titan05["titan-05"]
        titan06["titan-06"]
        titan07["titan-07"]
        titan08["titan-08"]
        titan09["titan-09"]
        titan10["titan-10"]
        titan11["titan-11"]
      end

      subgraph Storage["Storage workers (rpi4 + disks)"]
        titan12["titan-12<br/>8TB astreae + 12TB asteria"]
        titan13["titan-13<br/>8TB astreae + 12TB asteria"]
        titan14["titan-14<br/>8TB astreae + 12TB asteria"]
        titan15["titan-15<br/>8TB astreae + 12TB asteria"]
        titan16["titan-16<br/>offline"]:::down
        titan17["titan-17"]
        titan18["titan-18"]
        titan19["titan-19"]
      end

      subgraph Accel["Accelerators + heavy nodes"]
        titan20["titan-20<br/>Jetson Xavier 6c/16g"]
        titan21["titan-21<br/>Jetson Xavier 6c/16g"]
        titan22["titan-22<br/>10c/32g<br/>GPU streaming"]
        titan24["titan-24 (tethys)<br/>12c/64g<br/>bridge + metrics"]
      end

      longhorn["Longhorn<br/>astreae: 4x8TB<br/>asteria: 4x12TB"]
      traefik["Traefik ingress"]
      keycloak["Keycloak SSO"]
      services["Services<br/>cloud / stream / ci / registry / cd / secret"]

      keycloak --> traefik
      traefik --> services
      services --> longhorn
      longhorn --> Storage
    end

    subgraph Dedicated["Dedicated hosts (outside Atlas)"]
      titanDb["titan-db<br/>Postgres for HA control plane<br/>rpi5 4c/8g"]
      theia["titan-jh (theia)<br/>bastion<br/>rpi5 4c/8g"]
      oceanus["titan-23 (oceanus)<br/>SUI validator<br/>24c/256g<br/>2.5GbE"]
    end

    titanDb -->|DB| titan0a
    theia -->|ssh| titan0a
    oceanus -->|metrics| titan24
  end

  classDef down fill:#311023,stroke:#ff4f93,color:#fff,stroke-width:2px;
`;
}

export function buildNetworkDiagram() {
  return `
sequenceDiagram
  participant U as User
  participant DNS as DNS (*.bstein.dev)
  participant T as Traefik (Atlas)
  participant A as auth.bstein.dev (oauth2-proxy)
  participant K as sso.bstein.dev (Keycloak)
  participant S as Service (cloud/stream/ci/registry/cd/secret)
  participant L as Longhorn PVC

  U->>DNS: resolve host
  DNS-->>U: Traefik VIP
  U->>T: HTTPS request
  T->>A: forwardAuth
  A->>K: OIDC login/refresh
  K-->>A: token
  A-->>T: allow
  T->>S: route to service
  S-->>L: persistent storage operations
  S-->>U: response / stream / artifact
`;
}

export function buildPipelineDiagram() {
  return `
flowchart LR
  dev[Developer] -->|push| gitea[scm.bstein.dev]
  gitea -->|webhook| jenkins[ci.bstein.dev]
  jenkins -->|build + push| harbor[registry.bstein.dev]
  harbor -->|image update| flux[cd.bstein.dev]
  flux -->|reconcile| atlas[Atlas]
  atlas -->|deploy| svc[cloud / stream / secret / other apps]

  keycloak[sso.bstein.dev] --> gitea
  keycloak --> jenkins
  keycloak --> harbor
  keycloak --> flux
`;
}
