const express = require('express');
const router = express.Router();
const { Registration, Activity } = require('../models');

// 提交报名
router.post('/registrations', async (req, res) => {
  try {
    const { activityId, name, phone, carModel, carNumber, note } = req.body;
    
    const activity = await Activity.findByPk(activityId);
    if (!activity) {
      return res.status(404).json({ error: '活动不存在' });
    }
    
    const registration = await Registration.create({
      activityId, name, phone, carModel, carNumber, note
    });
    
    res.json(registration);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 查询报名状态
router.get('/registrations/:id', async (req, res) => {
  try {
    const registration = await Registration.findByPk(req.params.id);
    res.json(registration);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 获取活动报名列表（已通过）
router.get('/activities/:id/registrations', async (req, res) => {
  try {
    const registrations = await Registration.findAll({
      where: { activityId: req.params.id, status: 'approved' }
    });
    res.json(registrations);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

module.exports = router;
