"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBreachedPendings = exports.escalatePending = exports.updatePending = exports.createPending = exports.getPendings = exports.reopenLead = exports.closeLead = exports.getAuditLogs = void 0;
const auditService = __importStar(require("../services/auditService"));
const pendingService = __importStar(require("../services/pendingService"));
// Audit endpoints
const getAuditLogs = async (req, res) => {
    try {
        const { tableName, recordId } = req.query;
        const logs = await auditService.getAuditLogs(tableName, recordId ? parseInt(recordId, 10) : undefined);
        res.json(logs);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get audit logs' });
    }
};
exports.getAuditLogs = getAuditLogs;
const closeLead = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const leadId = parseInt(req.params.leadId, 10);
        const { closeReason, subStatus } = req.body;
        const lead = await auditService.closeLead(leadId, closeReason, subStatus, req.user.userId);
        res.json(lead);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to close lead' });
    }
};
exports.closeLead = closeLead;
const reopenLead = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const leadId = parseInt(req.params.leadId, 10);
        const lead = await auditService.reopenLead(leadId, req.user.userId);
        res.json(lead);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to reopen lead' });
    }
};
exports.reopenLead = reopenLead;
// Pendings endpoints
const getPendings = async (req, res) => {
    try {
        const pendings = await pendingService.getAllPendings(req.query);
        res.json(pendings);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get pendings' });
    }
};
exports.getPendings = getPendings;
const createPending = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const { leadId, pendingType, description, dueDate, assignedTo, slaHours } = req.body;
        const pending = await pendingService.createPending(leadId, pendingType, description, dueDate, assignedTo, req.user.userId, slaHours);
        res.status(201).json(pending);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to create pending' });
    }
};
exports.createPending = createPending;
const updatePending = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const pending = await pendingService.updatePending(id, req.body);
        res.json(pending);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to update pending' });
    }
};
exports.updatePending = updatePending;
const escalatePending = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const id = parseInt(req.params.id, 10);
        const { escalatedTo } = req.body;
        const pending = await pendingService.escalatePending(id, escalatedTo, req.user.userId);
        res.json(pending);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to escalate pending' });
    }
};
exports.escalatePending = escalatePending;
const getBreachedPendings = async (req, res) => {
    try {
        const pendings = await pendingService.getBreachedPendings();
        res.json(pendings);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get breached pendings' });
    }
};
exports.getBreachedPendings = getBreachedPendings;
