import express from 'express'
import { z } from 'zod'
import { authenticate } from '../middlewares/auth/authMiddleware.js'
import { validateBody, validateParams } from '../middlewares/security/validateInput.js'
import * as personalContactController from '../controllers/personal-contact.controller.js'

const router = express.Router()

const createContactSchema = z.object({
  firstName: z.string().min(1, 'Meno je povinné'),
  lastName: z.union([z.string(), z.literal(''), z.null()]).optional(),
  phone: z.union([z.string(), z.literal(''), z.null()]).optional(),
  email: z.string().email('Neplatný email'),
  secondaryEmail: z.union([z.string().email('Neplatný email'), z.literal(''), z.null()]).optional(),
  companyId: z.union([z.string().uuid(), z.literal(''), z.null()]).optional(),
  description: z.union([z.string().max(1000), z.literal(''), z.null()]).optional(),
})

const updateContactSchema = createContactSchema.partial()

const contactIdSchema = z.object({ contactId: z.string().uuid() })

router.use(authenticate)

router.get('/', personalContactController.listPersonalContacts)

router.post('/', validateBody(createContactSchema), personalContactController.createPersonalContact)

router.put(
  '/:contactId',
  validateParams(contactIdSchema),
  validateBody(updateContactSchema),
  personalContactController.updatePersonalContact
)

router.delete(
  '/:contactId',
  validateParams(contactIdSchema),
  personalContactController.deletePersonalContact
)

export default router
