"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.moveLeadToStage = exports.getKanbanBoard = exports.deleteStage = exports.updateStage = exports.createStage = exports.deletePipeline = exports.updatePipeline = exports.createPipeline = exports.getPipeline = exports.getPipelines = void 0;
const pipelineService = __importStar(require("../services/pipelineService"));
const getPipelines = async (req, res) => {
    try {
        const pipelines = await pipelineService.getAllPipelines();
        res.json(pipelines);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get pipelines' });
    }
};
exports.getPipelines = getPipelines;
const getPipeline = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const pipeline = await pipelineService.getPipelineById(id);
        res.json(pipeline);
    }
    catch (error) {
        res.status(404).json({ error: error.message || 'Pipeline not found' });
    }
};
exports.getPipeline = getPipeline;
const createPipeline = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const { name, description } = req.body;
        const pipeline = await pipelineService.createPipeline(name, description, req.user.userId);
        res.status(201).json(pipeline);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to create pipeline' });
    }
};
exports.createPipeline = createPipeline;
const updatePipeline = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const pipeline = await pipelineService.updatePipeline(id, req.body);
        res.json(pipeline);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to update pipeline' });
    }
};
exports.updatePipeline = updatePipeline;
const deletePipeline = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const result = await pipelineService.deletePipeline(id);
        res.json(result);
    }
    catch (error) {
        res.status(404).json({ error: error.message || 'Pipeline not found' });
    }
};
exports.deletePipeline = deletePipeline;
// Stage controllers
const createStage = async (req, res) => {
    try {
        const pipelineId = parseInt(req.params.pipelineId, 10);
        const { name, displayOrder, color } = req.body;
        const stage = await pipelineService.createStage(pipelineId, name, displayOrder, color);
        res.status(201).json(stage);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to create stage' });
    }
};
exports.createStage = createStage;
const updateStage = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const stage = await pipelineService.updateStage(id, req.body);
        res.json(stage);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to update stage' });
    }
};
exports.updateStage = updateStage;
const deleteStage = async (req, res) => {
    try {
        const id = parseInt(req.params.id, 10);
        const result = await pipelineService.deleteStage(id);
        res.json(result);
    }
    catch (error) {
        res.status(404).json({ error: error.message || 'Stage not found' });
    }
};
exports.deleteStage = deleteStage;
// Kanban board
const getKanbanBoard = async (req, res) => {
    try {
        const pipelineId = parseInt(req.params.pipelineId, 10);
        const kanbanData = await pipelineService.getKanbanBoard(pipelineId);
        res.json(kanbanData);
    }
    catch (error) {
        res.status(500).json({ error: error.message || 'Failed to get kanban board' });
    }
};
exports.getKanbanBoard = getKanbanBoard;
const moveLeadToStage = async (req, res) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }
        const leadId = parseInt(req.params.leadId, 10);
        const { stageId } = req.body;
        const lead = await pipelineService.moveLeadToStage(leadId, stageId, req.user.userId);
        res.json(lead);
    }
    catch (error) {
        res.status(400).json({ error: error.message || 'Failed to move lead' });
    }
};
exports.moveLeadToStage = moveLeadToStage;
