import express from 'express';
import * as emailAccountController from '../controllers/email-account.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { sensitiveOperationLimiter } from '../middlewares/security/rateLimiter.js';
import {
  createEmailAccountSchema,
  updateEmailAccountSchema,
} from '../validators/email-account.validators.js';
import { z } from 'zod';

const router = express.Router();

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

/**
 * Email account management
 */

// Get all email accounts for logged-in user
router.get('/', emailAccountController.getEmailAccounts);

// Create new email account
router.post(
  '/',
  sensitiveOperationLimiter,
  validateBody(createEmailAccountSchema),
  emailAccountController.createEmailAccount
);

// Set email account as primary
router.post(
  '/:id/set-primary',
  validateParams(z.object({ id: z.string().uuid() })),
  emailAccountController.setPrimaryEmailAccount
);

// Delete email account
router.delete(
  '/:id',
  validateParams(z.object({ id: z.string().uuid() })),
  sensitiveOperationLimiter,
  emailAccountController.deleteEmailAccount
);

export default router;
