import { defineConfig, presetUno, transformerDirectives, transformerVariantGroup } from 'unocss'
import { presetDaisy } from 'unocss-preset-daisy'
import { readFileSync } from 'fs'
import { createRequire } from 'module'

const require = createRequire(import.meta.url)
const daisyuiThemes = readFileSync(require.resolve('daisyui/dist/themes.css'), 'utf-8')

const basePreflight = `
*,::before,::after{box-sizing:border-box}
html{font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";line-height:1.5;-webkit-text-size-adjust:100%;tab-size:4}
body{margin:0;padding:0;line-height:inherit}
a{color:inherit;text-decoration:inherit}
img,svg,video,canvas,audio,iframe,embed,object{display:block;vertical-align:middle}
img,video{max-width:100%;height:auto}
h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}
ol,ul{list-style:none;margin:0;padding:0}
button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:inherit;color:inherit;margin:0;padding:0}
button,select{text-transform:none}
`

// DaisyUI v3 sets --btn-text-case:uppercase per [data-theme=*] in themes.css and
// drives text-transform via that variable in component styles. Both come after
// preflights, so !important is the only reliable escape.
// svg sizing is handled in base.html via higher-specificity (.btn svg.w-N) rules.
const daisyV3Overrides = `
.btn{text-transform:none!important;letter-spacing:normal!important}
`

export default defineConfig({
  preflights: [
    { getCSS: () => basePreflight },
    { getCSS: () => daisyuiThemes },
    { getCSS: () => daisyV3Overrides },
  ],
  content: {
    filesystem: [
      '../../crates/ontoref-daemon/templates/**/*.html',
    ],
  },
  presets: [
    presetUno(),
    presetDaisy({ themes: ['dark', 'light'] }),
  ],
  transformers: [
    transformerDirectives(),
    transformerVariantGroup(),
  ],
  safelist: [
    // DaisyUI component classes assembled dynamically in JS (authBadge, statusBadge)
    'badge', 'badge-xs', 'badge-ghost', 'badge-info', 'badge-error',
    'badge-success', 'badge-warning', 'badge-neutral', 'badge-lg', 'badge-outline',
    'loading', 'loading-spinner', 'loading-sm',
    // Utility classes assembled dynamically in JS
    'font-mono', 'hidden', 'line-through',
    'text-orange-400', 'text-cyan-400', 'text-purple-400', 'text-yellow-400',
  ],
})
