// Tests for lib/offline/registry.ts — registerOfflineStore, getOfflineConfig,
// getAllOfflineConfigs, isOfflineCapable.

import { describe, it, expect } from 'vitest'
import {
  registerOfflineStore,
  getOfflineConfig,
  getAllOfflineConfigs,
  isOfflineCapable,
} from '../registry'

// NOTE: registry.ts is a module-level singleton — registrations from the
// production code (imported transitively) are visible here. We only test the
// registry API behaviour and the LSC entries added by this wave.

describe('registry API', () => {
  it('returns undefined for an unknown table', () => {
    expect(getOfflineConfig('unknown_table_xyz')).toBeUndefined()
  })

  it('isOfflineCapable returns false for an unknown table', () => {
    expect(isOfflineCapable('unknown_table_xyz')).toBe(false)
  })

  it('registerOfflineStore makes a table retrievable', () => {
    registerOfflineStore({
      table:       '_test_registry_table',
      storeName:   '_testStore',
      ttlMs:       60_000,
      queueWrites: true,
      primaryKey:  'id',
    })
    const cfg = getOfflineConfig('_test_registry_table')
    expect(cfg).toBeDefined()
    expect(cfg?.storeName).toBe('_testStore')
    expect(cfg?.queueWrites).toBe(true)
  })

  it('getAllOfflineConfigs returns an array with at least one entry', () => {
    const all = getAllOfflineConfigs()
    expect(Array.isArray(all)).toBe(true)
    expect(all.length).toBeGreaterThan(0)
  })

  it('isOfflineCapable returns true after registration', () => {
    registerOfflineStore({
      table:       '_test_capable_table',
      storeName:   '_testCapable',
      ttlMs:       60_000,
      queueWrites: false,
      primaryKey:  'id',
    })
    expect(isOfflineCapable('_test_capable_table')).toBe(true)
  })
})

describe('LSC registrations (wave 2)', () => {
  it('lsc_shifts is registered', () => {
    const cfg = getOfflineConfig('lsc_shifts')
    expect(cfg).toBeDefined()
    expect(cfg?.storeName).toBe('lscShifts')
    expect(cfg?.queueWrites).toBe(true)
  })

  it('lsc_shift_hours is registered', () => {
    const cfg = getOfflineConfig('lsc_shift_hours')
    expect(cfg).toBeDefined()
    expect(cfg?.storeName).toBe('lscShiftHours')
    expect(cfg?.queueWrites).toBe(true)
  })

  it('lsc_shifts has a sane ttlMs (>= 1 minute)', () => {
    const cfg = getOfflineConfig('lsc_shifts')
    expect(cfg!.ttlMs).toBeGreaterThanOrEqual(60_000)
  })

  it('lsc_shift_hours has a sane ttlMs (>= 1 minute)', () => {
    const cfg = getOfflineConfig('lsc_shift_hours')
    expect(cfg!.ttlMs).toBeGreaterThanOrEqual(60_000)
  })
})
