import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'

import * as themeSkinModel from '../src/composables/useThemeSkin.js'

const settingsModel = readFileSync(new URL('../src/utils/settingsModelHelper.js', import.meta.url), 'utf8')
const settingsView = readFileSync(new URL('../src/views/SettingsView.vue', import.meta.url), 'utf8')
const settingsStyles = readFileSync(new URL('../src/assets/styles/views/settings-view.css', import.meta.url), 'utf8')
const aiModeStyles = readFileSync(
  new URL('../src/assets/styles/components/personal-workbench-ai-mode.css', import.meta.url),
  'utf8'
)

function testAppearanceSectionIsThemeSettings() {
  assert.match(settingsModel, /id:\s*'appearance'[\s\S]*label:\s*'主题设置'/)
  assert.match(settingsModel, /id:\s*'appearance'[\s\S]*title:\s*'主题风格与界面体验'/)
  assert.match(settingsModel, /id:\s*'appearance'[\s\S]*actionLabel:\s*'保存主题设置'/)
  assert.match(settingsView, /<h4>主题风格与界面体验<\/h4>/)
  assert.doesNotMatch(settingsModel, /label:\s*'界面皮肤'/)
  assert.doesNotMatch(settingsView, /界面皮肤与企业主色/)
}

function testThemeOptionsCollapseToThreeExperienceModes() {
  assert.deepEqual(
    themeSkinModel.THEME_SKIN_OPTIONS.map((theme) => theme.id),
    ['vivid', 'enterprise', 'intelligent']
  )
  assert.deepEqual(
    themeSkinModel.THEME_SKIN_OPTIONS.map((theme) => theme.label),
    ['动感活泼', '企业沉稳', '专业智能']
  )
  assert.equal(typeof themeSkinModel.normalizeThemeMode, 'function')
  assert.equal(themeSkinModel.normalizeThemeMode('sky'), 'vivid')
  assert.equal(themeSkinModel.normalizeThemeMode('blue'), 'vivid')
  assert.equal(themeSkinModel.normalizeThemeMode('legacy-green'), 'vivid')
  assert.equal(themeSkinModel.normalizeThemeMode('navy'), 'enterprise')
  assert.equal(themeSkinModel.normalizeThemeMode('slate'), 'enterprise')
  assert.equal(themeSkinModel.normalizeThemeMode('soft-violet'), 'intelligent')
  assert.equal(themeSkinModel.normalizeThemeMode(''), 'enterprise')
  assert.equal(themeSkinModel.normalizeThemeMode('unknown-theme'), 'enterprise')
}

function testSettingsThemeCardsAvoidLegacySkinLanguage() {
  assert.match(settingsView, /theme-option-grid/)
  assert.match(settingsView, /theme-preview-panel/)
  assert.match(settingsStyles, /\.theme-option-grid/)
  assert.match(settingsStyles, /\.theme-style-preview/)
  assert.doesNotMatch(settingsView, /skin-option-grid/)
  assert.doesNotMatch(settingsStyles, /\.skin-option-grid/)
}

function testEnterpriseThemeHasAiModeOverrides() {
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-mode\s*\{/)
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-mode\.has-conversation\s*\{/)
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-orb\s*\{/)
  assert.match(
    aiModeStyles,
    /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-orb\s*\{[\s\S]*border:\s*0;[\s\S]*border-radius:\s*50%;[\s\S]*background:\s*transparent;[\s\S]*box-shadow:\s*none;/
  )
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-composer[\s\S]*\{/)
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-message\s*\{/)
  assert.match(aiModeStyles, /\[data-theme-mode="enterprise"\]\s+\.workbench-ai-thinking-panel\s*\{/)
}

function run() {
  testAppearanceSectionIsThemeSettings()
  testThemeOptionsCollapseToThreeExperienceModes()
  testSettingsThemeCardsAvoidLegacySkinLanguage()
  testEnterpriseThemeHasAiModeOverrides()
  console.log('settings theme section tests passed')
}

run()
