import express from 'express';
import * as companyController from '../controllers/company.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { createCompanySchema, updateCompanySchema, createCompanyReminderSchema, updateCompanyReminderSchema } from '../validators/crm.validators.js';
import { z } from 'zod';

const router = express.Router();

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

/**
 * Company management
 */

// Get all companies
router.get('/', companyController.getAllCompanies);

// Get company by ID
router.get(
  '/:companyId',
  validateParams(z.object({ companyId: z.string().uuid() })),
  companyController.getCompanyById
);

// Create new company
router.post(
  '/',
  validateBody(createCompanySchema),
  companyController.createCompany
);

// Update company
router.patch(
  '/:companyId',
  validateParams(z.object({ companyId: z.string().uuid() })),
  validateBody(updateCompanySchema),
  companyController.updateCompany
);

// Delete company
router.delete(
  '/:companyId',
  validateParams(z.object({ companyId: z.string().uuid() })),
  companyController.deleteCompany
);

// Company Notes (nested resources)
router.get(
  '/:companyId/notes',
  validateParams(z.object({ companyId: z.string().uuid() })),
  companyController.getCompanyNotes
);

router.post(
  '/:companyId/notes',
  validateParams(z.object({ companyId: z.string().uuid() })),
  validateBody(z.object({
    content: z.string().min(1),
    reminderAt: z.string().optional().or(z.literal('')),
  })),
  companyController.addCompanyNote
);

router.patch(
  '/:companyId/notes/:noteId',
  validateParams(z.object({
    companyId: z.string().uuid(),
    noteId: z.string().uuid()
  })),
  validateBody(z.object({
    content: z.string().min(1).optional(),
    reminderAt: z.string().optional().or(z.literal('').or(z.null())),
  })),
  companyController.updateCompanyNote
);

router.delete(
  '/:companyId/notes/:noteId',
  validateParams(z.object({
    companyId: z.string().uuid(),
    noteId: z.string().uuid()
  })),
  companyController.deleteCompanyNote
);

// Company reminders
router.get(
  '/:companyId/reminders',
  validateParams(z.object({ companyId: z.string().uuid() })),
  companyController.getCompanyReminders
);

router.post(
  '/:companyId/reminders',
  validateParams(z.object({ companyId: z.string().uuid() })),
  validateBody(createCompanyReminderSchema),
  companyController.createCompanyReminder
);

router.patch(
  '/:companyId/reminders/:reminderId',
  validateParams(z.object({
    companyId: z.string().uuid(),
    reminderId: z.string().uuid()
  })),
  validateBody(updateCompanyReminderSchema),
  companyController.updateCompanyReminder
);

router.delete(
  '/:companyId/reminders/:reminderId',
  validateParams(z.object({
    companyId: z.string().uuid(),
    reminderId: z.string().uuid()
  })),
  companyController.deleteCompanyReminder
);

export default router;
