import express from 'express';
import * as serviceController from '../controllers/service.controller.js';
import * as serviceFolderController from '../controllers/service-folder.controller.js';
import * as serviceDocumentController from '../controllers/service-document.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireAdmin, requireTeamLeaderOrAdmin } from '../middlewares/auth/roleMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { createServiceSchema, updateServiceSchema } from '../validators/crm.validators.js';
import { createUpload } from '../config/upload.js';
import {
  serviceIdSchema,
  folderIdSchema,
  folderDocumentIdSchema,
  createFolderSchema,
  updateFolderSchema,
} from '../validators/service.validators.js';

const router = express.Router();

const upload = createUpload({ maxSizeMB: 50 });

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

// ==================== SERVICE FOLDERS (must be before :serviceId routes) ====================

/**
 * GET /api/services/folders - Get all folders (all authenticated users)
 */
router.get('/folders', serviceFolderController.getAllFolders);

/**
 * POST /api/services/folders - Create new folder (admin/team_leader)
 */
router.post(
  '/folders',
  requireTeamLeaderOrAdmin,
  validateBody(createFolderSchema),
  serviceFolderController.createFolder
);

/**
 * GET /api/services/folders/:folderId - Get folder by ID (all authenticated users)
 */
router.get(
  '/folders/:folderId',
  validateParams(folderIdSchema),
  serviceFolderController.getFolderById
);

/**
 * PUT /api/services/folders/:folderId - Update folder (admin/team_leader)
 */
router.put(
  '/folders/:folderId',
  requireTeamLeaderOrAdmin,
  validateParams(folderIdSchema),
  validateBody(updateFolderSchema),
  serviceFolderController.updateFolder
);

/**
 * DELETE /api/services/folders/:folderId - Delete folder (admin/team_leader)
 */
router.delete(
  '/folders/:folderId',
  requireTeamLeaderOrAdmin,
  validateParams(folderIdSchema),
  serviceFolderController.deleteFolder
);

// ==================== SERVICE DOCUMENTS ====================

/**
 * GET /api/services/folders/:folderId/documents - Get all documents in folder
 */
router.get(
  '/folders/:folderId/documents',
  validateParams(folderIdSchema),
  serviceDocumentController.getDocumentsByFolderId
);

/**
 * POST /api/services/folders/:folderId/documents - Upload document to folder
 */
router.post(
  '/folders/:folderId/documents',
  validateParams(folderIdSchema),
  upload.single('file'),
  serviceDocumentController.uploadDocument
);

/**
 * GET /api/services/folders/:folderId/documents/:documentId/download - Download document
 */
router.get(
  '/folders/:folderId/documents/:documentId/download',
  validateParams(folderDocumentIdSchema),
  serviceDocumentController.downloadDocument
);

/**
 * DELETE /api/services/folders/:folderId/documents/:documentId - Delete document (admin/team_leader)
 */
router.delete(
  '/folders/:folderId/documents/:documentId',
  requireTeamLeaderOrAdmin,
  validateParams(folderDocumentIdSchema),
  serviceDocumentController.deleteDocument
);

// ==================== SERVICES ====================

/**
 * GET /api/services - Get all services (all authenticated users)
 */
router.get('/', serviceController.getAllServices);

/**
 * POST /api/services - Create new service (admin/team_leader)
 */
router.post(
  '/',
  requireTeamLeaderOrAdmin,
  validateBody(createServiceSchema),
  serviceController.createService
);

/**
 * GET /api/services/:serviceId - Get service by ID (all authenticated users)
 */
router.get(
  '/:serviceId',
  validateParams(serviceIdSchema),
  serviceController.getServiceById
);

/**
 * PUT /api/services/:serviceId - Update service (admin/team_leader)
 */
router.put(
  '/:serviceId',
  requireTeamLeaderOrAdmin,
  validateParams(serviceIdSchema),
  validateBody(updateServiceSchema),
  serviceController.updateService
);

/**
 * DELETE /api/services/:serviceId - Delete service (admin/team_leader)
 */
router.delete(
  '/:serviceId',
  requireTeamLeaderOrAdmin,
  validateParams(serviceIdSchema),
  serviceController.deleteService
);

export default router;
