// routes/membershipFormRoutes.js
const express = require('express');
const router = express.Router();
const db = require('../models');
const MembershipForm = db.MembershipForm;

// GET
router.get('/', async (req, res) => {
  try {
    const form = await MembershipForm.findOne();
    if (!form) return res.status(404).json({ message: 'No form found' });
    res.json(form);
  } catch (err) {
    console.error('GET /membership-form error:', err);
    res.status(500).json({ message: 'Server error' });
  }
});

// PUT
router.put('/', async (req, res) => {
  try {
    const { url } = req.body;
    const form = await MembershipForm.findOne();
    if (!form) return res.status(404).json({ message: 'No form found' });
    form.url = url;
    await form.save();
    res.json({ message: 'Form URL updated', form });
  } catch (err) {
    console.error('PUT /membership-form error:', err);
    res.status(500).json({ message: 'Server error' });
  }
});

module.exports = router;
