import type { LucideIcon } from 'lucide-react';
import {
  Cloud,
  Shield,
  Wallet,
  Blocks,
  Cpu,
  Landmark,
  Globe2,
  Sparkles,
  Network,
  FileKey2,
  Building2,
  Scale,
} from 'lucide-react';

import generatedGrades from '@/data/institutional-grades.generated.json';

export interface CorporateNavItem {
  label: string;
  href: string;
  external?: boolean;
}

export interface ProductDivision {
  id: string;
  name: string;
  tagline: string;
  description: string;
  href: string;
  external?: boolean;
  icon: LucideIcon;
  highlights: string[];
}

export interface ServiceOffering {
  name: string;
  category: string;
  description: string;
  icon: LucideIcon;
}

export interface SolutionVertical {
  name: string;
  description: string;
  icon: LucideIcon;
}

export const ECOSYSTEM_SIGN_IN_PATH = '/api/auth/signin?callbackUrl=/dashboard';

export const corporateNav: CorporateNavItem[] = [
  { label: 'Products', href: '#products' },
  { label: 'Services', href: '#services' },
  { label: 'Solutions', href: '#solutions' },
  { label: 'Institutional grade', href: '#institutional-grade' },
  { label: 'Resources', href: '#resources' },
];

export const productDivisions: ProductDivision[] = [
  {
    id: 'phoenix',
    name: 'Phoenix Cloud',
    tagline: 'Sovereign cloud platform',
    description:
      'Infrastructure, identity, orchestration, and marketplace primitives — the sovereign alternative to hyperscale public cloud.',
    href: 'https://phoenix.sankofa.nexus',
    external: true,
    icon: Cloud,
    highlights: ['Multi-tenant compute', 'Keycloak identity plane', 'GitOps & Crossplane', 'Marketplace catalog'],
  },
  {
    id: 'complete-credential',
    name: 'Complete Credential',
    tagline: 'Sovereign credential issuance',
    description:
      'Institutional-grade verifiable credentials, entity packs, and eIDAS-aligned issuance hosted on Phoenix.',
    href: 'https://cc.sankofa.nexus',
    external: true,
    icon: FileKey2,
    highlights: ['Entity credential packs', 'OIDC / SAML integration', 'Compliance workflows', 'Operator admin'],
  },
  {
    id: 'studio',
    name: 'Sankofa Studio',
    tagline: 'FusionAI creator platform',
    description:
      'Build, deploy, and manage AI-powered applications on sovereign infrastructure with full data residency control.',
    href: 'https://studio.sankofa.nexus/studio/',
    external: true,
    icon: Sparkles,
    highlights: ['Model orchestration', 'Creator workflows', 'Tenant isolation', 'API-first design'],
  },
  {
    id: 'blockchain',
    name: 'Blockchain & DeFi',
    tagline: 'Chain 138 & cross-chain mesh',
    description:
      'Defi Oracle Meta Mainnet, PMM liquidity, CCIP bridges, and institutional-grade on-chain settlement rails.',
    href: 'https://explorer.d-bis.org',
    external: true,
    icon: Blocks,
    highlights: ['Chain 138 RPC & explorer', 'PMM / DODO liquidity', 'CCIP cross-chain', 'Token & RWA factory'],
  },
  {
    id: 'identity',
    name: 'Identity & Access',
    tagline: 'Sovereign IAM',
    description:
      'Centralized OIDC/SAML identity with device binding, passkeys, RBAC, and zero dependency on hyperscaler IdPs.',
    href: 'https://keycloak.sankofa.nexus',
    external: true,
    icon: Shield,
    highlights: ['Keycloak IdP', 'Multi-tenant RBAC', 'Passkeys & MFA', 'Client SSO portal'],
  },
  {
    id: 'treasury',
    name: 'Treasury & Settlement',
    tagline: 'Institutional finance rails',
    description:
      'Double-entry ledger, virtual accounts, wallet registry, and RTGS-aligned settlement for sovereign finance.',
    href: 'https://portal.sankofa.nexus',
    external: true,
    icon: Landmark,
    highlights: ['Phoenix Ledger', 'Virtual accounts', 'Wallet policy engine', 'DBIS settlement'],
  },
];

export const platformServices: ServiceOffering[] = [
  {
    name: 'Phoenix Ledger Service',
    category: 'Financial',
    description: 'Double-entry ledger with virtual accounts, holds, and multi-asset support.',
    icon: Landmark,
  },
  {
    name: 'Phoenix Identity Service',
    category: 'Security',
    description: 'Users, orgs, roles, permissions, device binding, passkeys, and OAuth/OIDC.',
    icon: Shield,
  },
  {
    name: 'Phoenix Wallet Registry',
    category: 'Custody',
    description: 'Wallet mapping, chain support, MPC/HSM policy engine, and ERC-4337 accounts.',
    icon: Wallet,
  },
  {
    name: 'Transaction Orchestrator',
    category: 'Orchestration',
    description: 'On-chain and off-chain workflows with retries, compensations, and failover.',
    icon: Network,
  },
  {
    name: 'Messaging Orchestrator',
    category: 'Communications',
    description: 'Multi-provider SMS, email, voice, and push with automatic failover.',
    icon: Globe2,
  },
  {
    name: 'Voice Orchestrator',
    category: 'AI Media',
    description: 'TTS/STT with caching, multi-provider routing, and PII-aware moderation.',
    icon: Cpu,
  },
];

export const solutionVerticals: SolutionVertical[] = [
  {
    name: 'Public Sector',
    description: 'Sovereign tenancy, SMOA compliance, and institutional registry for government programs.',
    icon: Building2,
  },
  {
    name: 'Financial Services',
    description: 'Ledger, settlement, EMI wallet infrastructure, and cross-border RTGS integration.',
    icon: Scale,
  },
  {
    name: 'Healthcare & Identity',
    description: 'Verifiable credentials, eIDAS-aligned issuance, and HIPAA-ready identity planes.',
    icon: FileKey2,
  },
  {
    name: 'Telecommunications',
    description: 'PanTel 6G infrastructure JV, sovereign edge compute, and carrier-grade networking.',
    icon: Network,
  },
];

export const philosophySteps = [
  { verb: 'Remember', detail: 'Where we came from — ancestral wisdom and institutional continuity' },
  { verb: 'Retrieve', detail: 'What was essential — sovereign identity and owned core primitives' },
  { verb: 'Restore', detail: 'Identity and sovereignty — independent infrastructure under our control' },
  { verb: 'Rise', detail: 'Forward with purpose — world-class cloud without hyperscaler dependency' },
];

export interface InstitutionalGradeEntry {
  id: string;
  name: string;
  score: number;
  maxScore: number;
  letterGrade: string;
  tier: string;
  audience: 'sovereign' | 'regulated' | 'platform';
  assessmentDate: string;
  href?: string;
}

/** Engineering readiness scores for sovereign & regulated institutions (not legal advice). */
const FALLBACK_RUBRIC = [
  { grade: 'A', range: '90–100', meaning: 'Production-grade; audit-ready; minimal material gaps' },
  { grade: 'B', range: '80–89', meaning: 'Pilot-ready with documented, bounded residual risk' },
  { grade: 'C', range: '70–79', meaning: 'Operational with material gaps blocking institutional scale' },
  { grade: 'D', range: '60–69', meaning: 'Engineering-complete; evidence and jurisdiction coverage incomplete' },
  { grade: 'F', range: '<60', meaning: 'Not suitable for broad institutional deployment claims' },
];

const FALLBACK_GRADES: InstitutionalGradeEntry[] = [
  {
    id: 'ecosystem-composite',
    name: 'DBIS / Chain 138 ecosystem',
    score: 83,
    maxScore: 100,
    letterGrade: 'B',
    tier: 'Advanced pilot — 138-native flows with disclosures',
    audience: 'sovereign',
    assessmentDate: '2026-05-26',
    href: 'https://gitea.d-bis.org/d-bis/proxmox/src/branch/main/ECOSYSTEM_READINESS.md',
  },
  {
    id: 'sovereign-cloud',
    name: 'Sovereign cloud infrastructure',
    score: 90,
    maxScore: 100,
    letterGrade: 'A−',
    tier: 'CI-clean operator stack; LAN inventory & E2E routing',
    audience: 'sovereign',
    assessmentDate: '2026-05-26',
  },
  {
    id: 'public-sector-compliance',
    name: 'Public sector & jurisdiction matrices',
    score: 74,
    maxScore: 100,
    letterGrade: 'C',
    tier: 'Indonesia pilot-ready; US stub; counsel sign-off paths documented',
    audience: 'sovereign',
    assessmentDate: '2026-05-26',
    href: 'https://docs.d-bis.org',
  },
  {
    id: 'settlement-rtgs',
    name: 'Settlement, RTGS & Rail',
    score: 72,
    maxScore: 100,
    letterGrade: 'C−',
    tier: 'Verification tooling live; on-chain Rail not yet deployed',
    audience: 'sovereign',
    assessmentDate: '2026-05-26',
  },
  {
    id: 'rwa-gov-critical',
    name: 'RWA index factory (government-critical lens)',
    score: 58,
    maxScore: 100,
    letterGrade: 'D',
    tier: 'Engineering pilot only — not for sovereign treasury without counsel',
    audience: 'sovereign',
    assessmentDate: '2026-05-24',
    href: 'https://docs.d-bis.org',
  },
  {
    id: 'complete-credential',
    name: 'Complete Credential / eIDAS program',
    score: 70,
    maxScore: 100,
    letterGrade: 'C',
    tier: 'SMOA evidence partial; QTSP path documented',
    audience: 'regulated',
    assessmentDate: '2026-03-25',
    href: 'https://cc.sankofa.nexus',
  },
  {
    id: 'omnl-registry',
    name: 'OMNL / HYBX institutional entity registry',
    score: 63,
    maxScore: 100,
    letterGrade: 'D',
    tier: 'Live Fineract offices; CB counterparty MOU evidence outstanding',
    audience: 'regulated',
    assessmentDate: '2026-06-05',
  },
  {
    id: 'chain138-l1',
    name: 'Chain 138 L1 health & oracle integrity',
    score: 91,
    maxScore: 100,
    letterGrade: 'A−',
    tier: '16/16 oracle strict; Stack A PMM live (~$87.8M)',
    audience: 'platform',
    assessmentDate: '2026-05-26',
  },
];

export const institutionalGradeRubric =
  generatedGrades.rubric?.length ? generatedGrades.rubric : FALLBACK_RUBRIC;

export const sovereignInstitutionGrades: InstitutionalGradeEntry[] =
  generatedGrades.sovereignInstitutionGrades?.length
    ? (generatedGrades.sovereignInstitutionGrades as InstitutionalGradeEntry[])
    : FALLBACK_GRADES;

export function gradeBadgeClass(letterGrade: string): string {
  const g = letterGrade.charAt(0).toUpperCase();
  switch (g) {
    case 'A':
      return 'bg-emerald-500/15 text-emerald-300 ring-emerald-500/30';
    case 'B':
      return 'bg-sky-500/15 text-sky-300 ring-sky-500/30';
    case 'C':
      return 'bg-amber-500/15 text-amber-300 ring-amber-500/30';
    case 'D':
      return 'bg-orange-500/15 text-orange-300 ring-orange-500/30';
    default:
      return 'bg-red-500/15 text-red-300 ring-red-500/30';
  }
}

export function scoreBarClass(letterGrade: string): string {
  const g = letterGrade.charAt(0).toUpperCase();
  switch (g) {
    case 'A':
      return 'bg-emerald-500';
    case 'B':
      return 'bg-sky-500';
    case 'C':
      return 'bg-amber-500';
    case 'D':
      return 'bg-orange-500';
    default:
      return 'bg-red-500';
  }
}
