import type { DownloadPlatformKey } from './site-routes'
import { buildProtectedDownloadPath, buildRegisterPath } from './site-routes'

const env = import.meta.env as Record<string, string | undefined>

function readTrimmedEnv(name: string, fallback = '') {
  return String(env[name] || fallback).trim()
}

function readBooleanEnv(...names: string[]) {
  for (const name of names) {
    const value = String(env[name] || '').trim().toLowerCase()
    if (!value) {
      continue
    }
    if (value === '1' || value === 'true' || value === 'yes' || value === 'on') {
      return true
    }
    if (value === '0' || value === 'false' || value === 'no' || value === 'off') {
      return false
    }
  }
  return false
}

export const brandConfig = {
  brandName: 'HyperTwist',
  legalName: 'HyperTwist',
  domain: 'hypertwist.app',
  tagline: 'Desktop cubing and hypercubing, from first solves to 120‑cell mastery.',
  contact: {
    email: readTrimmedEnv('VITE_SUPPORT_EMAIL', 'hello@hypertwist.app'),
    emailHref: `mailto:${readTrimmedEnv('VITE_SUPPORT_EMAIL', 'hello@hypertwist.app')}`,
  },
  payment: {
    provider: 'Paddle',
  },
}

const operatorCheckoutUrl = readTrimmedEnv('VITE_PADDLE_CHECKOUT_URL_OPERATOR')
const studioCheckoutUrl = readTrimmedEnv('VITE_PADDLE_CHECKOUT_URL_STUDIO')

type DownloadTarget = {
  platformKey: DownloadPlatformKey
  platform: string
  subtitle: string
  configured: boolean
  details: string
}

export const planCatalog = [
  {
    key: 'explorer',
    name: 'Explorer',
    price: 'Free',
    ctaLabel: 'Create account',
    ctaHref: buildRegisterPath('/app'),
    notes: 'Create your HyperTwist account to open the dashboard, read the manual, follow release notes, and get ready for your first desktop session.',
    features: [
      'Browser account and dashboard access',
      'Public manual, release notes, and resource center',
      'Desktop onboarding and pairing guidance',
      'Open-source notices and distribution transparency',
    ],
  },
  {
    key: 'operator',
    name: 'Operator',
    price: readTrimmedEnv('VITE_PLAN_PRICE_OPERATOR', 'Launch pricing via Paddle'),
    ctaLabel: operatorCheckoutUrl ? 'Subscribe with Paddle' : 'Create account for download access',
    ctaHref: operatorCheckoutUrl || buildProtectedDownloadPath('windows'),
    notes: 'Unlock the desktop simulator for recognition, replay review, guided training, and higher-dimensional practice on Windows.',
    features: [
      'Protected desktop download access',
      'Browser-issued desktop-link token handoff',
      'Recognition, replay review, and coaching tools',
      'Classic-cube training plus verified package delivery',
    ],
  },
    {
      key: 'studio',
      name: 'Studio',
      price: readTrimmedEnv('VITE_PLAN_PRICE_STUDIO', 'Contact for launch readiness'),
      ctaLabel: studioCheckoutUrl ? 'Open studio checkout' : 'Talk to HyperTwist',
      ctaHref: studioCheckoutUrl || '/contact',
      notes: 'For studios, educators, and advanced teams who want guided onboarding, higher-dimensional programs, and hands-on release support.',
      features: [
        'Magic120Cell and 5D training access planning',
        'Release planning and deployment coordination',
        'Desktop distribution and notice-readiness support',
        'Custom onboarding for classes, labs, and training teams',
      ],
    },
] as const

export const downloadTargets = [
  {
    platformKey: 'windows',
    platform: 'Windows',
    subtitle: 'Primary desktop release',
    configured: readBooleanEnv('VITE_WINDOWS_RELEASE_CONFIGURED', 'VITE_WINDOWS_DOWNLOAD_CONFIGURED'),
    details: 'The current packaged build and validation proof are strongest on Windows.',
  },
  {
    platformKey: 'macos',
    platform: 'macOS',
    subtitle: 'Planned release',
    configured: readBooleanEnv('VITE_MAC_RELEASE_CONFIGURED', 'VITE_MAC_DOWNLOAD_CONFIGURED'),
    details: 'macOS distribution will appear here when a signed desktop build is ready for account-gated delivery.',
  },
  {
    platformKey: 'linux',
    platform: 'Linux',
    subtitle: 'Later desktop release',
    configured: readBooleanEnv('VITE_LINUX_RELEASE_CONFIGURED', 'VITE_LINUX_DOWNLOAD_CONFIGURED'),
    details: 'Linux distribution will appear here when a supported desktop package is ready.',
  },
] as const satisfies readonly DownloadTarget[]

export const publicDocsUrl = readTrimmedEnv('VITE_PUBLIC_DOCS_URL')
export const releaseNotesUrl = readTrimmedEnv('VITE_RELEASE_NOTES_URL')
export const mplSourceUrl = readTrimmedEnv('VITE_MPL_SOURCE_URL')
export const openSourceRepoUrl = readTrimmedEnv('VITE_OPEN_SOURCE_REPO_URL')

export const launchReadiness = {
  operatorCheckoutConfigured: Boolean(operatorCheckoutUrl),
  studioCheckoutConfigured: Boolean(studioCheckoutUrl),
  windowsDownloadConfigured: Boolean(downloadTargets.find((target) => target.platformKey === 'windows')?.configured),
  macosDownloadConfigured: Boolean(downloadTargets.find((target) => target.platformKey === 'macos')?.configured),
  macDownloadConfigured: Boolean(downloadTargets.find((target) => target.platformKey === 'macos')?.configured),
  linuxDownloadConfigured: Boolean(downloadTargets.find((target) => target.platformKey === 'linux')?.configured),
  mplSourceConfigured: Boolean(mplSourceUrl),
  openSourceRepoConfigured: Boolean(openSourceRepoUrl),
  billingProductPlanMapConfigured: false,
  billingPricePlanMapConfigured: false,
  billingWebhookSecretConfigured: false,
  publicAuthRuntimeReady: false,
} as const

export const paddleReadyDescription =
  'Choose a plan, unlock your account, and keep downloads, billing, release notes, and support in one place. Then move into the desktop app for the full HyperTwist simulator, from guided classic-cube practice to serious 120‑cell and 5D sessions.'
