import express from 'express';
import * as pushController from '../controllers/push.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody } from '../middlewares/security/validateInput.js';
import { z } from 'zod';

const router = express.Router();

// Get VAPID public key (no auth required)
router.get('/vapid-public-key', pushController.getVapidPublicKey);

// All other routes require authentication
router.use(authenticate);

// Subscribe to push notifications
router.post(
  '/subscribe',
  validateBody(
    z.object({
      subscription: z.object({
        endpoint: z.string().url(),
        keys: z.object({
          p256dh: z.string(),
          auth: z.string(),
        }),
      }),
    })
  ),
  pushController.subscribe
);

// Unsubscribe from push notifications
router.post(
  '/unsubscribe',
  validateBody(
    z.object({
      endpoint: z.string().url().optional(),
    }).optional()
  ),
  pushController.unsubscribe
);

// Get subscription status
router.get('/status', pushController.getStatus);

// Test push notification
router.post('/test', pushController.testPush);

export default router;
