import type { DownloadPlatformKey } from '../site-routes'
import generatedWindowsValidationSummary from './generated/windows-package-validation-summary.json'

export interface ReleasePlatformValidationSmokeMap {
  map_url: string
  label: string
  result: 'passed' | 'failed'
}

export interface ReleasePlatformPrePackageEditorBuildSummary {
  lane: string
  performed: boolean
  result: 'passed' | 'not-run'
}

export interface ReleasePlatformValidationSummary {
  lane: string
  result: 'passed' | 'failed'
  generated_at: string
  configuration: string
  skip_build: boolean
  pre_package_editor_build?: ReleasePlatformPrePackageEditorBuildSummary | null
  smoke_map_count: number
  smoke_maps: ReleasePlatformValidationSmokeMap[]
}

const WINDOWS_VALIDATION_SUMMARY_FALLBACK: ReleasePlatformValidationSummary = {
  lane: 'Windows Unreal packaged validation',
  result: 'passed',
  generated_at: '2026-06-24T12:53:32.7234772Z',
  configuration: 'Development',
  skip_build: true,
  pre_package_editor_build: null,
  smoke_map_count: 2,
  smoke_maps: [
    {
      map_url: '/Game/HyperTwistTraining/Maps/L_HyperTwist_Magic120CellTraining',
      label: 'Magic120Cell dedicated-family training map',
      result: 'passed',
    },
    {
      map_url: '/Game/HyperTwistTraining/Maps/L_HyperTwist_MagicCube5DTraining',
      label: 'MagicCube5D dedicated-family training map',
      result: 'passed',
    },
  ],
}

function isValidationSmokeMap(
  input: unknown,
): input is ReleasePlatformValidationSmokeMap {
  return Boolean(
    input
    && typeof input === 'object'
    && typeof (input as ReleasePlatformValidationSmokeMap).map_url === 'string'
    && typeof (input as ReleasePlatformValidationSmokeMap).label === 'string'
    && ((input as ReleasePlatformValidationSmokeMap).result === 'passed'
      || (input as ReleasePlatformValidationSmokeMap).result === 'failed'),
  )
}

function isPrePackageEditorBuildSummary(
  input: unknown,
): input is ReleasePlatformPrePackageEditorBuildSummary {
  return Boolean(
    input
    && typeof input === 'object'
    && typeof (input as ReleasePlatformPrePackageEditorBuildSummary).lane === 'string'
    && typeof (input as ReleasePlatformPrePackageEditorBuildSummary).performed === 'boolean'
    && ((input as ReleasePlatformPrePackageEditorBuildSummary).result === 'passed'
      || (input as ReleasePlatformPrePackageEditorBuildSummary).result === 'not-run'),
  )
}

function isValidationSummary(
  input: unknown,
): input is ReleasePlatformValidationSummary {
  return Boolean(
    input
    && typeof input === 'object'
    && typeof (input as ReleasePlatformValidationSummary).lane === 'string'
    && ((input as ReleasePlatformValidationSummary).result === 'passed'
      || (input as ReleasePlatformValidationSummary).result === 'failed')
    && typeof (input as ReleasePlatformValidationSummary).generated_at === 'string'
    && typeof (input as ReleasePlatformValidationSummary).configuration === 'string'
    && typeof (input as ReleasePlatformValidationSummary).skip_build === 'boolean'
    && (
      (input as ReleasePlatformValidationSummary).pre_package_editor_build == null
      || isPrePackageEditorBuildSummary(
        (input as ReleasePlatformValidationSummary).pre_package_editor_build,
      )
    )
    && typeof (input as ReleasePlatformValidationSummary).smoke_map_count === 'number'
    && Array.isArray((input as ReleasePlatformValidationSummary).smoke_maps)
    && (input as ReleasePlatformValidationSummary).smoke_maps.every(isValidationSmokeMap),
  )
}

const WINDOWS_VALIDATION_SUMMARY: ReleasePlatformValidationSummary = isValidationSummary(
  generatedWindowsValidationSummary,
)
  ? generatedWindowsValidationSummary
  : WINDOWS_VALIDATION_SUMMARY_FALLBACK

export function getReleasePlatformValidationSummary(
  platformKey: DownloadPlatformKey,
): ReleasePlatformValidationSummary | null {
  if (platformKey === 'windows') {
    return WINDOWS_VALIDATION_SUMMARY
  }

  return null
}
