const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = process.env.API_KEY || '';
const API_BASE = 'https://api.aillm.ru';

const upload = multer({ storage: multer.memoryStorage() });

// ====== Models ======
const MODELS = {
  'gpt-oss-120b': { id: 'gpt-oss-120b', name: 'GPT-OSS-120B', desc: '120B reasoning model', temp: 0.7, tokens: 4096, system: 'Ты — полезный ассистент. Отвечай на том же языке, что и запрос.' },
};

// ====== Static ======
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());

// ====== Routes ======
app.get('/healthz', (_req, res) => res.json({ status: 'ok' }));

app.get('/api/profiles', (_req, res) => {
  res.json(Object.entries(MODELS).map(([id, m]) => ({ id, name: m.name, desc: m.desc })));
});

app.post('/api/whisper', upload.single('audio'), async (req, res) => {
  try {
    if (!req.file) return res.status(400).json({ error: 'No audio file' });
    const form = new FormData();
    form.append('file', new Blob([req.file.buffer], { type: req.file.mimetype }), 'audio.webm');
    form.append('model', 'whisper-large-v3');
    const r = await fetch(`${API_BASE}/v1/audio/transcriptions`, {
      method: 'POST', headers: { Authorization: `Bearer ${API_KEY}` }, body: form,
    });
    const data = await r.json();
    res.json({ text: data.text || '' });
  } catch (e) {
    res.status(500).json({ error: e.message });
  }
});

app.post('/api/chat', async (req, res) => {
  try {
    const { model, message } = req.body;
    const m = MODELS[model];
    if (!m) return res.status(400).json({ error: 'Unknown model' });
    const r = await fetch(`${API_BASE}/v1/chat/completions`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${API_KEY}`, 'Content-Type': 'application/json' },
      body: JSON.stringify({
        model: m.id, temperature: m.temp, max_tokens: m.tokens,
        messages: [
          { role: 'system', content: m.system },
          { role: 'user', content: message || 'Привет' },
        ],
      }),
    });
    const data = await r.json();
    res.json({ response: data.choices?.[0]?.message?.content || 'Нет ответа' });
  } catch (e) {
    res.status(500).json({ error: e.message });
  }
});

app.get('/', (_req, res) => res.sendFile(path.join(__dirname, 'public', 'index.html')));

app.listen(PORT, () => console.log(`Say:${PORT}`));