/* eslint-disable no-console */
import path from 'node:path'
import process from 'node:process'
import axios from 'axios'
import express from 'express'
import fs from 'fs-extra'

const app = express()
const PORT = 3001

console.log('Script started')
console.log('Arguments:', process.argv)

// 配置
const API_BASE_URL = 'http://localhost:9527'
const IMPORT_API_PATH = '/proxy-default/Base/ActivityMain/AddQuestionListDetail'
const ASSET_DIR = path.join(__dirname, '../asset')

// 来自 curl 命令的请求头
const HEADERS = {
  'Accept': 'application/json, text/plain, */*',
  'Accept-Language': 'zh-CN,zh;q=0.9',
  'Connection': 'keep-alive',
  'Custom-Platform': 'pc',
  'Origin': 'http://localhost:9527',
  'Referer': 'http://localhost:9527/question-store',
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36',
  'apifoxToken': 'XL299LiMEDZ0H5h3A29PxwQXdMJqWyY2',
  'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"macOS"',
}

interface Question {
  QuestionId: number
  Name: string
  Answer: string
  Type: string
  IsGood: number
  ImageUrl: string
  IsDisabled: boolean
  KeyWord: string
  KeyWordsIndex: number
}

async function importQuestions() {
  try {
    // 检查资源目录是否存在
    console.log(`Checking asset directory: ${ASSET_DIR}`)
    if (!fs.existsSync(ASSET_DIR)) {
      console.error(`Asset directory not found: ${ASSET_DIR}`)
      return
    }

    // 获取资源目录下的所有 JSON 文件
    const files = await fs.readdir(ASSET_DIR)
    console.log(`Files found in directory:`, files)
    const jsonFiles = files.filter(file => file.endsWith('.json'))

    console.log(`Found ${jsonFiles.length} JSON files in ${ASSET_DIR}`)

    for (const file of jsonFiles) {
      const filePath = path.join(ASSET_DIR, file)
      console.log(`\nStarting import for file: ${file}`)

      const data: any = await fs.readJSON(filePath)
      let questions: Question[] = []

      if (Array.isArray(data)) {
        questions = data
      }
      else if (data.question_bank && Array.isArray(data.question_bank)) {
        questions = data.question_bank
      }
      else {
        console.error(`Invalid JSON format in ${file}: question_bank array not found or data is not an array.`)
        continue
      }

      console.log(`Found ${questions.length} questions in ${file}.`)

      for (const [index, question] of questions.entries()) {
        console.log(`[${file}] Importing question ${index + 1}/${questions.length}: ID ${question.QuestionId}`)

        try {
          // 构建请求体 (JSON)
          const body = {
            QuestionId: question.QuestionId,
            Name: question.Name,
            Answer: question.Answer,
            Type: question.Type,
            IsGood: question.IsGood,
            ImageUrl: question.ImageUrl || '',
            IsDisabled: question.IsDisabled || false,
            KeyWord: question.KeyWord || '',
            KeyWordsIndex: question.KeyWordsIndex || 0,
          }

          // 发送请求
          const url = `${API_BASE_URL}${IMPORT_API_PATH}`

          const response = await axios.post(url, body, {
            headers: {
              ...HEADERS,
              'Content-Type': 'application/json',
            },
          })

          console.log(`Success: ${response.status} - ${JSON.stringify(response.data)}`)
        }
        catch (error: any) {
          console.error(`Failed to import question ${question.QuestionId}:`, error.message)
          if (error.response) {
            console.error('Response data:', error.response.data)
          }
        }

        // 添加短暂延迟以避免服务器过载
        await new Promise(resolve => setTimeout(resolve, 200))
      }
    }

    console.log('\nAll imports completed.')
  }
  catch (error) {
    console.error('Error during import process:', error)
  }
}

// 触发导入的路由
app.get('/run-import', async (req, res) => {
  console.log('Received manual trigger for import...')
  importQuestions() // 后台运行
  res.send('Import process started. Check console for logs.')
})

// 如果传递了参数则直接运行导入，不启动 Web 服务
if (process.argv.includes('--run-now')) {
  importQuestions().then(() => {
    process.exit(0)
  })
}
else {
  app.listen(PORT, () => {
    console.log(`Importer service running at http://localhost:${PORT}`)
    console.log('Ensure questions.json is present in the root of this service.')
    console.log(`To start import, visit http://localhost:${PORT}/run-import`)
  })
}
