import type { AuthHealthPayload } from './auth/auth-api'
import type { ReleaseManifestView } from './release-manifest'
import { launchReadiness } from './site-config'
import {
  buildPublicLaunchStatusSummary,
  getMissingPublicLaunchChecklistItems as getSharedMissingPublicLaunchChecklistItems,
  getPublicLaunchChecklist as getSharedPublicLaunchChecklist,
  isPublicLaunchReady as isSharedPublicLaunchReady,
  normalizePublicLaunchReadiness,
  resolveHealthPublicLaunchReadiness,
  resolveManifestPublicLaunchReadiness,
  resolvePublicLaunchStatusSummary as resolveSharedPublicLaunchStatusSummary,
  type PublicLaunchChecklistId,
  type PublicLaunchChecklistItem,
  type PublicLaunchReadiness,
  type PublicLaunchStatusSummary,
  type PublicLaunchTargets,
} from './shared/public-launch'

const defaultLaunchReadiness = normalizePublicLaunchReadiness(launchReadiness)

export type {
  PublicLaunchChecklistId,
  PublicLaunchChecklistItem,
  PublicLaunchReadiness,
  PublicLaunchStatusSummary,
  PublicLaunchTargets,
}

export function getPublicLaunchChecklist(
  readiness: Partial<PublicLaunchReadiness> = defaultLaunchReadiness,
): PublicLaunchChecklistItem[] {
  return getSharedPublicLaunchChecklist(readiness)
}

export function getMissingPublicLaunchChecklistItems(
  readiness: Partial<PublicLaunchReadiness> = defaultLaunchReadiness,
  options?: {
    requiredOnly?: boolean
  },
): PublicLaunchChecklistItem[] {
  return getSharedMissingPublicLaunchChecklistItems(readiness, options)
}

export function isPublicLaunchReady(
  readiness: Partial<PublicLaunchReadiness> = defaultLaunchReadiness,
): boolean {
  return isSharedPublicLaunchReady(readiness)
}

export function resolvePublicLaunchReadiness(
  manifest: ReleaseManifestView | null | undefined,
  fallback: Partial<PublicLaunchReadiness> = defaultLaunchReadiness,
): PublicLaunchReadiness {
  return resolveManifestPublicLaunchReadiness(manifest, fallback)
}

export function resolvePublicLaunchReadinessFromHealth({
  manifest,
  health,
  fallback = defaultLaunchReadiness,
}: {
  manifest?: ReleaseManifestView | null
  health?: AuthHealthPayload | null
  fallback?: Partial<PublicLaunchReadiness>
}): PublicLaunchReadiness {
  return resolveHealthPublicLaunchReadiness({
    manifest,
    health,
    fallback,
  })
}

export function resolvePublicLaunchStatusSummary(
  health?: AuthHealthPayload | null,
  manifest?: ReleaseManifestView | null,
  fallback: Partial<PublicLaunchReadiness> = defaultLaunchReadiness,
): PublicLaunchStatusSummary {
  return resolveSharedPublicLaunchStatusSummary({
    health,
    manifest,
    fallback,
  })
}

export function buildLiveLaunchBlockerDetails(
  authHealth: AuthHealthPayload | null | undefined,
  launchStatus: PublicLaunchStatusSummary,
) {
  if (!authHealth) {
    return []
  }

  const details = new Set<string>()

  if (!launchStatus.readiness.windowsDownloadConfigured) {
    details.add('Windows release delivery is still routed through signed-in account follow-up.')
  }
  if (!launchStatus.readiness.operatorCheckoutConfigured) {
    details.add('Operator checkout is currently handled through account or support follow-up.')
  }
  if (!launchStatus.readiness.studioCheckoutConfigured) {
    details.add('Studio checkout is currently handled through account or support follow-up.')
  }
  if (!launchStatus.readiness.publicAuthRuntimeReady) {
    details.add('Shared browser-auth runtime still needs production readiness confirmation on the current deployment.')
  }
  if (!authHealth.billing.webhookSecretConfigured) {
    details.add('Paddle webhook verification still needs to be enabled for fully automated billing updates.')
  }
  if (!authHealth.billing.productPlanMapConfigured) {
    details.add('Billing product-plan mapping still needs to be connected to the live subscription catalog.')
  }
  if (!authHealth.billing.pricePlanMapConfigured) {
    details.add('Billing price-plan mapping still needs to be connected to the live subscription catalog.')
  }

  authHealth.runtime.errors.forEach((item) => {
    details.add(`Runtime error: ${item}`)
  })
  authHealth.runtime.warnings.forEach((item) => {
    details.add(`Runtime warning: ${item}`)
  })

  return Array.from(details)
}

export {
  buildPublicLaunchStatusSummary,
  normalizePublicLaunchReadiness,
}
