const express = require('express');
const cors = require('cors');
const { createProxyMiddleware } = require('http-proxy-middleware');
const multer = require('multer');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const fs = require('fs');
const morgan = require('morgan');
require('dotenv').config();

const app = express();
const PORT = process.env.PORT || 3000;

// 配置全局请求日志，可以在终端里看到每个到达 Node 端点的请求记录
app.use(morgan(':method :url :status :res[content-length] - :response-time ms'));

// 配置 CORS，允许前端项目的跨域请求
app.use(cors());

// --- 1. 流式对话请求代理配置 ---
// 在请求交给代理组件之前，拦截所有的 /api/chat-ui/chat 并强行给 Header 加上 Bearer Token
app.use('/api/chat-ui/chat', (req, res, next) => {
    const apiKey = process.env.ALIYUN_API_KEY;
    if (!apiKey) {
      console.error("【错误】发送代理请求前未配置 ALIYUN_API_KEY !");
    } else {
      req.headers['authorization'] = `Bearer ${apiKey}`;
    }
    next();
});

// 注意：代理中间件需要在 body-parser (express.json) 之前，不然代理会导致请求体丢失
app.use(
  '/api/chat-ui/chat',
  createProxyMiddleware({
    // 阿里云百炼由于兼容 OpenAI 格式，所以代理到此接口
    target: 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
    changeOrigin: true,
    // 去除路径前缀，确保发往阿里云的路径是纯正的 completions 路径
    pathRewrite: {
      '^/api/chat-ui/chat': '',
    }
  })
);

// --- 下面的路由专门走 Node.js 业务逻辑，因此需要解析 JSON Body ---
app.use(express.json());

// --- 2. 获取模型列表 ---
app.get('/api/chat-ui/models', (req, res) => {
  res.json([
    {
      id: "qwen-max",
      name: "通义千问 Max",
      description: "最强大的模型",
      maxTokens: 8192,
      provider: "Aliyun"
    },
    {
      id: "qwen-plus",
      name: "通义千问 Plus",
      description: "能力均衡",
      maxTokens: 8192,
      provider: "Aliyun"
    }
  ]);
});

// 内存中暂时存放对话数据用于 Mock
const conversationsDB = {};

// --- 3. 获取所有对话历史 ---
app.get('/api/chat-ui/conversations', (req, res) => {
  res.json(Object.values(conversationsDB));
});

// --- 4. 获取单个对话 ---
app.get('/api/chat-ui/conversations/:id', (req, res) => {
  const { id } = req.params;
  const conversation = conversationsDB[id];
  if (conversation) {
    res.json(conversation);
  } else {
    res.status(404).json({ error: '对话不存在' });
  }
});

// --- 5. 保存或更新对话 ---
// 前端可能会在 /api/chat-ui/conversations/:id 用 POST 或 PUT 更新？ 也可以直接提供一个保存接口
app.post('/api/chat-ui/conversations', (req, res) => {
    const data = req.body;
    if(!data.id) data.id = uuidv4();
    conversationsDB[data.id] = data;
    res.json(data);
})

// --- 6. 删除对话 ---
app.delete('/api/chat-ui/conversations/:id', (req, res) => {
  const { id } = req.params;
  if (conversationsDB[id]) {
    delete conversationsDB[id];
    res.json({ success: true, message: "删除成功" });
  } else {
    res.status(404).json({ error: '对话不存在' });
  }
});

// 为了存储上传文件而建立临时目录
const uploadDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir);
}

// 配置文件上传
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, uploadDir);
  },
  filename: function (req, file, cb) {
    const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
    cb(null, uniqueSuffix + '-' + file.originalname);
  }
});
const upload = multer({ storage: storage });

// 提供静态文件访问支持
app.use('/uploads', express.static(uploadDir));

// --- 7. 上传文件 ---
app.post('/api/chat-ui/upload', upload.single('file'), (req, res) => {
  if (!req.file) {
    return res.status(400).json({ error: '没有文件上传' });
  }
  
  // 返回供前端使用和访问的 URL
  res.json({
    url: `http://localhost:${PORT}/uploads/${req.file.filename}`,
    name: req.file.originalname,
    size: req.file.size,
    mimeType: req.file.mimetype
  });
});

// --- 8. 停止生成 ---
// 这个接口对于本地代理没有实际效果，因为流的断开是通过底层 AbortController 控制的，此处直接返回成功
app.post(['/api/chat-ui/stop', '/api/chat-ui/stop/:id'], (req, res) => {
  res.json({ success: true, message: "已发出停止指令" });
});

// 其他所有路由返回404
app.use((req, res) => {
  res.status(404).json({ error: 'Endpoint not found' });
});

app.listen(PORT, () => {
    console.log('====================================');
    console.log(`本地代理服务器已启动，监听端口: ${PORT}`);
    console.log('====================================');
    if (!process.env.ALIYUN_API_KEY) {
        console.log('⚠️ 警告: 未在 .env 文件中检测到 ALIYUN_API_KEY!');
        console.log('请在 server/.env 中添加您的百炼 API Key。');
    } else {
        console.log('✅ 检测到了 API Key。');
    }
});
