import express from 'express';
import * as emailSignatureController from '../controllers/email-signature.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody } from '../middlewares/security/validateInput.js';
import { z } from 'zod';

const router = express.Router();

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

// Validation schemas
const signatureSchema = z.object({
  fullName: z.string().max(255).optional().or(z.literal('')).or(z.null()),
  position: z.string().max(255).optional().or(z.literal('')).or(z.null()),
  phone: z.string().max(50).optional().or(z.literal('')).or(z.null()),
  email: z.string().email('Neplatný formát emailu').max(255).optional().or(z.literal('')).or(z.null()),
  companyName: z.string().max(255).optional().or(z.literal('')).or(z.null()),
  website: z.string().max(255).optional().or(z.literal('')).or(z.null()),
  isEnabled: z.boolean().optional(),
});

const toggleSchema = z.object({
  isEnabled: z.boolean(),
});

// Get user's signature
router.get('/', emailSignatureController.getSignature);

// Get formatted signature text
router.get('/formatted', emailSignatureController.getFormattedSignature);

// Create or update signature
router.post('/', validateBody(signatureSchema), emailSignatureController.upsertSignature);

// Toggle signature on/off
router.patch('/toggle', validateBody(toggleSchema), emailSignatureController.toggleSignature);

// Delete signature
router.delete('/', emailSignatureController.deleteSignature);

export default router;
