import { Router } from 'express';
import { authenticateToken } from '@middlewares/auth.middleware';
import { requireAdmin } from '@middlewares/authorization.middleware';
import { validateBody, validateParams } from '../middlewares/validate.middleware';
import {
  createHolidaySchema,
  updateHolidaySchema,
  holidayParamsSchema,
  calendarParamsSchema,
  configKeyParamsSchema,
  updateConfigSchema,
  assignRoleSchema,
  updateRoleSchema,
  userIdParamsSchema,
  createActivityTypeSchema,
  updateActivityTypeSchema,
  activityTypeParamsSchema,
  updateForm16ConfigSchema,
  updateCpcCdcConfigSchema,
} from '../validators/admin.validator';
import {
  getAllHolidays,
  getHolidayCalendar,
  createHoliday,
  updateHoliday,
  deleteHoliday,
  bulkImportHolidays,
  getAllConfigurations,
  updateConfiguration,
  resetConfiguration,
  getForm16Config,
  putForm16Config,
  getCpcCdcConfig,
  putCpcCdcConfig,
  updateUserRole,
  getUsersByRole,
  getRoleStatistics,
  assignRoleByEmail,
  getAllActivityTypes,
  getActivityTypeById,
  createActivityType,
  updateActivityType,
  deleteActivityType
} from '@controllers/admin.controller';

const router = Router();

// All admin routes require authentication and admin role
router.use(authenticateToken);
router.use(requireAdmin);

// ==================== Holiday Management Routes ====================

/**
 * @route   GET /api/admin/holidays
 * @desc    Get all holidays (optional year filter)
 * @query   year (optional)
 * @access  Admin
 */
router.get('/holidays', getAllHolidays);

/**
 * @route   GET /api/admin/holidays/calendar/:year
 * @desc    Get holiday calendar for a specific year
 * @params  year
 * @access  Admin
 */
router.get('/holidays/calendar/:year', validateParams(calendarParamsSchema), getHolidayCalendar);

/**
 * @route   POST /api/admin/holidays
 * @desc    Create a new holiday
 * @body    { holidayDate, holidayName, description, holidayType, isRecurring, ... }
 * @access  Admin
 */
router.post('/holidays', validateBody(createHolidaySchema), createHoliday);

/**
 * @route   PUT /api/admin/holidays/:holidayId
 * @desc    Update a holiday
 * @params  holidayId
 * @body    Holiday fields to update
 * @access  Admin
 */
router.put('/holidays/:holidayId', validateParams(holidayParamsSchema), validateBody(updateHolidaySchema), updateHoliday);

/**
 * @route   DELETE /api/admin/holidays/:holidayId
 * @desc    Delete (deactivate) a holiday
 * @params  holidayId
 * @access  Admin
 */
router.delete('/holidays/:holidayId', validateParams(holidayParamsSchema), deleteHoliday);

/**
 * @route   POST /api/admin/holidays/bulk-import
 * @desc    Bulk import holidays from CSV/JSON
 * @body    { holidays: [...] }
 * @access  Admin
 */
router.post('/holidays/bulk-import', bulkImportHolidays);

// ==================== Configuration Management Routes ====================

/**
 * @route   GET /api/admin/configurations
 * @desc    Get all admin configurations (optional category filter)
 * @query   category (optional)
 * @access  Admin
 */
router.get('/configurations', getAllConfigurations);

/**
 * @route   PUT /api/admin/configurations/:configKey
 * @desc    Update a configuration value
 * @params  configKey
 * @body    { configValue }
 * @access  Admin
 */
router.put('/configurations/:configKey', validateParams(configKeyParamsSchema), validateBody(updateConfigSchema), updateConfiguration);

/**
 * @route   POST /api/admin/configurations/:configKey/reset
 * @desc    Reset configuration to default value
 * @params  configKey
 * @access  Admin
 */
router.post('/configurations/:configKey/reset', validateParams(configKeyParamsSchema), resetConfiguration);

/**
 * @route   GET /api/admin/form16-config
 * @desc    Get Form 16 admin config (submission/26AS viewers, reminders)
 * @access  Admin
 */
router.get('/form16-config', getForm16Config);

/**
 * @route   PUT /api/admin/form16-config
 * @desc    Update Form 16 admin config
 * @body    { submissionViewerEmails?, twentySixAsViewerEmails?, reminderEnabled?, reminderDays? }
 * @access  Admin
 */
router.put('/form16-config', validateBody(updateForm16ConfigSchema), putForm16Config);

/**
 * @route   GET /api/admin/cpc-csd-config
 * @desc    Get CPC-CSD admin config (viewer emails)
 * @access  Admin
 */
router.get('/cpc-csd-config', getCpcCdcConfig);

/**
 * @route   PUT /api/admin/cpc-csd-config
 * @desc    Update CPC-CSD admin config
 * @body    { viewerEmails? }
 * @access  Admin
 */
router.put('/cpc-csd-config', validateBody(updateCpcCdcConfigSchema), putCpcCdcConfig);

// ==================== User Role Management Routes (RBAC) ====================

/**
 * @route   POST /api/admin/users/assign-role
 * @desc    Assign role to user by email (creates user from Okta if doesn't exist)
 * @body    { email: string, role: 'USER' | 'MANAGEMENT' | 'ADMIN' }
 * @access  Admin
 */
router.post('/users/assign-role', validateBody(assignRoleSchema), assignRoleByEmail);

/**
 * @route   PUT /api/admin/users/:userId/role
 * @desc    Update user's role (USER, MANAGEMENT, ADMIN)
 * @params  userId
 * @body    { role: 'USER' | 'MANAGEMENT' | 'ADMIN' }
 * @access  Admin
 */
router.put('/users/:userId/role', validateParams(userIdParamsSchema), validateBody(updateRoleSchema), updateUserRole);

/**
 * @route   GET /api/admin/users/by-role
 * @desc    Get all users filtered by role
 * @query   role (optional): ADMIN | MANAGEMENT | USER
 * @access  Admin
 */
router.get('/users/by-role', getUsersByRole);

/**
 * @route   GET /api/admin/users/role-statistics
 * @desc    Get count of users in each role
 * @access  Admin
 */
router.get('/users/role-statistics', getRoleStatistics);

// ==================== Activity Type Management Routes ====================

/**
 * @route   GET /api/admin/activity-types
 * @desc    Get all activity types (optional activeOnly filter)
 * @query   activeOnly (optional): true | false
 * @access  Admin
 */
router.get('/activity-types', getAllActivityTypes);

/**
 * @route   GET /api/admin/activity-types/:activityTypeId
 * @desc    Get a single activity type by ID
 * @params  activityTypeId
 * @access  Admin
 */
router.get('/activity-types/:activityTypeId', validateParams(activityTypeParamsSchema), getActivityTypeById);

/**
 * @route   POST /api/admin/activity-types
 * @desc    Create a new activity type
 * @body    { title, itemCode?, taxationType?, sapRefNo? }
 * @access  Admin
 */
router.post('/activity-types', validateBody(createActivityTypeSchema), createActivityType);

/**
 * @route   PUT /api/admin/activity-types/:activityTypeId
 * @desc    Update an activity type
 * @params  activityTypeId
 * @body    Activity type fields to update
 * @access  Admin
 */
router.put('/activity-types/:activityTypeId', validateParams(activityTypeParamsSchema), validateBody(updateActivityTypeSchema), updateActivityType);

/**
 * @route   DELETE /api/admin/activity-types/:activityTypeId
 * @desc    Delete (deactivate) an activity type
 * @params  activityTypeId
 * @access  Admin
 */
router.delete('/activity-types/:activityTypeId', validateParams(activityTypeParamsSchema), deleteActivityType);

export default router;
