const express = require('express');
const router = express.Router();

const invoiceController = require('../controllers/invoiceController');
const { authMiddleware } = require('../middlewares/authMiddleware');

// ====== WHMCS HOOK (tanpa auth - sesuai permintaan) ======
router.post('/', invoiceController.create);

// ====== MANUAL CREATE (auth required) ======
router.post('/manual/preview', authMiddleware, invoiceController.previewInvoice);
router.post('/manual/create', authMiddleware, invoiceController.createManualInvoice);

// ====== GET INVOICES (auth required) ======
router.get('/get/unsended', authMiddleware, invoiceController.getUnsendedInvoices);
router.get('/get', authMiddleware, invoiceController.getInvoices);
router.get('/skipped', authMiddleware, invoiceController.getSkippedInvoices);

// ====== SKIP ACTIONS (auth required) ======
router.post('/skip/batch', authMiddleware, invoiceController.skipInvoicesBatch);
router.post('/skip/:inv_id', authMiddleware, invoiceController.skipInvoice);

// ====== REQUEUE ACTIONS (auth required) ======
router.post('/requeue/batch', authMiddleware, invoiceController.requeueInvoicesBatch);
router.post('/requeue/:inv_id', authMiddleware, invoiceController.requeueInvoice);

// ====== DEBUG (auth required + only non-production) ======
router.get('/debug/html/:inv_id', authMiddleware, (req, res, next) => {
    if (process.env.NODE_ENV === 'production') {
        return res.status(403).json({ status: false, message: 'Endpoint debug tidak tersedia di production.' });
    }
    next();
}, invoiceController.debugInvoiceHtml);

// ====== PDF FILE MANAGER (auth required) ======
router.get('/files', authMiddleware, invoiceController.getPDFFiles);
router.get('/files/:filename(*)', authMiddleware, invoiceController.downloadPDFFile);
router.put('/files/:filename(*)', authMiddleware, invoiceController.renamePDFFile);
router.delete('/files/:filename(*)', authMiddleware, invoiceController.deletePDFFile);

module.exports = router;