


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

const {
  upload,
  createOrganization,
  getAllOrganizations,
  getOrganizationById,
  getOrganizationStats,
  updateOrganization,
  deleteOrganization,
  toggleOrganizationStatus,
  getOrganizationAddresses,
  addOrganizationAddress,
  updateOrganizationAddress,
  deleteOrganizationAddress,
  uploadOrganizationDocument,
  getOrganizationDocuments,
  deleteOrganizationDocument,
  replaceOrganizationDocument,
  getOrganizationBankDetails,
  addOrganizationBankDetail,
  updateOrganizationBankDetail,
  deleteOrganizationBankDetail,
  getOrganizationFullDetails,
  // APIs
  getOrganizationApis,
  addOrganizationApi,
  updateOrganizationApi,
  deleteOrganizationApi,
  testOrganizationApi,
} = require("../../controllers/organizationController/organizationController");

const {
  getSmtpConfigs,
  createSmtpConfig,
  updateSmtpConfig,
  deleteSmtpConfig,
  testSmtpConnection,
  toggleSmtpActive,
  setAsDefault,
} = require("../../controllers/organizationController/smtpController");

const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Organizations
 *   description: Organization management APIs
 */

// ─────────────────────────────────────────────
// MAIN ORGANIZATION CRUD
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL ORGANIZATIONS
=========================== */
router.get("/", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all organizations with pagination, search and status filter'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['page'] = {
      in: 'query',
      description: 'Page number for pagination',
      type: 'integer'
    }
    #swagger.parameters['limit'] = {
      in: 'query',
      description: 'Items per page',
      type: 'integer'
    }
    #swagger.parameters['search'] = {
      in: 'query',
      description: 'Search term',
      type: 'string'
    }
    #swagger.parameters['status'] = {
      in: 'query',
      description: 'Organization status filter',
      type: 'string'
    }
  */
  getAllOrganizations(req, res);
});

/* ===========================
   ✅ CREATE ORGANIZATION
=========================== */
router.post("/", authenticate, upload, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Create a new organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
    #swagger.requestBody = {
      required: true,
      content: {
        "multipart/form-data": {
          schema: {
            type: "object",
            properties: {
              org_name: { type: "string" },
              email: { type: "string" },
              phone: { type: "string" },
              address: { type: "string" },
              gstin: { type: "string" },
              pan: { type: "string" },
              logo: { type: "string", format: "binary" }
            },
            required: ["org_name"]
          }
        }
      }
    }
  */
  createOrganization(req, res);
});

/* ===========================
   ✅ GET ORGANIZATION BY ID
=========================== */
router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get organization by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationById(req, res);
});

/* ===========================
   ✅ GET ORGANIZATION FULL DETAILS
=========================== */
router.get("/:id/full", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get organization full details including addresses, bank details, documents and SMTP configs'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationFullDetails(req, res);
});

/* ===========================
   ✅ UPDATE ORGANIZATION
=========================== */
router.put("/:id", authenticate, upload, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Update organization by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "multipart/form-data": {
          schema: {
            type: "object",
            properties: {
              org_name: { type: "string" },
              email: { type: "string" },
              phone: { type: "string" },
              address: { type: "string" },
              gstin: { type: "string" },
              pan: { type: "string" },
              logo: { type: "string", format: "binary" }
            }
          }
        }
      }
    }
  */
  updateOrganization(req, res);
});

/* ===========================
   ✅ DELETE ORGANIZATION (SOFT DELETE)
=========================== */
router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Soft delete organization by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  deleteOrganization(req, res);
});

/* ===========================
   ✅ GET ORGANIZATION STATS
=========================== */
router.get("/:id/stats", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get organization statistics'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationStats(req, res);
});

/* ===========================
   ✅ TOGGLE ORGANIZATION STATUS
=========================== */
router.patch("/:id/status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Toggle organization active status'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              status: { type: "string" }
            },
            required: ["status"]
          }
        }
      }
    }
  */
  toggleOrganizationStatus(req, res);
});

// ─────────────────────────────────────────────
// ADDRESS MANAGEMENT
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL ADDRESSES
=========================== */
router.get("/:orgId/addresses", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all addresses for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationAddresses(req, res);
});

/* ===========================
   ✅ ADD NEW ADDRESS
=========================== */
router.post("/:orgId/addresses", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Add new address to organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              address_line1: { type: "string" },
              address_line2: { type: "string" },
              city: { type: "string" },
              state: { type: "string" },
              pincode: { type: "string" },
              country: { type: "string" },
              is_primary: { type: "boolean" }
            }
          }
        }
      }
    }
  */
  addOrganizationAddress(req, res);
});

/* ===========================
   ✅ UPDATE ADDRESS
=========================== */
router.put("/:orgId/addresses/:addressId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Update organization address'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['addressId'] = {
      in: 'path',
      description: 'Address ID',
      required: true,
      type: 'string'
    }
  */
  updateOrganizationAddress(req, res);
});

/* ===========================
   ✅ DELETE ADDRESS
=========================== */
router.delete("/:orgId/addresses/:addressId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Delete organization address'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['addressId'] = {
      in: 'path',
      description: 'Address ID',
      required: true,
      type: 'string'
    }
  */
  deleteOrganizationAddress(req, res);
});

// ─────────────────────────────────────────────
// BANK DETAILS MANAGEMENT
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL BANK DETAILS
=========================== */
router.get("/:orgId/bank-details", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all bank details for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationBankDetails(req, res);
});

/* ===========================
   ✅ ADD NEW BANK DETAIL
=========================== */
router.post("/:orgId/bank-details", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Add new bank detail to organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              bank_name: { type: "string" },
              account_number: { type: "string" },
              account_holder_name: { type: "string" },
              ifsc_code: { type: "string" },
              branch: { type: "string" },
              is_primary: { type: "boolean" }
            }
          }
        }
      }
    }
  */
  addOrganizationBankDetail(req, res);
});

/* ===========================
   ✅ UPDATE BANK DETAIL
=========================== */
router.put("/:orgId/bank-details/:bankId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Update organization bank detail'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['bankId'] = {
      in: 'path',
      description: 'Bank detail ID',
      required: true,
      type: 'string'
    }
  */
  updateOrganizationBankDetail(req, res);
});

/* ===========================
   ✅ DELETE BANK DETAIL
=========================== */
router.delete("/:orgId/bank-details/:bankId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Delete organization bank detail'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['bankId'] = {
      in: 'path',
      description: 'Bank detail ID',
      required: true,
      type: 'string'
    }
  */
  deleteOrganizationBankDetail(req, res);
});

// ─────────────────────────────────────────────
// DOCUMENT MANAGEMENT
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL DOCUMENTS
=========================== */
router.get("/:orgId/documents", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all documents for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationDocuments(req, res);
});

/* ===========================
   ✅ UPLOAD DOCUMENT
=========================== */
router.post("/:orgId/documents", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Upload document for organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "multipart/form-data": {
          schema: {
            type: "object",
            properties: {
              file: { type: "string", format: "binary" },
              document_type: { type: "string" }
            },
            required: ["file"]
          }
        }
      }
    }
  */
  uploadOrganizationDocument(req, res);
});

/* ===========================
   ✅ REPLACE DOCUMENT
=========================== */
router.put("/:orgId/documents/:docId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Replace organization document'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.consumes = ['multipart/form-data']
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['docId'] = {
      in: 'path',
      description: 'Document ID',
      required: true,
      type: 'string'
    }
  */
  replaceOrganizationDocument(req, res);
});

/* ===========================
   ✅ DELETE DOCUMENT
=========================== */
router.delete("/:orgId/documents/:docId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Delete organization document'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['docId'] = {
      in: 'path',
      description: 'Document ID',
      required: true,
      type: 'string'
    }
  */
  deleteOrganizationDocument(req, res);
});

// ─────────────────────────────────────────────
// SMTP CONFIGURATION MANAGEMENT
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL SMTP CONFIGS
=========================== */
router.get("/:orgId/smtp-configs", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all SMTP configurations for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getSmtpConfigs(req, res);
});

/* ===========================
   ✅ CREATE SMTP CONFIG
=========================== */
router.post("/:orgId/smtp-configs", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Create SMTP configuration for organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              name: { type: "string" },
              host: { type: "string" },
              port: { type: "integer" },
              from_email: { type: "string" },
              from_name: { type: "string" },
              auth_username: { type: "string" },
              auth_password: { type: "string" },
              encryption: { type: "string" }
            },
            required: ["name", "host", "from_email"]
          }
        }
      }
    }
  */
  createSmtpConfig(req, res);
});

/* ===========================
   ✅ UPDATE SMTP CONFIG
=========================== */
router.put("/:orgId/smtp-configs/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Update SMTP configuration'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'SMTP Config ID',
      required: true,
      type: 'string'
    }
  */
  updateSmtpConfig(req, res);
});

/* ===========================
   ✅ DELETE SMTP CONFIG
=========================== */
router.delete("/:orgId/smtp-configs/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Delete SMTP configuration'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'SMTP Config ID',
      required: true,
      type: 'string'
    }
  */
  deleteSmtpConfig(req, res);
});

/* ===========================
   ✅ TOGGLE SMTP ACTIVE STATUS
=========================== */
router.patch("/:orgId/smtp-configs/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Toggle SMTP configuration active status'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'SMTP Config ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              is_active: { type: "boolean" }
            },
            required: ["is_active"]
          }
        }
      }
    }
  */
  toggleSmtpActive(req, res);
});

/* ===========================
   ✅ TEST SMTP CONNECTION
=========================== */
router.post("/:orgId/smtp-configs/:id/test", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Test SMTP connection'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'SMTP Config ID',
      required: true,
      type: 'string'
    }
  */
  testSmtpConnection(req, res);
});

/* ===========================
   ✅ SET SMTP AS DEFAULT
=========================== */
router.patch("/:orgId/smtp-configs/:id/set-default", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Set SMTP configuration as default'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['id'] = {
      in: 'path',
      description: 'SMTP Config ID',
      required: true,
      type: 'string'
    }
  */
  setAsDefault(req, res);
});

// ─────────────────────────────────────────────
// API MANAGEMENT
// ─────────────────────────────────────────────

/* ===========================
   ✅ GET ALL APIS
=========================== */
router.get("/:orgId/apis", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Get all API configurations for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
  */
  getOrganizationApis(req, res);
});

/* ===========================
   ✅ ADD NEW API
=========================== */
router.post("/:orgId/apis", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Add new API configuration to organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              api_name: { type: "string" },
              base_url: { type: "string" },
              description: { type: "string" },
              custom_headers: { type: "object" },
              status: { type: "string" }
            },
            required: ["api_name", "base_url"]
          }
        }
      }
    }
  */
  addOrganizationApi(req, res);
});

/* ===========================
   ✅ UPDATE API
=========================== */
router.put("/:orgId/apis/:apiId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Update organization API configuration'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['apiId'] = {
      in: 'path',
      description: 'API ID',
      required: true,
      type: 'string'
    }
  */
  updateOrganizationApi(req, res);
});

/* ===========================
   ✅ DELETE API
=========================== */
router.delete("/:orgId/apis/:apiId", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Delete organization API configuration'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['apiId'] = {
      in: 'path',
      description: 'API ID',
      required: true,
      type: 'string'
    }
  */
  deleteOrganizationApi(req, res);
});

/* ===========================
   ✅ TEST API CONNECTION
=========================== */
router.post("/:orgId/apis/:apiId/test", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Organizations']
    #swagger.summary = 'Test API connection'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = {
      in: 'path',
      description: 'Organization ID',
      required: true,
      type: 'string'
    }
    #swagger.parameters['apiId'] = {
      in: 'path',
      description: 'API ID',
      required: true,
      type: 'string'
    }
  */
  testOrganizationApi(req, res);
});

module.exports = router;