import express from 'express';
import * as projectController from '../controllers/project.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { createProjectSchema, updateProjectSchema } from '../validators/crm.validators.js';
import { z } from 'zod';

const router = express.Router();

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

/**
 * Project management
 */

// Get all projects
router.get('/', projectController.getAllProjects);

// Get project by ID
router.get(
  '/:projectId',
  validateParams(z.object({ projectId: z.string().uuid() })),
  projectController.getProjectById
);

// Create new project
router.post(
  '/',
  validateBody(createProjectSchema),
  projectController.createProject
);

// Update project
router.patch(
  '/:projectId',
  validateParams(z.object({ projectId: z.string().uuid() })),
  validateBody(updateProjectSchema),
  projectController.updateProject
);

// Delete project
router.delete(
  '/:projectId',
  validateParams(z.object({ projectId: z.string().uuid() })),
  projectController.deleteProject
);

// Project Notes (nested resources)
router.get(
  '/:projectId/notes',
  validateParams(z.object({ projectId: z.string().uuid() })),
  projectController.getProjectNotes
);

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

router.patch(
  '/:projectId/notes/:noteId',
  validateParams(z.object({
    projectId: 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())),
  })),
  projectController.updateProjectNote
);

router.delete(
  '/:projectId/notes/:noteId',
  validateParams(z.object({
    projectId: z.string().uuid(),
    noteId: z.string().uuid()
  })),
  projectController.deleteProjectNote
);

// Project Users (team members)
router.get(
  '/:projectId/users',
  validateParams(z.object({ projectId: z.string().uuid() })),
  projectController.getProjectUsers
);

router.post(
  '/:projectId/users',
  validateParams(z.object({ projectId: z.string().uuid() })),
  validateBody(z.object({
    userId: z.string().uuid('Neplatný formát user ID'),
    role: z.string().max(50).optional().or(z.literal('')),
  })),
  projectController.assignUserToProject
);

router.patch(
  '/:projectId/users/:userId',
  validateParams(z.object({
    projectId: z.string().uuid(),
    userId: z.string().uuid()
  })),
  validateBody(z.object({
    role: z.string().max(50).optional().or(z.literal('').or(z.null())),
  })),
  projectController.updateUserRoleOnProject
);

router.delete(
  '/:projectId/users/:userId',
  validateParams(z.object({
    projectId: z.string().uuid(),
    userId: z.string().uuid()
  })),
  projectController.removeUserFromProject
);

export default router;
