// Multi-project versions config
// Supports KubeStellar, a2a, and kubeflex with independent versioning
//
// Versioning Strategy:
// - Each project has its own version scheme
// - Branch naming convention:
//   - KubeStellar: main (latest), docs/{version} (e.g., docs/0.28.0)
//   - a2a: main (latest), docs/a2a/{version} (e.g., docs/a2a/0.1.0)
//   - kubeflex: main (latest), docs/kubeflex/{version} (e.g., docs/kubeflex/0.8.0)
// - The main branch always contains the latest version for all projects

// Netlify site name for branch deploys
export const NETLIFY_SITE_NAME = "kubestellar-docs"

// Production URL for latest version
export const PRODUCTION_URL = "https://kubestellar.io"

// Project identifiers
export type ProjectId = "kubestellar" | "a2a" | "kubeflex" | "multi-plugin" | "kubestellar-mcp" | "console" | "hive"

// Version info structure
export interface VersionInfo {
  label: string
  branch: string
  isDefault: boolean
  externalUrl?: string
  isDev?: boolean // marks development/unreleased versions
}

// Project configuration
export interface ProjectConfig {
  id: ProjectId
  name: string
  basePath: string // '' for kubestellar, 'a2a' for a2a, etc.
  currentVersion: string
  contentPath: string
  versions: Record<string, VersionInfo>
}

// KubeStellar versions (existing)
const KUBESTELLAR_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.30.0 (Latest)",
    branch: "docs/0.30.0",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
  "0.29.0": {
    label: "v0.29.0",
    branch: "docs/0.29.0",
    isDefault: false,
  },
  "0.28.0": {
    label: "v0.28.0",
    branch: "docs/0.28.0",
    isDefault: false,
  },
  "0.27.2": {
    label: "v0.27.2",
    branch: "docs/0.27.2",
    isDefault: false,
  },
  "0.27.1": {
    label: "v0.27.1",
    branch: "docs/0.27.1",
    isDefault: false,
  },
  "0.27.0": {
    label: "v0.27.0",
    branch: "docs/0.27.0",
    isDefault: false,
  },
  "0.26.0": {
    label: "v0.26.0",
    branch: "docs/0.26.0",
    isDefault: false,
  },
  "0.25.1": {
    label: "v0.25.1",
    branch: "docs/0.25.1",
    isDefault: false,
  },
  "0.25.0": {
    label: "v0.25.0",
    branch: "docs/0.25.0",
    isDefault: false,
  },
  "0.24.0": {
    label: "v0.24.0",
    branch: "docs/0.24.0",
    isDefault: false,
  },
  "0.23.1": {
    label: "v0.23.1",
    branch: "docs/0.23.1",
    isDefault: false,
  },
  "0.23.0": {
    label: "v0.23.0",
    branch: "docs/0.23.0",
    isDefault: false,
  },
  "0.22.0": {
    label: "v0.22.0",
    branch: "docs/0.22.0",
    isDefault: false,
  },
  "0.21.2": {
    label: "v0.21.2",
    branch: "docs/0.21.2",
    isDefault: false,
  },
  "0.21.1": {
    label: "v0.21.1",
    branch: "docs/0.21.1",
    isDefault: false,
  },
  "0.21.0": {
    label: "v0.21.0",
    branch: "docs/0.21.0",
    isDefault: false,
  },
  legacy: {
    label: "Older Versions",
    branch: "legacy",
    isDefault: false,
    externalUrl: "https://kubestellar.github.io/kubestellar",
  },
}

// a2a versions
const A2A_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.1.0 (Latest)",
    branch: "docs/a2a/0.1.0",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
}

// kubeflex versions
const KUBEFLEX_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.9.3 (Latest)",
    branch: "docs/kubeflex/0.9.3",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
  "0.8.0": {
    label: "v0.8.0",
    branch: "docs/kubeflex/0.8.0",
    isDefault: false,
  },
  "0.7.0": {
    label: "v0.7.0",
    branch: "docs/kubeflex/0.7.0",
    isDefault: false,
  },
}

// multi-plugin versions
const MULTI_PLUGIN_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.1.0 (Latest)",
    branch: "docs/multi-plugin/0.1.0",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
}

// kubestellar-mcp versions
const KUBESTELLAR_MCP_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.9.6 (Latest)",
    branch: "docs/kubestellar-mcp/0.9.6",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
  "0.9.4": {
    label: "v0.9.4",
    branch: "docs/kubestellar-mcp/0.9.4",
    isDefault: false,
  },
  "0.9.3": {
    label: "v0.9.3",
    branch: "docs/kubestellar-mcp/0.9.3",
    isDefault: false,
  },
  "0.9.2": {
    label: "v0.9.2",
    branch: "docs/kubestellar-mcp/0.9.2",
    isDefault: false,
  },
  "0.8.21": {
    label: "v0.8.21",
    branch: "docs/kubestellar-mcp/0.8.21",
    isDefault: false,
  },
  "0.8.20": {
    label: "v0.8.20",
    branch: "docs/kubestellar-mcp/0.8.20",
    isDefault: false,
  },
  "0.8.17": {
    label: "v0.8.17",
    branch: "docs/kubestellar-mcp/0.8.17",
    isDefault: false,
  },
  "0.8.16": {
    label: "v0.8.16",
    branch: "docs/kubestellar-mcp/0.8.16",
    isDefault: false,
  },
  "0.8.1": {
    label: "v0.8.1",
    branch: "docs/kubestellar-mcp/0.8.1",
    isDefault: false,
  },
}

// console versions
// The "latest" entry tracks the most recent stable weekly release.
// The console release sync workflow auto-updates this when a new release is detected.
const CONSOLE_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "v0.3.35 (Latest)",
    branch: "docs/console/0.3.35",
    isDefault: true,
  },
  main: {
    label: "main (dev)",
    branch: "main",
    isDefault: false,
    isDev: true,
  },
  "0.3.34": {
    label: "v0.3.34",
    branch: "docs/console/0.3.34",
    isDefault: false,
  },
  "0.3.33": {
    label: "v0.3.33",
    branch: "docs/console/0.3.33",
    isDefault: false,
  },
  "0.3.32": {
    label: "v0.3.32",
    branch: "docs/console/0.3.32",
    isDefault: false,
  },
  "0.3.31": {
    label: "v0.3.31",
    branch: "docs/console/0.3.31",
    isDefault: false,
  },
  "0.3.30": {
    label: "v0.3.30",
    branch: "docs/console/0.3.30",
    isDefault: false,
  },
  "0.3.29": {
    label: "v0.3.29",
    branch: "docs/console/0.3.29",
    isDefault: false,
  },
  "0.3.28": {
    label: "v0.3.28",
    branch: "docs/console/0.3.28",
    isDefault: false,
  },
  "0.3.27": {
    label: "v0.3.27",
    branch: "docs/console/0.3.27",
    isDefault: false,
  },
  "0.3.26": {
    label: "v0.3.26",
    branch: "docs/console/0.3.26",
    isDefault: false,
  },
  "0.3.25": {
    label: "v0.3.25",
    branch: "docs/console/0.3.25",
    isDefault: false,
  },
  "0.3.22": {
    label: "v0.3.22",
    branch: "docs/console/0.3.22",
    isDefault: false,
  },
  "0.3.21": {
    label: "v0.3.21",
    branch: "docs/console/0.3.21",
    isDefault: false,
  },
  "0.3.18": {
    label: "v0.3.18",
    branch: "docs/console/0.3.18",
    isDefault: false,
  },
  "0.3.17": {
    label: "v0.3.17",
    branch: "docs/console/0.3.17",
    isDefault: false,
  },
  "0.3.16": {
    label: "v0.3.16",
    branch: "docs/console/0.3.16",
    isDefault: false,
  },
  "0.3.15": {
    label: "v0.3.15",
    branch: "docs/console/0.3.15",
    isDefault: false,
  },
  "0.3.14": {
    label: "v0.3.14",
    branch: "docs/console/0.3.14",
    isDefault: false,
  },
  "0.3.13": {
    label: "v0.3.13",
    branch: "docs/console/0.3.13",
    isDefault: false,
  },
  "0.3.12": {
    label: "v0.3.12",
    branch: "docs/console/0.3.12",
    isDefault: false,
  },
  "0.3.11": {
    label: "v0.3.11",
    branch: "docs/console/0.3.11",
    isDefault: false,
  },
  "0.3.10": {
    label: "v0.3.10",
    branch: "docs/console/0.3.10",
    isDefault: false,
  },
  "0.3.9": {
    label: "v0.3.9",
    branch: "docs/console/0.3.9",
    isDefault: false,
  },
  "0.3.8": {
    label: "v0.3.8",
    branch: "docs/console/0.3.8",
    isDefault: false,
  },
  "0.3.7": {
    label: "v0.3.7",
    branch: "docs/console/0.3.7",
    isDefault: false,
  },
  "0.3.5": {
    label: "v0.3.5",
    branch: "docs/console/0.3.5",
    isDefault: false,
  },
  "0.3.4": {
    label: "v0.3.4",
    branch: "docs/console/0.3.4",
    isDefault: false,
  },
  "0.3.3": {
    label: "v0.3.3",
    branch: "docs/console/0.3.3",
    isDefault: false,
  },
  "0.3.2": {
    label: "v0.3.2",
    branch: "docs/console/0.3.2",
    isDefault: false,
  },
  "0.3.1": {
    label: "v0.3.1",
    branch: "docs/console/0.3.1",
    isDefault: false,
  },
  "0.3.0": {
    label: "v0.3.0",
    branch: "docs/console/0.3.0",
    isDefault: false,
  },
  "0.3.23": {
    label: "v0.3.23",
    branch: "docs/console/0.3.23",
    isDefault: false,
  },
  "0.3.24": {
    label: "v0.3.24",
    branch: "docs/console/0.3.24",
    isDefault: false,
  },
  "0.3.20": {
    label: "v0.3.20",
    branch: "docs/console/0.3.20",
    isDefault: false,
  },
  "0.3.19": {
    label: "v0.3.19",
    branch: "docs/console/0.3.19",
    isDefault: false,
  },
  "0.3.6": {
    label: "v0.3.6",
    branch: "docs/console/0.3.6",
    isDefault: false,
  },
  "0.1.0": {
    label: "v0.1.0",
    branch: "docs/console/0.1.0",
    isDefault: false,
  },
}

// hive versions
const HIVE_VERSIONS: Record<string, VersionInfo> = {
  latest: {
    label: "main (Latest)",
    branch: "main",
    isDefault: true,
  },
}

// All projects configuration
export const PROJECTS: Record<ProjectId, ProjectConfig> = {
  kubestellar: {
    id: "kubestellar",
    name: "KubeStellar",
    basePath: "",
    currentVersion: "0.30.0",
    contentPath: "docs/content",
    versions: KUBESTELLAR_VERSIONS,
  },
  a2a: {
    id: "a2a",
    name: "A2A",
    basePath: "a2a",
    currentVersion: "0.1.0",
    contentPath: "docs/content/a2a",
    versions: A2A_VERSIONS,
  },
  kubeflex: {
    id: "kubeflex",
    name: "KubeFlex",
    basePath: "kubeflex",
    currentVersion: "0.9.3",
    contentPath: "docs/content/kubeflex",
    versions: KUBEFLEX_VERSIONS,
  },
  "multi-plugin": {
    id: "multi-plugin",
    name: "Multi Plugin",
    basePath: "multi-plugin",
    currentVersion: "0.1.0",
    contentPath: "docs/content/multi-plugin",
    versions: MULTI_PLUGIN_VERSIONS,
  },
  "kubestellar-mcp": {
    id: "kubestellar-mcp",
    name: "KubeStellar MCP",
    basePath: "kubestellar-mcp",
    currentVersion: "0.9.6",
    contentPath: "docs/content/kubestellar-mcp",
    versions: KUBESTELLAR_MCP_VERSIONS,
  },
  "console": {
    id: "console",
    name: "Console",
    basePath: "console",
    currentVersion: "0.3.35",
    contentPath: "docs/content/console",
    versions: CONSOLE_VERSIONS,
  },
  "hive": {
    id: "hive",
    name: "Hive",
    basePath: "hive",
    currentVersion: "main",
    contentPath: "docs/content/hive",
    versions: HIVE_VERSIONS,
  },
}

// Get project from URL pathname
export function getProjectFromPath(pathname: string): ProjectConfig {
  if (pathname.startsWith("/docs/a2a")) {
    return PROJECTS.a2a
  }
  if (pathname.startsWith("/docs/kubeflex")) {
    return PROJECTS.kubeflex
  }
  if (pathname.startsWith("/docs/multi-plugin")) {
    return PROJECTS["multi-plugin"]
  }
  if (pathname.startsWith("/docs/kubestellar-mcp") || pathname.startsWith("/docs/related-projects/kubestellar-mcp")) {
    return PROJECTS["kubestellar-mcp"]
  }
  if (pathname.startsWith("/docs/console")) {
    return PROJECTS["console"]
  }
  if (pathname.startsWith("/docs/hive")) {
    return PROJECTS["hive"]
  }
  return PROJECTS.kubestellar
}

// Get project by ID
export function getProject(projectId: ProjectId): ProjectConfig {
  return PROJECTS[projectId]
}

// Get all projects
export function getAllProjects(): ProjectConfig[] {
  return Object.values(PROJECTS)
}

// ============================================
// Backwards-compatible exports for KubeStellar
// ============================================

export const CURRENT_VERSION = PROJECTS.kubestellar.currentVersion
export const VERSIONS = KUBESTELLAR_VERSIONS

export type VersionKey = keyof typeof KUBESTELLAR_VERSIONS

export function getDefaultVersion(): VersionKey {
  return "latest"
}

export function getCurrentVersion(): string {
  return CURRENT_VERSION
}

export function getBranchForVersion(version: VersionKey): string {
  return KUBESTELLAR_VERSIONS[version]?.branch ?? "main"
}

export function getVersionFromBranch(branch: string): VersionKey | null {
  // Check if branch matches docs/{version} pattern
  const match = branch.match(/^docs\/(.+)$/)
  if (match) {
    const versionNum = match[1]
    // Find version entry with matching branch
    for (const [key, value] of Object.entries(KUBESTELLAR_VERSIONS)) {
      if (value.branch === branch || key === versionNum) {
        return key as VersionKey
      }
    }
  }

  // Check for main branch
  if (branch === "main" || branch === "master") {
    return "latest"
  }

  return null
}

export function getAllVersions(): Array<{ key: VersionKey } & VersionInfo> {
  return Object.entries(KUBESTELLAR_VERSIONS).map(([key, value]) => ({
    key: key as VersionKey,
    ...value,
  }))
}

// Helper to validate if a branch name follows version convention
export function isVersionBranch(branch: string): boolean {
  return branch === "main" || branch.startsWith("docs/")
}

// Get the URL for a specific version (project-aware)
export function getVersionUrl(
  versionKey: string,
  pathname: string = "/docs",
  projectId: ProjectId = "kubestellar"
): string {
  const project = PROJECTS[projectId]
  const version = project.versions[versionKey]

  if (!version) {
    return `${PRODUCTION_URL}${pathname}`
  }

  // If it has an external URL (like legacy), use that
  if ("externalUrl" in version && version.externalUrl) {
    return version.externalUrl
  }

  // Latest version uses production URL
  if (versionKey === "latest" || version.isDefault) {
    return `${PRODUCTION_URL}${pathname}`
  }

  // Other versions use Netlify branch deploys
  // Netlify converts branch names: docs/0.28.0 -> docs-0-28-0
  const branchSlug = version.branch.replace(/\//g, "-").replace(/\./g, "-")
  return `https://${branchSlug}--${NETLIFY_SITE_NAME}.netlify.app${pathname}`
}

// Get versions for a specific project
export function getProjectVersions(
  projectId: ProjectId
): Array<{ key: string } & VersionInfo> {
  const project = PROJECTS[projectId]
  return Object.entries(project.versions).map(([key, value]) => ({
    key,
    ...value,
  }))
}

// Check if a version has been migrated (branch exists)
export function isVersionMigrated(
  versionKey: string,
  projectId: ProjectId = "kubestellar"
): boolean {
  const project = PROJECTS[projectId]

  // Latest is always available
  if (versionKey === "latest") return true

  // Legacy links externally, so it's "available"
  if (versionKey === "legacy") return true

  // For other versions, assume they exist if in the versions list
  return versionKey in project.versions
}
