require('dotenv').config();
const express = require('express');
const path = require('path');
const cors = require('cors');

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

// 中间件
app.use(cors());
app.use(express.json());

// API 路由
app.use('/api/auth', require('./src/routes/auth'));
app.use('/api/roles', require('./src/routes/roles'));
app.use('/api/apikeys', require('./src/routes/apikeys'));
app.use('/api/orders', require('./src/routes/orders'));
app.use('/api/hermes', require('./src/routes/hermes'));
app.use('/api/admin-auth', require('./src/routes/admin-auth'));
app.use('/api/admin', require('./src/routes/admin'));
app.use('/api/admin/config', require('./src/routes/admin-config'));
app.use('/api/admin/sync', require('./src/routes/admin-sync'));

// Mock Hermes 端点（仅 development/test 环境注册，显式 allowlist 防止误开放）
const MOCK_HERMES_ALLOWED_ENVS = ['development', 'test'];
if (MOCK_HERMES_ALLOWED_ENVS.includes(process.env.NODE_ENV || 'development')) {
  app.use('/api/mock-hermes', require('./src/routes/mock-hermes'));
}

// 静态文件
app.use(express.static('.'));

// 主页
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(PORT, '0.0.0.0', () => {
  console.log(`EternalAI server running on http://0.0.0.0:${PORT}`);
});
