import express from 'express';
const router = express.Router();
import {
    submitApplication, getApplications, getApplicationById, updateApplicationStatus,
    uploadDocuments, getApplicationDocuments, bulkShortlist,
    assignArchitectureTeam, updateArchitectureStatus, generateDealerCodes,
    retriggerEvaluators, getDocumentConfigs, getDocumentConfigMetadata,
    createDocumentConfig, updateDocumentConfig, deleteDocumentConfig, updateApplication,
    exportApplicationResponses, convertToOpportunity, bulkConvertToOpportunity, sendBulkReminders,
    sendBulkDocumentReminders, sendBulkLoiAckReminders, rejectOnboardingDocument,
    requestProspectDocuments
} from './onboarding.controller.js';
import { authenticate } from '../../common/middleware/auth.js';
import { checkRevocation } from '../../common/middleware/checkRevocation.js';

import { uploadSingle } from '../../common/middleware/upload.js';

// All routes require authentication (or public for submission? Keeping auth for now)
// Public route for application submission
router.post('/apply', submitApplication);

// All subsequent routes require authentication
router.use(authenticate as any);

router.post('/document-configs', createDocumentConfig);
router.put('/document-configs/:id', updateDocumentConfig);
router.delete('/document-configs/:id', deleteDocumentConfig);

router.get('/applications', getApplications);
router.get('/applications/export-responses', exportApplicationResponses);
router.get('/document-configs/metadata', getDocumentConfigMetadata);
router.get('/document-configs', getDocumentConfigs);
router.post('/applications/shortlist', bulkShortlist); // Existing route, updated to named import
router.post('/applications/reminders', sendBulkReminders);
router.post('/applications/document-reminders', sendBulkDocumentReminders);
router.post('/applications/loi-ack-reminders', sendBulkLoiAckReminders);
router.get('/applications/:id', checkRevocation as any, getApplicationById);
router.put('/applications/:id', checkRevocation as any, updateApplication);
router.put('/applications/:id/status', checkRevocation as any, updateApplicationStatus);
router.post('/applications/:id/documents', uploadSingle, checkRevocation as any, uploadDocuments);
router.post('/applications/:id/documents/:documentId/reject', checkRevocation as any, rejectOnboardingDocument);
router.post('/applications/:id/request-documents', checkRevocation as any, requestProspectDocuments);
router.get('/applications/:id/documents', checkRevocation as any, getApplicationDocuments);
router.post('/applications/bulk-convert-to-opportunity', bulkConvertToOpportunity);
router.post('/applications/:id/convert-to-opportunity', checkRevocation as any, convertToOpportunity);

// Architecture-related routes
router.post('/applications/:id/assign-architecture', checkRevocation as any, assignArchitectureTeam);
router.put('/applications/:id/architecture-status', checkRevocation as any, updateArchitectureStatus);
router.post('/applications/:id/generate-codes', checkRevocation as any, generateDealerCodes);
router.post('/applications/:id/retrigger-evaluators', checkRevocation as any, retriggerEvaluators);


// Questionnaire Routes
router.get('/questionnaires', (req, res, next) => {
    import('./questionnaire.controller.js').then(c => c.getAllQuestionnaires(req, res)).catch(next);
});
router.get('/questionnaires/:id', (req, res, next) => {
    import('./questionnaire.controller.js').then(c => c.getQuestionnaireById(req, res)).catch(next);
});

export default router;
