"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.updateLead = exports.getLead = exports.getLeads = exports.createLead = void 0;
const leadService = __importStar(require("../services/leadService"));
const createLead = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const lead = await leadService.createLead(req.body, req.user.userId);
        res.status(201).json(lead);
    }
    catch (error) {
        console.error('Create lead error:', error);
        res.status(400).json({ error: error.message || 'Failed to create lead' });
    }
};
exports.createLead = createLead;
const getLeads = async (req, res) => {
    try {
        const leads = await leadService.getAllLeads(req.query);
        res.json(leads);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get leads' });
    }
};
exports.getLeads = getLeads;
const getLead = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const lead = await leadService.getLeadById(id);
        res.json(lead);
    }
    catch (error) {
        res.status(404).json({ error: error.message || 'Lead not found' });
    }
};
exports.getLead = getLead;
const updateLead = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const id = parseInt(req.params.id, 10);
        const lead = await leadService.updateLead(id, req.body, req.user.userId);
        res.json(lead);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to update lead' });
    }
};
exports.updateLead = updateLead;
