import { Router } from 'express';
import multer from 'multer';
import { cpcCdcController } from '../controllers/CpcCdcController';
import { CpcReportController } from '../controllers/CpcReportController';
import { asyncHandler } from '../middlewares/errorHandler.middleware';
import { authenticateToken } from '../middlewares/auth.middleware';
import { requireCpcCdcAccess } from '../middlewares/cpcPermission.middleware';

const router = Router();
const cpcReportController = new CpcReportController();


const disallowZipUpload: multer.Options['fileFilter'] = (_req, file, cb) => {
    const lowerName = String(file.originalname || '').toLowerCase();
    const lowerMime = String(file.mimetype || '').toLowerCase();
    const isZip = lowerName.endsWith('.zip') || lowerMime.includes('zip');
    if (isZip) {
        cb(new Error('ZIP files are not allowed for CPC-CSD validation'));
        return;
    }
    cb(null, true);
};

// Configure Multer for memory storage (buffers needed for GCS/Gemini)
const upload = multer({
    storage: multer.memoryStorage(),
    limits: { fileSize: 15 * 1024 * 1024 }, // 15MB limit
    fileFilter: disallowZipUpload
});

// All CPC-CSD routes require authentication (mounted at /cpc-csd and legacy /cpc-cdc)
router.use(authenticateToken);
router.use(requireCpcCdcAccess);

// OCR / Validation — mirror CPC-CSD: bulk uses `files[]`, single upload uses `file`
router.post('/v1/ocr/upload', upload.array('files', 20), asyncHandler(cpcCdcController.validateDocumentUpload.bind(cpcCdcController)));
router.post('/v1/ocr/validate-upload', upload.single('file'), asyncHandler(cpcCdcController.validateDocumentUpload.bind(cpcCdcController)));
router.post('/v1/ocr/validate', asyncHandler(cpcCdcController.validateDocumentByUrlStub.bind(cpcCdcController)));


// History and Documents (order aligned with CPC-CSD/server/src/routes/index.js)
router.get('/documents/analytics', asyncHandler(cpcCdcController.getAnalytics.bind(cpcCdcController)));
router.get('/documents/history', asyncHandler(cpcCdcController.getClaimHistory.bind(cpcCdcController)));
router.get('/documents/recent', asyncHandler(cpcCdcController.getRecentDocuments.bind(cpcCdcController)));
router.get('/documents/:id/file', asyncHandler(cpcCdcController.getDocumentFile.bind(cpcCdcController)));
router.get('/documents/:id', asyncHandler(cpcCdcController.getDocumentById.bind(cpcCdcController)));
router.put('/documents/:id/status', asyncHandler(cpcCdcController.updateDocumentStatus.bind(cpcCdcController)));
router.delete('/documents/:id', asyncHandler(cpcCdcController.deleteDocument.bind(cpcCdcController)));

// Reports (Matching History.jsx exactly)
router.get('/v1/ocr/report/all/download', asyncHandler(cpcReportController.downloadAllReport.bind(cpcReportController)));
router.get('/v1/ocr/report/:claimId/download', asyncHandler(cpcReportController.downloadReport.bind(cpcReportController)));

// Backwards compatibility or alternative paths
router.get('/report/all/download', asyncHandler(cpcReportController.downloadAllReport.bind(cpcReportController)));
router.get('/report/:claimId/download', asyncHandler(cpcReportController.downloadReport.bind(cpcReportController)));

export default router;


