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

const paymentController = require("../../controllers/accounts/paymentController");
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");

/**
 * @swagger
 * tags:
 *   name: Payments
 *   description: Customer payment (receipt) management APIs
 */

/* =====================================================
   CREATE PAYMENT (RECORD PAYMENT)
   Admin / Manager
   POST /payments
===================================================== */
router.post(
  "/create",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payments']
      #swagger.summary = 'Record a customer payment'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['body'] = {
        in: 'body',
        required: true,
        schema: {
          customer: {
            name: "John Doe",
            phone: "9876543210",
            email: "john@gmail.com"
          },
          invoiceId: "65a12f9e2b8c123456789abc",
          amount: 5000,
          paymentDate: "2026-01-10",
          mode: "UPI",
          reference: "UTR123456789",
          notes: "Advance payment received"
        }
      }
    */
    paymentController.createPayment(req, res);
  }
);

/* =====================================================
   GET PAYMENTS BY CUSTOMER
   Admin / Manager
   GET /payments
===================================================== */
router.get(
  "/",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payments']
      #swagger.summary = 'Get payments by customer'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['name'] = {
        in: 'query',
        description: 'Customer name'
      }
      #swagger.parameters['phone'] = {
        in: 'query',
        description: 'Customer phone'
      }
      #swagger.parameters['email'] = {
        in: 'query',
        description: 'Customer email'
      }
    */
    paymentController.getPaymentsByCustomer(req, res);
  }
);

/* =====================================================
   GET SINGLE PAYMENT
   Admin / Manager
   GET /payments/:id
===================================================== */
router.get(
  "/payment/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payments']
      #swagger.summary = 'Get payment by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Payment ID'
      }
    */
    paymentController.getPaymentById(req, res);
  }
);

/* =====================================================
   DELETE PAYMENT (SOFT DELETE)
   Admin / Manager
   DELETE /payments/:id
===================================================== */
router.delete(
  "/delete/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Payments']
      #swagger.summary = 'Delete a payment (soft delete)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Payment ID'
      }
    */
    paymentController.deletePayment(req, res);
  }
);

module.exports = router;
