import { describe, expect, it } from 'vitest'
import {
  getMissingPublicLaunchChecklistItems,
  getPublicLaunchChecklist,
  isPublicLaunchReady,
  resolvePublicLaunchStatusSummary,
} from '../public-launch'

describe('public launch readiness helpers', () => {
  it('reports the missing public launch requirements that still block external launch', () => {
    const missingItems = getMissingPublicLaunchChecklistItems({
      windowsDownloadConfigured: true,
      operatorCheckoutConfigured: false,
      studioCheckoutConfigured: false,
      mplSourceConfigured: false,
      openSourceRepoConfigured: true,
    }, {
      requiredOnly: true,
    })

    expect(missingItems.map((item) => item.label)).toEqual([
      'Operator checkout URL',
      'Studio checkout URL',
      'MPL corresponding-source URL',
      'Billing product-plan map',
      'Billing price-plan map',
      'Paddle webhook secret',
      'Public auth runtime status',
    ])
    expect(isPublicLaunchReady({
      windowsDownloadConfigured: true,
      operatorCheckoutConfigured: false,
      studioCheckoutConfigured: false,
      mplSourceConfigured: false,
      openSourceRepoConfigured: true,
    })).toBe(false)
  })

  it('treats the bounded website lane as launch-ready only when the full checklist is configured', () => {
    const checklist = getPublicLaunchChecklist({
      windowsDownloadConfigured: true,
      macosDownloadConfigured: true,
      linuxDownloadConfigured: true,
      operatorCheckoutConfigured: true,
      studioCheckoutConfigured: true,
      mplSourceConfigured: true,
      openSourceRepoConfigured: true,
      billingProductPlanMapConfigured: true,
      billingPricePlanMapConfigured: true,
      billingWebhookSecretConfigured: true,
      publicAuthRuntimeReady: true,
    })

    expect(checklist.every((item) => item.configured)).toBe(true)
    expect(isPublicLaunchReady({
      windowsDownloadConfigured: true,
      macosDownloadConfigured: false,
      linuxDownloadConfigured: false,
      operatorCheckoutConfigured: true,
      studioCheckoutConfigured: true,
      mplSourceConfigured: true,
      openSourceRepoConfigured: true,
      billingProductPlanMapConfigured: true,
      billingPricePlanMapConfigured: true,
      billingWebhookSecretConfigured: true,
      publicAuthRuntimeReady: true,
    })).toBe(true)
  })

  it('prefers the authoritative auth-health launch summary when the server already resolved rollout posture', () => {
    const launchStatus = resolvePublicLaunchStatusSummary(
      {
        ok: true,
        service: 'hypertwist-auth-server',
        supertokens: {
          configured: true,
          reachable: true,
          ready: true,
        },
        fallback: {
          enabled: true,
          active: false,
          reason: null,
        },
        billing: {
          statePath: '/tmp/hypertwist-billing.json',
          processedEventCount: 3,
          pricePlanMapConfigured: true,
          productPlanMapConfigured: true,
          webhookSecretConfigured: true,
        },
        runtime: {
          mode: 'public',
          public_origin_ready: true,
          cookie_secure: true,
          api_domain: 'https://hypertwist.app',
          website_domain: 'https://hypertwist.app',
          warnings: [],
          errors: [],
        },
        launch: {
          posture: 'preview',
          ready: false,
          readiness: {
            windowsDownloadConfigured: true,
            macosDownloadConfigured: false,
            linuxDownloadConfigured: false,
            operatorCheckoutConfigured: true,
            studioCheckoutConfigured: true,
            mplSourceConfigured: true,
            openSourceRepoConfigured: true,
            billingProductPlanMapConfigured: true,
            billingPricePlanMapConfigured: true,
            billingWebhookSecretConfigured: true,
            publicAuthRuntimeReady: true,
          },
          checklist: [
            {
              id: 'windows-download',
              label: 'Windows release authority',
              configured: true,
              requiredForPublicLaunch: true,
            },
          ],
          missingLabels: ['preview-tier launch gate'],
          targets: {
            operatorCheckoutTarget: 'https://buy.paddle.com/operator-authoritative',
            studioCheckoutTarget: 'https://buy.paddle.com/studio-authoritative',
          },
        },
      },
      {
        generated_at: '2026-06-29T12:00:00.000Z',
        support_email: 'hello@hypertwist.app',
        public_docs_url: 'https://docs.hypertwist.app',
        release_notes_url: 'https://notes.hypertwist.app',
        platforms: [
          {
            platform_key: 'windows',
            platform: 'Windows',
            subtitle: 'Primary shipping lane',
            details: 'Current packaged validation is strongest on the Windows Unreal lane.',
            configured: true,
            channel: 'preview',
            version: null,
            build_id: null,
            published_at: null,
            file_name: null,
            file_size_bytes: null,
            checksum_sha256: null,
            download_url: null,
            download_available: false,
            validation_summary: null,
          },
        ],
        commerce: {
          operator_checkout_url: null,
          studio_checkout_url: null,
          plan_price_operator: 'Launch pricing via Paddle',
          plan_price_studio: 'Contact for launch readiness',
        },
        corresponding_source_url: null,
        open_source_repo_url: null,
        viewer: {
          authenticated: false,
          canDownload: false,
          plan: null,
          role: null,
          accessStatus: null,
        },
      },
      {
        windowsDownloadConfigured: true,
      },
    )

    expect(launchStatus.ready).toBe(false)
    expect(launchStatus.posture).toBe('preview')
    expect(launchStatus.missingLabels).toEqual(['public launch gate'])
    expect(launchStatus.targets).toEqual({
      operatorCheckoutTarget: 'https://buy.paddle.com/operator-authoritative',
      studioCheckoutTarget: 'https://buy.paddle.com/studio-authoritative',
    })
    expect(launchStatus.checklist.find((item) => item.id === 'windows-download')?.label).toBe('Windows release authority')
    expect(launchStatus.readiness.operatorCheckoutConfigured).toBe(true)
    expect(launchStatus.readiness.publicAuthRuntimeReady).toBe(true)
  })
})
