import assert from 'node:assert/strict'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import test from 'node:test'

test('system skills can be saved, listed and deleted by admin only', async (t) => {
  const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'aipros-system-skills-'))
  process.env.DB_PATH = path.join(tempRoot, 'test.db')
  process.env.SYSTEM_SKILL_ROOT = path.join(tempRoot, 'local-skills')
  fs.mkdirSync(path.join(process.env.SYSTEM_SKILL_ROOT, 'skill-creator'), { recursive: true })
  fs.mkdirSync(path.join(process.env.SYSTEM_SKILL_ROOT, 'skill-evaluator'), { recursive: true })
  fs.writeFileSync(
    path.join(process.env.SYSTEM_SKILL_ROOT, 'skill-creator', 'SKILL.md'),
    '---\nname: skill-creator\n---\n# Local Skill Creator\nProduce skill.spec.json.\n'
  )
  fs.writeFileSync(
    path.join(process.env.SYSTEM_SKILL_ROOT, 'skill-evaluator', 'SKILL.md'),
    '---\nname: skill-evaluator\n---\n# Local Skill Evaluator\nScore skills.\n'
  )

  t.after(() => {
    delete process.env.SYSTEM_SKILL_ROOT
    fs.rmSync(tempRoot, { recursive: true, force: true })
  })

  const { default: app } = await import('../index.js')

  const nonAdmin = await app.request('/api/system-skills/creator', {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-User-Email': 'user@example.com',
    },
    body: JSON.stringify({ content: 'not allowed' }),
  })
  assert.equal(nonAdmin.status, 403)

  const save = await app.request('/api/system-skills/creator', {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-User-Email': '494161546@qq.com',
    },
    body: JSON.stringify({
      name: '本地 Skill Creator',
      description: '平台创建 Skill 时使用',
      sourceName: 'SKILL.md',
      enabled: true,
      content: '# creator unique marker',
    }),
  })
  assert.equal(save.status, 200)
  const saved = await save.json()
  assert.equal(saved.skill.kind, 'creator')
  assert.equal(saved.skill.content, '# creator unique marker')
  assert.equal(saved.skill.sourceName, 'SKILL.md')
  assert.equal(saved.skill.enabled, true)

  const list = await app.request('/api/system-skills', {
    headers: { 'X-User-Email': '494161546@qq.com' },
  })
  assert.equal(list.status, 200)
  const body = await list.json()
  assert.equal(body.slots.length, 2)
  assert.equal(body.slots.find(s => s.kind === 'creator').content, '# creator unique marker')
  assert.equal(body.slots.find(s => s.kind === 'evaluator').content, '')

  const deleted = await app.request('/api/system-skills/creator', {
    method: 'DELETE',
    headers: { 'X-User-Email': '494161546@qq.com' },
  })
  assert.equal(deleted.status, 200)

  const afterDelete = await app.request('/api/system-skills/creator', {
    headers: { 'X-User-Email': '494161546@qq.com' },
  })
  assert.equal(afterDelete.status, 200)
  const cleared = await afterDelete.json()
  assert.equal(cleared.skill.content, '')
  assert.equal(cleared.skill.enabled, false)

  const imported = await app.request('/api/system-skills/creator/import-default', {
    method: 'POST',
    headers: { 'X-User-Email': '494161546@qq.com' },
  })
  assert.equal(imported.status, 200)
  const importedBody = await imported.json()
  assert.equal(importedBody.skill.kind, 'creator')
  assert.match(importedBody.skill.content, /Local Skill Creator/)
  assert.equal(importedBody.skill.sourceName, '本机默认 skill-creator/SKILL.md')
  assert.equal(importedBody.skill.enabled, true)
})
