const express = require("express");
const router = express.Router();
const approvalController = require("../../controllers/procurement/approvalController");
const authenticate = require("../../middlewares/authenticate");

// ==================== GET ====================

// Get all module types
router.get("/module-types", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get all module types'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.getModuleTypes(req, res);
});

// Get all modules
router.get("/modules", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get all module types'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.getModuleTypes(req, res);
});

// Get all users
router.get("/users", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get all users for approval assignment'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.getUsers(req, res);
});

// Get workflow by module type
router.get("/workflow/:moduleTypeId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get approval workflow by module type ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['moduleTypeId'] = {
      in: 'path',
      description: 'Module Type ID',
      required: true,
      type: 'integer'
    }
  */
  approvalController.getWorkflowByModule(req, res);
});

// Get workflow entry by ID
router.get("/workflow-entry/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get single workflow entry by ID'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.getWorkflowById(req, res);
});

// Get my approval tasks (for logged-in user)
router.get("/my-tasks", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Get approval tasks for logged-in user'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.getMyApprovalTasks(req, res);
});

// Check if user is approver for a module
router.get("/check-approver/:moduleTypeId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Check if current user is an approver for a module'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.checkIsApprover(req, res);
});

// ==================== PO APPROVAL ROUTES ====================
// IMPORTANT: These must come BEFORE any wildcard routes like /:id

// Get PO approvals for a specific PO
router.get("/po/:poId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['PO Approvals']
    #swagger.summary = 'Get all approval records for a specific PO'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['poId'] = {
      in: 'path',
      description: 'Purchase Order ID',
      required: true,
      type: 'integer'
    }
  */
  approvalController.getPOApprovals(req, res);
});

// Approve a PO
router.patch("/po/:poId/approve", authenticate, (req, res) => {
  /*
    #swagger.tags = ['PO Approvals']
    #swagger.summary = 'Approve a purchase order'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['poId'] = {
      in: 'path',
      description: 'Purchase Order ID',
      required: true,
      type: 'integer'
    }
    #swagger.parameters['body'] = {
      in: 'body',
      required: false,
      schema: {
        comments: "Optional approval comments"
      }
    }
  */
  approvalController.approvePO(req, res);
});

// Reject a PO
router.patch("/po/:poId/reject", authenticate, (req, res) => {
  /*
    #swagger.tags = ['PO Approvals']
    #swagger.summary = 'Reject a purchase order'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['poId'] = {
      in: 'path',
      description: 'Purchase Order ID',
      required: true,
      type: 'integer'
    }
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        comments: "Rejection reason (required)"
      }
    }
  */
  approvalController.rejectPO(req, res);
});

// ==================== CREATE/UPDATE ====================

// Save/Update workflow
router.post("/workflow", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Save or update approval workflow'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        moduleTypeId: 1,
        userIds: [1, 2, 3]
      }
    }
  */
  approvalController.saveWorkflow(req, res);
});

// Update workflow order
router.put("/workflow-order/:moduleTypeId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Update workflow approval order'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      required: true,
      schema: {
        workflowOrders: [
          { id: 1, approval_order: 1 },
          { id: 2, approval_order: 2 }
        ]
      }
    }
  */
  approvalController.updateWorkflowOrder(req, res);
});

// ==================== DELETE ====================

// Delete entire workflow for a module
router.delete("/workflow/:moduleTypeId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Approval Workflow']
    #swagger.summary = 'Delete entire workflow for a module type'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  approvalController.deleteWorkflow(req, res);
});

// Add these to your approvalRoutes.js

// PO Approval Progress routes
router.get("/po-progress/:poId", authenticate, (req, res) => {
  approvalController.getPOApprovalProgress(req, res);
});

// Admin: Backfill approval progress for existing POs
router.post("/backfill-progress", authenticate, (req, res) => {
  approvalController.backfillApprovalProgress(req, res);
});

// Admin: Create approval progress table (run once)
router.post("/create-progress-table", authenticate, (req, res) => {
  approvalController.createApprovalProgressTable(req, res);
});

// GRN Approval routes
router.get("/grn/:grnId", authenticate, (req, res) => {
  approvalController.getGRNApprovals(req, res);
});

router.patch("/grn/:grnId/approve", authenticate, (req, res) => {
  approvalController.approveGRN(req, res);
});

router.patch("/grn/:grnId/reject", authenticate, (req, res) => {
  approvalController.rejectGRN(req, res);
});
router.patch("/grn/:grnId/approve-invoice", authenticate, (req, res) => {
  approvalController.approvePurchaseInvoice(req, res);
});



// ==================== SALES INVOICE APPROVAL ROUTES ====================

// Get approval workflow and status
router.get("/sales-invoice/:invoiceId", authenticate, (req, res) => {
  approvalController.getSalesInvoiceApprovals(req, res);
});

// Approve sales invoice (step-by-step)
router.post("/sales-invoice-approvals/:invoiceId/approve", authenticate, (req, res) => {
  approvalController.approveSalesInvoice(req, res);
});

// Reject sales invoice
router.post("/sales-invoice-approvals/:invoiceId/reject", authenticate, (req, res) => {
  approvalController.rejectSalesInvoice(req, res);
});

// Super approve sales invoice (admin/CEO bypass)
router.post("/sales-invoice-approvals/:invoiceId/super-approve", authenticate, (req, res) => {
  approvalController.superApproveSalesInvoice(req, res);



});



// Sales Invoice Approval Progress
router.get("/sales-invoice-progress/:invoiceId", authenticate, (req, res) => {
  approvalController.getSalesInvoiceApprovalProgress(req, res);
});

// Backfill Sales Invoice Approval Progress (admin only)
router.post("/backfill-sales-invoice-progress", authenticate, (req, res) => {
  approvalController.backfillSalesInvoiceApprovalProgress(req, res);
});

// Create Sales Invoice Approval Progress Table (admin only - run once)
router.post("/create-sales-invoice-progress-table", authenticate, (req, res) => {
  approvalController.createSalesInvoiceApprovalProgressTable(req, res);
});


// Add these routes to your existing approvalRoutes.js

// Sales Order Approval Routes
router.get("/sales-order/:orderId/approvals", authenticate, (req, res) => {
  approvalController.getSalesOrderApprovals(req, res);
});

router.post("/sales-order/:orderId/approve", authenticate, (req, res) => {
  approvalController.approveSalesOrderStep(req, res);
});

router.post("/sales-order/:orderId/reject", authenticate, (req, res) => {
  approvalController.rejectSalesOrder(req, res);
});

router.post("/sales-order/:orderId/super-approve", authenticate, (req, res) => {
  approvalController.superApproveSalesOrder(req, res);
});

router.get("/sales-order/:orderId/progress", authenticate, (req, res) => {
  approvalController.getSalesOrderApprovalProgress(req, res);
});

router.post("/sales-order/backfill-progress", authenticate, (req, res) => {
  approvalController.backfillSalesOrderApprovalProgress(req, res);
});

router.post("/create-sales-order-progress-table", authenticate, (req, res) => {
  approvalController.createSalesOrderApprovalProgressTable(req, res);
});




// Add these routes to your existing approvalRoutes.js

module.exports = router;