import { Router } from 'express';
import { z } from 'zod';
import { validate } from '../middleware/validate.js';

const router = Router();

const newsletterSchema = z.object({
  email: z.string().email('Valid email is required'),
});

const contactSchema = z.object({
  name: z.string().min(2, 'Name is required'),
  email: z.string().email('Valid email is required'),
  subject: z.string().min(2, 'Subject is required'),
  message: z.string().min(10, 'Message must be at least 10 characters'),
});

router.post('/newsletter', validate(newsletterSchema), async (req, res, next) => {
  try {
    const { email } = req.body;
    // In production, store in a newsletter subscribers table or send to email service
    // For now, log and return success
    console.log(`Newsletter subscription: ${email}`);
    res.json({ message: 'Successfully subscribed to newsletter' });
  } catch (error) {
    next(error);
  }
});

router.post('/contact', validate(contactSchema), async (req, res, next) => {
  try {
    const { name, email, subject, message } = req.body;
    // In production, store in a contact submissions table or send email
    console.log(`Contact form: ${name} <${email}> - ${subject}: ${message}`);
    res.json({ message: 'Contact form submitted successfully' });
  } catch (error) {
    next(error);
  }
});

export default router;
