import User from '../models/User.js';
import { sendMobileFeedbackEmail } from '../services/emailService.js';

const clean = (value, max = 4000) => String(value ?? '').trim().slice(0, max);

export const sendMobileFeedback = async (req, res) => {
  try {
    const message = clean(req.body?.message, 5000);
    if (!message) {
      return res.status(400).json({ error: 'message_required' });
    }

    const user = req.user?.id ? await User.findByPk(req.user.id) : null;
    await sendMobileFeedbackEmail({
      message,
      screen: clean(req.body?.screen, 200),
      clubId: req.body?.clubId ?? null,
      appVersion: clean(req.body?.appVersion, 80),
      platform: clean(req.body?.platform, 80) || 'Android',
      backendBaseUrl: clean(req.body?.backendBaseUrl, 300),
      user: user ? { id: user.id, username: user.username, email: user.email } : { id: req.user?.id },
    });

    return res.status(200).json({ success: true });
  } catch (error) {
    console.error('[sendMobileFeedback] - error:', error);
    return res.status(500).json({ error: 'internalerror' });
  }
};
