import express from 'express';
import { 
  getMyBusiness, 
  createListing, 
  updateMyBusiness, 
  getListingById,
  getPublicListings, 
  getAllListingsAdmin,
  getAdminStats,
  approveListing, 
  rejectListing,
  updateListingStatusAdmin,
  updateListingRemarksAdmin,
  approveListingUpdate,
  rejectListingUpdate,
  setListingUpdateReviewing,
  getListingHistoryAdmin,
  deleteListingAdmin,
  getAuditLogsAdmin,
  exportAuditLogsCSV
} from '../controllers/listingController.js';
import verifyToken from '../middleware/authMiddleware.js';

const router = express.Router();

// 1. Public listings retrieval (returns status = approved)
router.get('/', getPublicListings);

// 2. Retrieve active merchant business info
router.get('/my-business', verifyToken, getMyBusiness);

// 3. Admin / Moderator audit operations
router.get('/admin/all', verifyToken, getAllListingsAdmin);
router.get('/admin/stats', verifyToken, getAdminStats);
router.get('/admin/logs/export', verifyToken, exportAuditLogsCSV);
router.get('/admin/logs', verifyToken, getAuditLogsAdmin);
router.get('/admin/history/:id', verifyToken, getListingHistoryAdmin);
router.put('/admin/approve/:id', verifyToken, approveListing);
router.put('/admin/reject/:id', verifyToken, rejectListing);
router.put('/admin/status/:id', verifyToken, updateListingStatusAdmin);
router.put('/admin/remarks/:id', verifyToken, updateListingRemarksAdmin);
router.put('/admin/approve-update/:id', verifyToken, approveListingUpdate);
router.put('/admin/reject-update/:id', verifyToken, rejectListingUpdate);
router.put('/admin/review-update/:id', verifyToken, setListingUpdateReviewing);
router.delete('/admin/:id', verifyToken, deleteListingAdmin);

// 4. Get single listing by ID (public) — must be after /admin routes
router.get('/:id', getListingById);

// 5. Submit a new listing linked to merchant
router.post('/', verifyToken, createListing);

// 6. Modify business details
router.put('/my-business/:id', verifyToken, updateMyBusiness);

export default router;
