import Router from '@koa/router';
import { feedbackService, FeedbackInput } from './feedback.service';

const router = new Router();

/**
 * 提交反馈
 * POST /api/feedback
 */
router.post('/', async (ctx) => {
  try {
    const { type, title, content, contact, screenshotUrls } = ctx.request.body as FeedbackInput;

    // 参数验证
    if (!type || !['suggestion', 'bug', 'other'].includes(type)) {
      ctx.status = 400;
      ctx.body = { code: 400, message: 'type must be one of: suggestion, bug, other' };
      return;
    }
    if (!title || typeof title !== 'string' || title.trim().length === 0) {
      ctx.status = 400;
      ctx.body = { code: 400, message: 'title is required' };
      return;
    }
    if (!content || typeof content !== 'string' || content.trim().length === 0) {
      ctx.status = 400;
      ctx.body = { code: 400, message: 'content is required' };
      return;
    }

    const result = await feedbackService.submitFeedback({
      type,
      title: title.trim(),
      content: content.trim(),
      contact: contact?.trim(),
      screenshotUrls,
    });

    ctx.body = {
      code: 0,
      message: 'success',
      data: result,
    };
  } catch (error: any) {
    ctx.status = 500;
    ctx.body = {
      code: 500,
      message: error.message || '提交反馈失败',
    };
  }
});

export default router;