import express from 'express';
const router = express.Router();
import * as adminController from './admin.controller.js';
import { authenticate } from '../../common/middleware/auth.js';
import { EmailTemplateController } from '../../controllers/admin/EmailTemplateController.js';
import { checkRole } from '../../common/middleware/roleCheck.js';
import { ROLES } from '../../common/config/constants.js';

// All admin routes require authentication and typically Admin roles
// For now, allowing all for dev, but should be: checkRole([ROLES.SUPER_ADMIN])
router.use(authenticate as any);

// Roles
router.get('/roles', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.getRoles);
router.post('/roles', checkRole([ROLES.SUPER_ADMIN]) as any, adminController.createRole);
router.put('/roles/:id', checkRole([ROLES.SUPER_ADMIN]) as any, adminController.updateRole);

// Permissions
router.get('/permissions', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.getPermissions);

// Users (Admin View)
router.post('/users', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.createUser);
router.get('/users', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.getAllUsers);
router.patch('/users/:id/status', checkRole([ROLES.SUPER_ADMIN]) as any, adminController.updateUserStatus);
router.put('/users/:id', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.updateUser);

// Email Templates
router.get('/email-templates', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, EmailTemplateController.getAllTemplates);
router.get('/email-templates/:id', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, EmailTemplateController.getTemplate);
router.post('/email-templates', checkRole([ROLES.SUPER_ADMIN]) as any, EmailTemplateController.createTemplate);
router.put('/email-templates/:id', checkRole([ROLES.SUPER_ADMIN]) as any, EmailTemplateController.updateTemplate);
router.delete('/email-templates/:id', checkRole([ROLES.SUPER_ADMIN]) as any, EmailTemplateController.deleteTemplate);
router.post('/email-templates/preview', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, EmailTemplateController.previewTemplate);

// Dealer Codes
router.post('/dealer-codes/generate', checkRole([ROLES.SUPER_ADMIN, ROLES.DD_ADMIN]) as any, adminController.generateDealerCode);

export default router;
