"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSupportAgentController = createSupportAgentController;
function createSupportAgentController(agentService, logger) {
    return {
        list: async (_req, res) => {
            try {
                const agents = await agentService.listAgents();
                res.json({ success: true, data: agents });
            }
            catch (error) {
                logger.error("Failed to list agents.", { error: error instanceof Error ? error.message : String(error) });
                res.status(500).json({ success: false, error: "Failed to list agents." });
            }
        },
        create: async (req, res) => {
            try {
                const { adminUserId, displayName, active, maxConcurrentTickets, role } = req.body;
                const agent = await agentService.createAgent({ adminUserId, displayName, active, maxConcurrentTickets, role });
                res.status(201).json({ success: true, data: agent });
            }
            catch (error) {
                logger.error("Failed to create agent.", { error: error instanceof Error ? error.message : String(error) });
                res.status(500).json({ success: false, error: "Failed to create agent." });
            }
        },
        update: async (req, res) => {
            try {
                const agent = await agentService.updateAgent(String(req.params.id), req.body);
                if (!agent) {
                    res.status(404).json({ success: false, error: "Agent not found." });
                    return;
                }
                res.json({ success: true, data: agent });
            }
            catch (error) {
                logger.error("Failed to update agent.", { error: error instanceof Error ? error.message : String(error) });
                res.status(500).json({ success: false, error: "Failed to update agent." });
            }
        },
        setOnline: async (req, res) => {
            try {
                const { online } = req.body;
                const adminUserId = req.adminUser?.id;
                const agent = adminUserId ? await agentService.getByAdminUserId(adminUserId) : null;
                if (!agent) {
                    res.status(404).json({ success: false, error: "Agent profile not found." });
                    return;
                }
                const updated = await agentService.setOnline(agent.id, online);
                res.json({ success: true, data: updated });
            }
            catch (error) {
                logger.error("Failed to set agent online status.", { error: error instanceof Error ? error.message : String(error) });
                res.status(500).json({ success: false, error: "Failed to set online status." });
            }
        },
        delete: async (req, res) => {
            try {
                const success = await agentService.deleteAgent(String(req.params.id));
                if (!success) {
                    res.status(404).json({ success: false, error: "Agent not found." });
                    return;
                }
                res.json({ success: true });
            }
            catch (error) {
                logger.error("Failed to delete agent.", { error: error instanceof Error ? error.message : String(error) });
                res.status(500).json({ success: false, error: "Failed to delete agent." });
            }
        }
    };
}
