import type { NextApiRequest, NextApiResponse } from 'next'; import { MembershipPlan } from '@/models'; import withDatabase from '@/lib/withDatabase'; async function handler(req: NextApiRequest, res: NextApiResponse) { const { id } = req.query; if (req.method === 'GET') { try { const plan = await MembershipPlan.findById(id); if (!plan) return res.status(404).json({ message: 'Plan not found' }); return res.status(200).json(plan); } catch (error) { return res.status(500).json({ message: 'Failed to fetch plan' }); } } else if (req.method === 'PUT') { try { const plan = await MembershipPlan.findByIdAndUpdate(id, req.body, { new: true }); if (!plan) return res.status(404).json({ message: 'Plan not found' }); return res.status(200).json(plan); } catch (error) { return res.status(500).json({ message: 'Failed to update plan' }); } } else if (req.method === 'DELETE') { try { const plan = await MembershipPlan.findByIdAndDelete(id); if (!plan) return res.status(404).json({ message: 'Plan not found' }); return res.status(200).json({ message: 'Plan deleted' }); } catch (error) { return res.status(500).json({ message: 'Failed to delete plan' }); } } else { return res.status(405).json({ message: 'Method not allowed' }); } } export default withDatabase(handler);