import { Router } from 'express';
import {
    // Districts
    getDistricts,
    getAreas,
    createDistrict,
    updateLocation,
    deleteLocation,
    // Regions
    getRegions,
    createRegion,
    updateRegion,
    // Zones
    getZones,
    createZone,
    updateZone,
    // States
    getStates,
    createState,
    // Managers
    getManagersByRole,
    getAreaManagers,
    getASMs,
    getZonalManagers,
    saveZM,
    getDDLeads,
    saveDDLead,
    getSystemConfigs,
    saveSystemConfig,
    getDealerAsmMappings,
    saveDealerAsmMapping,
    getSlaConfigs,
    saveSlaConfig,
    initializeDefaultSlas
} from './master.controller.js';

import {
    getInterviewConfigByType,
    getInterviewConfigs,
    getInterviewConfigById,
    createInterviewConfig,
    updateInterviewConfig,
    deleteInterviewConfig,
    initializeDefaultInterviewConfigs
} from './interviewConfig.controller.js';

import { authenticate, optionalAuth } from '../../common/middleware/auth.js';

const router = Router();

// Read endpoints stay open for the public application form (states / districts /
// areas) and for un-authenticated bootstrap calls; `optionalAuth` still attaches
// `req.user` when a token IS present, so the system-audit helper can snapshot
// the actor for any reads we later decide to log.
router.use(optionalAuth as any);

// All mutating endpoints (POST/PUT/DELETE) below require a logged-in user.
// This is what populates `req.user` so the system-audit log records userId,
// actorName, and actorRole. Without this, IP/UA are captured but the actor
// columns stay null.
const requireAuth = authenticate as any;

// --- Districts ---
router.get('/districts', getDistricts);
router.post('/districts', requireAuth, createDistrict);
router.put('/districts/:id', requireAuth, updateLocation);
router.delete('/districts/:id', requireAuth, deleteLocation);

// --- Areas ---
router.get('/areas', getAreas);
router.post('/areas', requireAuth, createDistrict);
router.put('/areas/:id', requireAuth, updateLocation);
router.delete('/areas/:id', requireAuth, deleteLocation);

// --- Regions ---
router.get('/regions', getRegions);
router.post('/regions', requireAuth, createRegion);
router.put('/regions/:id', requireAuth, updateRegion);

// --- Zones ---
router.get('/zones', getZones);
router.post('/zones', requireAuth, createZone);
router.put('/zones/:id', requireAuth, updateZone);

// --- States ---
router.get('/states', getStates);
router.post('/states', requireAuth, createState);

// --- Managers ---
router.get('/managers', getManagersByRole);
router.get('/area-managers', getAreaManagers);
router.get('/asms', getASMs);
router.get('/zonal-managers', getZonalManagers);
router.post('/zonal-managers', requireAuth, saveZM);
router.get('/dd-leads', getDDLeads);
router.post('/dd-leads', requireAuth, saveDDLead);
router.get('/system-configs', getSystemConfigs);
router.post('/system-configs', requireAuth, saveSystemConfig);
router.get('/dealer-asm-mappings', getDealerAsmMappings);
router.post('/dealer-asm-mappings', requireAuth, saveDealerAsmMapping);

// --- SLA Configuration ---
router.get('/sla-configs', getSlaConfigs);
router.post('/sla-configs', requireAuth, saveSlaConfig);
router.post('/sla-configs/initialize', requireAuth, initializeDefaultSlas);

// --- Interview Configurations (KT Matrix, Level 2, Level 3 Feedback) ---
router.get('/interview-configs', getInterviewConfigs);
router.get('/interview-configs/active/:configType', getInterviewConfigByType);
router.get('/interview-configs/:id', getInterviewConfigById);
router.post('/interview-configs', requireAuth, createInterviewConfig);
router.put('/interview-configs/:id', requireAuth, updateInterviewConfig);
router.delete('/interview-configs/:id', requireAuth, deleteInterviewConfig);
router.post('/interview-configs/initialize', requireAuth, initializeDefaultInterviewConfigs);

export default router;
