import { Router, Response, NextFunction } from 'express'
import { authMiddleware, AuthRequest } from '../middleware/auth'
import { getAllTemplates, getTemplateById, getTemplatesByCategory, renderTemplate } from '../services/template.service'

const router = Router()

/** 获取所有模板列表 */
router.get('/', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
  try {
    const category = req.query.category as string | undefined
    const templates = category ? getTemplatesByCategory(category) : getAllTemplates()
    res.json({ success: true, data: templates })
  } catch (err) {
    next(err)
  }
})

/** 获取模板详情 */
router.get('/:id', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
  try {
    const template = getTemplateById(req.params.id)
    if (!template) {
      return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
    }
    res.json({ success: true, data: template })
  } catch (err) {
    next(err)
  }
})

/** 渲染模板 */
router.post('/:id/render', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
  try {
    const { variables } = req.body as { variables: Record<string, string> }
    const content = renderTemplate(req.params.id, variables || {})
    if (!content) {
      return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
    }
    res.json({ success: true, data: { content } })
  } catch (err) {
    next(err)
  }
})

/** 下载模板（Word .doc 格式，支持变量替换） */
router.get('/:id/download', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
  try {
    const template = getTemplateById(req.params.id)
    if (!template) {
      return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
    }
    // 支持通过 query 参数传入变量（如 ?name=张三&idCardNumber=xxx）
    const variables: Record<string, string> = {}
    for (const [key, value] of Object.entries(req.query)) {
      if (typeof value === 'string' && key !== 'token') variables[key] = value
    }
    let content = template.content
    if (Object.keys(variables).length > 0) {
      content = renderTemplate(req.params.id, variables) || content
    }
    // 将纯文本转换为HTML段落，使Word样式生效
    const textToHtml = (text: string): string => {
      const lines = text.split(/\n/)
      let html = ''
      let inTable = false
      for (const line of lines) {
        const trimmed = line.trim()
        if (!trimmed) {
          if (inTable) { html += '</table>'; inTable = false }
          html += '<p style="text-indent:0">&nbsp;</p>'
          continue
        }
        // 标题检测：以"第X条"开头的行作为小标题
        if (/^第[一二三四五六七八九十百]+条/.test(trimmed)) {
          if (inTable) { html += '</table>'; inTable = false }
          html += `<h3>${trimmed}</h3>`
        } else if (/^劳动合同书$|^协议书$|^通知书$|^解除劳动合同协议书$/.test(trimmed)) {
          if (inTable) { html += '</table>'; inTable = false }
          html += `<h1>${trimmed}</h1>`
        } else if (/^(甲方|乙方)（(盖章|签字)）/.test(trimmed) || /^日期[:：]/.test(trimmed)) {
          if (inTable) { html += '</table>'; inTable = false }
          html += `<p class="sign">${trimmed}</p>`
        } else {
          if (inTable) { html += '</table>'; inTable = false }
          html += `<p>${trimmed}</p>`
        }
      }
      if (inTable) html += '</table>'
      return html
    }
    const htmlContent = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head><meta charset="utf-8"><title>${template.name}</title>
<!--[if gte mso 9]><xml>
<w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom><w:DoNotOptimizeForBrowser/></w:WordDocument>
</xml><![endif]-->
<style>
@page { size: A4; margin: 2.54cm 3.17cm 2.54cm 3.17cm; }
body { font-family: SimSun, serif; font-size: 14pt; line-height: 2; text-align: justify; }
h1 { font-size: 22pt; font-weight: bold; text-align: center; margin: 30pt 0 20pt 0; font-family: SimHei, sans-serif; }
h2 { font-size: 16pt; font-weight: bold; margin: 20pt 0 10pt 0; font-family: SimHei, sans-serif; }
h3 { font-size: 14pt; font-weight: bold; margin: 15pt 0 8pt 0; font-family: SimHei, sans-serif; text-indent: 0; }
p { text-indent: 2em; margin: 0 0 10pt 0; }
table { border-collapse: collapse; width: 100%; margin: 10pt 0; }
td, th { border: 1pt solid #000; padding: 4pt 8pt; font-size: 12pt; }
th { background: #f0f0f0; font-weight: bold; text-align: center; }
.sign { text-align: right; margin-top: 30pt; margin-right: 20pt; text-indent: 0; }
</style></head>
<body>${textToHtml(content)}</body></html>`
    const encoded = encodeURIComponent(template.name + '.doc')
    res.setHeader('Content-Type', 'application/msword; charset=utf-8')
    res.setHeader('Content-Disposition', `attachment; filename="${encoded}"; filename*=UTF-8''${encoded}`)
    res.send(htmlContent)
  } catch (err) {
    next(err)
  }
})

export default router
