import { getPayload, Payload } from 'payload'
import config from '@/payload.config'
import { describe, it, beforeAll, expect } from 'vitest'
import { locales, defaultLocale, isValidLocale, getLocaleFromPathname, addLocaleToPathname } from '@/lib/i18n'

let payload: Payload

describe('i18n Library', () => {
  describe('locale validation', () => {
    it('validates correct locales', () => {
      expect(isValidLocale('de')).toBe(true)
      expect(isValidLocale('en')).toBe(true)
    })

    it('rejects invalid locales', () => {
      expect(isValidLocale('fr')).toBe(false)
      expect(isValidLocale('es')).toBe(false)
      expect(isValidLocale('')).toBe(false)
      expect(isValidLocale('deutsch')).toBe(false)
    })
  })

  describe('locale from pathname', () => {
    it('extracts locale from pathname', () => {
      expect(getLocaleFromPathname('/de/page')).toBe('de')
      expect(getLocaleFromPathname('/en/page')).toBe('en')
    })

    it('returns default locale for paths without locale', () => {
      expect(getLocaleFromPathname('/page')).toBe(defaultLocale)
      expect(getLocaleFromPathname('/')).toBe(defaultLocale)
    })

    it('returns default locale for invalid locale in path', () => {
      expect(getLocaleFromPathname('/fr/page')).toBe(defaultLocale)
    })
  })

  describe('addLocaleToPathname', () => {
    it('adds locale prefix to pathname', () => {
      expect(addLocaleToPathname('/page', 'de')).toBe('/de/page')
      expect(addLocaleToPathname('/page', 'en')).toBe('/en/page')
    })

    it('handles root path', () => {
      expect(addLocaleToPathname('/', 'de')).toBe('/de')
      expect(addLocaleToPathname('/', 'en')).toBe('/en')
    })

    it('replaces existing locale', () => {
      expect(addLocaleToPathname('/de/page', 'en')).toBe('/en/page')
      expect(addLocaleToPathname('/en/page', 'de')).toBe('/de/page')
    })
  })

  describe('locales configuration', () => {
    it('has correct default locale', () => {
      expect(defaultLocale).toBe('de')
    })

    it('supports German and English', () => {
      expect(locales).toContain('de')
      expect(locales).toContain('en')
      expect(locales.length).toBe(2)
    })
  })
})

describe('Payload Localization Integration', () => {
  beforeAll(async () => {
    const payloadConfig = await config
    payload = await getPayload({ config: payloadConfig })
  })

  it('payload config has localization enabled', async () => {
    const payloadConfig = await config
    expect(payloadConfig.localization).toBeDefined()
    expect(payloadConfig.localization).not.toBe(false)
    // Type guard for localization config
    const localization = payloadConfig.localization
    if (localization && typeof localization === 'object') {
      expect(localization.locales).toBeDefined()
      expect(localization.defaultLocale).toBe('de')
    }
  })

  it('payload config has i18n enabled', async () => {
    const payloadConfig = await config
    expect(payloadConfig.i18n).toBeDefined()
    expect(payloadConfig.i18n?.supportedLanguages).toBeDefined()
    expect(payloadConfig.i18n?.fallbackLanguage).toBe('de')
  })

  // Note: Actual database operations require migration to be run first
  // These tests verify the configuration is correct
})

describe('Search with Locale', () => {
  beforeAll(async () => {
    const payloadConfig = await config
    payload = await getPayload({ config: payloadConfig })
  })

  it('search supports locale parameter', async () => {
    // Import search functions
    const { searchPosts } = await import('@/lib/search')

    // Search with German locale
    const resultDe = await searchPosts(payload, {
      query: 'test',
      locale: 'de',
      limit: 10,
      offset: 0,
    })

    expect(resultDe.filters.locale).toBe('de')
  })

  it('suggestions support locale parameter', async () => {
    const { getSearchSuggestions } = await import('@/lib/search')

    const suggestions = await getSearchSuggestions(payload, {
      query: 'test',
      locale: 'en',
      limit: 5,
    })

    // Should return array (may be empty if no matching posts)
    expect(Array.isArray(suggestions)).toBe(true)
  })

  it('getPostsByCategory supports locale parameter', async () => {
    const { getPostsByCategory } = await import('@/lib/search')

    const result = await getPostsByCategory(payload, {
      locale: 'de',
      page: 1,
      limit: 10,
    })

    expect(result).toHaveProperty('docs')
    expect(result).toHaveProperty('totalDocs')
  })
})
