'use strict';

/**
 * ui/routes/export.js — GET /export.
 * Доступен всем аутентифицированным (не только admin).
 * Проксирует GET /api/v1/entries/export (text/plain).
 */

const { Router } = require('express');
const api = require('../api-client');

function createRouter() {
  const router = Router();

  router.get('/export', async (req, res) => {
    const token = api.token(req);
    const companyQuery = req.query.company ? '?company=' + parseInt(req.query.company, 10) : '';
    try {
      const r = await api.get('/api/v1/entries/export' + companyQuery, token);
      res.set('Content-Type', 'text/plain; charset=utf-8');
      res.set('Content-Disposition', 'inline; filename="whitelist.txt"');
      res.send(r.data);
    } catch (e) {
      res.status(500).send('Ошибка экспорта: ' + e.message);
    }
  });

  return router;
}

module.exports = { createRouter };
