import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
import {
  TOKEN_SHADOWS,
  TOKEN_MOTION_DURATIONS,
  TOKEN_MOTION_EASES,
  TOKEN_FOCUS_RING,
  TOKEN_OPACITIES,
  TOKEN_SIZES,
  TOKEN_Z_INDEXES,
  TOKEN_LEADING,
  TOKEN_TRACKING,
  TOKEN_FONT_FAMILIES,
  TOKEN_MEASURES,
  TOKEN_TEXT_FLUID,
} from './tokens'

const base = readFileSync(new URL('./base.css', import.meta.url), 'utf8')
const css = readFileSync(new URL('./theme.css', import.meta.url), 'utf8')

describe('ui-tokens non-color scales', () => {
  it('defines shadow tokens in base.css', () => {
    for (const scale of TOKEN_SHADOWS) {
      expect(base).toContain(`--mod-shadow-${scale}:`)
    }
  })

  it('maps shadow tokens to Tailwind theme variables', () => {
    for (const scale of TOKEN_SHADOWS) {
      expect(css).toContain(`--shadow-${scale}: var(--mod-shadow-${scale});`)
    }
  })

  it('defines motion tokens in base.css', () => {
    for (const scale of TOKEN_MOTION_DURATIONS) {
      expect(base).toContain(`--mod-duration-${scale}:`)
    }
    for (const scale of TOKEN_MOTION_EASES) {
      expect(base).toContain(`--mod-ease-${scale}:`)
    }
  })

  it('maps motion tokens to Tailwind theme variables', () => {
    for (const scale of TOKEN_MOTION_DURATIONS) {
      expect(css).toContain(`--duration-${scale}: var(--mod-duration-${scale});`)
    }
    for (const scale of TOKEN_MOTION_EASES) {
      expect(css).toContain(`--ease-${scale}: var(--mod-ease-${scale});`)
    }
  })

  it('defines focus-ring tokens in base.css', () => {
    for (const scale of TOKEN_FOCUS_RING) {
      expect(base).toContain(`--mod-focus-ring-${scale}:`)
    }
  })

  it('maps focus-ring tokens to Tailwind theme variables', () => {
    expect(css).toContain('--ring-width: var(--mod-focus-ring-width);')
    expect(css).toContain('--ring-offset-width: var(--mod-focus-ring-offset);')
    expect(css).toContain('--ring-color: var(--mod-focus-ring-color);')
  })

  it('defines opacity tokens in base.css', () => {
    for (const scale of TOKEN_OPACITIES) {
      expect(base).toContain(`--mod-opacity-${scale}:`)
    }
  })

  it('maps opacity tokens to Tailwind theme variables', () => {
    for (const scale of TOKEN_OPACITIES) {
      expect(css).toContain(`--opacity-${scale}: var(--mod-opacity-${scale});`)
    }
  })

  it('defines sizing tokens in base.css', () => {
    for (const scale of TOKEN_SIZES) {
      expect(base).toContain(`--mod-size-${scale}:`)
    }
    expect(base).toContain('--mod-touch-min:')
  })

  it('maps sizing tokens to Tailwind theme variables', () => {
    expect(css).toContain('--size-control-sm: var(--mod-size-control-sm);')
    expect(css).toContain('--size-control-md: var(--mod-size-control-md);')
    expect(css).toContain('--size-control-lg: var(--mod-size-control-lg);')
    expect(css).toContain('--touch-min: var(--mod-touch-min);')
  })

  it('defines z-index tokens in base.css', () => {
    for (const scale of TOKEN_Z_INDEXES) {
      expect(base).toContain(`--mod-z-${scale}:`)
    }
  })

  it('maps z-index tokens to Tailwind theme variables', () => {
    for (const scale of TOKEN_Z_INDEXES) {
      expect(css).toContain(`--z-${scale}: var(--mod-z-${scale});`)
    }
  })

  it('defines typography tokens in base.css', () => {
    for (const scale of TOKEN_LEADING) {
      expect(base).toContain(`--mod-leading-${scale}:`)
    }
    for (const scale of TOKEN_TRACKING) {
      expect(base).toContain(`--mod-tracking-${scale}:`)
    }
    for (const scale of TOKEN_FONT_FAMILIES) {
      expect(base).toContain(`--mod-font-family-${scale}:`)
    }
    expect(base).toContain('--mod-underline-offset:')
    for (const scale of TOKEN_MEASURES) {
      expect(base).toContain(`--mod-${scale}:`)
    }
    for (const scale of TOKEN_TEXT_FLUID) {
      expect(base).toContain(`--mod-text-fluid-${scale}:`)
    }
  })

  it('maps typography tokens to Tailwind theme variables', () => {
    for (const scale of TOKEN_LEADING) {
      expect(css).toContain(`--leading-${scale}: var(--mod-leading-${scale});`)
    }
    for (const scale of TOKEN_TRACKING) {
      expect(css).toContain(`--tracking-${scale}: var(--mod-tracking-${scale});`)
    }
    for (const scale of TOKEN_FONT_FAMILIES) {
      expect(css).toContain(`--font-${scale}: var(--mod-font-family-${scale});`)
    }
    expect(css).toContain('--underline-offset: var(--mod-underline-offset);')
    for (const scale of TOKEN_MEASURES) {
      expect(css).toContain(`--${scale}: var(--mod-${scale});`)
    }
    for (const scale of TOKEN_TEXT_FLUID) {
      expect(css).toContain(`--text-fluid-${scale}: var(--mod-text-fluid-${scale});`)
    }
  })
})
