import {expectTypeOf, test} from 'vitest'

import {type ApplicationInterface} from '../applications/applications'
import {type SanityInstance} from '../store/createSanityInstance'
import {type StateSource} from '../store/createStateSourceAction'
import {type FetcherSnapshot} from '../store/fetcherStore'
import {
  type Installation,
  installation,
  type InstallationAccess,
  type InstallationActiveConfig,
  type InstallationBase,
  type InstallationInclude,
  installations,
  type InstallationsResponse,
} from './installations'

const instance = {} as SanityInstance

type IncludeKeys = 'activeConfig' | 'access' | 'interfaces'

// Whether key `K` of `T` is optional, without an `{}`-based check.
type KeyModifier<T, K extends keyof T> =
  Pick<T, K> extends Required<Pick<T, K>> ? 'required' : 'optional'

test('Installation — no includes: only the base shape', () => {
  expectTypeOf<Installation>().toEqualTypeOf<InstallationBase>()
  expectTypeOf<Installation<never>>().toEqualTypeOf<InstallationBase>()
  expectTypeOf<Extract<keyof Installation, IncludeKeys>>().toEqualTypeOf<never>()
  expectTypeOf<Extract<keyof Installation['application'], 'config'>>().toEqualTypeOf<never>()
})

test('InstallationBase — the application sub-object carries name and reference', () => {
  expectTypeOf<InstallationBase['application']['name']>().toEqualTypeOf<string>()
  expectTypeOf<InstallationBase['application']['reference']>().toEqualTypeOf<string>()
})

test('Installation — each token adds its top-level field, required, others absent', () => {
  expectTypeOf<Installation<'access'>['access']>().toEqualTypeOf<InstallationAccess[]>()
  expectTypeOf<
    Extract<keyof Installation<'access'>, 'activeConfig' | 'interfaces'>
  >().toEqualTypeOf<never>()

  expectTypeOf<Installation<'interfaces'>['interfaces']>().toEqualTypeOf<ApplicationInterface[]>()
  expectTypeOf<
    Installation<'activeConfig'>['activeConfig']
  >().toEqualTypeOf<InstallationActiveConfig | null>()
})

test('Installation — multiple tokens add each field', () => {
  type Both = Installation<'access' | 'interfaces'>
  expectTypeOf<Both['access']>().toEqualTypeOf<InstallationAccess[]>()
  expectTypeOf<Both['interfaces']>().toEqualTypeOf<ApplicationInterface[]>()
  expectTypeOf<Extract<keyof Both, 'activeConfig'>>().toEqualTypeOf<never>()
})

test('Installation — module federation manifest include adds the manifest', () => {
  type Item = Installation<'config.mfManifest'>
  expectTypeOf<Item['application']['config']['mfManifest']>().toEqualTypeOf<unknown>()
  expectTypeOf<
    KeyModifier<Item['application']['config'], 'mfManifest'>
  >().toEqualTypeOf<'optional'>()
  expectTypeOf<Extract<keyof Item, IncludeKeys>>().toEqualTypeOf<never>()
})

test('Installation — a wide include array makes the fields optional', () => {
  type Wide = Installation<InstallationInclude>
  expectTypeOf<KeyModifier<Wide, 'access'>>().toEqualTypeOf<'optional'>()
  expectTypeOf<KeyModifier<Wide, 'interfaces'>>().toEqualTypeOf<'optional'>()
  expectTypeOf<KeyModifier<Wide, 'activeConfig'>>().toEqualTypeOf<'optional'>()
  expectTypeOf<KeyModifier<Wide['application'], 'config'>>().toEqualTypeOf<'optional'>()
})

test('installations.resolveState — resolves the wide list envelope', () => {
  expectTypeOf(
    installations.resolveState(instance, {organizationId: 'org_1'}),
  ).resolves.toEqualTypeOf<InstallationsResponse<InstallationInclude>>()
})

test('installation.getState — exposes the snapshot envelope', () => {
  expectTypeOf(installation.getState(instance, 'inst_1')).toEqualTypeOf<
    StateSource<FetcherSnapshot<Installation<InstallationInclude>>>
  >()
})
