import { Router } from "express";
import multer from "multer";

import { mgxAdminController } from "../controllers/mgxAdminController.js";
import { mgxAdminOpsController } from "../controllers/mgxAdminOpsController.js";
import { mgxAgentController } from "../controllers/mgxAgentController.js";
import { mgxAlertsController } from "../controllers/mgxAlertsController.js";
import { mgxConsumablesController } from "../controllers/mgxConsumablesController.js";
import { mgxDashboardController } from "../controllers/mgxDashboardController.js";
import { mgxDriversController } from "../controllers/mgxDriversController.js";
import { mgxFuelController } from "../controllers/mgxFuelController.js";
import { mgxItController } from "../controllers/mgxItController.js";
import { mgxMaintenanceController } from "../controllers/mgxMaintenanceController.js";
import { mgxProcurementController } from "../controllers/mgxProcurementController.js";
import { mgxReportsController } from "../controllers/mgxReportsController.js";
import { mgxSuppliersTreasuryController } from "../controllers/mgxSuppliersTreasuryController.js";
import { mgxUploadsController } from "../controllers/mgxUploadsController.js";
import { mgxVehiclesController } from "../controllers/mgxVehiclesController.js";
import { mgxMobileAccountsController, mgxStaffProfilesController } from "../controllers/mgxStaffMobileController.js";
import { asyncHandler } from "../utils/asyncHandler.js";

const router = Router();
const upload = multer({
  storage: multer.memoryStorage(),
  limits: { fileSize: 8 * 1024 * 1024 },
});

router.get("/dashboard/kpis", asyncHandler(mgxDashboardController.getKpis));
router.get(
  "/dashboard/depot-distribution",
  asyncHandler(mgxDashboardController.getDepotDistribution),
);
router.get(
  "/dashboard/vehicle-statuses",
  asyncHandler(mgxDashboardController.getVehicleStatuses),
);
router.get(
  "/dashboard/alerts-summary",
  asyncHandler(mgxDashboardController.getAlertsSummary),
);
router.get(
  "/dashboard/monthly-expenses",
  asyncHandler(mgxDashboardController.getMonthlyExpenses),
);
router.get(
  "/dashboard/upcoming-deadlines",
  asyncHandler(mgxDashboardController.getUpcomingDeadlines),
);
router.get(
  "/dashboard/fuel-by-vehicle",
  asyncHandler(mgxDashboardController.getFuelByVehicle),
);
router.get(
  "/dashboard/recent-activity",
  asyncHandler(mgxDashboardController.getRecentActivity),
);

router.get("/reports/vehicles", asyncHandler(mgxReportsController.getVehiclesReport));
router.get("/reports/fuel-history", asyncHandler(mgxReportsController.getFuelHistoryReport));
router.get("/reports/maintenance-history", asyncHandler(mgxReportsController.getMaintenanceHistoryReport));
router.get("/reports/documents-expiry", asyncHandler(mgxReportsController.getDocumentsExpiryReport));
router.get("/reports/purchases", asyncHandler(mgxReportsController.getPurchasesReport));
router.get("/reports/cost-by-vehicle", asyncHandler(mgxReportsController.getCostByVehicleReport));
router.get("/reports/cost-by-depot", asyncHandler(mgxReportsController.getCostByDepotReport));
router.get("/reports/consumables-by-depot", asyncHandler(mgxReportsController.getConsumablesByDepotReport));
router.get("/reports/expense-by-category", asyncHandler(mgxReportsController.getExpenseByCategoryReport));
router.get("/reports/supplier-balance", asyncHandler(mgxReportsController.getSupplierBalanceReport));
router.get("/reports/direction-summary", asyncHandler(mgxReportsController.getDirectionSummaryReport));
router.get("/reports/rentals-active", asyncHandler(mgxReportsController.getRentalsActive));
router.get("/reports/treasury-journal", asyncHandler(mgxReportsController.getTreasuryJournal));
router.get("/reports/alerts-analysis", asyncHandler(mgxReportsController.getAlertsAnalysis));

router.post("/import/vehicles", upload.single("file"), asyncHandler(mgxAdminOpsController.importVehicles));
router.post("/import/suppliers", upload.single("file"), asyncHandler(mgxAdminOpsController.importSuppliers));
router.post("/import/drivers", upload.single("file"), asyncHandler(mgxAdminOpsController.importDrivers));
router.post("/import/fuel", upload.single("file"), asyncHandler(mgxAdminOpsController.importFuel));
router.post("/import/purchases", upload.single("file"), asyncHandler(mgxAdminOpsController.importPurchases));
router.post("/import/consumables", upload.single("file"), asyncHandler(mgxAdminOpsController.importConsumables));

router.get("/export/vehicles", asyncHandler(mgxAdminOpsController.exportVehicles));
router.get("/export/fuel", asyncHandler(mgxAdminOpsController.exportFuel));
router.get("/export/suppliers", asyncHandler(mgxAdminOpsController.exportSuppliers));
router.get("/export/maintenance", asyncHandler(mgxAdminOpsController.exportMaintenance));
router.get("/export/treasury", asyncHandler(mgxAdminOpsController.exportTreasury));

router.get("/audit/:id", asyncHandler(mgxAdminOpsController.getAuditDetail));
router.get("/audit", asyncHandler(mgxAdminOpsController.listAudit));
router.get("/logins", asyncHandler(mgxAdminOpsController.listLogins));

router.get("/alerts/summary", asyncHandler(mgxAlertsController.getSummary));
router.get("/alerts/history", asyncHandler(mgxAlertsController.getHistory));
router.post("/alerts/run-engine", asyncHandler(mgxAlertsController.runEngine));
router.get("/alerts", asyncHandler(mgxAlertsController.listAlerts));
router.put("/alerts/:id/treat", asyncHandler(mgxAlertsController.treatAlert));
router.put("/alerts/:id/cancel", asyncHandler(mgxAlertsController.cancelAlert));

router.post("/uploads", upload.single("file"), asyncHandler(mgxUploadsController.uploadFile));

router.get("/agent-reports", asyncHandler(mgxAgentController.listVehicleReports));
router.post("/agent-reports", asyncHandler(mgxAgentController.createVehicleReport));
router.get("/agent-reports/:id", asyncHandler(mgxAgentController.getVehicleReport));
router.post("/agent-reports/:id/comment", asyncHandler(mgxAgentController.addVehicleReportComment));
router.post("/vehicles/confirm-location", asyncHandler(mgxAgentController.confirmVehicleLocation));
router.post("/agent-reports/:id/approve-driver-assignment", asyncHandler(mgxAgentController.approveDriverAssignment));

router.get("/vehicle-agent-reports", asyncHandler(mgxAgentController.listVehicleReports));
router.post("/vehicle-agent-reports", asyncHandler(mgxAgentController.createVehicleReport));
router.put("/vehicle-agent-reports/:id", asyncHandler(mgxAgentController.updateVehicleReport));
router.post("/vehicle-agent-reports/:id/approve-driver-assignment", asyncHandler(mgxAgentController.approveDriverAssignment));
router.post(
  "/vehicle-agent-reports/:id/convert-purchase-request",
  asyncHandler(mgxAgentController.convertToPurchaseRequest),
);
router.post(
  "/vehicle-agent-reports/:id/convert-intervention",
  asyncHandler(mgxAgentController.convertToIntervention),
);

router.get("/vehicles", asyncHandler(mgxVehiclesController.listVehicles));
router.post("/vehicles", asyncHandler(mgxVehiclesController.createVehicle));
router.get("/vehicles/:id", asyncHandler(mgxVehiclesController.getVehicle));
router.put("/vehicles/:id", asyncHandler(mgxVehiclesController.updateVehicle));
router.delete("/vehicles/:id", asyncHandler(mgxVehiclesController.deleteVehicle));
router.post("/vehicles/:id/update-km", asyncHandler(mgxVehiclesController.updateKm));
router.post("/vehicles/:id/driver-assignments", asyncHandler(mgxVehiclesController.assignDriver));
router.post("/vehicles/:id/driver-unassign", asyncHandler(mgxVehiclesController.unassignDriver));
router.post("/vehicles/:id/depot-transfers", asyncHandler(mgxVehiclesController.transferDepot));
router.get("/vehicles/:id/costs", asyncHandler(mgxVehiclesController.getVehicleCosts));
router.get("/vehicles/:id/history", asyncHandler(mgxVehiclesController.getVehicleHistory));

router.get("/drivers", asyncHandler(mgxDriversController.listDrivers));
router.get("/drivers/my-vehicles", asyncHandler(mgxDriversController.listMyVehicles));
router.post("/drivers", asyncHandler(mgxDriversController.saveDriver));
router.get("/drivers/:id", asyncHandler(mgxDriversController.getDriver));
router.put("/drivers/:id", asyncHandler(mgxDriversController.saveDriver));
router.delete("/drivers/:id", asyncHandler(mgxDriversController.deleteDriver));

router.get("/staff-profiles", asyncHandler(mgxStaffProfilesController.list));
router.post("/staff-profiles", asyncHandler(mgxStaffProfilesController.save));
router.get("/staff-profiles/:id", asyncHandler(mgxStaffProfilesController.get));
router.put("/staff-profiles/:id", asyncHandler(mgxStaffProfilesController.save));
router.delete("/staff-profiles/:id", asyncHandler(mgxStaffProfilesController.remove));

router.get("/mobile-accounts", asyncHandler(mgxMobileAccountsController.list));
router.post("/mobile-accounts", asyncHandler(mgxMobileAccountsController.save));
router.get("/mobile-accounts/:id", asyncHandler(mgxMobileAccountsController.get));
router.get("/mobile-accounts/:id/qr-login-payload", asyncHandler(mgxMobileAccountsController.getQrLoginPayload));
router.put("/mobile-accounts/:id", asyncHandler(mgxMobileAccountsController.save));
router.delete("/mobile-accounts/:id", asyncHandler(mgxMobileAccountsController.remove));

router.get("/it-assets", asyncHandler(mgxItController.listAssets));
router.post("/it-assets", asyncHandler(mgxItController.saveAsset));
router.get("/it-assets/:id", asyncHandler(mgxItController.getAsset));
router.put("/it-assets/:id", asyncHandler(mgxItController.saveAsset));
router.delete("/it-assets/:id", asyncHandler(mgxItController.deleteAsset));

router.get("/fuel/reports", asyncHandler(mgxFuelController.getReports));
router.get("/fuel/anomalies", asyncHandler(mgxFuelController.getAnomalies));
router.get("/fuel", asyncHandler(mgxFuelController.listFuel));
router.post("/fuel", asyncHandler(mgxFuelController.createFuelEntry));
router.get("/fuel/:id", asyncHandler(mgxFuelController.getFuelEntry));
router.put("/fuel/:id", asyncHandler(mgxFuelController.updateFuelEntry));
router.delete("/fuel/:id", asyncHandler(mgxFuelController.deleteFuelEntry));

router.get("/suppliers/balance", asyncHandler(mgxSuppliersTreasuryController.getSupplierBalance));
router.get("/suppliers/grand-livre", asyncHandler(mgxSuppliersTreasuryController.getSupplierGrandLivre));
router.get("/suppliers/schedule", asyncHandler(mgxSuppliersTreasuryController.getSupplierSchedule));
router.get("/suppliers/:id/statement", asyncHandler(mgxSuppliersTreasuryController.getSupplierStatement));
router.get("/suppliers", asyncHandler(mgxSuppliersTreasuryController.listSuppliers));
router.post("/suppliers", asyncHandler(mgxSuppliersTreasuryController.saveSupplier));
router.get("/suppliers/:id", asyncHandler(mgxSuppliersTreasuryController.getSupplier));
router.put("/suppliers/:id", asyncHandler(mgxSuppliersTreasuryController.saveSupplier));
router.delete("/suppliers/:id", asyncHandler(mgxSuppliersTreasuryController.deleteSupplier));

router.get("/maintenance-plans/due-soon", asyncHandler(mgxMaintenanceController.dueSoon));
router.get("/maintenance-plans", asyncHandler(mgxMaintenanceController.listPlans));
router.post("/maintenance-plans", asyncHandler(mgxMaintenanceController.savePlan));
router.put("/maintenance-plans/:id", asyncHandler(mgxMaintenanceController.savePlan));
router.delete("/maintenance-plans/:id", asyncHandler(mgxMaintenanceController.deletePlan));

router.get("/interventions", asyncHandler(mgxMaintenanceController.listInterventions));
router.post("/interventions", asyncHandler(mgxMaintenanceController.saveIntervention));
router.get("/interventions/:id", asyncHandler(mgxMaintenanceController.getIntervention));
router.put("/interventions/:id", asyncHandler(mgxMaintenanceController.saveIntervention));
router.put("/interventions/:id/take-charge", asyncHandler(mgxMaintenanceController.takeChargeIntervention));
router.put("/interventions/:id/update-status", asyncHandler(mgxMaintenanceController.updateInterventionStatus));
router.put("/interventions/:id/close", asyncHandler(mgxMaintenanceController.closeIntervention));
router.post("/interventions/:id/close", asyncHandler(mgxMaintenanceController.closeIntervention));

router.get("/maintenance/reports", asyncHandler(mgxMaintenanceController.getReports));

router.get("/consumable-items/low-stock", asyncHandler(mgxConsumablesController.getLowStock));
router.get("/consumable-items", asyncHandler(mgxConsumablesController.listItems));
router.post("/consumable-items", asyncHandler(mgxConsumablesController.saveItem));
router.get("/consumable-items/:id", asyncHandler(mgxConsumablesController.getItem));
router.put("/consumable-items/:id", asyncHandler(mgxConsumablesController.saveItem));
router.delete("/consumable-items/:id", asyncHandler(mgxConsumablesController.deleteItem));

router.get("/consumable-movements", asyncHandler(mgxConsumablesController.listMovements));
router.post("/consumable-movements", asyncHandler(mgxConsumablesController.createMovement));
router.get("/consumable-movements/:id", asyncHandler(mgxConsumablesController.getMovement));

router.get("/consumables/stock-by-depot", asyncHandler(mgxConsumablesController.getStockByDepot));
router.get("/consumables/reports", asyncHandler(mgxConsumablesController.getReports));

router.get("/treasury/reports", asyncHandler(mgxSuppliersTreasuryController.getTreasuryReports));
router.get("/treasury/balance-by-depot", asyncHandler(mgxSuppliersTreasuryController.getTreasuryBalanceByDepot));
router.get("/treasury", asyncHandler(mgxSuppliersTreasuryController.listTreasury));
router.post("/treasury", asyncHandler(mgxSuppliersTreasuryController.saveTreasury));
router.get("/treasury/:id", asyncHandler(mgxSuppliersTreasuryController.getTreasury));
router.put("/treasury/:id", asyncHandler(mgxSuppliersTreasuryController.saveTreasury));
router.delete("/treasury/:id", asyncHandler(mgxSuppliersTreasuryController.deleteTreasury));

router.get("/purchase-requests", asyncHandler(mgxProcurementController.listPurchaseRequests));
router.post("/purchase-requests", asyncHandler(mgxProcurementController.savePurchaseRequest));
router.get("/purchase-requests/history", asyncHandler(mgxProcurementController.listPurchaseRequestHistory));
router.get("/purchase-requests/:id", asyncHandler(mgxProcurementController.getPurchaseRequest));
router.put("/purchase-requests/:id", asyncHandler(mgxProcurementController.savePurchaseRequest));
router.delete("/purchase-requests/:id", asyncHandler(mgxProcurementController.deletePurchaseRequest));
router.put("/purchase-requests/:id/validate", asyncHandler(mgxProcurementController.validatePurchaseRequest));
router.post("/purchase-requests/:id/validate", asyncHandler(mgxProcurementController.validatePurchaseRequest));
router.put("/purchase-requests/:id/reject", asyncHandler(mgxProcurementController.rejectPurchaseRequest));
router.post("/purchase-requests/:id/reject", asyncHandler(mgxProcurementController.rejectPurchaseRequest));

router.get("/purchase-orders", asyncHandler(mgxProcurementController.listPurchaseOrders));
router.post("/purchase-orders", asyncHandler(mgxProcurementController.savePurchaseOrder));
router.get("/purchase-orders/:id", asyncHandler(mgxProcurementController.getPurchaseOrder));
router.put("/purchase-orders/:id", asyncHandler(mgxProcurementController.savePurchaseOrder));
router.delete("/purchase-orders/:id", asyncHandler(mgxProcurementController.deletePurchaseOrder));
router.post("/purchase-orders/:id/receive", asyncHandler(mgxProcurementController.createReception));

router.get("/receptions", asyncHandler(mgxProcurementController.listReceptions));
router.post("/receptions", asyncHandler(mgxProcurementController.createReception));
router.get("/receptions/:id", asyncHandler(mgxProcurementController.getReception));

router.get("/purchase-returns", asyncHandler(mgxProcurementController.listPurchaseReturns));
router.post("/purchase-returns", asyncHandler(mgxProcurementController.createPurchaseReturn));
router.get("/purchase-returns/:id", asyncHandler(mgxProcurementController.getPurchaseReturn));

router.get("/services", asyncHandler(mgxProcurementController.listServices));
router.post("/services", asyncHandler(mgxProcurementController.saveService));
router.get("/services/:id", asyncHandler(mgxProcurementController.getService));
router.put("/services/:id", asyncHandler(mgxProcurementController.saveService));
router.delete("/services/:id", asyncHandler(mgxProcurementController.deleteService));

router.get("/rentals", asyncHandler(mgxProcurementController.listRentals));
router.post("/rentals", asyncHandler(mgxProcurementController.saveRental));
router.get("/rentals/:id", asyncHandler(mgxProcurementController.getRental));
router.put("/rentals/:id", asyncHandler(mgxProcurementController.saveRental));
router.delete("/rentals/:id", asyncHandler(mgxProcurementController.deleteRental));

router.get("/vehicle-documents", asyncHandler(mgxVehiclesController.listDocuments));
router.post("/vehicle-documents", asyncHandler(mgxVehiclesController.createDocument));
router.put("/vehicle-documents/:id", asyncHandler(mgxVehiclesController.updateDocument));
router.delete("/vehicle-documents/:id", asyncHandler(mgxVehiclesController.deleteDocument));

router.get("/settings", asyncHandler(mgxAdminController.getSettings));
router.put("/settings", asyncHandler(mgxAdminController.updateSettings));
router.post("/settings", asyncHandler(mgxAdminController.updateSettings));

router.get("/depots", asyncHandler(mgxAdminController.listDepots));
router.post("/depots", asyncHandler(mgxAdminController.createDepot));
router.put("/depots/:id", asyncHandler(mgxAdminController.updateDepot));
router.delete("/depots/:id", asyncHandler(mgxAdminController.deleteDepot));

router.get("/referentiels/all", asyncHandler(mgxAdminController.listAllReferentiels));
router.get("/referentiels/:id/usage-count", asyncHandler(mgxAdminController.getReferentielUsageCount));
router.post("/referentiels/:category/seed", asyncHandler(mgxAdminController.seedReferentielCategory));
router.get("/referentiels", asyncHandler(mgxAdminController.listReferentiels));
router.post("/referentiels", asyncHandler(mgxAdminController.createReferentiel));
router.put("/referentiels/:id", asyncHandler(mgxAdminController.updateReferentiel));
router.delete("/referentiels/:id", asyncHandler(mgxAdminController.deleteReferentiel));

router.get("/alert-configs", asyncHandler(mgxAdminController.listAlertConfigs));
router.post("/alert-configs", asyncHandler(mgxAdminController.saveAlertConfig));
router.put("/alert-configs/:id", asyncHandler(mgxAdminController.saveAlertConfig));
router.post("/alert-configs/test-run", asyncHandler(mgxAdminController.testAlertConfigs));

export default router;
