"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.deleteDocument = exports.aiVerify = exports.processOCR = exports.verifyDocument = exports.createVersion = exports.uploadDocument = exports.getDocuments = void 0;
const documentService = __importStar(require("../services/documentService"));
const getDocuments = async (req, res) => {
    try {
        const leadId = parseInt(req.params.leadId, 10);
        const documents = await documentService.getDocumentsByLead(leadId);
        res.json(documents);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get documents' });
    }
};
exports.getDocuments = getDocuments;
const uploadDocument = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const { leadId, documentType, documentName, filePath, fileSize, mimeType, password } = req.body;
        const document = await documentService.uploadDocument(leadId, documentType, documentName, filePath, fileSize, mimeType, req.user.userId, password);
        res.status(201).json(document);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to upload document' });
    }
};
exports.uploadDocument = uploadDocument;
const createVersion = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const documentId = parseInt(req.params.documentId, 10);
        const { filePath, fileSize } = req.body;
        const version = await documentService.createDocumentVersion(documentId, filePath, fileSize, req.user.userId);
        res.status(201).json(version);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to create version' });
    }
};
exports.createVersion = createVersion;
const verifyDocument = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const documentId = parseInt(req.params.documentId, 10);
        const { isVerified } = req.body;
        const document = await documentService.verifyDocument(documentId, isVerified, req.user.userId);
        res.json(document);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to verify document' });
    }
};
exports.verifyDocument = verifyDocument;
const processOCR = async (req, res) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const document = await documentService.processOCR(documentId);
        res.json(document);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to process OCR' });
    }
};
exports.processOCR = processOCR;
const aiVerify = async (req, res) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const document = await documentService.aiVerifyDocument(documentId);
        res.json(document);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to AI verify document' });
    }
};
exports.aiVerify = aiVerify;
const deleteDocument = async (req, res) => {
    try {
        const documentId = parseInt(req.params.documentId, 10);
        const result = await documentService.deleteDocument(documentId);
        res.json(result);
    }
    catch (error) {
        res.status(404).json({ error: error.message || 'Document not found' });
    }
};
exports.deleteDocument = deleteDocument;
