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);
});
module.exports = router;